blob: 73143d6ac9a86b1b89df22c8c50a609380e3b7a0 [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"
Ian Rogers7e70b002014-10-08 11:47:24 -070021#include "mirror/array-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010022#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
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000032namespace x86 {
33
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010034static constexpr bool kExplicitStackOverflowCheck = false;
35
36static constexpr int kNumberOfPushedRegistersAtEntry = 1;
37static constexpr int kCurrentMethodStackOffset = 0;
38
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010039static constexpr Register kRuntimeParameterCoreRegisters[] = { EAX, ECX, EDX };
40static constexpr size_t kRuntimeParameterCoreRegistersLength =
41 arraysize(kRuntimeParameterCoreRegisters);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010042static constexpr XmmRegister kRuntimeParameterFpuRegisters[] = { };
43static constexpr size_t kRuntimeParameterFpuRegistersLength = 0;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010044
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010045class InvokeRuntimeCallingConvention : public CallingConvention<Register, XmmRegister> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010046 public:
47 InvokeRuntimeCallingConvention()
48 : CallingConvention(kRuntimeParameterCoreRegisters,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010049 kRuntimeParameterCoreRegistersLength,
50 kRuntimeParameterFpuRegisters,
51 kRuntimeParameterFpuRegistersLength) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010052
53 private:
54 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
55};
56
Nicolas Geoffraye5038322014-07-04 09:41:32 +010057#define __ reinterpret_cast<X86Assembler*>(codegen->GetAssembler())->
58
59class NullCheckSlowPathX86 : public SlowPathCode {
60 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010061 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010062
63 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
64 __ Bind(GetEntryLabel());
65 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowNullPointer)));
Nicolas Geoffray39468442014-09-02 15:17:15 +010066 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010067 }
68
69 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010070 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010071 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
72};
73
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010074class StackOverflowCheckSlowPathX86 : public SlowPathCode {
75 public:
76 StackOverflowCheckSlowPathX86() {}
77
78 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
79 __ Bind(GetEntryLabel());
80 __ addl(ESP,
81 Immediate(codegen->GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
82 __ fs()->jmp(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowStackOverflow)));
83 }
84
85 private:
86 DISALLOW_COPY_AND_ASSIGN(StackOverflowCheckSlowPathX86);
87};
88
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010089class BoundsCheckSlowPathX86 : public SlowPathCode {
90 public:
Roland Levillain5799fc02014-09-25 12:15:20 +010091 BoundsCheckSlowPathX86(HBoundsCheck* instruction,
92 Location index_location,
93 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +010094 : instruction_(instruction), index_location_(index_location), length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010095
96 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
97 CodeGeneratorX86* x86_codegen = reinterpret_cast<CodeGeneratorX86*>(codegen);
98 __ Bind(GetEntryLabel());
99 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100100 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), index_location_);
101 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(1)), length_location_);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100102 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowArrayBounds)));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100103 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100104 }
105
106 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100107 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100108 const Location index_location_;
109 const Location length_location_;
110
111 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
112};
113
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000114class SuspendCheckSlowPathX86 : public SlowPathCode {
115 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100116 explicit SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
117 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000118
119 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
120 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100121 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000122 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pTestSuspend)));
123 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100124 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100125 if (successor_ == nullptr) {
126 __ jmp(GetReturnLabel());
127 } else {
128 __ jmp(codegen->GetLabelOf(successor_));
129 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000130 }
131
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100132 Label* GetReturnLabel() {
133 DCHECK(successor_ == nullptr);
134 return &return_label_;
135 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000136
137 private:
138 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100139 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000140 Label return_label_;
141
142 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
143};
144
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100145#undef __
146#define __ reinterpret_cast<X86Assembler*>(GetAssembler())->
147
Dave Allison20dfc792014-06-16 20:44:29 -0700148inline Condition X86Condition(IfCondition cond) {
149 switch (cond) {
150 case kCondEQ: return kEqual;
151 case kCondNE: return kNotEqual;
152 case kCondLT: return kLess;
153 case kCondLE: return kLessEqual;
154 case kCondGT: return kGreater;
155 case kCondGE: return kGreaterEqual;
156 default:
157 LOG(FATAL) << "Unknown if condition";
158 }
159 return kEqual;
160}
161
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100162void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
163 stream << X86ManagedRegister::FromCpuRegister(Register(reg));
164}
165
166void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
167 stream << X86ManagedRegister::FromXmmRegister(XmmRegister(reg));
168}
169
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100170void CodeGeneratorX86::SaveCoreRegister(Location stack_location, uint32_t reg_id) {
171 __ movl(Address(ESP, stack_location.GetStackIndex()), static_cast<Register>(reg_id));
172}
173
174void CodeGeneratorX86::RestoreCoreRegister(Location stack_location, uint32_t reg_id) {
175 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_location.GetStackIndex()));
176}
177
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100178CodeGeneratorX86::CodeGeneratorX86(HGraph* graph)
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100179 : CodeGenerator(graph, kNumberOfCpuRegisters, kNumberOfXmmRegisters, kNumberOfRegisterPairs),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100180 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100181 instruction_visitor_(graph, this),
182 move_resolver_(graph->GetArena(), this) {}
183
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100184size_t CodeGeneratorX86::FrameEntrySpillSize() const {
185 return kNumberOfPushedRegistersAtEntry * kX86WordSize;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100186}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100187
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100188Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100189 switch (type) {
190 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100191 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100192 X86ManagedRegister pair =
193 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100194 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
195 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100196 // Block all other register pairs that share a register with `pair`.
197 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
198 X86ManagedRegister current =
199 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
200 if (current.AsRegisterPairLow() == pair.AsRegisterPairLow()
201 || current.AsRegisterPairLow() == pair.AsRegisterPairHigh()
202 || current.AsRegisterPairHigh() == pair.AsRegisterPairLow()
203 || current.AsRegisterPairHigh() == pair.AsRegisterPairHigh()) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100204 blocked_register_pairs_[i] = true;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100205 }
206 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100207 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100208 }
209
210 case Primitive::kPrimByte:
211 case Primitive::kPrimBoolean:
212 case Primitive::kPrimChar:
213 case Primitive::kPrimShort:
214 case Primitive::kPrimInt:
215 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100216 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100217 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100218 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100219 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
220 X86ManagedRegister current =
221 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
222 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100223 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100224 }
225 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100226 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100227 }
228
229 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100230 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100231 return Location::FpuRegisterLocation(
232 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100233 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100234
235 case Primitive::kPrimVoid:
236 LOG(FATAL) << "Unreachable type " << type;
237 }
238
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100239 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100240}
241
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100242void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100243 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100244 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100245
246 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100247 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100248
249 // TODO: We currently don't use Quick's callee saved registers.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100250 blocked_core_registers_[EBP] = true;
251 blocked_core_registers_[ESI] = true;
252 blocked_core_registers_[EDI] = true;
253 blocked_register_pairs_[EAX_EDI] = true;
254 blocked_register_pairs_[EDX_EDI] = true;
255 blocked_register_pairs_[ECX_EDI] = true;
256 blocked_register_pairs_[EBX_EDI] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100257}
258
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100259InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
260 : HGraphVisitor(graph),
261 assembler_(codegen->GetAssembler()),
262 codegen_(codegen) {}
263
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000264void CodeGeneratorX86::GenerateFrameEntry() {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000265 // Create a fake register to mimic Quick.
266 static const int kFakeReturnRegister = 8;
267 core_spill_mask_ |= (1 << kFakeReturnRegister);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000268
Dave Allison648d7112014-07-25 16:15:27 -0700269 bool skip_overflow_check = IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100270 if (!skip_overflow_check && !kExplicitStackOverflowCheck) {
271 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100272 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100273 }
274
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100275 // The return PC has already been pushed on the stack.
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100276 __ subl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100277
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100278 if (!skip_overflow_check && kExplicitStackOverflowCheck) {
279 SlowPathCode* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathX86();
280 AddSlowPath(slow_path);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100281
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100282 __ fs()->cmpl(ESP, Address::Absolute(Thread::StackEndOffset<kX86WordSize>()));
283 __ j(kLess, slow_path->GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100284 }
285
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100286 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000287}
288
289void CodeGeneratorX86::GenerateFrameExit() {
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100290 __ addl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000291}
292
293void CodeGeneratorX86::Bind(Label* label) {
294 __ Bind(label);
295}
296
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000297void InstructionCodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100298 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000299}
300
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100301Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
302 switch (load->GetType()) {
303 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100304 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100305 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
306 break;
307
308 case Primitive::kPrimInt:
309 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100310 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100311 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100312
313 case Primitive::kPrimBoolean:
314 case Primitive::kPrimByte:
315 case Primitive::kPrimChar:
316 case Primitive::kPrimShort:
317 case Primitive::kPrimVoid:
318 LOG(FATAL) << "Unexpected type " << load->GetType();
319 }
320
321 LOG(FATAL) << "Unreachable";
322 return Location();
323}
324
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100325Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
326 switch (type) {
327 case Primitive::kPrimBoolean:
328 case Primitive::kPrimByte:
329 case Primitive::kPrimChar:
330 case Primitive::kPrimShort:
331 case Primitive::kPrimInt:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100332 case Primitive::kPrimFloat:
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100333 case Primitive::kPrimNot: {
334 uint32_t index = gp_index_++;
335 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100336 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100337 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100338 return Location::StackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100339 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100340 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100341
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100342 case Primitive::kPrimLong:
343 case Primitive::kPrimDouble: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100344 uint32_t index = gp_index_;
345 gp_index_ += 2;
346 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100347 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
348 calling_convention.GetRegisterPairAt(index));
349 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100350 } else if (index + 1 == calling_convention.GetNumberOfRegisters()) {
351 return Location::QuickParameter(index);
352 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100353 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100354 }
355 }
356
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100357 case Primitive::kPrimVoid:
358 LOG(FATAL) << "Unexpected parameter type " << type;
359 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100360 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100361 return Location();
362}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100363
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100364void CodeGeneratorX86::Move32(Location destination, Location source) {
365 if (source.Equals(destination)) {
366 return;
367 }
368 if (destination.IsRegister()) {
369 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100370 __ movl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100371 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100372 __ movd(destination.As<Register>(), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100373 } else {
374 DCHECK(source.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100375 __ movl(destination.As<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100376 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100377 } else if (destination.IsFpuRegister()) {
378 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100379 __ movd(destination.As<XmmRegister>(), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100380 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100381 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100382 } else {
383 DCHECK(source.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100384 __ movss(destination.As<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100385 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100386 } else {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100387 DCHECK(destination.IsStackSlot());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100388 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100389 __ movl(Address(ESP, destination.GetStackIndex()), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100390 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100391 __ movss(Address(ESP, destination.GetStackIndex()), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100392 } else {
393 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100394 __ pushl(Address(ESP, source.GetStackIndex()));
395 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100396 }
397 }
398}
399
400void CodeGeneratorX86::Move64(Location destination, Location source) {
401 if (source.Equals(destination)) {
402 return;
403 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100404 if (destination.IsRegisterPair()) {
405 if (source.IsRegisterPair()) {
406 __ movl(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
407 __ movl(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100408 } else if (source.IsFpuRegister()) {
409 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100410 } else if (source.IsQuickParameter()) {
411 uint32_t argument_index = source.GetQuickParameterIndex();
412 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100413 __ movl(destination.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100414 calling_convention.GetRegisterAt(argument_index));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100415 __ movl(destination.AsRegisterPairHigh<Register>(), Address(ESP,
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100416 calling_convention.GetStackOffsetOf(argument_index + 1) + GetFrameSize()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100417 } else {
418 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100419 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
420 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100421 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
422 }
423 } else if (destination.IsQuickParameter()) {
424 InvokeDexCallingConvention calling_convention;
425 uint32_t argument_index = destination.GetQuickParameterIndex();
426 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100427 __ movl(calling_convention.GetRegisterAt(argument_index), source.AsRegisterPairLow<Register>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100428 __ movl(Address(ESP, calling_convention.GetStackOffsetOf(argument_index + 1)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100429 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100430 } else if (source.IsFpuRegister()) {
431 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100432 } else {
433 DCHECK(source.IsDoubleStackSlot());
434 __ movl(calling_convention.GetRegisterAt(argument_index),
435 Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100436 __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100437 __ popl(Address(ESP, calling_convention.GetStackOffsetOf(argument_index + 1)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100438 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100439 } else if (destination.IsFpuRegister()) {
440 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100441 __ movsd(destination.As<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100442 } else {
443 LOG(FATAL) << "Unimplemented";
444 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100445 } else {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100446 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100447 if (source.IsRegisterPair()) {
448 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100449 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100450 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100451 } else if (source.IsQuickParameter()) {
452 InvokeDexCallingConvention calling_convention;
453 uint32_t argument_index = source.GetQuickParameterIndex();
454 __ movl(Address(ESP, destination.GetStackIndex()),
455 calling_convention.GetRegisterAt(argument_index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100456 DCHECK_EQ(calling_convention.GetStackOffsetOf(argument_index + 1) + GetFrameSize(),
457 static_cast<size_t>(destination.GetHighStackIndex(kX86WordSize)));
458 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100459 __ movsd(Address(ESP, destination.GetStackIndex()), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100460 } else {
461 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100462 __ pushl(Address(ESP, source.GetStackIndex()));
463 __ popl(Address(ESP, destination.GetStackIndex()));
464 __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize)));
465 __ popl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100466 }
467 }
468}
469
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100470void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
471 if (instruction->AsIntConstant() != nullptr) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100472 Immediate imm(instruction->AsIntConstant()->GetValue());
473 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100474 __ movl(location.As<Register>(), imm);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100475 } else {
476 __ movl(Address(ESP, location.GetStackIndex()), imm);
477 }
478 } else if (instruction->AsLongConstant() != nullptr) {
479 int64_t value = instruction->AsLongConstant()->GetValue();
480 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100481 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
482 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100483 } else {
484 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
485 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
486 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100487 } else if (instruction->AsLoadLocal() != nullptr) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100488 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100489 switch (instruction->GetType()) {
490 case Primitive::kPrimBoolean:
491 case Primitive::kPrimByte:
492 case Primitive::kPrimChar:
493 case Primitive::kPrimShort:
494 case Primitive::kPrimInt:
495 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100496 case Primitive::kPrimFloat:
497 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100498 break;
499
500 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100501 case Primitive::kPrimDouble:
502 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100503 break;
504
505 default:
506 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
507 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000508 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100509 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100510 switch (instruction->GetType()) {
511 case Primitive::kPrimBoolean:
512 case Primitive::kPrimByte:
513 case Primitive::kPrimChar:
514 case Primitive::kPrimShort:
515 case Primitive::kPrimInt:
516 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100517 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100518 Move32(location, instruction->GetLocations()->Out());
519 break;
520
521 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100522 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100523 Move64(location, instruction->GetLocations()->Out());
524 break;
525
526 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100527 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100528 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000529 }
530}
531
532void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000533 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000534}
535
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000536void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000537 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100538 DCHECK(!successor->IsExitBlock());
539
540 HBasicBlock* block = got->GetBlock();
541 HInstruction* previous = got->GetPrevious();
542
543 HLoopInformation* info = block->GetLoopInformation();
544 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
545 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
546 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
547 return;
548 }
549
550 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
551 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
552 }
553 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000554 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000555 }
556}
557
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000558void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000559 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000560}
561
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000562void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000563 if (kIsDebugBuild) {
564 __ Comment("Unreachable");
565 __ int3();
566 }
567}
568
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000569void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100570 LocationSummary* locations =
571 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100572 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100573 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +0100574 locations->SetInAt(0, Location::Any(), Location::kDiesAtEntry);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100575 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000576}
577
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000578void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700579 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100580 bool materialized = !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
581 // Moves do not affect the eflags register, so if the condition is evaluated
582 // just before the if, we don't need to evaluate it again.
583 bool eflags_set = cond->IsCondition()
584 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
585 if (materialized) {
586 if (!eflags_set) {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100587 // Materialized condition, compare against 0.
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100588 Location lhs = if_instr->GetLocations()->InAt(0);
589 if (lhs.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100590 __ cmpl(lhs.As<Register>(), Immediate(0));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100591 } else {
592 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
593 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100594 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
595 } else {
596 __ j(X86Condition(cond->AsCondition()->GetCondition()),
597 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700598 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100599 } else {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100600 Location lhs = cond->GetLocations()->InAt(0);
601 Location rhs = cond->GetLocations()->InAt(1);
Dave Allison20dfc792014-06-16 20:44:29 -0700602 // LHS is guaranteed to be in a register (see LocationsBuilderX86::VisitCondition).
603 if (rhs.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100604 __ cmpl(lhs.As<Register>(), rhs.As<Register>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100605 } else if (rhs.IsConstant()) {
606 HIntConstant* instruction = rhs.GetConstant()->AsIntConstant();
607 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100608 __ cmpl(lhs.As<Register>(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700609 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100610 __ cmpl(lhs.As<Register>(), Address(ESP, rhs.GetStackIndex()));
Dave Allison20dfc792014-06-16 20:44:29 -0700611 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100612 __ j(X86Condition(cond->AsCondition()->GetCondition()),
Dave Allison20dfc792014-06-16 20:44:29 -0700613 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100614 }
Dave Allison20dfc792014-06-16 20:44:29 -0700615 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(), if_instr->IfFalseSuccessor())) {
616 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000617 }
618}
619
620void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000621 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000622}
623
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000624void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
625 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000626}
627
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000628void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100629 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000630}
631
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000632void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100633 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000634}
635
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100636void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100637 LocationSummary* locations =
638 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100639 switch (store->InputAt(1)->GetType()) {
640 case Primitive::kPrimBoolean:
641 case Primitive::kPrimByte:
642 case Primitive::kPrimChar:
643 case Primitive::kPrimShort:
644 case Primitive::kPrimInt:
645 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100646 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100647 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
648 break;
649
650 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100651 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100652 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
653 break;
654
655 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100656 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100657 }
658 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000659}
660
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000661void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000662}
663
Dave Allison20dfc792014-06-16 20:44:29 -0700664void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100665 LocationSummary* locations =
666 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +0100667 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
668 locations->SetInAt(1, Location::Any(), Location::kDiesAtEntry);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100669 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100670 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100671 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000672}
673
Dave Allison20dfc792014-06-16 20:44:29 -0700674void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
675 if (comp->NeedsMaterialization()) {
676 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100677 Register reg = locations->Out().As<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100678 // Clear register: setcc only sets the low byte.
679 __ xorl(reg, reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700680 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100681 __ cmpl(locations->InAt(0).As<Register>(),
682 locations->InAt(1).As<Register>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100683 } else if (locations->InAt(1).IsConstant()) {
684 HConstant* instruction = locations->InAt(1).GetConstant();
685 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100686 __ cmpl(locations->InAt(0).As<Register>(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700687 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100688 __ cmpl(locations->InAt(0).As<Register>(),
Dave Allison20dfc792014-06-16 20:44:29 -0700689 Address(ESP, locations->InAt(1).GetStackIndex()));
690 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100691 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100692 }
Dave Allison20dfc792014-06-16 20:44:29 -0700693}
694
695void LocationsBuilderX86::VisitEqual(HEqual* comp) {
696 VisitCondition(comp);
697}
698
699void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
700 VisitCondition(comp);
701}
702
703void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
704 VisitCondition(comp);
705}
706
707void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
708 VisitCondition(comp);
709}
710
711void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
712 VisitCondition(comp);
713}
714
715void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
716 VisitCondition(comp);
717}
718
719void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
720 VisitCondition(comp);
721}
722
723void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
724 VisitCondition(comp);
725}
726
727void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
728 VisitCondition(comp);
729}
730
731void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
732 VisitCondition(comp);
733}
734
735void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
736 VisitCondition(comp);
737}
738
739void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
740 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000741}
742
743void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100744 LocationSummary* locations =
745 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100746 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000747}
748
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000749void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000750}
751
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100752void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100753 LocationSummary* locations =
754 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100755 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100756}
757
758void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
759 // Will be generated at use site.
760}
761
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000762void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000763 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000764}
765
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000766void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
767 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000768 __ ret();
769}
770
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000771void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100772 LocationSummary* locations =
773 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100774 switch (ret->InputAt(0)->GetType()) {
775 case Primitive::kPrimBoolean:
776 case Primitive::kPrimByte:
777 case Primitive::kPrimChar:
778 case Primitive::kPrimShort:
779 case Primitive::kPrimInt:
780 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100781 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100782 break;
783
784 case Primitive::kPrimLong:
785 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100786 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100787 break;
788
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100789 case Primitive::kPrimFloat:
790 case Primitive::kPrimDouble:
791 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100792 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100793 break;
794
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100795 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100796 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100797 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000798}
799
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000800void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100801 if (kIsDebugBuild) {
802 switch (ret->InputAt(0)->GetType()) {
803 case Primitive::kPrimBoolean:
804 case Primitive::kPrimByte:
805 case Primitive::kPrimChar:
806 case Primitive::kPrimShort:
807 case Primitive::kPrimInt:
808 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100809 DCHECK_EQ(ret->GetLocations()->InAt(0).As<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100810 break;
811
812 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100813 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
814 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100815 break;
816
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100817 case Primitive::kPrimFloat:
818 case Primitive::kPrimDouble:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100819 DCHECK_EQ(ret->GetLocations()->InAt(0).As<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100820 break;
821
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100822 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100823 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100824 }
825 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000826 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000827 __ ret();
828}
829
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000830void LocationsBuilderX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100831 HandleInvoke(invoke);
832}
833
834void InstructionCodeGeneratorX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100835 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100836 uint32_t heap_reference_size = sizeof(mirror::HeapReference<mirror::Object>);
837 size_t index_in_cache = mirror::Array::DataOffset(heap_reference_size).Int32Value() +
838 invoke->GetIndexInDexCache() * kX86WordSize;
839
840 // TODO: Implement all kinds of calls:
841 // 1) boot -> boot
842 // 2) app -> boot
843 // 3) app -> app
844 //
845 // Currently we implement the app -> app logic, which looks up in the resolve cache.
846
847 // temp = method;
848 LoadCurrentMethod(temp);
849 // temp = temp->dex_cache_resolved_methods_;
850 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
851 // temp = temp[index_in_cache]
852 __ movl(temp, Address(temp, index_in_cache));
853 // (temp + offset_of_quick_compiled_code)()
854 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
855
856 DCHECK(!codegen_->IsLeafMethod());
857 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
858}
859
860void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
861 HandleInvoke(invoke);
862}
863
864void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100865 LocationSummary* locations =
866 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100867 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100868
869 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100870 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100871 HInstruction* input = invoke->InputAt(i);
872 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
873 }
874
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100875 switch (invoke->GetType()) {
876 case Primitive::kPrimBoolean:
877 case Primitive::kPrimByte:
878 case Primitive::kPrimChar:
879 case Primitive::kPrimShort:
880 case Primitive::kPrimInt:
881 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100882 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100883 break;
884
885 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100886 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100887 break;
888
889 case Primitive::kPrimVoid:
890 break;
891
892 case Primitive::kPrimDouble:
893 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100894 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100895 break;
896 }
897
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000898 invoke->SetLocations(locations);
899}
900
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100901void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100902 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100903 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
904 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
905 LocationSummary* locations = invoke->GetLocations();
906 Location receiver = locations->InAt(0);
907 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
908 // temp = object->GetClass();
909 if (receiver.IsStackSlot()) {
910 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
911 __ movl(temp, Address(temp, class_offset));
912 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100913 __ movl(temp, Address(receiver.As<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100914 }
915 // temp = temp->GetMethodAt(method_offset);
916 __ movl(temp, Address(temp, method_offset));
917 // call temp->GetEntryPoint();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000918 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
919
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100920 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +0100921 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000922}
923
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000924void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100925 LocationSummary* locations =
926 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000927 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100928 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100929 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100930 locations->SetInAt(0, Location::RequiresRegister());
931 locations->SetInAt(1, Location::Any());
932 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100933 break;
934 }
935
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100936 case Primitive::kPrimFloat:
937 case Primitive::kPrimDouble: {
938 locations->SetInAt(0, Location::RequiresFpuRegister());
939 locations->SetInAt(1, Location::Any());
940 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100941 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100942 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100943
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000944 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100945 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
946 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000947 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000948}
949
950void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
951 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100952 Location first = locations->InAt(0);
953 Location second = locations->InAt(1);
954
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000955 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100956 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100957 DCHECK_EQ(first.As<Register>(), locations->Out().As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100958 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100959 __ addl(first.As<Register>(), second.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100960 } else if (second.IsConstant()) {
961 HConstant* instruction = second.GetConstant();
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100962 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100963 __ addl(first.As<Register>(), imm);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100964 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100965 __ addl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100966 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000967 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100968 }
969
970 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100971 DCHECK_EQ(first.AsRegisterPairLow<Register>(),
972 locations->Out().AsRegisterPairLow<Register>());
973 DCHECK_EQ(first.AsRegisterPairHigh<Register>(),
974 locations->Out().AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100975 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100976 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
977 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100978 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100979 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
980 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100981 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100982 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100983 break;
984 }
985
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100986 case Primitive::kPrimFloat: {
987 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100988 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100989 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100990 __ addss(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100991 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100992 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100993 }
994
995 case Primitive::kPrimDouble: {
996 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100997 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100998 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100999 __ addsd(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001000 }
1001 break;
1002 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001003
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001004 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001005 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001006 }
1007}
1008
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001009void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001010 LocationSummary* locations =
1011 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001012 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001013 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001014 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001015 locations->SetInAt(0, Location::RequiresRegister());
1016 locations->SetInAt(1, Location::Any());
1017 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001018 break;
1019 }
1020
1021 case Primitive::kPrimBoolean:
1022 case Primitive::kPrimByte:
1023 case Primitive::kPrimChar:
1024 case Primitive::kPrimShort:
1025 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
1026 break;
1027
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001028 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001029 LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001030 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001031}
1032
1033void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
1034 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001035 Location first = locations->InAt(0);
1036 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001037 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001038 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001039 DCHECK_EQ(first.As<Register>(),
1040 locations->Out().As<Register>());
1041 if (second.IsRegister()) {
1042 __ subl(first.As<Register>(),
1043 second.As<Register>());
1044 } else if (second.IsConstant()) {
1045 HConstant* instruction = second.GetConstant();
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001046 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001047 __ subl(first.As<Register>(), imm);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001048 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001049 __ subl(first.As<Register>(),
1050 Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001051 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001052 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001053 }
1054
1055 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001056 DCHECK_EQ(first.AsRegisterPairLow<Register>(),
1057 locations->Out().AsRegisterPairLow<Register>());
1058 DCHECK_EQ(first.AsRegisterPairHigh<Register>(),
1059 locations->Out().AsRegisterPairHigh<Register>());
1060 if (second.IsRegister()) {
1061 __ subl(first.AsRegisterPairLow<Register>(),
1062 second.AsRegisterPairLow<Register>());
1063 __ sbbl(first.AsRegisterPairHigh<Register>(),
1064 second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001065 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001066 __ subl(first.AsRegisterPairLow<Register>(),
1067 Address(ESP, second.GetStackIndex()));
1068 __ sbbl(first.AsRegisterPairHigh<Register>(),
1069 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001070 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001071 break;
1072 }
1073
1074 case Primitive::kPrimBoolean:
1075 case Primitive::kPrimByte:
1076 case Primitive::kPrimChar:
1077 case Primitive::kPrimShort:
1078 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
1079 break;
1080
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001081 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001082 LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001083 }
1084}
1085
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001086void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001087 LocationSummary* locations =
1088 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001089 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001090 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001091 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1092 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001093}
1094
1095void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
1096 InvokeRuntimeCallingConvention calling_convention;
1097 LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001098 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001099
Nicolas Geoffray707c8092014-04-04 10:50:14 +01001100 __ fs()->call(
1101 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001102
Nicolas Geoffray39468442014-09-02 15:17:15 +01001103 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001104 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001105}
1106
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001107void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001108 LocationSummary* locations =
1109 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001110 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1111 if (location.IsStackSlot()) {
1112 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1113 } else if (location.IsDoubleStackSlot()) {
1114 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001115 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001116 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001117}
1118
1119void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001120}
1121
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001122void LocationsBuilderX86::VisitNot(HNot* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001123 LocationSummary* locations =
1124 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001125 locations->SetInAt(0, Location::RequiresRegister());
1126 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001127}
1128
1129void InstructionCodeGeneratorX86::VisitNot(HNot* instruction) {
1130 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001131 Location out = locations->Out();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001132 DCHECK_EQ(locations->InAt(0).As<Register>(), out.As<Register>());
1133 __ xorl(out.As<Register>(), Immediate(1));
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001134}
1135
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001136void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001137 LocationSummary* locations =
1138 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001139 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
1140 locations->SetInAt(1, Location::Any(), Location::kDiesAtEntry);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001141 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001142}
1143
1144void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
1145 Label greater, done;
1146 LocationSummary* locations = compare->GetLocations();
1147 switch (compare->InputAt(0)->GetType()) {
1148 case Primitive::kPrimLong: {
1149 Label less, greater, done;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001150 Register output = locations->Out().As<Register>();
1151 Location left = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001152 Location right = locations->InAt(1);
1153 if (right.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001154 __ cmpl(left.AsRegisterPairHigh<Register>(), right.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001155 } else {
1156 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001157 __ cmpl(left.AsRegisterPairHigh<Register>(),
1158 Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001159 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001160 __ j(kLess, &less); // Signed compare.
1161 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001162 if (right.IsRegisterPair()) {
1163 __ cmpl(left.AsRegisterPairLow<Register>(), right.AsRegisterPairLow<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001164 } else {
1165 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001166 __ cmpl(left.AsRegisterPairLow<Register>(), Address(ESP, right.GetStackIndex()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001167 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001168 __ movl(output, Immediate(0));
1169 __ j(kEqual, &done);
1170 __ j(kBelow, &less); // Unsigned compare.
1171
1172 __ Bind(&greater);
1173 __ movl(output, Immediate(1));
1174 __ jmp(&done);
1175
1176 __ Bind(&less);
1177 __ movl(output, Immediate(-1));
1178
1179 __ Bind(&done);
1180 break;
1181 }
1182 default:
1183 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
1184 }
1185}
1186
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001187void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001188 LocationSummary* locations =
1189 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001190 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1191 locations->SetInAt(i, Location::Any());
1192 }
1193 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001194}
1195
1196void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001197 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001198}
1199
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001200void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001201 LocationSummary* locations =
1202 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001203 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001204 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001205 bool is_object_type = field_type == Primitive::kPrimNot;
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001206 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
1207 || (field_type == Primitive::kPrimByte);
1208 // The register allocator does not support multiple
1209 // inputs that die at entry with one in a specific register.
1210 bool dies_at_entry = !is_object_type && !is_byte_type;
1211 if (is_byte_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001212 // Ensure the value is in a byte register.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001213 locations->SetInAt(1, Location::RegisterLocation(EAX), dies_at_entry);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001214 } else {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001215 locations->SetInAt(1, Location::RequiresRegister(), dies_at_entry);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001216 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001217 // Temporary registers for the write barrier.
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001218 if (is_object_type) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001219 locations->AddTemp(Location::RequiresRegister());
1220 // Ensure the card is in a byte register.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001221 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001222 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001223}
1224
1225void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1226 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001227 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001228 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001229 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001230
1231 switch (field_type) {
1232 case Primitive::kPrimBoolean:
1233 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001234 ByteRegister value = locations->InAt(1).As<ByteRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001235 __ movb(Address(obj, offset), value);
1236 break;
1237 }
1238
1239 case Primitive::kPrimShort:
1240 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001241 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001242 __ movw(Address(obj, offset), value);
1243 break;
1244 }
1245
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001246 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001247 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001248 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001249 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001250
1251 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001252 Register temp = locations->GetTemp(0).As<Register>();
1253 Register card = locations->GetTemp(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001254 codegen_->MarkGCCard(temp, card, obj, value);
1255 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001256 break;
1257 }
1258
1259 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001260 Location value = locations->InAt(1);
1261 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
1262 __ movl(Address(obj, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001263 break;
1264 }
1265
1266 case Primitive::kPrimFloat:
1267 case Primitive::kPrimDouble:
1268 LOG(FATAL) << "Unimplemented register type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001269 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001270 case Primitive::kPrimVoid:
1271 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001272 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001273 }
1274}
1275
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001276void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
1277 Label is_null;
1278 __ testl(value, value);
1279 __ j(kEqual, &is_null);
1280 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
1281 __ movl(temp, object);
1282 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
1283 __ movb(Address(temp, card, TIMES_1, 0),
1284 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
1285 __ Bind(&is_null);
1286}
1287
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001288void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001289 LocationSummary* locations =
1290 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001291 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001292 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001293}
1294
1295void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1296 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001297 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001298 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
1299
1300 switch (instruction->GetType()) {
1301 case Primitive::kPrimBoolean: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001302 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001303 __ movzxb(out, Address(obj, offset));
1304 break;
1305 }
1306
1307 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001308 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001309 __ movsxb(out, Address(obj, offset));
1310 break;
1311 }
1312
1313 case Primitive::kPrimShort: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001314 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001315 __ movsxw(out, Address(obj, offset));
1316 break;
1317 }
1318
1319 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001320 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001321 __ movzxw(out, Address(obj, offset));
1322 break;
1323 }
1324
1325 case Primitive::kPrimInt:
1326 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001327 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001328 __ movl(out, Address(obj, offset));
1329 break;
1330 }
1331
1332 case Primitive::kPrimLong: {
1333 // TODO: support volatile.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001334 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(obj, offset));
1335 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(obj, kX86WordSize + offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001336 break;
1337 }
1338
1339 case Primitive::kPrimFloat:
1340 case Primitive::kPrimDouble:
1341 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001342 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001343 case Primitive::kPrimVoid:
1344 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001345 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001346 }
1347}
1348
1349void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001350 LocationSummary* locations =
1351 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001352 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001353 if (instruction->HasUses()) {
1354 locations->SetOut(Location::SameAsFirstInput());
1355 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001356}
1357
1358void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001359 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001360 codegen_->AddSlowPath(slow_path);
1361
1362 LocationSummary* locations = instruction->GetLocations();
1363 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001364
1365 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001366 __ cmpl(obj.As<Register>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001367 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001368 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001369 } else {
1370 DCHECK(obj.IsConstant()) << obj;
1371 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
1372 __ jmp(slow_path->GetEntryLabel());
1373 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001374 }
1375 __ j(kEqual, slow_path->GetEntryLabel());
1376}
1377
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001378void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001379 LocationSummary* locations =
1380 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001381 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
1382 locations->SetInAt(
1383 1, Location::RegisterOrConstant(instruction->InputAt(1)), Location::kDiesAtEntry);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001384 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001385}
1386
1387void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
1388 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001389 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001390 Location index = locations->InAt(1);
1391
1392 switch (instruction->GetType()) {
1393 case Primitive::kPrimBoolean: {
1394 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001395 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001396 if (index.IsConstant()) {
1397 __ movzxb(out, Address(obj,
1398 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1399 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001400 __ movzxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001401 }
1402 break;
1403 }
1404
1405 case Primitive::kPrimByte: {
1406 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001407 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001408 if (index.IsConstant()) {
1409 __ movsxb(out, Address(obj,
1410 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1411 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001412 __ movsxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001413 }
1414 break;
1415 }
1416
1417 case Primitive::kPrimShort: {
1418 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001419 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001420 if (index.IsConstant()) {
1421 __ movsxw(out, Address(obj,
1422 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1423 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001424 __ movsxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001425 }
1426 break;
1427 }
1428
1429 case Primitive::kPrimChar: {
1430 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001431 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001432 if (index.IsConstant()) {
1433 __ movzxw(out, Address(obj,
1434 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1435 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001436 __ movzxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001437 }
1438 break;
1439 }
1440
1441 case Primitive::kPrimInt:
1442 case Primitive::kPrimNot: {
1443 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001444 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001445 if (index.IsConstant()) {
1446 __ movl(out, Address(obj,
1447 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
1448 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001449 __ movl(out, Address(obj, index.As<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001450 }
1451 break;
1452 }
1453
1454 case Primitive::kPrimLong: {
1455 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001456 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001457 if (index.IsConstant()) {
1458 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001459 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
1460 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001461 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001462 __ movl(out.AsRegisterPairLow<Register>(),
1463 Address(obj, index.As<Register>(), TIMES_8, data_offset));
1464 __ movl(out.AsRegisterPairHigh<Register>(),
1465 Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001466 }
1467 break;
1468 }
1469
1470 case Primitive::kPrimFloat:
1471 case Primitive::kPrimDouble:
1472 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001473 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001474 case Primitive::kPrimVoid:
1475 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001476 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001477 }
1478}
1479
1480void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001481 Primitive::Type value_type = instruction->GetComponentType();
1482 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1483 instruction,
1484 value_type == Primitive::kPrimNot ? LocationSummary::kCall : LocationSummary::kNoCall);
1485
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001486 if (value_type == Primitive::kPrimNot) {
1487 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001488 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1489 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1490 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001491 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001492 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
1493 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001494 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001495 // In case of a byte operation, the register allocator does not support multiple
1496 // inputs that die at entry with one in a specific register.
1497 bool dies_at_entry = value_type != Primitive::kPrimLong && !is_byte_type;
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001498 locations->SetInAt(0, Location::RequiresRegister(), dies_at_entry);
1499 locations->SetInAt(
1500 1, Location::RegisterOrConstant(instruction->InputAt(1)), dies_at_entry);
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001501 if (is_byte_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001502 // Ensure the value is in a byte register.
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001503 locations->SetInAt(2, Location::ByteRegisterOrConstant(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001504 EAX, instruction->InputAt(2)), dies_at_entry);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001505 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001506 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)), dies_at_entry);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001507 }
1508 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001509}
1510
1511void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
1512 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001513 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001514 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001515 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001516 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001517
1518 switch (value_type) {
1519 case Primitive::kPrimBoolean:
1520 case Primitive::kPrimByte: {
1521 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001522 if (index.IsConstant()) {
1523 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001524 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001525 __ movb(Address(obj, offset), value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001526 } else {
1527 __ movb(Address(obj, offset),
1528 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1529 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001530 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001531 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001532 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
1533 value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001534 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001535 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001536 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1537 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001538 }
1539 break;
1540 }
1541
1542 case Primitive::kPrimShort:
1543 case Primitive::kPrimChar: {
1544 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001545 if (index.IsConstant()) {
1546 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001547 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001548 __ movw(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001549 } else {
1550 __ movw(Address(obj, offset),
1551 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1552 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001553 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001554 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001555 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
1556 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001557 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001558 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001559 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1560 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001561 }
1562 break;
1563 }
1564
1565 case Primitive::kPrimInt: {
1566 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001567 if (index.IsConstant()) {
1568 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001569 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001570 __ movl(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001571 } else {
1572 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1573 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001574 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001575 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001576 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
1577 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001578 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001579 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001580 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1581 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001582 }
1583 break;
1584 }
1585
1586 case Primitive::kPrimNot: {
1587 DCHECK(!codegen_->IsLeafMethod());
1588 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001589 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001590 break;
1591 }
1592
1593 case Primitive::kPrimLong: {
1594 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001595 if (index.IsConstant()) {
1596 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001597 if (value.IsRegisterPair()) {
1598 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
1599 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001600 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001601 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001602 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
1603 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
1604 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
1605 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001606 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001607 if (value.IsRegisterPair()) {
1608 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
1609 value.AsRegisterPairLow<Register>());
1610 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
1611 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001612 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001613 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001614 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001615 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001616 Immediate(Low32Bits(val)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001617 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001618 Immediate(High32Bits(val)));
1619 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001620 }
1621 break;
1622 }
1623
1624 case Primitive::kPrimFloat:
1625 case Primitive::kPrimDouble:
1626 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001627 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001628 case Primitive::kPrimVoid:
1629 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001630 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001631 }
1632}
1633
1634void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
1635 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001636 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001637 locations->SetOut(Location::RequiresRegister());
1638 instruction->SetLocations(locations);
1639}
1640
1641void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
1642 LocationSummary* locations = instruction->GetLocations();
1643 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001644 Register obj = locations->InAt(0).As<Register>();
1645 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001646 __ movl(out, Address(obj, offset));
1647}
1648
1649void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001650 LocationSummary* locations =
1651 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001652 locations->SetInAt(0, Location::RequiresRegister());
1653 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001654 if (instruction->HasUses()) {
1655 locations->SetOut(Location::SameAsFirstInput());
1656 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001657}
1658
1659void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
1660 LocationSummary* locations = instruction->GetLocations();
1661 SlowPathCode* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01001662 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001663 codegen_->AddSlowPath(slow_path);
1664
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001665 Register index = locations->InAt(0).As<Register>();
1666 Register length = locations->InAt(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001667
1668 __ cmpl(index, length);
1669 __ j(kAboveEqual, slow_path->GetEntryLabel());
1670}
1671
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001672void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
1673 temp->SetLocations(nullptr);
1674}
1675
1676void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
1677 // Nothing to do, this is driven by the code generator.
1678}
1679
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001680void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001681 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001682}
1683
1684void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001685 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
1686}
1687
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001688void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
1689 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
1690}
1691
1692void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001693 HBasicBlock* block = instruction->GetBlock();
1694 if (block->GetLoopInformation() != nullptr) {
1695 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
1696 // The back edge will generate the suspend check.
1697 return;
1698 }
1699 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
1700 // The goto will generate the suspend check.
1701 return;
1702 }
1703 GenerateSuspendCheck(instruction, nullptr);
1704}
1705
1706void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
1707 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001708 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001709 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001710 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001711 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001712 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001713 if (successor == nullptr) {
1714 __ j(kNotEqual, slow_path->GetEntryLabel());
1715 __ Bind(slow_path->GetReturnLabel());
1716 } else {
1717 __ j(kEqual, codegen_->GetLabelOf(successor));
1718 __ jmp(slow_path->GetEntryLabel());
1719 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001720}
1721
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001722X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
1723 return codegen_->GetAssembler();
1724}
1725
1726void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
1727 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001728 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001729 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
1730 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
1731 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
1732}
1733
1734void ParallelMoveResolverX86::EmitMove(size_t index) {
1735 MoveOperands* move = moves_.Get(index);
1736 Location source = move->GetSource();
1737 Location destination = move->GetDestination();
1738
1739 if (source.IsRegister()) {
1740 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001741 __ movl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001742 } else {
1743 DCHECK(destination.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001744 __ movl(Address(ESP, destination.GetStackIndex()), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001745 }
1746 } else if (source.IsStackSlot()) {
1747 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001748 __ movl(destination.As<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001749 } else {
1750 DCHECK(destination.IsStackSlot());
1751 MoveMemoryToMemory(destination.GetStackIndex(),
1752 source.GetStackIndex());
1753 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001754 } else if (source.IsConstant()) {
1755 HIntConstant* instruction = source.GetConstant()->AsIntConstant();
1756 Immediate imm(instruction->AsIntConstant()->GetValue());
1757 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001758 __ movl(destination.As<Register>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001759 } else {
1760 __ movl(Address(ESP, destination.GetStackIndex()), imm);
1761 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001762 } else {
1763 LOG(FATAL) << "Unimplemented";
1764 }
1765}
1766
1767void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001768 Register suggested_scratch = reg == EAX ? EBX : EAX;
1769 ScratchRegisterScope ensure_scratch(
1770 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
1771
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001772 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
1773 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
1774 __ movl(Address(ESP, mem + stack_offset), reg);
1775 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
1776}
1777
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001778void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
1779 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001780 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
1781
1782 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001783 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001784 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
1785
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001786 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
1787 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
1788 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
1789 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
1790 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
1791 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
1792}
1793
1794void ParallelMoveResolverX86::EmitSwap(size_t index) {
1795 MoveOperands* move = moves_.Get(index);
1796 Location source = move->GetSource();
1797 Location destination = move->GetDestination();
1798
1799 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001800 __ xchgl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001801 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001802 Exchange(source.As<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001803 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001804 Exchange(destination.As<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001805 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
1806 Exchange(destination.GetStackIndex(), source.GetStackIndex());
1807 } else {
1808 LOG(FATAL) << "Unimplemented";
1809 }
1810}
1811
1812void ParallelMoveResolverX86::SpillScratch(int reg) {
1813 __ pushl(static_cast<Register>(reg));
1814}
1815
1816void ParallelMoveResolverX86::RestoreScratch(int reg) {
1817 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001818}
1819
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001820} // namespace x86
1821} // namespace art