blob: 0f6d6359ae1b038600306788cb0f2707c619d450 [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
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010059class SlowPathCodeX86 : public SlowPathCode {
60 public:
61 SlowPathCodeX86() : entry_label_(), exit_label_() {}
62
63 Label* GetEntryLabel() { return &entry_label_; }
64 Label* GetExitLabel() { return &exit_label_; }
65
66 private:
67 Label entry_label_;
68 Label exit_label_;
69
70 DISALLOW_COPY_AND_ASSIGN(SlowPathCodeX86);
71};
72
73class NullCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010074 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010075 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010076
77 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
78 __ Bind(GetEntryLabel());
79 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowNullPointer)));
Nicolas Geoffray39468442014-09-02 15:17:15 +010080 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010081 }
82
83 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010084 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010085 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
86};
87
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010088class StackOverflowCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010089 public:
90 StackOverflowCheckSlowPathX86() {}
91
92 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
93 __ Bind(GetEntryLabel());
94 __ addl(ESP,
95 Immediate(codegen->GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
96 __ fs()->jmp(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowStackOverflow)));
97 }
98
99 private:
100 DISALLOW_COPY_AND_ASSIGN(StackOverflowCheckSlowPathX86);
101};
102
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100103class BoundsCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100104 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100105 BoundsCheckSlowPathX86(HBoundsCheck* instruction,
106 Location index_location,
107 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +0100108 : instruction_(instruction), index_location_(index_location), length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100109
110 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100111 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100112 __ Bind(GetEntryLabel());
113 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100114 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), index_location_);
115 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(1)), length_location_);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100116 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowArrayBounds)));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100117 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100118 }
119
120 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100121 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100122 const Location index_location_;
123 const Location length_location_;
124
125 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
126};
127
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100128class SuspendCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000129 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100130 explicit SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
131 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000132
133 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100134 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000135 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100136 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000137 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pTestSuspend)));
138 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100139 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100140 if (successor_ == nullptr) {
141 __ jmp(GetReturnLabel());
142 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100143 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100144 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000145 }
146
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100147 Label* GetReturnLabel() {
148 DCHECK(successor_ == nullptr);
149 return &return_label_;
150 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000151
152 private:
153 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100154 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000155 Label return_label_;
156
157 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
158};
159
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100160#undef __
161#define __ reinterpret_cast<X86Assembler*>(GetAssembler())->
162
Dave Allison20dfc792014-06-16 20:44:29 -0700163inline Condition X86Condition(IfCondition cond) {
164 switch (cond) {
165 case kCondEQ: return kEqual;
166 case kCondNE: return kNotEqual;
167 case kCondLT: return kLess;
168 case kCondLE: return kLessEqual;
169 case kCondGT: return kGreater;
170 case kCondGE: return kGreaterEqual;
171 default:
172 LOG(FATAL) << "Unknown if condition";
173 }
174 return kEqual;
175}
176
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100177void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
178 stream << X86ManagedRegister::FromCpuRegister(Register(reg));
179}
180
181void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
182 stream << X86ManagedRegister::FromXmmRegister(XmmRegister(reg));
183}
184
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100185void CodeGeneratorX86::SaveCoreRegister(Location stack_location, uint32_t reg_id) {
186 __ movl(Address(ESP, stack_location.GetStackIndex()), static_cast<Register>(reg_id));
187}
188
189void CodeGeneratorX86::RestoreCoreRegister(Location stack_location, uint32_t reg_id) {
190 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_location.GetStackIndex()));
191}
192
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100193CodeGeneratorX86::CodeGeneratorX86(HGraph* graph)
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100194 : CodeGenerator(graph, kNumberOfCpuRegisters, kNumberOfXmmRegisters, kNumberOfRegisterPairs),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100195 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100196 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100197 instruction_visitor_(graph, this),
198 move_resolver_(graph->GetArena(), this) {}
199
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100200size_t CodeGeneratorX86::FrameEntrySpillSize() const {
201 return kNumberOfPushedRegistersAtEntry * kX86WordSize;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100202}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100203
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100204Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100205 switch (type) {
206 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100207 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100208 X86ManagedRegister pair =
209 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100210 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
211 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100212 // Block all other register pairs that share a register with `pair`.
213 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
214 X86ManagedRegister current =
215 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
216 if (current.AsRegisterPairLow() == pair.AsRegisterPairLow()
217 || current.AsRegisterPairLow() == pair.AsRegisterPairHigh()
218 || current.AsRegisterPairHigh() == pair.AsRegisterPairLow()
219 || current.AsRegisterPairHigh() == pair.AsRegisterPairHigh()) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100220 blocked_register_pairs_[i] = true;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100221 }
222 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100223 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100224 }
225
226 case Primitive::kPrimByte:
227 case Primitive::kPrimBoolean:
228 case Primitive::kPrimChar:
229 case Primitive::kPrimShort:
230 case Primitive::kPrimInt:
231 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100232 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100233 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100234 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100235 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
236 X86ManagedRegister current =
237 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
238 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100239 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100240 }
241 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100242 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100243 }
244
245 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100246 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100247 return Location::FpuRegisterLocation(
248 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100249 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100250
251 case Primitive::kPrimVoid:
252 LOG(FATAL) << "Unreachable type " << type;
253 }
254
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100255 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100256}
257
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100258void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100259 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100260 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100261
262 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100263 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100264
265 // TODO: We currently don't use Quick's callee saved registers.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100266 blocked_core_registers_[EBP] = true;
267 blocked_core_registers_[ESI] = true;
268 blocked_core_registers_[EDI] = true;
269 blocked_register_pairs_[EAX_EDI] = true;
270 blocked_register_pairs_[EDX_EDI] = true;
271 blocked_register_pairs_[ECX_EDI] = true;
272 blocked_register_pairs_[EBX_EDI] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100273}
274
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100275InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
276 : HGraphVisitor(graph),
277 assembler_(codegen->GetAssembler()),
278 codegen_(codegen) {}
279
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000280void CodeGeneratorX86::GenerateFrameEntry() {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000281 // Create a fake register to mimic Quick.
282 static const int kFakeReturnRegister = 8;
283 core_spill_mask_ |= (1 << kFakeReturnRegister);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000284
Dave Allison648d7112014-07-25 16:15:27 -0700285 bool skip_overflow_check = IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100286 if (!skip_overflow_check && !kExplicitStackOverflowCheck) {
287 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100288 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100289 }
290
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100291 // The return PC has already been pushed on the stack.
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100292 __ subl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100293
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100294 if (!skip_overflow_check && kExplicitStackOverflowCheck) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100295 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathX86();
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100296 AddSlowPath(slow_path);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100297
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100298 __ fs()->cmpl(ESP, Address::Absolute(Thread::StackEndOffset<kX86WordSize>()));
299 __ j(kLess, slow_path->GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100300 }
301
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100302 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000303}
304
305void CodeGeneratorX86::GenerateFrameExit() {
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100306 __ addl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000307}
308
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100309void CodeGeneratorX86::Bind(HBasicBlock* block) {
310 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000311}
312
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000313void InstructionCodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100314 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000315}
316
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100317Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
318 switch (load->GetType()) {
319 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100320 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100321 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
322 break;
323
324 case Primitive::kPrimInt:
325 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100326 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100327 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100328
329 case Primitive::kPrimBoolean:
330 case Primitive::kPrimByte:
331 case Primitive::kPrimChar:
332 case Primitive::kPrimShort:
333 case Primitive::kPrimVoid:
334 LOG(FATAL) << "Unexpected type " << load->GetType();
335 }
336
337 LOG(FATAL) << "Unreachable";
338 return Location();
339}
340
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100341Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
342 switch (type) {
343 case Primitive::kPrimBoolean:
344 case Primitive::kPrimByte:
345 case Primitive::kPrimChar:
346 case Primitive::kPrimShort:
347 case Primitive::kPrimInt:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100348 case Primitive::kPrimFloat:
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100349 case Primitive::kPrimNot: {
350 uint32_t index = gp_index_++;
351 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100352 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100353 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100354 return Location::StackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100355 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100356 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100357
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100358 case Primitive::kPrimLong:
359 case Primitive::kPrimDouble: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100360 uint32_t index = gp_index_;
361 gp_index_ += 2;
362 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100363 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
364 calling_convention.GetRegisterPairAt(index));
365 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100366 } else if (index + 1 == calling_convention.GetNumberOfRegisters()) {
367 return Location::QuickParameter(index);
368 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100369 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100370 }
371 }
372
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100373 case Primitive::kPrimVoid:
374 LOG(FATAL) << "Unexpected parameter type " << type;
375 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100376 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100377 return Location();
378}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100379
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100380void CodeGeneratorX86::Move32(Location destination, Location source) {
381 if (source.Equals(destination)) {
382 return;
383 }
384 if (destination.IsRegister()) {
385 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100386 __ movl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100387 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100388 __ movd(destination.As<Register>(), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100389 } else {
390 DCHECK(source.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100391 __ movl(destination.As<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100392 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100393 } else if (destination.IsFpuRegister()) {
394 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100395 __ movd(destination.As<XmmRegister>(), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100396 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100397 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100398 } else {
399 DCHECK(source.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100400 __ movss(destination.As<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100401 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100402 } else {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100403 DCHECK(destination.IsStackSlot());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100404 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100405 __ movl(Address(ESP, destination.GetStackIndex()), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100406 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100407 __ movss(Address(ESP, destination.GetStackIndex()), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100408 } else {
409 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100410 __ pushl(Address(ESP, source.GetStackIndex()));
411 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100412 }
413 }
414}
415
416void CodeGeneratorX86::Move64(Location destination, Location source) {
417 if (source.Equals(destination)) {
418 return;
419 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100420 if (destination.IsRegisterPair()) {
421 if (source.IsRegisterPair()) {
422 __ movl(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
423 __ movl(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100424 } else if (source.IsFpuRegister()) {
425 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100426 } else if (source.IsQuickParameter()) {
427 uint32_t argument_index = source.GetQuickParameterIndex();
428 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100429 __ movl(destination.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100430 calling_convention.GetRegisterAt(argument_index));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100431 __ movl(destination.AsRegisterPairHigh<Register>(), Address(ESP,
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100432 calling_convention.GetStackOffsetOf(argument_index + 1) + GetFrameSize()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100433 } else {
434 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100435 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
436 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100437 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
438 }
439 } else if (destination.IsQuickParameter()) {
440 InvokeDexCallingConvention calling_convention;
441 uint32_t argument_index = destination.GetQuickParameterIndex();
442 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100443 __ movl(calling_convention.GetRegisterAt(argument_index), source.AsRegisterPairLow<Register>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100444 __ movl(Address(ESP, calling_convention.GetStackOffsetOf(argument_index + 1)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100445 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100446 } else if (source.IsFpuRegister()) {
447 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100448 } else {
449 DCHECK(source.IsDoubleStackSlot());
450 __ movl(calling_convention.GetRegisterAt(argument_index),
451 Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100452 __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100453 __ popl(Address(ESP, calling_convention.GetStackOffsetOf(argument_index + 1)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100454 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100455 } else if (destination.IsFpuRegister()) {
456 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100457 __ movsd(destination.As<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100458 } else {
459 LOG(FATAL) << "Unimplemented";
460 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100461 } else {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100462 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100463 if (source.IsRegisterPair()) {
464 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100465 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100466 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100467 } else if (source.IsQuickParameter()) {
468 InvokeDexCallingConvention calling_convention;
469 uint32_t argument_index = source.GetQuickParameterIndex();
470 __ movl(Address(ESP, destination.GetStackIndex()),
471 calling_convention.GetRegisterAt(argument_index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100472 DCHECK_EQ(calling_convention.GetStackOffsetOf(argument_index + 1) + GetFrameSize(),
473 static_cast<size_t>(destination.GetHighStackIndex(kX86WordSize)));
474 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100475 __ movsd(Address(ESP, destination.GetStackIndex()), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100476 } else {
477 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100478 __ pushl(Address(ESP, source.GetStackIndex()));
479 __ popl(Address(ESP, destination.GetStackIndex()));
480 __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize)));
481 __ popl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100482 }
483 }
484}
485
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100486void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Roland Levillain476df552014-10-09 17:51:36 +0100487 if (instruction->IsIntConstant()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100488 Immediate imm(instruction->AsIntConstant()->GetValue());
489 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100490 __ movl(location.As<Register>(), imm);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100491 } else {
492 __ movl(Address(ESP, location.GetStackIndex()), imm);
493 }
Roland Levillain476df552014-10-09 17:51:36 +0100494 } else if (instruction->IsLongConstant()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100495 int64_t value = instruction->AsLongConstant()->GetValue();
496 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100497 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
498 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100499 } else {
500 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
501 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
502 }
Roland Levillain476df552014-10-09 17:51:36 +0100503 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100504 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100505 switch (instruction->GetType()) {
506 case Primitive::kPrimBoolean:
507 case Primitive::kPrimByte:
508 case Primitive::kPrimChar:
509 case Primitive::kPrimShort:
510 case Primitive::kPrimInt:
511 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100512 case Primitive::kPrimFloat:
513 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100514 break;
515
516 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100517 case Primitive::kPrimDouble:
518 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100519 break;
520
521 default:
522 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
523 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000524 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100525 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100526 switch (instruction->GetType()) {
527 case Primitive::kPrimBoolean:
528 case Primitive::kPrimByte:
529 case Primitive::kPrimChar:
530 case Primitive::kPrimShort:
531 case Primitive::kPrimInt:
532 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100533 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100534 Move32(location, instruction->GetLocations()->Out());
535 break;
536
537 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100538 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100539 Move64(location, instruction->GetLocations()->Out());
540 break;
541
542 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100543 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100544 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000545 }
546}
547
548void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000549 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000550}
551
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000552void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000553 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100554 DCHECK(!successor->IsExitBlock());
555
556 HBasicBlock* block = got->GetBlock();
557 HInstruction* previous = got->GetPrevious();
558
559 HLoopInformation* info = block->GetLoopInformation();
560 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
561 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
562 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
563 return;
564 }
565
566 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
567 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
568 }
569 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000570 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000571 }
572}
573
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000574void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000575 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000576}
577
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000578void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000579 if (kIsDebugBuild) {
580 __ Comment("Unreachable");
581 __ int3();
582 }
583}
584
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000585void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100586 LocationSummary* locations =
587 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100588 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100589 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +0100590 locations->SetInAt(0, Location::Any(), Location::kDiesAtEntry);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100591 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000592}
593
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000594void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700595 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100596 bool materialized = !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
597 // Moves do not affect the eflags register, so if the condition is evaluated
598 // just before the if, we don't need to evaluate it again.
599 bool eflags_set = cond->IsCondition()
600 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
601 if (materialized) {
602 if (!eflags_set) {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100603 // Materialized condition, compare against 0.
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100604 Location lhs = if_instr->GetLocations()->InAt(0);
605 if (lhs.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100606 __ cmpl(lhs.As<Register>(), Immediate(0));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100607 } else {
608 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
609 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100610 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
611 } else {
612 __ j(X86Condition(cond->AsCondition()->GetCondition()),
613 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700614 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100615 } else {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100616 Location lhs = cond->GetLocations()->InAt(0);
617 Location rhs = cond->GetLocations()->InAt(1);
Dave Allison20dfc792014-06-16 20:44:29 -0700618 // LHS is guaranteed to be in a register (see LocationsBuilderX86::VisitCondition).
619 if (rhs.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100620 __ cmpl(lhs.As<Register>(), rhs.As<Register>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100621 } else if (rhs.IsConstant()) {
622 HIntConstant* instruction = rhs.GetConstant()->AsIntConstant();
623 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100624 __ cmpl(lhs.As<Register>(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700625 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100626 __ cmpl(lhs.As<Register>(), Address(ESP, rhs.GetStackIndex()));
Dave Allison20dfc792014-06-16 20:44:29 -0700627 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100628 __ j(X86Condition(cond->AsCondition()->GetCondition()),
Dave Allison20dfc792014-06-16 20:44:29 -0700629 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100630 }
Dave Allison20dfc792014-06-16 20:44:29 -0700631 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(), if_instr->IfFalseSuccessor())) {
632 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000633 }
634}
635
636void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000637 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000638}
639
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000640void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
641 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000642}
643
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000644void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100645 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000646}
647
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000648void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100649 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000650}
651
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100652void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100653 LocationSummary* locations =
654 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100655 switch (store->InputAt(1)->GetType()) {
656 case Primitive::kPrimBoolean:
657 case Primitive::kPrimByte:
658 case Primitive::kPrimChar:
659 case Primitive::kPrimShort:
660 case Primitive::kPrimInt:
661 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100662 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100663 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
664 break;
665
666 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100667 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100668 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
669 break;
670
671 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100672 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100673 }
674 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000675}
676
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000677void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000678}
679
Dave Allison20dfc792014-06-16 20:44:29 -0700680void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100681 LocationSummary* locations =
682 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +0100683 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
684 locations->SetInAt(1, Location::Any(), Location::kDiesAtEntry);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100685 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100686 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100687 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000688}
689
Dave Allison20dfc792014-06-16 20:44:29 -0700690void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
691 if (comp->NeedsMaterialization()) {
692 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100693 Register reg = locations->Out().As<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100694 // Clear register: setcc only sets the low byte.
695 __ xorl(reg, reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700696 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100697 __ cmpl(locations->InAt(0).As<Register>(),
698 locations->InAt(1).As<Register>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100699 } else if (locations->InAt(1).IsConstant()) {
700 HConstant* instruction = locations->InAt(1).GetConstant();
701 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100702 __ cmpl(locations->InAt(0).As<Register>(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700703 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100704 __ cmpl(locations->InAt(0).As<Register>(),
Dave Allison20dfc792014-06-16 20:44:29 -0700705 Address(ESP, locations->InAt(1).GetStackIndex()));
706 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100707 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100708 }
Dave Allison20dfc792014-06-16 20:44:29 -0700709}
710
711void LocationsBuilderX86::VisitEqual(HEqual* comp) {
712 VisitCondition(comp);
713}
714
715void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
716 VisitCondition(comp);
717}
718
719void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
720 VisitCondition(comp);
721}
722
723void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
724 VisitCondition(comp);
725}
726
727void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
728 VisitCondition(comp);
729}
730
731void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
732 VisitCondition(comp);
733}
734
735void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
736 VisitCondition(comp);
737}
738
739void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
740 VisitCondition(comp);
741}
742
743void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
744 VisitCondition(comp);
745}
746
747void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
748 VisitCondition(comp);
749}
750
751void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
752 VisitCondition(comp);
753}
754
755void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
756 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000757}
758
759void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100760 LocationSummary* locations =
761 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100762 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000763}
764
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000765void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000766}
767
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100768void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100769 LocationSummary* locations =
770 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100771 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100772}
773
774void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
775 // Will be generated at use site.
776}
777
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000778void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000779 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000780}
781
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000782void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
783 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000784 __ ret();
785}
786
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000787void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100788 LocationSummary* locations =
789 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100790 switch (ret->InputAt(0)->GetType()) {
791 case Primitive::kPrimBoolean:
792 case Primitive::kPrimByte:
793 case Primitive::kPrimChar:
794 case Primitive::kPrimShort:
795 case Primitive::kPrimInt:
796 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100797 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100798 break;
799
800 case Primitive::kPrimLong:
801 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100802 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100803 break;
804
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100805 case Primitive::kPrimFloat:
806 case Primitive::kPrimDouble:
807 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100808 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100809 break;
810
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100811 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100812 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100813 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000814}
815
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000816void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100817 if (kIsDebugBuild) {
818 switch (ret->InputAt(0)->GetType()) {
819 case Primitive::kPrimBoolean:
820 case Primitive::kPrimByte:
821 case Primitive::kPrimChar:
822 case Primitive::kPrimShort:
823 case Primitive::kPrimInt:
824 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100825 DCHECK_EQ(ret->GetLocations()->InAt(0).As<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100826 break;
827
828 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100829 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
830 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100831 break;
832
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100833 case Primitive::kPrimFloat:
834 case Primitive::kPrimDouble:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100835 DCHECK_EQ(ret->GetLocations()->InAt(0).As<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100836 break;
837
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100838 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100839 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100840 }
841 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000842 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000843 __ ret();
844}
845
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000846void LocationsBuilderX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100847 HandleInvoke(invoke);
848}
849
850void InstructionCodeGeneratorX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100851 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100852 uint32_t heap_reference_size = sizeof(mirror::HeapReference<mirror::Object>);
853 size_t index_in_cache = mirror::Array::DataOffset(heap_reference_size).Int32Value() +
854 invoke->GetIndexInDexCache() * kX86WordSize;
855
856 // TODO: Implement all kinds of calls:
857 // 1) boot -> boot
858 // 2) app -> boot
859 // 3) app -> app
860 //
861 // Currently we implement the app -> app logic, which looks up in the resolve cache.
862
863 // temp = method;
864 LoadCurrentMethod(temp);
865 // temp = temp->dex_cache_resolved_methods_;
866 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
867 // temp = temp[index_in_cache]
868 __ movl(temp, Address(temp, index_in_cache));
869 // (temp + offset_of_quick_compiled_code)()
870 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
871
872 DCHECK(!codegen_->IsLeafMethod());
873 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
874}
875
876void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
877 HandleInvoke(invoke);
878}
879
880void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100881 LocationSummary* locations =
882 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100883 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100884
885 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100886 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100887 HInstruction* input = invoke->InputAt(i);
888 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
889 }
890
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100891 switch (invoke->GetType()) {
892 case Primitive::kPrimBoolean:
893 case Primitive::kPrimByte:
894 case Primitive::kPrimChar:
895 case Primitive::kPrimShort:
896 case Primitive::kPrimInt:
897 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100898 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100899 break;
900
901 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100902 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100903 break;
904
905 case Primitive::kPrimVoid:
906 break;
907
908 case Primitive::kPrimDouble:
909 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100910 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100911 break;
912 }
913
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000914 invoke->SetLocations(locations);
915}
916
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100917void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100918 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100919 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
920 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
921 LocationSummary* locations = invoke->GetLocations();
922 Location receiver = locations->InAt(0);
923 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
924 // temp = object->GetClass();
925 if (receiver.IsStackSlot()) {
926 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
927 __ movl(temp, Address(temp, class_offset));
928 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100929 __ movl(temp, Address(receiver.As<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100930 }
931 // temp = temp->GetMethodAt(method_offset);
932 __ movl(temp, Address(temp, method_offset));
933 // call temp->GetEntryPoint();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000934 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
935
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100936 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +0100937 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000938}
939
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000940void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100941 LocationSummary* locations =
942 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000943 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100944 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100945 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100946 locations->SetInAt(0, Location::RequiresRegister());
947 locations->SetInAt(1, Location::Any());
948 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100949 break;
950 }
951
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100952 case Primitive::kPrimFloat:
953 case Primitive::kPrimDouble: {
954 locations->SetInAt(0, Location::RequiresFpuRegister());
955 locations->SetInAt(1, Location::Any());
956 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100957 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100958 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100959
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000960 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100961 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
962 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000963 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000964}
965
966void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
967 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100968 Location first = locations->InAt(0);
969 Location second = locations->InAt(1);
970
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000971 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100972 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100973 DCHECK_EQ(first.As<Register>(), locations->Out().As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100974 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100975 __ addl(first.As<Register>(), second.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100976 } else if (second.IsConstant()) {
977 HConstant* instruction = second.GetConstant();
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100978 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100979 __ addl(first.As<Register>(), imm);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100980 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100981 __ addl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100982 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000983 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100984 }
985
986 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100987 DCHECK_EQ(first.AsRegisterPairLow<Register>(),
988 locations->Out().AsRegisterPairLow<Register>());
989 DCHECK_EQ(first.AsRegisterPairHigh<Register>(),
990 locations->Out().AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100991 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100992 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
993 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100994 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100995 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
996 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100997 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100998 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100999 break;
1000 }
1001
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001002 case Primitive::kPrimFloat: {
1003 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001004 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001005 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001006 __ addss(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001007 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001008 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001009 }
1010
1011 case Primitive::kPrimDouble: {
1012 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001013 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001014 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001015 __ addsd(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001016 }
1017 break;
1018 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001019
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001020 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001021 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001022 }
1023}
1024
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001025void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001026 LocationSummary* locations =
1027 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001028 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001029 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001030 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001031 locations->SetInAt(0, Location::RequiresRegister());
1032 locations->SetInAt(1, Location::Any());
1033 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001034 break;
1035 }
1036
1037 case Primitive::kPrimBoolean:
1038 case Primitive::kPrimByte:
1039 case Primitive::kPrimChar:
1040 case Primitive::kPrimShort:
1041 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
1042 break;
1043
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001044 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001045 LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001046 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001047}
1048
1049void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
1050 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001051 Location first = locations->InAt(0);
1052 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001053 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001054 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001055 DCHECK_EQ(first.As<Register>(),
1056 locations->Out().As<Register>());
1057 if (second.IsRegister()) {
1058 __ subl(first.As<Register>(),
1059 second.As<Register>());
1060 } else if (second.IsConstant()) {
1061 HConstant* instruction = second.GetConstant();
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001062 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001063 __ subl(first.As<Register>(), imm);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001064 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001065 __ subl(first.As<Register>(),
1066 Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001067 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001068 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001069 }
1070
1071 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001072 DCHECK_EQ(first.AsRegisterPairLow<Register>(),
1073 locations->Out().AsRegisterPairLow<Register>());
1074 DCHECK_EQ(first.AsRegisterPairHigh<Register>(),
1075 locations->Out().AsRegisterPairHigh<Register>());
1076 if (second.IsRegister()) {
1077 __ subl(first.AsRegisterPairLow<Register>(),
1078 second.AsRegisterPairLow<Register>());
1079 __ sbbl(first.AsRegisterPairHigh<Register>(),
1080 second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001081 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001082 __ subl(first.AsRegisterPairLow<Register>(),
1083 Address(ESP, second.GetStackIndex()));
1084 __ sbbl(first.AsRegisterPairHigh<Register>(),
1085 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001086 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001087 break;
1088 }
1089
1090 case Primitive::kPrimBoolean:
1091 case Primitive::kPrimByte:
1092 case Primitive::kPrimChar:
1093 case Primitive::kPrimShort:
1094 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
1095 break;
1096
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001097 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001098 LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001099 }
1100}
1101
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001102void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001103 LocationSummary* locations =
1104 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001105 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001106 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001107 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1108 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001109}
1110
1111void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
1112 InvokeRuntimeCallingConvention calling_convention;
1113 LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001114 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001115
Nicolas Geoffray707c8092014-04-04 10:50:14 +01001116 __ fs()->call(
1117 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001118
Nicolas Geoffray39468442014-09-02 15:17:15 +01001119 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001120 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001121}
1122
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001123void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001124 LocationSummary* locations =
1125 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001126 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1127 if (location.IsStackSlot()) {
1128 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1129 } else if (location.IsDoubleStackSlot()) {
1130 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001131 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001132 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001133}
1134
1135void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001136}
1137
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001138void LocationsBuilderX86::VisitNot(HNot* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001139 LocationSummary* locations =
1140 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001141 locations->SetInAt(0, Location::RequiresRegister());
1142 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001143}
1144
1145void InstructionCodeGeneratorX86::VisitNot(HNot* instruction) {
1146 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001147 Location out = locations->Out();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001148 DCHECK_EQ(locations->InAt(0).As<Register>(), out.As<Register>());
1149 __ xorl(out.As<Register>(), Immediate(1));
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001150}
1151
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001152void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001153 LocationSummary* locations =
1154 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001155 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
1156 locations->SetInAt(1, Location::Any(), Location::kDiesAtEntry);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001157 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001158}
1159
1160void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
1161 Label greater, done;
1162 LocationSummary* locations = compare->GetLocations();
1163 switch (compare->InputAt(0)->GetType()) {
1164 case Primitive::kPrimLong: {
1165 Label less, greater, done;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001166 Register output = locations->Out().As<Register>();
1167 Location left = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001168 Location right = locations->InAt(1);
1169 if (right.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001170 __ cmpl(left.AsRegisterPairHigh<Register>(), right.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001171 } else {
1172 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001173 __ cmpl(left.AsRegisterPairHigh<Register>(),
1174 Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001175 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001176 __ j(kLess, &less); // Signed compare.
1177 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001178 if (right.IsRegisterPair()) {
1179 __ cmpl(left.AsRegisterPairLow<Register>(), right.AsRegisterPairLow<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001180 } else {
1181 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001182 __ cmpl(left.AsRegisterPairLow<Register>(), Address(ESP, right.GetStackIndex()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001183 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001184 __ movl(output, Immediate(0));
1185 __ j(kEqual, &done);
1186 __ j(kBelow, &less); // Unsigned compare.
1187
1188 __ Bind(&greater);
1189 __ movl(output, Immediate(1));
1190 __ jmp(&done);
1191
1192 __ Bind(&less);
1193 __ movl(output, Immediate(-1));
1194
1195 __ Bind(&done);
1196 break;
1197 }
1198 default:
1199 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
1200 }
1201}
1202
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001203void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001204 LocationSummary* locations =
1205 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001206 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1207 locations->SetInAt(i, Location::Any());
1208 }
1209 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001210}
1211
1212void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001213 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001214}
1215
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001216void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001217 LocationSummary* locations =
1218 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001219 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001220 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001221 bool is_object_type = field_type == Primitive::kPrimNot;
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001222 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
1223 || (field_type == Primitive::kPrimByte);
1224 // The register allocator does not support multiple
1225 // inputs that die at entry with one in a specific register.
1226 bool dies_at_entry = !is_object_type && !is_byte_type;
1227 if (is_byte_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001228 // Ensure the value is in a byte register.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001229 locations->SetInAt(1, Location::RegisterLocation(EAX), dies_at_entry);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001230 } else {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001231 locations->SetInAt(1, Location::RequiresRegister(), dies_at_entry);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001232 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001233 // Temporary registers for the write barrier.
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001234 if (is_object_type) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001235 locations->AddTemp(Location::RequiresRegister());
1236 // Ensure the card is in a byte register.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001237 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001238 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001239}
1240
1241void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1242 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001243 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001244 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001245 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001246
1247 switch (field_type) {
1248 case Primitive::kPrimBoolean:
1249 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001250 ByteRegister value = locations->InAt(1).As<ByteRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001251 __ movb(Address(obj, offset), value);
1252 break;
1253 }
1254
1255 case Primitive::kPrimShort:
1256 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001257 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001258 __ movw(Address(obj, offset), value);
1259 break;
1260 }
1261
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001262 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001263 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001264 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001265 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001266
1267 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001268 Register temp = locations->GetTemp(0).As<Register>();
1269 Register card = locations->GetTemp(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001270 codegen_->MarkGCCard(temp, card, obj, value);
1271 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001272 break;
1273 }
1274
1275 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001276 Location value = locations->InAt(1);
1277 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
1278 __ movl(Address(obj, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001279 break;
1280 }
1281
1282 case Primitive::kPrimFloat:
1283 case Primitive::kPrimDouble:
1284 LOG(FATAL) << "Unimplemented register type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001285 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001286 case Primitive::kPrimVoid:
1287 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001288 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001289 }
1290}
1291
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001292void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
1293 Label is_null;
1294 __ testl(value, value);
1295 __ j(kEqual, &is_null);
1296 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
1297 __ movl(temp, object);
1298 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
1299 __ movb(Address(temp, card, TIMES_1, 0),
1300 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
1301 __ Bind(&is_null);
1302}
1303
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001304void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001305 LocationSummary* locations =
1306 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001307 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001308 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001309}
1310
1311void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1312 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001313 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001314 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
1315
1316 switch (instruction->GetType()) {
1317 case Primitive::kPrimBoolean: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001318 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001319 __ movzxb(out, Address(obj, offset));
1320 break;
1321 }
1322
1323 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001324 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001325 __ movsxb(out, Address(obj, offset));
1326 break;
1327 }
1328
1329 case Primitive::kPrimShort: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001330 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001331 __ movsxw(out, Address(obj, offset));
1332 break;
1333 }
1334
1335 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001336 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001337 __ movzxw(out, Address(obj, offset));
1338 break;
1339 }
1340
1341 case Primitive::kPrimInt:
1342 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001343 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001344 __ movl(out, Address(obj, offset));
1345 break;
1346 }
1347
1348 case Primitive::kPrimLong: {
1349 // TODO: support volatile.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001350 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(obj, offset));
1351 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(obj, kX86WordSize + offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001352 break;
1353 }
1354
1355 case Primitive::kPrimFloat:
1356 case Primitive::kPrimDouble:
1357 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001358 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001359 case Primitive::kPrimVoid:
1360 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001361 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001362 }
1363}
1364
1365void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001366 LocationSummary* locations =
1367 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001368 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001369 if (instruction->HasUses()) {
1370 locations->SetOut(Location::SameAsFirstInput());
1371 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001372}
1373
1374void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001375 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001376 codegen_->AddSlowPath(slow_path);
1377
1378 LocationSummary* locations = instruction->GetLocations();
1379 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001380
1381 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001382 __ cmpl(obj.As<Register>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001383 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001384 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001385 } else {
1386 DCHECK(obj.IsConstant()) << obj;
1387 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
1388 __ jmp(slow_path->GetEntryLabel());
1389 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001390 }
1391 __ j(kEqual, slow_path->GetEntryLabel());
1392}
1393
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001394void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001395 LocationSummary* locations =
1396 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001397 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
1398 locations->SetInAt(
1399 1, Location::RegisterOrConstant(instruction->InputAt(1)), Location::kDiesAtEntry);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001400 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001401}
1402
1403void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
1404 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001405 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001406 Location index = locations->InAt(1);
1407
1408 switch (instruction->GetType()) {
1409 case Primitive::kPrimBoolean: {
1410 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001411 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001412 if (index.IsConstant()) {
1413 __ movzxb(out, Address(obj,
1414 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1415 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001416 __ movzxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001417 }
1418 break;
1419 }
1420
1421 case Primitive::kPrimByte: {
1422 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001423 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001424 if (index.IsConstant()) {
1425 __ movsxb(out, Address(obj,
1426 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1427 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001428 __ movsxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001429 }
1430 break;
1431 }
1432
1433 case Primitive::kPrimShort: {
1434 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001435 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001436 if (index.IsConstant()) {
1437 __ movsxw(out, Address(obj,
1438 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1439 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001440 __ movsxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001441 }
1442 break;
1443 }
1444
1445 case Primitive::kPrimChar: {
1446 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001447 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001448 if (index.IsConstant()) {
1449 __ movzxw(out, Address(obj,
1450 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1451 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001452 __ movzxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001453 }
1454 break;
1455 }
1456
1457 case Primitive::kPrimInt:
1458 case Primitive::kPrimNot: {
1459 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001460 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001461 if (index.IsConstant()) {
1462 __ movl(out, Address(obj,
1463 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
1464 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001465 __ movl(out, Address(obj, index.As<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001466 }
1467 break;
1468 }
1469
1470 case Primitive::kPrimLong: {
1471 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001472 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001473 if (index.IsConstant()) {
1474 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001475 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
1476 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001477 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001478 __ movl(out.AsRegisterPairLow<Register>(),
1479 Address(obj, index.As<Register>(), TIMES_8, data_offset));
1480 __ movl(out.AsRegisterPairHigh<Register>(),
1481 Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001482 }
1483 break;
1484 }
1485
1486 case Primitive::kPrimFloat:
1487 case Primitive::kPrimDouble:
1488 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001489 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001490 case Primitive::kPrimVoid:
1491 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001492 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001493 }
1494}
1495
1496void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001497 Primitive::Type value_type = instruction->GetComponentType();
1498 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1499 instruction,
1500 value_type == Primitive::kPrimNot ? LocationSummary::kCall : LocationSummary::kNoCall);
1501
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001502 if (value_type == Primitive::kPrimNot) {
1503 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001504 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1505 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1506 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001507 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001508 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
1509 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001510 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001511 // In case of a byte operation, the register allocator does not support multiple
1512 // inputs that die at entry with one in a specific register.
1513 bool dies_at_entry = value_type != Primitive::kPrimLong && !is_byte_type;
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001514 locations->SetInAt(0, Location::RequiresRegister(), dies_at_entry);
1515 locations->SetInAt(
1516 1, Location::RegisterOrConstant(instruction->InputAt(1)), dies_at_entry);
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001517 if (is_byte_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001518 // Ensure the value is in a byte register.
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001519 locations->SetInAt(2, Location::ByteRegisterOrConstant(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001520 EAX, instruction->InputAt(2)), dies_at_entry);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001521 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001522 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)), dies_at_entry);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001523 }
1524 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001525}
1526
1527void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
1528 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001529 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001530 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001531 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001532 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001533
1534 switch (value_type) {
1535 case Primitive::kPrimBoolean:
1536 case Primitive::kPrimByte: {
1537 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001538 if (index.IsConstant()) {
1539 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001540 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001541 __ movb(Address(obj, offset), value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001542 } else {
1543 __ movb(Address(obj, offset),
1544 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1545 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001546 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001547 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001548 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
1549 value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001550 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001551 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001552 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1553 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001554 }
1555 break;
1556 }
1557
1558 case Primitive::kPrimShort:
1559 case Primitive::kPrimChar: {
1560 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001561 if (index.IsConstant()) {
1562 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001563 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001564 __ movw(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001565 } else {
1566 __ movw(Address(obj, offset),
1567 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1568 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001569 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001570 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001571 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
1572 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001573 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001574 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001575 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1576 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001577 }
1578 break;
1579 }
1580
1581 case Primitive::kPrimInt: {
1582 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001583 if (index.IsConstant()) {
1584 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001585 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001586 __ movl(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001587 } else {
1588 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1589 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001590 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001591 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001592 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
1593 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001594 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001595 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001596 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1597 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001598 }
1599 break;
1600 }
1601
1602 case Primitive::kPrimNot: {
1603 DCHECK(!codegen_->IsLeafMethod());
1604 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001605 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001606 break;
1607 }
1608
1609 case Primitive::kPrimLong: {
1610 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001611 if (index.IsConstant()) {
1612 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001613 if (value.IsRegisterPair()) {
1614 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
1615 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001616 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001617 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001618 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
1619 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
1620 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
1621 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001622 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001623 if (value.IsRegisterPair()) {
1624 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
1625 value.AsRegisterPairLow<Register>());
1626 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
1627 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001628 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001629 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001630 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001631 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001632 Immediate(Low32Bits(val)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001633 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001634 Immediate(High32Bits(val)));
1635 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001636 }
1637 break;
1638 }
1639
1640 case Primitive::kPrimFloat:
1641 case Primitive::kPrimDouble:
1642 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001643 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001644 case Primitive::kPrimVoid:
1645 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001646 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001647 }
1648}
1649
1650void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
1651 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001652 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001653 locations->SetOut(Location::RequiresRegister());
1654 instruction->SetLocations(locations);
1655}
1656
1657void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
1658 LocationSummary* locations = instruction->GetLocations();
1659 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001660 Register obj = locations->InAt(0).As<Register>();
1661 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001662 __ movl(out, Address(obj, offset));
1663}
1664
1665void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001666 LocationSummary* locations =
1667 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001668 locations->SetInAt(0, Location::RequiresRegister());
1669 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001670 if (instruction->HasUses()) {
1671 locations->SetOut(Location::SameAsFirstInput());
1672 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001673}
1674
1675void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
1676 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001677 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01001678 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001679 codegen_->AddSlowPath(slow_path);
1680
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001681 Register index = locations->InAt(0).As<Register>();
1682 Register length = locations->InAt(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001683
1684 __ cmpl(index, length);
1685 __ j(kAboveEqual, slow_path->GetEntryLabel());
1686}
1687
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001688void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
1689 temp->SetLocations(nullptr);
1690}
1691
1692void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
1693 // Nothing to do, this is driven by the code generator.
1694}
1695
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001696void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001697 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001698}
1699
1700void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001701 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
1702}
1703
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001704void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
1705 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
1706}
1707
1708void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001709 HBasicBlock* block = instruction->GetBlock();
1710 if (block->GetLoopInformation() != nullptr) {
1711 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
1712 // The back edge will generate the suspend check.
1713 return;
1714 }
1715 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
1716 // The goto will generate the suspend check.
1717 return;
1718 }
1719 GenerateSuspendCheck(instruction, nullptr);
1720}
1721
1722void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
1723 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001724 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001725 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001726 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001727 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001728 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001729 if (successor == nullptr) {
1730 __ j(kNotEqual, slow_path->GetEntryLabel());
1731 __ Bind(slow_path->GetReturnLabel());
1732 } else {
1733 __ j(kEqual, codegen_->GetLabelOf(successor));
1734 __ jmp(slow_path->GetEntryLabel());
1735 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001736}
1737
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001738X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
1739 return codegen_->GetAssembler();
1740}
1741
1742void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
1743 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001744 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001745 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
1746 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
1747 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
1748}
1749
1750void ParallelMoveResolverX86::EmitMove(size_t index) {
1751 MoveOperands* move = moves_.Get(index);
1752 Location source = move->GetSource();
1753 Location destination = move->GetDestination();
1754
1755 if (source.IsRegister()) {
1756 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001757 __ movl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001758 } else {
1759 DCHECK(destination.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001760 __ movl(Address(ESP, destination.GetStackIndex()), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001761 }
1762 } else if (source.IsStackSlot()) {
1763 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001764 __ movl(destination.As<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001765 } else {
1766 DCHECK(destination.IsStackSlot());
1767 MoveMemoryToMemory(destination.GetStackIndex(),
1768 source.GetStackIndex());
1769 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001770 } else if (source.IsConstant()) {
1771 HIntConstant* instruction = source.GetConstant()->AsIntConstant();
1772 Immediate imm(instruction->AsIntConstant()->GetValue());
1773 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001774 __ movl(destination.As<Register>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001775 } else {
1776 __ movl(Address(ESP, destination.GetStackIndex()), imm);
1777 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001778 } else {
1779 LOG(FATAL) << "Unimplemented";
1780 }
1781}
1782
1783void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001784 Register suggested_scratch = reg == EAX ? EBX : EAX;
1785 ScratchRegisterScope ensure_scratch(
1786 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
1787
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001788 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
1789 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
1790 __ movl(Address(ESP, mem + stack_offset), reg);
1791 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
1792}
1793
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001794void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
1795 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001796 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
1797
1798 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001799 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001800 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
1801
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001802 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
1803 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
1804 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
1805 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
1806 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
1807 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
1808}
1809
1810void ParallelMoveResolverX86::EmitSwap(size_t index) {
1811 MoveOperands* move = moves_.Get(index);
1812 Location source = move->GetSource();
1813 Location destination = move->GetDestination();
1814
1815 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001816 __ xchgl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001817 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001818 Exchange(source.As<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001819 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001820 Exchange(destination.As<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001821 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
1822 Exchange(destination.GetStackIndex(), source.GetStackIndex());
1823 } else {
1824 LOG(FATAL) << "Unimplemented";
1825 }
1826}
1827
1828void ParallelMoveResolverX86::SpillScratch(int reg) {
1829 __ pushl(static_cast<Register>(reg));
1830}
1831
1832void ParallelMoveResolverX86::RestoreScratch(int reg) {
1833 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001834}
1835
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001836} // namespace x86
1837} // namespace art