blob: cc2be82b94e6bec3d2b5f871444ea46dbd8a415c [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));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100210 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
211 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100212 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
213 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100214 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100215 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100216 }
217
218 case Primitive::kPrimByte:
219 case Primitive::kPrimBoolean:
220 case Primitive::kPrimChar:
221 case Primitive::kPrimShort:
222 case Primitive::kPrimInt:
223 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100224 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100225 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100226 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100227 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
228 X86ManagedRegister current =
229 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
230 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100231 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100232 }
233 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100234 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100235 }
236
237 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100238 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100239 return Location::FpuRegisterLocation(
240 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100241 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100242
243 case Primitive::kPrimVoid:
244 LOG(FATAL) << "Unreachable type " << type;
245 }
246
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100247 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100248}
249
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100250void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100251 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100252 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100253
254 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100255 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100256
257 // TODO: We currently don't use Quick's callee saved registers.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100258 blocked_core_registers_[EBP] = true;
259 blocked_core_registers_[ESI] = true;
260 blocked_core_registers_[EDI] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100261
262 UpdateBlockedPairRegisters();
263}
264
265void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
266 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
267 X86ManagedRegister current =
268 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
269 if (blocked_core_registers_[current.AsRegisterPairLow()]
270 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
271 blocked_register_pairs_[i] = true;
272 }
273 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100274}
275
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100276InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
277 : HGraphVisitor(graph),
278 assembler_(codegen->GetAssembler()),
279 codegen_(codegen) {}
280
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000281void CodeGeneratorX86::GenerateFrameEntry() {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000282 // Create a fake register to mimic Quick.
283 static const int kFakeReturnRegister = 8;
284 core_spill_mask_ |= (1 << kFakeReturnRegister);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000285
Dave Allison648d7112014-07-25 16:15:27 -0700286 bool skip_overflow_check = IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100287 if (!skip_overflow_check && !kExplicitStackOverflowCheck) {
288 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100289 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100290 }
291
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100292 // The return PC has already been pushed on the stack.
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100293 __ subl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100294
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100295 if (!skip_overflow_check && kExplicitStackOverflowCheck) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100296 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathX86();
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100297 AddSlowPath(slow_path);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100298
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100299 __ fs()->cmpl(ESP, Address::Absolute(Thread::StackEndOffset<kX86WordSize>()));
300 __ j(kLess, slow_path->GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100301 }
302
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100303 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000304}
305
306void CodeGeneratorX86::GenerateFrameExit() {
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100307 __ addl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000308}
309
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100310void CodeGeneratorX86::Bind(HBasicBlock* block) {
311 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000312}
313
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000314void InstructionCodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100315 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000316}
317
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100318Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
319 switch (load->GetType()) {
320 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100321 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100322 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
323 break;
324
325 case Primitive::kPrimInt:
326 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100327 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100328 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100329
330 case Primitive::kPrimBoolean:
331 case Primitive::kPrimByte:
332 case Primitive::kPrimChar:
333 case Primitive::kPrimShort:
334 case Primitive::kPrimVoid:
335 LOG(FATAL) << "Unexpected type " << load->GetType();
336 }
337
338 LOG(FATAL) << "Unreachable";
339 return Location();
340}
341
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100342Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
343 switch (type) {
344 case Primitive::kPrimBoolean:
345 case Primitive::kPrimByte:
346 case Primitive::kPrimChar:
347 case Primitive::kPrimShort:
348 case Primitive::kPrimInt:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100349 case Primitive::kPrimFloat:
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100350 case Primitive::kPrimNot: {
351 uint32_t index = gp_index_++;
352 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100353 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100354 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100355 return Location::StackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100356 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100357 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100358
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100359 case Primitive::kPrimLong:
360 case Primitive::kPrimDouble: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100361 uint32_t index = gp_index_;
362 gp_index_ += 2;
363 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100364 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
365 calling_convention.GetRegisterPairAt(index));
366 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100367 } else if (index + 1 == calling_convention.GetNumberOfRegisters()) {
368 return Location::QuickParameter(index);
369 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100370 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100371 }
372 }
373
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100374 case Primitive::kPrimVoid:
375 LOG(FATAL) << "Unexpected parameter type " << type;
376 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100377 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100378 return Location();
379}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100380
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100381void CodeGeneratorX86::Move32(Location destination, Location source) {
382 if (source.Equals(destination)) {
383 return;
384 }
385 if (destination.IsRegister()) {
386 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100387 __ movl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100388 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100389 __ movd(destination.As<Register>(), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100390 } else {
391 DCHECK(source.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100392 __ movl(destination.As<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100393 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100394 } else if (destination.IsFpuRegister()) {
395 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100396 __ movd(destination.As<XmmRegister>(), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100397 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100398 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100399 } else {
400 DCHECK(source.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100401 __ movss(destination.As<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100402 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100403 } else {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100404 DCHECK(destination.IsStackSlot());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100405 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100406 __ movl(Address(ESP, destination.GetStackIndex()), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100407 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100408 __ movss(Address(ESP, destination.GetStackIndex()), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100409 } else {
410 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100411 __ pushl(Address(ESP, source.GetStackIndex()));
412 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100413 }
414 }
415}
416
417void CodeGeneratorX86::Move64(Location destination, Location source) {
418 if (source.Equals(destination)) {
419 return;
420 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100421 if (destination.IsRegisterPair()) {
422 if (source.IsRegisterPair()) {
423 __ movl(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
424 __ movl(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100425 } else if (source.IsFpuRegister()) {
426 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100427 } else if (source.IsQuickParameter()) {
428 uint32_t argument_index = source.GetQuickParameterIndex();
429 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100430 __ movl(destination.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100431 calling_convention.GetRegisterAt(argument_index));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100432 __ movl(destination.AsRegisterPairHigh<Register>(), Address(ESP,
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100433 calling_convention.GetStackOffsetOf(argument_index + 1) + GetFrameSize()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100434 } else {
435 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100436 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
437 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100438 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
439 }
440 } else if (destination.IsQuickParameter()) {
441 InvokeDexCallingConvention calling_convention;
442 uint32_t argument_index = destination.GetQuickParameterIndex();
443 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100444 __ movl(calling_convention.GetRegisterAt(argument_index), source.AsRegisterPairLow<Register>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100445 __ movl(Address(ESP, calling_convention.GetStackOffsetOf(argument_index + 1)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100446 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100447 } else if (source.IsFpuRegister()) {
448 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100449 } else {
450 DCHECK(source.IsDoubleStackSlot());
451 __ movl(calling_convention.GetRegisterAt(argument_index),
452 Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100453 __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100454 __ popl(Address(ESP, calling_convention.GetStackOffsetOf(argument_index + 1)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100455 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100456 } else if (destination.IsFpuRegister()) {
457 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100458 __ movsd(destination.As<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100459 } else {
460 LOG(FATAL) << "Unimplemented";
461 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100462 } else {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100463 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100464 if (source.IsRegisterPair()) {
465 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100466 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100467 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100468 } else if (source.IsQuickParameter()) {
469 InvokeDexCallingConvention calling_convention;
470 uint32_t argument_index = source.GetQuickParameterIndex();
471 __ movl(Address(ESP, destination.GetStackIndex()),
472 calling_convention.GetRegisterAt(argument_index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100473 DCHECK_EQ(calling_convention.GetStackOffsetOf(argument_index + 1) + GetFrameSize(),
474 static_cast<size_t>(destination.GetHighStackIndex(kX86WordSize)));
475 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100476 __ movsd(Address(ESP, destination.GetStackIndex()), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100477 } else {
478 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100479 __ pushl(Address(ESP, source.GetStackIndex()));
480 __ popl(Address(ESP, destination.GetStackIndex()));
481 __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize)));
482 __ popl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100483 }
484 }
485}
486
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100487void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Roland Levillain476df552014-10-09 17:51:36 +0100488 if (instruction->IsIntConstant()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100489 Immediate imm(instruction->AsIntConstant()->GetValue());
490 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100491 __ movl(location.As<Register>(), imm);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100492 } else {
493 __ movl(Address(ESP, location.GetStackIndex()), imm);
494 }
Roland Levillain476df552014-10-09 17:51:36 +0100495 } else if (instruction->IsLongConstant()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100496 int64_t value = instruction->AsLongConstant()->GetValue();
497 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100498 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
499 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100500 } else {
501 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
502 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
503 }
Roland Levillain476df552014-10-09 17:51:36 +0100504 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100505 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100506 switch (instruction->GetType()) {
507 case Primitive::kPrimBoolean:
508 case Primitive::kPrimByte:
509 case Primitive::kPrimChar:
510 case Primitive::kPrimShort:
511 case Primitive::kPrimInt:
512 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100513 case Primitive::kPrimFloat:
514 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100515 break;
516
517 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100518 case Primitive::kPrimDouble:
519 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100520 break;
521
522 default:
523 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
524 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000525 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100526 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100527 switch (instruction->GetType()) {
528 case Primitive::kPrimBoolean:
529 case Primitive::kPrimByte:
530 case Primitive::kPrimChar:
531 case Primitive::kPrimShort:
532 case Primitive::kPrimInt:
533 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100534 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100535 Move32(location, instruction->GetLocations()->Out());
536 break;
537
538 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100539 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100540 Move64(location, instruction->GetLocations()->Out());
541 break;
542
543 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100544 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100545 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000546 }
547}
548
549void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000550 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000551}
552
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000553void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000554 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100555 DCHECK(!successor->IsExitBlock());
556
557 HBasicBlock* block = got->GetBlock();
558 HInstruction* previous = got->GetPrevious();
559
560 HLoopInformation* info = block->GetLoopInformation();
561 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
562 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
563 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
564 return;
565 }
566
567 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
568 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
569 }
570 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000571 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000572 }
573}
574
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000575void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000576 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000577}
578
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000579void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000580 if (kIsDebugBuild) {
581 __ Comment("Unreachable");
582 __ int3();
583 }
584}
585
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000586void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100587 LocationSummary* locations =
588 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100589 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100590 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100591 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100592 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000593}
594
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000595void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700596 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100597 if (cond->IsIntConstant()) {
598 // Constant condition, statically compared against 1.
599 int32_t cond_value = cond->AsIntConstant()->GetValue();
600 if (cond_value == 1) {
601 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
602 if_instr->IfTrueSuccessor())) {
603 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100604 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100605 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100606 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100607 DCHECK_EQ(cond_value, 0);
608 }
609 } else {
610 bool materialized =
611 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
612 // Moves do not affect the eflags register, so if the condition is
613 // evaluated just before the if, we don't need to evaluate it
614 // again.
615 bool eflags_set = cond->IsCondition()
616 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
617 if (materialized) {
618 if (!eflags_set) {
619 // Materialized condition, compare against 0.
620 Location lhs = if_instr->GetLocations()->InAt(0);
621 if (lhs.IsRegister()) {
622 __ cmpl(lhs.As<Register>(), Immediate(0));
623 } else {
624 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
625 }
626 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
627 } else {
628 __ j(X86Condition(cond->AsCondition()->GetCondition()),
629 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
630 }
631 } else {
632 Location lhs = cond->GetLocations()->InAt(0);
633 Location rhs = cond->GetLocations()->InAt(1);
634 // LHS is guaranteed to be in a register (see
635 // LocationsBuilderX86::VisitCondition).
636 if (rhs.IsRegister()) {
637 __ cmpl(lhs.As<Register>(), rhs.As<Register>());
638 } else if (rhs.IsConstant()) {
639 HIntConstant* instruction = rhs.GetConstant()->AsIntConstant();
640 Immediate imm(instruction->AsIntConstant()->GetValue());
641 __ cmpl(lhs.As<Register>(), imm);
642 } else {
643 __ cmpl(lhs.As<Register>(), Address(ESP, rhs.GetStackIndex()));
644 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100645 __ j(X86Condition(cond->AsCondition()->GetCondition()),
646 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700647 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100648 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100649 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
650 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700651 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000652 }
653}
654
655void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000656 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000657}
658
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000659void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
660 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000661}
662
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000663void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100664 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000665}
666
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000667void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100668 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000669}
670
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100671void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100672 LocationSummary* locations =
673 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100674 switch (store->InputAt(1)->GetType()) {
675 case Primitive::kPrimBoolean:
676 case Primitive::kPrimByte:
677 case Primitive::kPrimChar:
678 case Primitive::kPrimShort:
679 case Primitive::kPrimInt:
680 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100681 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100682 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
683 break;
684
685 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100686 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100687 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
688 break;
689
690 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100691 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100692 }
693 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000694}
695
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000696void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000697}
698
Dave Allison20dfc792014-06-16 20:44:29 -0700699void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100700 LocationSummary* locations =
701 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100702 locations->SetInAt(0, Location::RequiresRegister());
703 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100704 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100705 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100706 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000707}
708
Dave Allison20dfc792014-06-16 20:44:29 -0700709void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
710 if (comp->NeedsMaterialization()) {
711 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100712 Register reg = locations->Out().As<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100713 // Clear register: setcc only sets the low byte.
714 __ xorl(reg, reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700715 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100716 __ cmpl(locations->InAt(0).As<Register>(),
717 locations->InAt(1).As<Register>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100718 } else if (locations->InAt(1).IsConstant()) {
719 HConstant* instruction = locations->InAt(1).GetConstant();
720 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100721 __ cmpl(locations->InAt(0).As<Register>(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700722 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100723 __ cmpl(locations->InAt(0).As<Register>(),
Dave Allison20dfc792014-06-16 20:44:29 -0700724 Address(ESP, locations->InAt(1).GetStackIndex()));
725 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100726 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100727 }
Dave Allison20dfc792014-06-16 20:44:29 -0700728}
729
730void LocationsBuilderX86::VisitEqual(HEqual* comp) {
731 VisitCondition(comp);
732}
733
734void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
735 VisitCondition(comp);
736}
737
738void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
739 VisitCondition(comp);
740}
741
742void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
743 VisitCondition(comp);
744}
745
746void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
747 VisitCondition(comp);
748}
749
750void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
751 VisitCondition(comp);
752}
753
754void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
755 VisitCondition(comp);
756}
757
758void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
759 VisitCondition(comp);
760}
761
762void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
763 VisitCondition(comp);
764}
765
766void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
767 VisitCondition(comp);
768}
769
770void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
771 VisitCondition(comp);
772}
773
774void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
775 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000776}
777
778void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100779 LocationSummary* locations =
780 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100781 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000782}
783
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000784void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100785 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000786}
787
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100788void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100789 LocationSummary* locations =
790 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100791 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100792}
793
794void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
795 // Will be generated at use site.
796}
797
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000798void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000799 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000800}
801
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000802void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
803 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000804 __ ret();
805}
806
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000807void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100808 LocationSummary* locations =
809 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100810 switch (ret->InputAt(0)->GetType()) {
811 case Primitive::kPrimBoolean:
812 case Primitive::kPrimByte:
813 case Primitive::kPrimChar:
814 case Primitive::kPrimShort:
815 case Primitive::kPrimInt:
816 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100817 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100818 break;
819
820 case Primitive::kPrimLong:
821 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100822 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100823 break;
824
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100825 case Primitive::kPrimFloat:
826 case Primitive::kPrimDouble:
827 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100828 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100829 break;
830
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100831 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100832 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100833 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000834}
835
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000836void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100837 if (kIsDebugBuild) {
838 switch (ret->InputAt(0)->GetType()) {
839 case Primitive::kPrimBoolean:
840 case Primitive::kPrimByte:
841 case Primitive::kPrimChar:
842 case Primitive::kPrimShort:
843 case Primitive::kPrimInt:
844 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100845 DCHECK_EQ(ret->GetLocations()->InAt(0).As<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100846 break;
847
848 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100849 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
850 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100851 break;
852
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100853 case Primitive::kPrimFloat:
854 case Primitive::kPrimDouble:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100855 DCHECK_EQ(ret->GetLocations()->InAt(0).As<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100856 break;
857
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100858 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100859 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100860 }
861 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000862 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000863 __ ret();
864}
865
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000866void LocationsBuilderX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100867 HandleInvoke(invoke);
868}
869
870void InstructionCodeGeneratorX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100871 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100872 uint32_t heap_reference_size = sizeof(mirror::HeapReference<mirror::Object>);
873 size_t index_in_cache = mirror::Array::DataOffset(heap_reference_size).Int32Value() +
874 invoke->GetIndexInDexCache() * kX86WordSize;
875
876 // TODO: Implement all kinds of calls:
877 // 1) boot -> boot
878 // 2) app -> boot
879 // 3) app -> app
880 //
881 // Currently we implement the app -> app logic, which looks up in the resolve cache.
882
883 // temp = method;
884 LoadCurrentMethod(temp);
885 // temp = temp->dex_cache_resolved_methods_;
886 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
887 // temp = temp[index_in_cache]
888 __ movl(temp, Address(temp, index_in_cache));
889 // (temp + offset_of_quick_compiled_code)()
890 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
891
892 DCHECK(!codegen_->IsLeafMethod());
893 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
894}
895
896void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
897 HandleInvoke(invoke);
898}
899
900void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100901 LocationSummary* locations =
902 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100903 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100904
905 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100906 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100907 HInstruction* input = invoke->InputAt(i);
908 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
909 }
910
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100911 switch (invoke->GetType()) {
912 case Primitive::kPrimBoolean:
913 case Primitive::kPrimByte:
914 case Primitive::kPrimChar:
915 case Primitive::kPrimShort:
916 case Primitive::kPrimInt:
917 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100918 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100919 break;
920
921 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100922 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100923 break;
924
925 case Primitive::kPrimVoid:
926 break;
927
928 case Primitive::kPrimDouble:
929 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100930 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100931 break;
932 }
933
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000934 invoke->SetLocations(locations);
935}
936
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100937void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100938 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100939 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
940 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
941 LocationSummary* locations = invoke->GetLocations();
942 Location receiver = locations->InAt(0);
943 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
944 // temp = object->GetClass();
945 if (receiver.IsStackSlot()) {
946 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
947 __ movl(temp, Address(temp, class_offset));
948 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100949 __ movl(temp, Address(receiver.As<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100950 }
951 // temp = temp->GetMethodAt(method_offset);
952 __ movl(temp, Address(temp, method_offset));
953 // call temp->GetEntryPoint();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000954 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
955
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100956 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +0100957 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000958}
959
Roland Levillain88cb1752014-10-20 16:36:47 +0100960void LocationsBuilderX86::VisitNeg(HNeg* neg) {
961 LocationSummary* locations =
962 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
963 switch (neg->GetResultType()) {
964 case Primitive::kPrimInt:
965 locations->SetInAt(0, Location::RequiresRegister());
966 locations->SetOut(Location::SameAsFirstInput());
967 break;
968
969 case Primitive::kPrimLong:
970 case Primitive::kPrimFloat:
971 case Primitive::kPrimDouble:
972 LOG(FATAL) << "Not yet implemented neg type " << neg->GetResultType();
973 break;
974
975 default:
976 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
977 }
978}
979
980void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
981 LocationSummary* locations = neg->GetLocations();
982 Location out = locations->Out();
983 Location in = locations->InAt(0);
984 switch (neg->GetResultType()) {
985 case Primitive::kPrimInt:
986 DCHECK(in.IsRegister());
987 __ negl(out.As<Register>());
988 break;
989
990 case Primitive::kPrimLong:
991 case Primitive::kPrimFloat:
992 case Primitive::kPrimDouble:
993 LOG(FATAL) << "Not yet implemented neg type " << neg->GetResultType();
994 break;
995
996 default:
997 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
998 }
999}
1000
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001001void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001002 LocationSummary* locations =
1003 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001004 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001005 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001006 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001007 locations->SetInAt(0, Location::RequiresRegister());
1008 locations->SetInAt(1, Location::Any());
1009 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001010 break;
1011 }
1012
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001013 case Primitive::kPrimFloat:
1014 case Primitive::kPrimDouble: {
1015 locations->SetInAt(0, Location::RequiresFpuRegister());
1016 locations->SetInAt(1, Location::Any());
1017 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001018 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001019 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001020
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001021 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001022 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1023 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001024 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001025}
1026
1027void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1028 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001029 Location first = locations->InAt(0);
1030 Location second = locations->InAt(1);
1031
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001032 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001033 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001034 DCHECK_EQ(first.As<Register>(), locations->Out().As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001035 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001036 __ addl(first.As<Register>(), second.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001037 } else if (second.IsConstant()) {
1038 HConstant* instruction = second.GetConstant();
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001039 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001040 __ addl(first.As<Register>(), imm);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001041 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001042 __ addl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001043 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001044 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001045 }
1046
1047 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001048 DCHECK_EQ(first.AsRegisterPairLow<Register>(),
1049 locations->Out().AsRegisterPairLow<Register>());
1050 DCHECK_EQ(first.AsRegisterPairHigh<Register>(),
1051 locations->Out().AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001052 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001053 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1054 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001055 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001056 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1057 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001058 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001059 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001060 break;
1061 }
1062
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001063 case Primitive::kPrimFloat: {
1064 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001065 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001066 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001067 __ addss(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001068 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001069 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001070 }
1071
1072 case Primitive::kPrimDouble: {
1073 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001074 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001075 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001076 __ addsd(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001077 }
1078 break;
1079 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001080
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001081 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001082 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001083 }
1084}
1085
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001086void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001087 LocationSummary* locations =
1088 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001089 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001090 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001091 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001092 locations->SetInAt(0, Location::RequiresRegister());
1093 locations->SetInAt(1, Location::Any());
1094 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001095 break;
1096 }
1097
1098 case Primitive::kPrimBoolean:
1099 case Primitive::kPrimByte:
1100 case Primitive::kPrimChar:
1101 case Primitive::kPrimShort:
1102 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
1103 break;
1104
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001105 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001106 LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001107 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001108}
1109
1110void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
1111 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001112 Location first = locations->InAt(0);
1113 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001114 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001115 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001116 DCHECK_EQ(first.As<Register>(),
1117 locations->Out().As<Register>());
1118 if (second.IsRegister()) {
1119 __ subl(first.As<Register>(),
1120 second.As<Register>());
1121 } else if (second.IsConstant()) {
1122 HConstant* instruction = second.GetConstant();
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001123 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001124 __ subl(first.As<Register>(), imm);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001125 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001126 __ subl(first.As<Register>(),
1127 Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001128 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001129 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001130 }
1131
1132 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001133 DCHECK_EQ(first.AsRegisterPairLow<Register>(),
1134 locations->Out().AsRegisterPairLow<Register>());
1135 DCHECK_EQ(first.AsRegisterPairHigh<Register>(),
1136 locations->Out().AsRegisterPairHigh<Register>());
1137 if (second.IsRegister()) {
1138 __ subl(first.AsRegisterPairLow<Register>(),
1139 second.AsRegisterPairLow<Register>());
1140 __ sbbl(first.AsRegisterPairHigh<Register>(),
1141 second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001142 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001143 __ subl(first.AsRegisterPairLow<Register>(),
1144 Address(ESP, second.GetStackIndex()));
1145 __ sbbl(first.AsRegisterPairHigh<Register>(),
1146 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001147 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001148 break;
1149 }
1150
1151 case Primitive::kPrimBoolean:
1152 case Primitive::kPrimByte:
1153 case Primitive::kPrimChar:
1154 case Primitive::kPrimShort:
1155 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
1156 break;
1157
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001158 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001159 LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001160 }
1161}
1162
Calin Juravle34bacdf2014-10-07 20:23:36 +01001163void LocationsBuilderX86::VisitMul(HMul* mul) {
1164 LocationSummary* locations =
1165 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1166 switch (mul->GetResultType()) {
1167 case Primitive::kPrimInt:
1168 locations->SetInAt(0, Location::RequiresRegister());
1169 locations->SetInAt(1, Location::Any());
1170 locations->SetOut(Location::SameAsFirstInput());
1171 break;
1172 case Primitive::kPrimLong: {
1173 locations->SetInAt(0, Location::RequiresRegister());
1174 // TODO: Currently this handles only stack operands:
1175 // - we don't have enough registers because we currently use Quick ABI.
1176 // - by the time we have a working register allocator we will probably change the ABI
1177 // and fix the above.
1178 // - we don't have a way yet to request operands on stack but the base line compiler
1179 // will leave the operands on the stack with Any().
1180 locations->SetInAt(1, Location::Any());
1181 locations->SetOut(Location::SameAsFirstInput());
1182 // Needed for imul on 32bits with 64bits output.
1183 locations->AddTemp(Location::RegisterLocation(EAX));
1184 locations->AddTemp(Location::RegisterLocation(EDX));
1185 break;
1186 }
1187
1188 case Primitive::kPrimBoolean:
1189 case Primitive::kPrimByte:
1190 case Primitive::kPrimChar:
1191 case Primitive::kPrimShort:
1192 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
1193 break;
1194
1195 default:
1196 LOG(FATAL) << "Unimplemented mul type " << mul->GetResultType();
1197 }
1198}
1199
1200void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
1201 LocationSummary* locations = mul->GetLocations();
1202 Location first = locations->InAt(0);
1203 Location second = locations->InAt(1);
1204 DCHECK(first.Equals(locations->Out()));
1205
1206 switch (mul->GetResultType()) {
1207 case Primitive::kPrimInt: {
1208 if (second.IsRegister()) {
1209 __ imull(first.As<Register>(), second.As<Register>());
1210 } else if (second.IsConstant()) {
1211 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1212 __ imull(first.As<Register>(), imm);
1213 } else {
1214 DCHECK(second.IsStackSlot());
1215 __ imull(first.As<Register>(), Address(ESP, second.GetStackIndex()));
1216 }
1217 break;
1218 }
1219
1220 case Primitive::kPrimLong: {
1221 DCHECK(second.IsDoubleStackSlot());
1222
1223 Register in1_hi = first.AsRegisterPairHigh<Register>();
1224 Register in1_lo = first.AsRegisterPairLow<Register>();
1225 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
1226 Address in2_lo(ESP, second.GetStackIndex());
1227 Register eax = locations->GetTemp(0).As<Register>();
1228 Register edx = locations->GetTemp(1).As<Register>();
1229
1230 DCHECK_EQ(EAX, eax);
1231 DCHECK_EQ(EDX, edx);
1232
1233 // input: in1 - 64 bits, in2 - 64 bits
1234 // output: in1
1235 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
1236 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
1237 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
1238
1239 __ movl(eax, in2_hi);
1240 // eax <- in1.lo * in2.hi
1241 __ imull(eax, in1_lo);
1242 // in1.hi <- in1.hi * in2.lo
1243 __ imull(in1_hi, in2_lo);
1244 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
1245 __ addl(in1_hi, eax);
1246 // move in1_lo to eax to prepare for double precision
1247 __ movl(eax, in1_lo);
1248 // edx:eax <- in1.lo * in2.lo
1249 __ mull(in2_lo);
1250 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
1251 __ addl(in1_hi, edx);
1252 // in1.lo <- (in1.lo * in2.lo)[31:0];
1253 __ movl(in1_lo, eax);
1254
1255 break;
1256 }
1257
1258 case Primitive::kPrimBoolean:
1259 case Primitive::kPrimByte:
1260 case Primitive::kPrimChar:
1261 case Primitive::kPrimShort:
1262 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
1263 break;
1264
1265 default:
1266 LOG(FATAL) << "Unimplemented mul type " << mul->GetResultType();
1267 }
1268}
1269
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001270void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001271 LocationSummary* locations =
1272 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001273 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001274 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001275 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1276 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001277}
1278
1279void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
1280 InvokeRuntimeCallingConvention calling_convention;
1281 LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001282 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001283
Nicolas Geoffray707c8092014-04-04 10:50:14 +01001284 __ fs()->call(
1285 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001286
Nicolas Geoffray39468442014-09-02 15:17:15 +01001287 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001288 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001289}
1290
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001291void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001292 LocationSummary* locations =
1293 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001294 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1295 if (location.IsStackSlot()) {
1296 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1297 } else if (location.IsDoubleStackSlot()) {
1298 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001299 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001300 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001301}
1302
1303void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001304}
1305
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001306void LocationsBuilderX86::VisitNot(HNot* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001307 LocationSummary* locations =
1308 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001309 locations->SetInAt(0, Location::RequiresRegister());
1310 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001311}
1312
1313void InstructionCodeGeneratorX86::VisitNot(HNot* instruction) {
1314 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001315 Location out = locations->Out();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001316 DCHECK_EQ(locations->InAt(0).As<Register>(), out.As<Register>());
1317 __ xorl(out.As<Register>(), Immediate(1));
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001318}
1319
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001320void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001321 LocationSummary* locations =
1322 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001323 locations->SetInAt(0, Location::RequiresRegister());
1324 locations->SetInAt(1, Location::Any());
1325 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001326}
1327
1328void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
1329 Label greater, done;
1330 LocationSummary* locations = compare->GetLocations();
1331 switch (compare->InputAt(0)->GetType()) {
1332 case Primitive::kPrimLong: {
1333 Label less, greater, done;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001334 Register output = locations->Out().As<Register>();
1335 Location left = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001336 Location right = locations->InAt(1);
1337 if (right.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001338 __ cmpl(left.AsRegisterPairHigh<Register>(), right.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001339 } else {
1340 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001341 __ cmpl(left.AsRegisterPairHigh<Register>(),
1342 Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001343 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001344 __ j(kLess, &less); // Signed compare.
1345 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001346 if (right.IsRegisterPair()) {
1347 __ cmpl(left.AsRegisterPairLow<Register>(), right.AsRegisterPairLow<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001348 } else {
1349 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001350 __ cmpl(left.AsRegisterPairLow<Register>(), Address(ESP, right.GetStackIndex()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001351 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001352 __ movl(output, Immediate(0));
1353 __ j(kEqual, &done);
1354 __ j(kBelow, &less); // Unsigned compare.
1355
1356 __ Bind(&greater);
1357 __ movl(output, Immediate(1));
1358 __ jmp(&done);
1359
1360 __ Bind(&less);
1361 __ movl(output, Immediate(-1));
1362
1363 __ Bind(&done);
1364 break;
1365 }
1366 default:
1367 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
1368 }
1369}
1370
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001371void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001372 LocationSummary* locations =
1373 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001374 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1375 locations->SetInAt(i, Location::Any());
1376 }
1377 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001378}
1379
1380void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001381 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001382}
1383
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001384void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001385 LocationSummary* locations =
1386 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001387 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001388 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001389 bool is_object_type = field_type == Primitive::kPrimNot;
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001390 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
1391 || (field_type == Primitive::kPrimByte);
1392 // The register allocator does not support multiple
1393 // inputs that die at entry with one in a specific register.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001394 if (is_byte_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001395 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001396 locations->SetInAt(1, Location::RegisterLocation(EAX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001397 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001398 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001399 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001400 // Temporary registers for the write barrier.
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001401 if (is_object_type) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001402 locations->AddTemp(Location::RequiresRegister());
1403 // Ensure the card is in a byte register.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001404 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001405 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001406}
1407
1408void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1409 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001410 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001411 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001412 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001413
1414 switch (field_type) {
1415 case Primitive::kPrimBoolean:
1416 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001417 ByteRegister value = locations->InAt(1).As<ByteRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001418 __ movb(Address(obj, offset), value);
1419 break;
1420 }
1421
1422 case Primitive::kPrimShort:
1423 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001424 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001425 __ movw(Address(obj, offset), value);
1426 break;
1427 }
1428
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001429 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001430 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001431 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001432 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001433
1434 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001435 Register temp = locations->GetTemp(0).As<Register>();
1436 Register card = locations->GetTemp(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001437 codegen_->MarkGCCard(temp, card, obj, value);
1438 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001439 break;
1440 }
1441
1442 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001443 Location value = locations->InAt(1);
1444 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
1445 __ movl(Address(obj, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001446 break;
1447 }
1448
1449 case Primitive::kPrimFloat:
1450 case Primitive::kPrimDouble:
1451 LOG(FATAL) << "Unimplemented register type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001452 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001453 case Primitive::kPrimVoid:
1454 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001455 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001456 }
1457}
1458
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001459void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
1460 Label is_null;
1461 __ testl(value, value);
1462 __ j(kEqual, &is_null);
1463 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
1464 __ movl(temp, object);
1465 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
1466 __ movb(Address(temp, card, TIMES_1, 0),
1467 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
1468 __ Bind(&is_null);
1469}
1470
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001471void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001472 LocationSummary* locations =
1473 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001474 locations->SetInAt(0, Location::RequiresRegister());
1475 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001476}
1477
1478void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1479 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001480 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001481 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
1482
1483 switch (instruction->GetType()) {
1484 case Primitive::kPrimBoolean: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001485 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001486 __ movzxb(out, Address(obj, offset));
1487 break;
1488 }
1489
1490 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001491 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001492 __ movsxb(out, Address(obj, offset));
1493 break;
1494 }
1495
1496 case Primitive::kPrimShort: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001497 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001498 __ movsxw(out, Address(obj, offset));
1499 break;
1500 }
1501
1502 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001503 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001504 __ movzxw(out, Address(obj, offset));
1505 break;
1506 }
1507
1508 case Primitive::kPrimInt:
1509 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001510 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001511 __ movl(out, Address(obj, offset));
1512 break;
1513 }
1514
1515 case Primitive::kPrimLong: {
1516 // TODO: support volatile.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001517 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(obj, offset));
1518 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(obj, kX86WordSize + offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001519 break;
1520 }
1521
1522 case Primitive::kPrimFloat:
1523 case Primitive::kPrimDouble:
1524 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001525 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001526 case Primitive::kPrimVoid:
1527 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001528 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001529 }
1530}
1531
1532void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001533 LocationSummary* locations =
1534 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001535 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001536 if (instruction->HasUses()) {
1537 locations->SetOut(Location::SameAsFirstInput());
1538 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001539}
1540
1541void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001542 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001543 codegen_->AddSlowPath(slow_path);
1544
1545 LocationSummary* locations = instruction->GetLocations();
1546 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001547
1548 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001549 __ cmpl(obj.As<Register>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001550 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001551 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001552 } else {
1553 DCHECK(obj.IsConstant()) << obj;
1554 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
1555 __ jmp(slow_path->GetEntryLabel());
1556 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001557 }
1558 __ j(kEqual, slow_path->GetEntryLabel());
1559}
1560
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001561void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001562 LocationSummary* locations =
1563 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001564 locations->SetInAt(0, Location::RequiresRegister());
1565 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1566 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001567}
1568
1569void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
1570 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001571 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001572 Location index = locations->InAt(1);
1573
1574 switch (instruction->GetType()) {
1575 case Primitive::kPrimBoolean: {
1576 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001577 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001578 if (index.IsConstant()) {
1579 __ movzxb(out, Address(obj,
1580 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1581 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001582 __ movzxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001583 }
1584 break;
1585 }
1586
1587 case Primitive::kPrimByte: {
1588 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001589 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001590 if (index.IsConstant()) {
1591 __ movsxb(out, Address(obj,
1592 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1593 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001594 __ movsxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001595 }
1596 break;
1597 }
1598
1599 case Primitive::kPrimShort: {
1600 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001601 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001602 if (index.IsConstant()) {
1603 __ movsxw(out, Address(obj,
1604 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1605 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001606 __ movsxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001607 }
1608 break;
1609 }
1610
1611 case Primitive::kPrimChar: {
1612 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001613 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001614 if (index.IsConstant()) {
1615 __ movzxw(out, Address(obj,
1616 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1617 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001618 __ movzxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001619 }
1620 break;
1621 }
1622
1623 case Primitive::kPrimInt:
1624 case Primitive::kPrimNot: {
1625 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001626 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001627 if (index.IsConstant()) {
1628 __ movl(out, Address(obj,
1629 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
1630 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001631 __ movl(out, Address(obj, index.As<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001632 }
1633 break;
1634 }
1635
1636 case Primitive::kPrimLong: {
1637 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001638 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001639 if (index.IsConstant()) {
1640 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001641 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
1642 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001643 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001644 __ movl(out.AsRegisterPairLow<Register>(),
1645 Address(obj, index.As<Register>(), TIMES_8, data_offset));
1646 __ movl(out.AsRegisterPairHigh<Register>(),
1647 Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001648 }
1649 break;
1650 }
1651
1652 case Primitive::kPrimFloat:
1653 case Primitive::kPrimDouble:
1654 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001655 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001656 case Primitive::kPrimVoid:
1657 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001658 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001659 }
1660}
1661
1662void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001663 Primitive::Type value_type = instruction->GetComponentType();
1664 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1665 instruction,
1666 value_type == Primitive::kPrimNot ? LocationSummary::kCall : LocationSummary::kNoCall);
1667
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001668 if (value_type == Primitive::kPrimNot) {
1669 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001670 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1671 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1672 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001673 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001674 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
1675 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001676 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001677 // In case of a byte operation, the register allocator does not support multiple
1678 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001679 locations->SetInAt(0, Location::RequiresRegister());
1680 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001681 if (is_byte_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001682 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001683 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001684 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001685 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001686 }
1687 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001688}
1689
1690void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
1691 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001692 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001693 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001694 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001695 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001696
1697 switch (value_type) {
1698 case Primitive::kPrimBoolean:
1699 case Primitive::kPrimByte: {
1700 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001701 if (index.IsConstant()) {
1702 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001703 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001704 __ movb(Address(obj, offset), value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001705 } else {
1706 __ movb(Address(obj, offset),
1707 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1708 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001709 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001710 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001711 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
1712 value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001713 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001714 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001715 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1716 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001717 }
1718 break;
1719 }
1720
1721 case Primitive::kPrimShort:
1722 case Primitive::kPrimChar: {
1723 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001724 if (index.IsConstant()) {
1725 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001726 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001727 __ movw(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001728 } else {
1729 __ movw(Address(obj, offset),
1730 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1731 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001732 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001733 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001734 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
1735 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001736 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001737 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001738 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1739 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001740 }
1741 break;
1742 }
1743
1744 case Primitive::kPrimInt: {
1745 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001746 if (index.IsConstant()) {
1747 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001748 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001749 __ movl(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001750 } else {
1751 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1752 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001753 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001754 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001755 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
1756 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001757 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001758 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001759 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1760 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001761 }
1762 break;
1763 }
1764
1765 case Primitive::kPrimNot: {
1766 DCHECK(!codegen_->IsLeafMethod());
1767 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001768 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001769 break;
1770 }
1771
1772 case Primitive::kPrimLong: {
1773 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001774 if (index.IsConstant()) {
1775 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001776 if (value.IsRegisterPair()) {
1777 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
1778 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001779 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001780 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001781 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
1782 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
1783 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
1784 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001785 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001786 if (value.IsRegisterPair()) {
1787 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
1788 value.AsRegisterPairLow<Register>());
1789 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
1790 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001791 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001792 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001793 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001794 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001795 Immediate(Low32Bits(val)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001796 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001797 Immediate(High32Bits(val)));
1798 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001799 }
1800 break;
1801 }
1802
1803 case Primitive::kPrimFloat:
1804 case Primitive::kPrimDouble:
1805 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001806 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001807 case Primitive::kPrimVoid:
1808 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001809 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001810 }
1811}
1812
1813void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
1814 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001815 locations->SetInAt(0, Location::RequiresRegister());
1816 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001817 instruction->SetLocations(locations);
1818}
1819
1820void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
1821 LocationSummary* locations = instruction->GetLocations();
1822 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001823 Register obj = locations->InAt(0).As<Register>();
1824 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001825 __ movl(out, Address(obj, offset));
1826}
1827
1828void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001829 LocationSummary* locations =
1830 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001831 locations->SetInAt(0, Location::RequiresRegister());
1832 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001833 if (instruction->HasUses()) {
1834 locations->SetOut(Location::SameAsFirstInput());
1835 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001836}
1837
1838void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
1839 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001840 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01001841 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001842 codegen_->AddSlowPath(slow_path);
1843
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001844 Register index = locations->InAt(0).As<Register>();
1845 Register length = locations->InAt(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001846
1847 __ cmpl(index, length);
1848 __ j(kAboveEqual, slow_path->GetEntryLabel());
1849}
1850
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001851void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
1852 temp->SetLocations(nullptr);
1853}
1854
1855void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
1856 // Nothing to do, this is driven by the code generator.
1857}
1858
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001859void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001860 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001861}
1862
1863void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001864 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
1865}
1866
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001867void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
1868 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
1869}
1870
1871void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001872 HBasicBlock* block = instruction->GetBlock();
1873 if (block->GetLoopInformation() != nullptr) {
1874 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
1875 // The back edge will generate the suspend check.
1876 return;
1877 }
1878 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
1879 // The goto will generate the suspend check.
1880 return;
1881 }
1882 GenerateSuspendCheck(instruction, nullptr);
1883}
1884
1885void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
1886 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001887 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001888 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001889 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001890 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001891 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001892 if (successor == nullptr) {
1893 __ j(kNotEqual, slow_path->GetEntryLabel());
1894 __ Bind(slow_path->GetReturnLabel());
1895 } else {
1896 __ j(kEqual, codegen_->GetLabelOf(successor));
1897 __ jmp(slow_path->GetEntryLabel());
1898 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001899}
1900
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001901X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
1902 return codegen_->GetAssembler();
1903}
1904
1905void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
1906 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001907 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001908 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
1909 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
1910 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
1911}
1912
1913void ParallelMoveResolverX86::EmitMove(size_t index) {
1914 MoveOperands* move = moves_.Get(index);
1915 Location source = move->GetSource();
1916 Location destination = move->GetDestination();
1917
1918 if (source.IsRegister()) {
1919 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001920 __ movl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001921 } else {
1922 DCHECK(destination.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001923 __ movl(Address(ESP, destination.GetStackIndex()), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001924 }
1925 } else if (source.IsStackSlot()) {
1926 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001927 __ movl(destination.As<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001928 } else {
1929 DCHECK(destination.IsStackSlot());
1930 MoveMemoryToMemory(destination.GetStackIndex(),
1931 source.GetStackIndex());
1932 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001933 } else if (source.IsConstant()) {
1934 HIntConstant* instruction = source.GetConstant()->AsIntConstant();
1935 Immediate imm(instruction->AsIntConstant()->GetValue());
1936 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001937 __ movl(destination.As<Register>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001938 } else {
1939 __ movl(Address(ESP, destination.GetStackIndex()), imm);
1940 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001941 } else {
1942 LOG(FATAL) << "Unimplemented";
1943 }
1944}
1945
1946void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001947 Register suggested_scratch = reg == EAX ? EBX : EAX;
1948 ScratchRegisterScope ensure_scratch(
1949 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
1950
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001951 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
1952 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
1953 __ movl(Address(ESP, mem + stack_offset), reg);
1954 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
1955}
1956
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001957void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
1958 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001959 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
1960
1961 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001962 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001963 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
1964
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001965 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
1966 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
1967 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
1968 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
1969 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
1970 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
1971}
1972
1973void ParallelMoveResolverX86::EmitSwap(size_t index) {
1974 MoveOperands* move = moves_.Get(index);
1975 Location source = move->GetSource();
1976 Location destination = move->GetDestination();
1977
1978 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001979 __ xchgl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001980 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001981 Exchange(source.As<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001982 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001983 Exchange(destination.As<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001984 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
1985 Exchange(destination.GetStackIndex(), source.GetStackIndex());
1986 } else {
1987 LOG(FATAL) << "Unimplemented";
1988 }
1989}
1990
1991void ParallelMoveResolverX86::SpillScratch(int reg) {
1992 __ pushl(static_cast<Register>(reg));
1993}
1994
1995void ParallelMoveResolverX86::RestoreScratch(int reg) {
1996 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001997}
1998
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001999} // namespace x86
2000} // namespace art