blob: 5fc389569bd339331b0fc5936e58b35d1818eb82 [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) {
Roland Levillain476df552014-10-09 17:51:36 +0100471 if (instruction->IsIntConstant()) {
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 }
Roland Levillain476df552014-10-09 17:51:36 +0100478 } else if (instruction->IsLongConstant()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100479 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 }
Roland Levillain476df552014-10-09 17:51:36 +0100487 } else if (instruction->IsLoadLocal()) {
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);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100580 if (cond->IsIntConstant()) {
581 // Constant condition, statically compared against 1.
582 int32_t cond_value = cond->AsIntConstant()->GetValue();
583 if (cond_value == 1) {
584 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
585 if_instr->IfTrueSuccessor())) {
586 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100587 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100588 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100589 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100590 DCHECK_EQ(cond_value, 0);
591 }
592 } else {
593 bool materialized =
594 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
595 // Moves do not affect the eflags register, so if the condition is
596 // evaluated just before the if, we don't need to evaluate it
597 // again.
598 bool eflags_set = cond->IsCondition()
599 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
600 if (materialized) {
601 if (!eflags_set) {
602 // Materialized condition, compare against 0.
603 Location lhs = if_instr->GetLocations()->InAt(0);
604 if (lhs.IsRegister()) {
605 __ cmpl(lhs.As<Register>(), Immediate(0));
606 } else {
607 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
608 }
609 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
610 } else {
611 __ j(X86Condition(cond->AsCondition()->GetCondition()),
612 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
613 }
614 } else {
615 Location lhs = cond->GetLocations()->InAt(0);
616 Location rhs = cond->GetLocations()->InAt(1);
617 // LHS is guaranteed to be in a register (see
618 // LocationsBuilderX86::VisitCondition).
619 if (rhs.IsRegister()) {
620 __ cmpl(lhs.As<Register>(), rhs.As<Register>());
621 } else if (rhs.IsConstant()) {
622 HIntConstant* instruction = rhs.GetConstant()->AsIntConstant();
623 Immediate imm(instruction->AsIntConstant()->GetValue());
624 __ cmpl(lhs.As<Register>(), imm);
625 } else {
626 __ cmpl(lhs.As<Register>(), Address(ESP, rhs.GetStackIndex()));
627 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100628 __ j(X86Condition(cond->AsCondition()->GetCondition()),
629 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700630 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100631 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100632 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
633 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700634 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000635 }
636}
637
638void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000639 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000640}
641
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000642void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
643 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000644}
645
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000646void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100647 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000648}
649
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000650void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100651 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000652}
653
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100654void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100655 LocationSummary* locations =
656 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100657 switch (store->InputAt(1)->GetType()) {
658 case Primitive::kPrimBoolean:
659 case Primitive::kPrimByte:
660 case Primitive::kPrimChar:
661 case Primitive::kPrimShort:
662 case Primitive::kPrimInt:
663 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100664 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100665 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
666 break;
667
668 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100669 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100670 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
671 break;
672
673 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100674 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100675 }
676 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000677}
678
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000679void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000680}
681
Dave Allison20dfc792014-06-16 20:44:29 -0700682void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100683 LocationSummary* locations =
684 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +0100685 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
686 locations->SetInAt(1, Location::Any(), Location::kDiesAtEntry);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100687 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100688 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100689 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000690}
691
Dave Allison20dfc792014-06-16 20:44:29 -0700692void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
693 if (comp->NeedsMaterialization()) {
694 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100695 Register reg = locations->Out().As<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100696 // Clear register: setcc only sets the low byte.
697 __ xorl(reg, reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700698 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100699 __ cmpl(locations->InAt(0).As<Register>(),
700 locations->InAt(1).As<Register>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100701 } else if (locations->InAt(1).IsConstant()) {
702 HConstant* instruction = locations->InAt(1).GetConstant();
703 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100704 __ cmpl(locations->InAt(0).As<Register>(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700705 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100706 __ cmpl(locations->InAt(0).As<Register>(),
Dave Allison20dfc792014-06-16 20:44:29 -0700707 Address(ESP, locations->InAt(1).GetStackIndex()));
708 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100709 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100710 }
Dave Allison20dfc792014-06-16 20:44:29 -0700711}
712
713void LocationsBuilderX86::VisitEqual(HEqual* comp) {
714 VisitCondition(comp);
715}
716
717void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
718 VisitCondition(comp);
719}
720
721void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
722 VisitCondition(comp);
723}
724
725void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
726 VisitCondition(comp);
727}
728
729void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
730 VisitCondition(comp);
731}
732
733void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
734 VisitCondition(comp);
735}
736
737void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
738 VisitCondition(comp);
739}
740
741void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
742 VisitCondition(comp);
743}
744
745void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
746 VisitCondition(comp);
747}
748
749void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
750 VisitCondition(comp);
751}
752
753void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
754 VisitCondition(comp);
755}
756
757void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
758 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000759}
760
761void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100762 LocationSummary* locations =
763 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100764 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000765}
766
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000767void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100768 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000769}
770
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100771void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100772 LocationSummary* locations =
773 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100774 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100775}
776
777void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
778 // Will be generated at use site.
779}
780
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000781void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000782 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000783}
784
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000785void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
786 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000787 __ ret();
788}
789
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000790void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100791 LocationSummary* locations =
792 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100793 switch (ret->InputAt(0)->GetType()) {
794 case Primitive::kPrimBoolean:
795 case Primitive::kPrimByte:
796 case Primitive::kPrimChar:
797 case Primitive::kPrimShort:
798 case Primitive::kPrimInt:
799 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100800 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100801 break;
802
803 case Primitive::kPrimLong:
804 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100805 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100806 break;
807
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100808 case Primitive::kPrimFloat:
809 case Primitive::kPrimDouble:
810 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100811 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100812 break;
813
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100814 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100815 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100816 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000817}
818
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000819void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100820 if (kIsDebugBuild) {
821 switch (ret->InputAt(0)->GetType()) {
822 case Primitive::kPrimBoolean:
823 case Primitive::kPrimByte:
824 case Primitive::kPrimChar:
825 case Primitive::kPrimShort:
826 case Primitive::kPrimInt:
827 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100828 DCHECK_EQ(ret->GetLocations()->InAt(0).As<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100829 break;
830
831 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100832 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
833 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100834 break;
835
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100836 case Primitive::kPrimFloat:
837 case Primitive::kPrimDouble:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100838 DCHECK_EQ(ret->GetLocations()->InAt(0).As<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100839 break;
840
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100841 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100842 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100843 }
844 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000845 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000846 __ ret();
847}
848
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000849void LocationsBuilderX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100850 HandleInvoke(invoke);
851}
852
853void InstructionCodeGeneratorX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100854 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100855 uint32_t heap_reference_size = sizeof(mirror::HeapReference<mirror::Object>);
856 size_t index_in_cache = mirror::Array::DataOffset(heap_reference_size).Int32Value() +
857 invoke->GetIndexInDexCache() * kX86WordSize;
858
859 // TODO: Implement all kinds of calls:
860 // 1) boot -> boot
861 // 2) app -> boot
862 // 3) app -> app
863 //
864 // Currently we implement the app -> app logic, which looks up in the resolve cache.
865
866 // temp = method;
867 LoadCurrentMethod(temp);
868 // temp = temp->dex_cache_resolved_methods_;
869 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
870 // temp = temp[index_in_cache]
871 __ movl(temp, Address(temp, index_in_cache));
872 // (temp + offset_of_quick_compiled_code)()
873 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
874
875 DCHECK(!codegen_->IsLeafMethod());
876 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
877}
878
879void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
880 HandleInvoke(invoke);
881}
882
883void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100884 LocationSummary* locations =
885 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100886 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100887
888 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100889 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100890 HInstruction* input = invoke->InputAt(i);
891 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
892 }
893
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100894 switch (invoke->GetType()) {
895 case Primitive::kPrimBoolean:
896 case Primitive::kPrimByte:
897 case Primitive::kPrimChar:
898 case Primitive::kPrimShort:
899 case Primitive::kPrimInt:
900 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100901 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100902 break;
903
904 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100905 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100906 break;
907
908 case Primitive::kPrimVoid:
909 break;
910
911 case Primitive::kPrimDouble:
912 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100913 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100914 break;
915 }
916
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000917 invoke->SetLocations(locations);
918}
919
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100920void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100921 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100922 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
923 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
924 LocationSummary* locations = invoke->GetLocations();
925 Location receiver = locations->InAt(0);
926 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
927 // temp = object->GetClass();
928 if (receiver.IsStackSlot()) {
929 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
930 __ movl(temp, Address(temp, class_offset));
931 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100932 __ movl(temp, Address(receiver.As<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100933 }
934 // temp = temp->GetMethodAt(method_offset);
935 __ movl(temp, Address(temp, method_offset));
936 // call temp->GetEntryPoint();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000937 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
938
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100939 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +0100940 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000941}
942
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000943void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100944 LocationSummary* locations =
945 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000946 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100947 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100948 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100949 locations->SetInAt(0, Location::RequiresRegister());
950 locations->SetInAt(1, Location::Any());
951 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100952 break;
953 }
954
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100955 case Primitive::kPrimFloat:
956 case Primitive::kPrimDouble: {
957 locations->SetInAt(0, Location::RequiresFpuRegister());
958 locations->SetInAt(1, Location::Any());
959 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100960 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100961 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100962
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000963 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100964 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
965 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000966 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000967}
968
969void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
970 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100971 Location first = locations->InAt(0);
972 Location second = locations->InAt(1);
973
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000974 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100975 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100976 DCHECK_EQ(first.As<Register>(), locations->Out().As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100977 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100978 __ addl(first.As<Register>(), second.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100979 } else if (second.IsConstant()) {
980 HConstant* instruction = second.GetConstant();
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100981 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100982 __ addl(first.As<Register>(), imm);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100983 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100984 __ addl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100985 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000986 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100987 }
988
989 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100990 DCHECK_EQ(first.AsRegisterPairLow<Register>(),
991 locations->Out().AsRegisterPairLow<Register>());
992 DCHECK_EQ(first.AsRegisterPairHigh<Register>(),
993 locations->Out().AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100994 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100995 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
996 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100997 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100998 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
999 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001000 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001001 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001002 break;
1003 }
1004
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001005 case Primitive::kPrimFloat: {
1006 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001007 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001008 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001009 __ addss(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001010 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001011 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001012 }
1013
1014 case Primitive::kPrimDouble: {
1015 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001016 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001017 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001018 __ addsd(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001019 }
1020 break;
1021 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001022
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001023 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001024 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001025 }
1026}
1027
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001028void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001029 LocationSummary* locations =
1030 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001031 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001032 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001033 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001034 locations->SetInAt(0, Location::RequiresRegister());
1035 locations->SetInAt(1, Location::Any());
1036 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001037 break;
1038 }
1039
1040 case Primitive::kPrimBoolean:
1041 case Primitive::kPrimByte:
1042 case Primitive::kPrimChar:
1043 case Primitive::kPrimShort:
1044 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
1045 break;
1046
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001047 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001048 LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001049 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001050}
1051
1052void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
1053 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001054 Location first = locations->InAt(0);
1055 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001056 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001057 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001058 DCHECK_EQ(first.As<Register>(),
1059 locations->Out().As<Register>());
1060 if (second.IsRegister()) {
1061 __ subl(first.As<Register>(),
1062 second.As<Register>());
1063 } else if (second.IsConstant()) {
1064 HConstant* instruction = second.GetConstant();
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001065 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001066 __ subl(first.As<Register>(), imm);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001067 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001068 __ subl(first.As<Register>(),
1069 Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001070 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001071 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001072 }
1073
1074 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001075 DCHECK_EQ(first.AsRegisterPairLow<Register>(),
1076 locations->Out().AsRegisterPairLow<Register>());
1077 DCHECK_EQ(first.AsRegisterPairHigh<Register>(),
1078 locations->Out().AsRegisterPairHigh<Register>());
1079 if (second.IsRegister()) {
1080 __ subl(first.AsRegisterPairLow<Register>(),
1081 second.AsRegisterPairLow<Register>());
1082 __ sbbl(first.AsRegisterPairHigh<Register>(),
1083 second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001084 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001085 __ subl(first.AsRegisterPairLow<Register>(),
1086 Address(ESP, second.GetStackIndex()));
1087 __ sbbl(first.AsRegisterPairHigh<Register>(),
1088 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001089 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001090 break;
1091 }
1092
1093 case Primitive::kPrimBoolean:
1094 case Primitive::kPrimByte:
1095 case Primitive::kPrimChar:
1096 case Primitive::kPrimShort:
1097 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
1098 break;
1099
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001100 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001101 LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001102 }
1103}
1104
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001105void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001106 LocationSummary* locations =
1107 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001108 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001109 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001110 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1111 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001112}
1113
1114void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
1115 InvokeRuntimeCallingConvention calling_convention;
1116 LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001117 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001118
Nicolas Geoffray707c8092014-04-04 10:50:14 +01001119 __ fs()->call(
1120 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001121
Nicolas Geoffray39468442014-09-02 15:17:15 +01001122 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001123 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001124}
1125
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001126void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001127 LocationSummary* locations =
1128 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001129 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1130 if (location.IsStackSlot()) {
1131 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1132 } else if (location.IsDoubleStackSlot()) {
1133 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001134 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001135 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001136}
1137
1138void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001139}
1140
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001141void LocationsBuilderX86::VisitNot(HNot* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001142 LocationSummary* locations =
1143 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001144 locations->SetInAt(0, Location::RequiresRegister());
1145 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001146}
1147
1148void InstructionCodeGeneratorX86::VisitNot(HNot* instruction) {
1149 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001150 Location out = locations->Out();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001151 DCHECK_EQ(locations->InAt(0).As<Register>(), out.As<Register>());
1152 __ xorl(out.As<Register>(), Immediate(1));
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001153}
1154
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001155void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001156 LocationSummary* locations =
1157 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001158 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
1159 locations->SetInAt(1, Location::Any(), Location::kDiesAtEntry);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001160 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001161}
1162
1163void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
1164 Label greater, done;
1165 LocationSummary* locations = compare->GetLocations();
1166 switch (compare->InputAt(0)->GetType()) {
1167 case Primitive::kPrimLong: {
1168 Label less, greater, done;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001169 Register output = locations->Out().As<Register>();
1170 Location left = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001171 Location right = locations->InAt(1);
1172 if (right.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001173 __ cmpl(left.AsRegisterPairHigh<Register>(), right.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001174 } else {
1175 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001176 __ cmpl(left.AsRegisterPairHigh<Register>(),
1177 Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001178 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001179 __ j(kLess, &less); // Signed compare.
1180 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001181 if (right.IsRegisterPair()) {
1182 __ cmpl(left.AsRegisterPairLow<Register>(), right.AsRegisterPairLow<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001183 } else {
1184 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001185 __ cmpl(left.AsRegisterPairLow<Register>(), Address(ESP, right.GetStackIndex()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001186 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001187 __ movl(output, Immediate(0));
1188 __ j(kEqual, &done);
1189 __ j(kBelow, &less); // Unsigned compare.
1190
1191 __ Bind(&greater);
1192 __ movl(output, Immediate(1));
1193 __ jmp(&done);
1194
1195 __ Bind(&less);
1196 __ movl(output, Immediate(-1));
1197
1198 __ Bind(&done);
1199 break;
1200 }
1201 default:
1202 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
1203 }
1204}
1205
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001206void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001207 LocationSummary* locations =
1208 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001209 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1210 locations->SetInAt(i, Location::Any());
1211 }
1212 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001213}
1214
1215void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001216 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001217}
1218
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001219void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001220 LocationSummary* locations =
1221 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001222 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001223 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001224 bool is_object_type = field_type == Primitive::kPrimNot;
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001225 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
1226 || (field_type == Primitive::kPrimByte);
1227 // The register allocator does not support multiple
1228 // inputs that die at entry with one in a specific register.
1229 bool dies_at_entry = !is_object_type && !is_byte_type;
1230 if (is_byte_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001231 // Ensure the value is in a byte register.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001232 locations->SetInAt(1, Location::RegisterLocation(EAX), dies_at_entry);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001233 } else {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001234 locations->SetInAt(1, Location::RequiresRegister(), dies_at_entry);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001235 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001236 // Temporary registers for the write barrier.
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001237 if (is_object_type) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001238 locations->AddTemp(Location::RequiresRegister());
1239 // Ensure the card is in a byte register.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001240 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001241 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001242}
1243
1244void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1245 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001246 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001247 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001248 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001249
1250 switch (field_type) {
1251 case Primitive::kPrimBoolean:
1252 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001253 ByteRegister value = locations->InAt(1).As<ByteRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001254 __ movb(Address(obj, offset), value);
1255 break;
1256 }
1257
1258 case Primitive::kPrimShort:
1259 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001260 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001261 __ movw(Address(obj, offset), value);
1262 break;
1263 }
1264
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001265 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001266 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001267 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001268 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001269
1270 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001271 Register temp = locations->GetTemp(0).As<Register>();
1272 Register card = locations->GetTemp(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001273 codegen_->MarkGCCard(temp, card, obj, value);
1274 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001275 break;
1276 }
1277
1278 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001279 Location value = locations->InAt(1);
1280 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
1281 __ movl(Address(obj, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001282 break;
1283 }
1284
1285 case Primitive::kPrimFloat:
1286 case Primitive::kPrimDouble:
1287 LOG(FATAL) << "Unimplemented register type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001288 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001289 case Primitive::kPrimVoid:
1290 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001291 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001292 }
1293}
1294
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001295void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
1296 Label is_null;
1297 __ testl(value, value);
1298 __ j(kEqual, &is_null);
1299 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
1300 __ movl(temp, object);
1301 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
1302 __ movb(Address(temp, card, TIMES_1, 0),
1303 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
1304 __ Bind(&is_null);
1305}
1306
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001307void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001308 LocationSummary* locations =
1309 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001310 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001311 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001312}
1313
1314void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1315 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001316 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001317 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
1318
1319 switch (instruction->GetType()) {
1320 case Primitive::kPrimBoolean: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001321 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001322 __ movzxb(out, Address(obj, offset));
1323 break;
1324 }
1325
1326 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001327 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001328 __ movsxb(out, Address(obj, offset));
1329 break;
1330 }
1331
1332 case Primitive::kPrimShort: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001333 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001334 __ movsxw(out, Address(obj, offset));
1335 break;
1336 }
1337
1338 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001339 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001340 __ movzxw(out, Address(obj, offset));
1341 break;
1342 }
1343
1344 case Primitive::kPrimInt:
1345 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001346 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001347 __ movl(out, Address(obj, offset));
1348 break;
1349 }
1350
1351 case Primitive::kPrimLong: {
1352 // TODO: support volatile.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001353 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(obj, offset));
1354 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(obj, kX86WordSize + offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001355 break;
1356 }
1357
1358 case Primitive::kPrimFloat:
1359 case Primitive::kPrimDouble:
1360 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001361 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001362 case Primitive::kPrimVoid:
1363 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001364 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001365 }
1366}
1367
1368void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001369 LocationSummary* locations =
1370 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001371 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001372 if (instruction->HasUses()) {
1373 locations->SetOut(Location::SameAsFirstInput());
1374 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001375}
1376
1377void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001378 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001379 codegen_->AddSlowPath(slow_path);
1380
1381 LocationSummary* locations = instruction->GetLocations();
1382 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001383
1384 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001385 __ cmpl(obj.As<Register>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001386 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001387 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001388 } else {
1389 DCHECK(obj.IsConstant()) << obj;
1390 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
1391 __ jmp(slow_path->GetEntryLabel());
1392 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001393 }
1394 __ j(kEqual, slow_path->GetEntryLabel());
1395}
1396
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001397void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001398 LocationSummary* locations =
1399 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001400 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
1401 locations->SetInAt(
1402 1, Location::RegisterOrConstant(instruction->InputAt(1)), Location::kDiesAtEntry);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001403 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001404}
1405
1406void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
1407 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001408 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001409 Location index = locations->InAt(1);
1410
1411 switch (instruction->GetType()) {
1412 case Primitive::kPrimBoolean: {
1413 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001414 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001415 if (index.IsConstant()) {
1416 __ movzxb(out, Address(obj,
1417 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1418 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001419 __ movzxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001420 }
1421 break;
1422 }
1423
1424 case Primitive::kPrimByte: {
1425 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001426 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001427 if (index.IsConstant()) {
1428 __ movsxb(out, Address(obj,
1429 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1430 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001431 __ movsxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001432 }
1433 break;
1434 }
1435
1436 case Primitive::kPrimShort: {
1437 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001438 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001439 if (index.IsConstant()) {
1440 __ movsxw(out, Address(obj,
1441 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1442 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001443 __ movsxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001444 }
1445 break;
1446 }
1447
1448 case Primitive::kPrimChar: {
1449 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001450 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001451 if (index.IsConstant()) {
1452 __ movzxw(out, Address(obj,
1453 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1454 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001455 __ movzxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001456 }
1457 break;
1458 }
1459
1460 case Primitive::kPrimInt:
1461 case Primitive::kPrimNot: {
1462 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001463 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001464 if (index.IsConstant()) {
1465 __ movl(out, Address(obj,
1466 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
1467 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001468 __ movl(out, Address(obj, index.As<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001469 }
1470 break;
1471 }
1472
1473 case Primitive::kPrimLong: {
1474 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001475 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001476 if (index.IsConstant()) {
1477 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001478 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
1479 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001480 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001481 __ movl(out.AsRegisterPairLow<Register>(),
1482 Address(obj, index.As<Register>(), TIMES_8, data_offset));
1483 __ movl(out.AsRegisterPairHigh<Register>(),
1484 Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001485 }
1486 break;
1487 }
1488
1489 case Primitive::kPrimFloat:
1490 case Primitive::kPrimDouble:
1491 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001492 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001493 case Primitive::kPrimVoid:
1494 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001495 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001496 }
1497}
1498
1499void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001500 Primitive::Type value_type = instruction->GetComponentType();
1501 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1502 instruction,
1503 value_type == Primitive::kPrimNot ? LocationSummary::kCall : LocationSummary::kNoCall);
1504
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001505 if (value_type == Primitive::kPrimNot) {
1506 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001507 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1508 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1509 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001510 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001511 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
1512 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001513 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001514 // In case of a byte operation, the register allocator does not support multiple
1515 // inputs that die at entry with one in a specific register.
1516 bool dies_at_entry = value_type != Primitive::kPrimLong && !is_byte_type;
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001517 locations->SetInAt(0, Location::RequiresRegister(), dies_at_entry);
1518 locations->SetInAt(
1519 1, Location::RegisterOrConstant(instruction->InputAt(1)), dies_at_entry);
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001520 if (is_byte_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001521 // Ensure the value is in a byte register.
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001522 locations->SetInAt(2, Location::ByteRegisterOrConstant(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001523 EAX, instruction->InputAt(2)), dies_at_entry);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001524 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001525 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)), dies_at_entry);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001526 }
1527 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001528}
1529
1530void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
1531 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001532 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001533 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001534 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001535 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001536
1537 switch (value_type) {
1538 case Primitive::kPrimBoolean:
1539 case Primitive::kPrimByte: {
1540 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001541 if (index.IsConstant()) {
1542 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001543 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001544 __ movb(Address(obj, offset), value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001545 } else {
1546 __ movb(Address(obj, offset),
1547 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1548 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001549 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001550 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001551 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
1552 value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001553 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001554 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001555 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1556 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001557 }
1558 break;
1559 }
1560
1561 case Primitive::kPrimShort:
1562 case Primitive::kPrimChar: {
1563 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001564 if (index.IsConstant()) {
1565 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001566 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001567 __ movw(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001568 } else {
1569 __ movw(Address(obj, offset),
1570 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1571 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001572 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001573 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001574 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
1575 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001576 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001577 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001578 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1579 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001580 }
1581 break;
1582 }
1583
1584 case Primitive::kPrimInt: {
1585 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001586 if (index.IsConstant()) {
1587 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001588 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001589 __ movl(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001590 } else {
1591 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1592 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001593 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001594 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001595 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
1596 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001597 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001598 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001599 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1600 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001601 }
1602 break;
1603 }
1604
1605 case Primitive::kPrimNot: {
1606 DCHECK(!codegen_->IsLeafMethod());
1607 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001608 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001609 break;
1610 }
1611
1612 case Primitive::kPrimLong: {
1613 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001614 if (index.IsConstant()) {
1615 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001616 if (value.IsRegisterPair()) {
1617 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
1618 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001619 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001620 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001621 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
1622 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
1623 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
1624 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001625 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001626 if (value.IsRegisterPair()) {
1627 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
1628 value.AsRegisterPairLow<Register>());
1629 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
1630 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001631 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001632 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001633 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001634 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001635 Immediate(Low32Bits(val)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001636 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001637 Immediate(High32Bits(val)));
1638 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001639 }
1640 break;
1641 }
1642
1643 case Primitive::kPrimFloat:
1644 case Primitive::kPrimDouble:
1645 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001646 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001647 case Primitive::kPrimVoid:
1648 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001649 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001650 }
1651}
1652
1653void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
1654 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001655 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001656 locations->SetOut(Location::RequiresRegister());
1657 instruction->SetLocations(locations);
1658}
1659
1660void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
1661 LocationSummary* locations = instruction->GetLocations();
1662 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001663 Register obj = locations->InAt(0).As<Register>();
1664 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001665 __ movl(out, Address(obj, offset));
1666}
1667
1668void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001669 LocationSummary* locations =
1670 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001671 locations->SetInAt(0, Location::RequiresRegister());
1672 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001673 if (instruction->HasUses()) {
1674 locations->SetOut(Location::SameAsFirstInput());
1675 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001676}
1677
1678void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
1679 LocationSummary* locations = instruction->GetLocations();
1680 SlowPathCode* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01001681 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001682 codegen_->AddSlowPath(slow_path);
1683
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001684 Register index = locations->InAt(0).As<Register>();
1685 Register length = locations->InAt(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001686
1687 __ cmpl(index, length);
1688 __ j(kAboveEqual, slow_path->GetEntryLabel());
1689}
1690
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001691void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
1692 temp->SetLocations(nullptr);
1693}
1694
1695void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
1696 // Nothing to do, this is driven by the code generator.
1697}
1698
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001699void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001700 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001701}
1702
1703void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001704 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
1705}
1706
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001707void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
1708 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
1709}
1710
1711void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001712 HBasicBlock* block = instruction->GetBlock();
1713 if (block->GetLoopInformation() != nullptr) {
1714 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
1715 // The back edge will generate the suspend check.
1716 return;
1717 }
1718 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
1719 // The goto will generate the suspend check.
1720 return;
1721 }
1722 GenerateSuspendCheck(instruction, nullptr);
1723}
1724
1725void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
1726 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001727 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001728 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001729 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001730 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001731 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001732 if (successor == nullptr) {
1733 __ j(kNotEqual, slow_path->GetEntryLabel());
1734 __ Bind(slow_path->GetReturnLabel());
1735 } else {
1736 __ j(kEqual, codegen_->GetLabelOf(successor));
1737 __ jmp(slow_path->GetEntryLabel());
1738 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001739}
1740
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001741X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
1742 return codegen_->GetAssembler();
1743}
1744
1745void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
1746 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001747 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001748 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
1749 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
1750 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
1751}
1752
1753void ParallelMoveResolverX86::EmitMove(size_t index) {
1754 MoveOperands* move = moves_.Get(index);
1755 Location source = move->GetSource();
1756 Location destination = move->GetDestination();
1757
1758 if (source.IsRegister()) {
1759 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001760 __ movl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001761 } else {
1762 DCHECK(destination.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001763 __ movl(Address(ESP, destination.GetStackIndex()), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001764 }
1765 } else if (source.IsStackSlot()) {
1766 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001767 __ movl(destination.As<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001768 } else {
1769 DCHECK(destination.IsStackSlot());
1770 MoveMemoryToMemory(destination.GetStackIndex(),
1771 source.GetStackIndex());
1772 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001773 } else if (source.IsConstant()) {
1774 HIntConstant* instruction = source.GetConstant()->AsIntConstant();
1775 Immediate imm(instruction->AsIntConstant()->GetValue());
1776 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001777 __ movl(destination.As<Register>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001778 } else {
1779 __ movl(Address(ESP, destination.GetStackIndex()), imm);
1780 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001781 } else {
1782 LOG(FATAL) << "Unimplemented";
1783 }
1784}
1785
1786void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001787 Register suggested_scratch = reg == EAX ? EBX : EAX;
1788 ScratchRegisterScope ensure_scratch(
1789 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
1790
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001791 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
1792 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
1793 __ movl(Address(ESP, mem + stack_offset), reg);
1794 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
1795}
1796
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001797void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
1798 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001799 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
1800
1801 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001802 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001803 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
1804
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001805 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
1806 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
1807 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
1808 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
1809 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
1810 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
1811}
1812
1813void ParallelMoveResolverX86::EmitSwap(size_t index) {
1814 MoveOperands* move = moves_.Get(index);
1815 Location source = move->GetSource();
1816 Location destination = move->GetDestination();
1817
1818 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001819 __ xchgl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001820 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001821 Exchange(source.As<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001822 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001823 Exchange(destination.As<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001824 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
1825 Exchange(destination.GetStackIndex(), source.GetStackIndex());
1826 } else {
1827 LOG(FATAL) << "Unimplemented";
1828 }
1829}
1830
1831void ParallelMoveResolverX86::SpillScratch(int reg) {
1832 __ pushl(static_cast<Register>(reg));
1833}
1834
1835void ParallelMoveResolverX86::RestoreScratch(int reg) {
1836 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001837}
1838
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001839} // namespace x86
1840} // namespace art