blob: 267edca2b0325f785f441942f89323aa1984b21b [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 Geoffray19a19cf2014-10-22 16:07:05 +0100160class ClinitCheckSlowPathX86 : public SlowPathCodeX86 {
161 public:
162 explicit ClinitCheckSlowPathX86(HClinitCheck* instruction) : instruction_(instruction) {}
163
164 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
165 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
166 __ Bind(GetEntryLabel());
167 codegen->SaveLiveRegisters(instruction_->GetLocations());
168
169 HLoadClass* cls = instruction_->GetLoadClass();
170 InvokeRuntimeCallingConvention calling_convention;
171 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls->GetTypeIndex()));
172 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
173 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeStaticStorage)));
174 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
175 x86_codegen->Move32(instruction_->GetLocations()->InAt(0), Location::RegisterLocation(EAX));
176 codegen->RestoreLiveRegisters(instruction_->GetLocations());
177 __ jmp(GetExitLabel());
178 }
179
180 private:
181 HClinitCheck* const instruction_;
182
183 DISALLOW_COPY_AND_ASSIGN(ClinitCheckSlowPathX86);
184};
185
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100186#undef __
187#define __ reinterpret_cast<X86Assembler*>(GetAssembler())->
188
Dave Allison20dfc792014-06-16 20:44:29 -0700189inline Condition X86Condition(IfCondition cond) {
190 switch (cond) {
191 case kCondEQ: return kEqual;
192 case kCondNE: return kNotEqual;
193 case kCondLT: return kLess;
194 case kCondLE: return kLessEqual;
195 case kCondGT: return kGreater;
196 case kCondGE: return kGreaterEqual;
197 default:
198 LOG(FATAL) << "Unknown if condition";
199 }
200 return kEqual;
201}
202
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100203void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
204 stream << X86ManagedRegister::FromCpuRegister(Register(reg));
205}
206
207void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
208 stream << X86ManagedRegister::FromXmmRegister(XmmRegister(reg));
209}
210
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100211size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
212 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
213 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100214}
215
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100216size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
217 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
218 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100219}
220
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100221CodeGeneratorX86::CodeGeneratorX86(HGraph* graph)
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100222 : CodeGenerator(graph, kNumberOfCpuRegisters, kNumberOfXmmRegisters, kNumberOfRegisterPairs),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100223 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100224 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100225 instruction_visitor_(graph, this),
226 move_resolver_(graph->GetArena(), this) {}
227
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100228size_t CodeGeneratorX86::FrameEntrySpillSize() const {
229 return kNumberOfPushedRegistersAtEntry * kX86WordSize;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100230}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100231
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100232Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100233 switch (type) {
234 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100235 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100236 X86ManagedRegister pair =
237 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100238 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
239 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100240 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
241 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100242 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100243 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100244 }
245
246 case Primitive::kPrimByte:
247 case Primitive::kPrimBoolean:
248 case Primitive::kPrimChar:
249 case Primitive::kPrimShort:
250 case Primitive::kPrimInt:
251 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100252 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100253 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100254 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100255 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
256 X86ManagedRegister current =
257 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
258 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100259 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100260 }
261 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100262 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100263 }
264
265 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100266 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100267 return Location::FpuRegisterLocation(
268 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100269 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100270
271 case Primitive::kPrimVoid:
272 LOG(FATAL) << "Unreachable type " << type;
273 }
274
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100275 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100276}
277
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100278void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100279 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100280 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100281
282 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100283 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100284
285 // TODO: We currently don't use Quick's callee saved registers.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100286 blocked_core_registers_[EBP] = true;
287 blocked_core_registers_[ESI] = true;
288 blocked_core_registers_[EDI] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100289
290 UpdateBlockedPairRegisters();
291}
292
293void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
294 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
295 X86ManagedRegister current =
296 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
297 if (blocked_core_registers_[current.AsRegisterPairLow()]
298 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
299 blocked_register_pairs_[i] = true;
300 }
301 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100302}
303
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100304InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
305 : HGraphVisitor(graph),
306 assembler_(codegen->GetAssembler()),
307 codegen_(codegen) {}
308
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000309void CodeGeneratorX86::GenerateFrameEntry() {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000310 // Create a fake register to mimic Quick.
311 static const int kFakeReturnRegister = 8;
312 core_spill_mask_ |= (1 << kFakeReturnRegister);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000313
Dave Allison648d7112014-07-25 16:15:27 -0700314 bool skip_overflow_check = IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100315 if (!skip_overflow_check && !kExplicitStackOverflowCheck) {
316 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100317 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100318 }
319
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100320 // The return PC has already been pushed on the stack.
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100321 __ subl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100322
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100323 if (!skip_overflow_check && kExplicitStackOverflowCheck) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100324 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathX86();
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100325 AddSlowPath(slow_path);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100326
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100327 __ fs()->cmpl(ESP, Address::Absolute(Thread::StackEndOffset<kX86WordSize>()));
328 __ j(kLess, slow_path->GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100329 }
330
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100331 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000332}
333
334void CodeGeneratorX86::GenerateFrameExit() {
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100335 __ addl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000336}
337
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100338void CodeGeneratorX86::Bind(HBasicBlock* block) {
339 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000340}
341
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100342void CodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100343 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000344}
345
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100346Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
347 switch (load->GetType()) {
348 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100349 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100350 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
351 break;
352
353 case Primitive::kPrimInt:
354 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100355 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100356 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100357
358 case Primitive::kPrimBoolean:
359 case Primitive::kPrimByte:
360 case Primitive::kPrimChar:
361 case Primitive::kPrimShort:
362 case Primitive::kPrimVoid:
363 LOG(FATAL) << "Unexpected type " << load->GetType();
364 }
365
366 LOG(FATAL) << "Unreachable";
367 return Location();
368}
369
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100370Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
371 switch (type) {
372 case Primitive::kPrimBoolean:
373 case Primitive::kPrimByte:
374 case Primitive::kPrimChar:
375 case Primitive::kPrimShort:
376 case Primitive::kPrimInt:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100377 case Primitive::kPrimFloat:
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100378 case Primitive::kPrimNot: {
379 uint32_t index = gp_index_++;
380 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100381 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100382 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100383 return Location::StackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100384 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100385 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100386
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100387 case Primitive::kPrimLong:
388 case Primitive::kPrimDouble: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100389 uint32_t index = gp_index_;
390 gp_index_ += 2;
391 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100392 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
393 calling_convention.GetRegisterPairAt(index));
394 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100395 } else if (index + 1 == calling_convention.GetNumberOfRegisters()) {
396 return Location::QuickParameter(index);
397 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100398 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100399 }
400 }
401
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100402 case Primitive::kPrimVoid:
403 LOG(FATAL) << "Unexpected parameter type " << type;
404 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100405 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100406 return Location();
407}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100408
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100409void CodeGeneratorX86::Move32(Location destination, Location source) {
410 if (source.Equals(destination)) {
411 return;
412 }
413 if (destination.IsRegister()) {
414 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100415 __ movl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100416 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100417 __ movd(destination.As<Register>(), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100418 } else {
419 DCHECK(source.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100420 __ movl(destination.As<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100421 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100422 } else if (destination.IsFpuRegister()) {
423 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100424 __ movd(destination.As<XmmRegister>(), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100425 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100426 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100427 } else {
428 DCHECK(source.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100429 __ movss(destination.As<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100430 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100431 } else {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100432 DCHECK(destination.IsStackSlot());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100433 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100434 __ movl(Address(ESP, destination.GetStackIndex()), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100435 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100436 __ movss(Address(ESP, destination.GetStackIndex()), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100437 } else {
438 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100439 __ pushl(Address(ESP, source.GetStackIndex()));
440 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100441 }
442 }
443}
444
445void CodeGeneratorX86::Move64(Location destination, Location source) {
446 if (source.Equals(destination)) {
447 return;
448 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100449 if (destination.IsRegisterPair()) {
450 if (source.IsRegisterPair()) {
451 __ movl(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
452 __ movl(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100453 } else if (source.IsFpuRegister()) {
454 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100455 } else if (source.IsQuickParameter()) {
456 uint32_t argument_index = source.GetQuickParameterIndex();
457 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100458 __ movl(destination.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100459 calling_convention.GetRegisterAt(argument_index));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100460 __ movl(destination.AsRegisterPairHigh<Register>(), Address(ESP,
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100461 calling_convention.GetStackOffsetOf(argument_index + 1) + GetFrameSize()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100462 } else {
463 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100464 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
465 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100466 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
467 }
468 } else if (destination.IsQuickParameter()) {
469 InvokeDexCallingConvention calling_convention;
470 uint32_t argument_index = destination.GetQuickParameterIndex();
471 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100472 __ movl(calling_convention.GetRegisterAt(argument_index), source.AsRegisterPairLow<Register>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100473 __ movl(Address(ESP, calling_convention.GetStackOffsetOf(argument_index + 1)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100474 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100475 } else if (source.IsFpuRegister()) {
476 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100477 } else {
478 DCHECK(source.IsDoubleStackSlot());
479 __ movl(calling_convention.GetRegisterAt(argument_index),
480 Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100481 __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100482 __ popl(Address(ESP, calling_convention.GetStackOffsetOf(argument_index + 1)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100483 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100484 } else if (destination.IsFpuRegister()) {
485 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100486 __ movsd(destination.As<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100487 } else {
488 LOG(FATAL) << "Unimplemented";
489 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100490 } else {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100491 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100492 if (source.IsRegisterPair()) {
493 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100494 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100495 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100496 } else if (source.IsQuickParameter()) {
497 InvokeDexCallingConvention calling_convention;
498 uint32_t argument_index = source.GetQuickParameterIndex();
499 __ movl(Address(ESP, destination.GetStackIndex()),
500 calling_convention.GetRegisterAt(argument_index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100501 DCHECK_EQ(calling_convention.GetStackOffsetOf(argument_index + 1) + GetFrameSize(),
502 static_cast<size_t>(destination.GetHighStackIndex(kX86WordSize)));
503 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100504 __ movsd(Address(ESP, destination.GetStackIndex()), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100505 } else {
506 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100507 __ pushl(Address(ESP, source.GetStackIndex()));
508 __ popl(Address(ESP, destination.GetStackIndex()));
509 __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize)));
510 __ popl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100511 }
512 }
513}
514
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100515void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Roland Levillain476df552014-10-09 17:51:36 +0100516 if (instruction->IsIntConstant()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100517 Immediate imm(instruction->AsIntConstant()->GetValue());
518 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100519 __ movl(location.As<Register>(), imm);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100520 } else if (location.IsStackSlot()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100521 __ movl(Address(ESP, location.GetStackIndex()), imm);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100522 } else {
523 DCHECK(location.IsConstant());
524 DCHECK_EQ(location.GetConstant(), instruction);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100525 }
Roland Levillain476df552014-10-09 17:51:36 +0100526 } else if (instruction->IsLongConstant()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100527 int64_t value = instruction->AsLongConstant()->GetValue();
528 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100529 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
530 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100531 } else if (location.IsDoubleStackSlot()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100532 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
533 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100534 } else {
535 DCHECK(location.IsConstant());
536 DCHECK_EQ(location.GetConstant(), instruction);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100537 }
Roland Levillain476df552014-10-09 17:51:36 +0100538 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100539 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100540 switch (instruction->GetType()) {
541 case Primitive::kPrimBoolean:
542 case Primitive::kPrimByte:
543 case Primitive::kPrimChar:
544 case Primitive::kPrimShort:
545 case Primitive::kPrimInt:
546 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100547 case Primitive::kPrimFloat:
548 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100549 break;
550
551 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100552 case Primitive::kPrimDouble:
553 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100554 break;
555
556 default:
557 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
558 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000559 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100560 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100561 switch (instruction->GetType()) {
562 case Primitive::kPrimBoolean:
563 case Primitive::kPrimByte:
564 case Primitive::kPrimChar:
565 case Primitive::kPrimShort:
566 case Primitive::kPrimInt:
567 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100568 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100569 Move32(location, instruction->GetLocations()->Out());
570 break;
571
572 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100573 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100574 Move64(location, instruction->GetLocations()->Out());
575 break;
576
577 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100578 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100579 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000580 }
581}
582
583void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000584 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000585}
586
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000587void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000588 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100589 DCHECK(!successor->IsExitBlock());
590
591 HBasicBlock* block = got->GetBlock();
592 HInstruction* previous = got->GetPrevious();
593
594 HLoopInformation* info = block->GetLoopInformation();
595 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
596 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
597 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
598 return;
599 }
600
601 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
602 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
603 }
604 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000605 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000606 }
607}
608
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000609void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000610 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000611}
612
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000613void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000614 if (kIsDebugBuild) {
615 __ Comment("Unreachable");
616 __ int3();
617 }
618}
619
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000620void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100621 LocationSummary* locations =
622 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100623 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100624 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100625 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100626 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000627}
628
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000629void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700630 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100631 if (cond->IsIntConstant()) {
632 // Constant condition, statically compared against 1.
633 int32_t cond_value = cond->AsIntConstant()->GetValue();
634 if (cond_value == 1) {
635 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
636 if_instr->IfTrueSuccessor())) {
637 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100638 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100639 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100640 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100641 DCHECK_EQ(cond_value, 0);
642 }
643 } else {
644 bool materialized =
645 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
646 // Moves do not affect the eflags register, so if the condition is
647 // evaluated just before the if, we don't need to evaluate it
648 // again.
649 bool eflags_set = cond->IsCondition()
650 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
651 if (materialized) {
652 if (!eflags_set) {
653 // Materialized condition, compare against 0.
654 Location lhs = if_instr->GetLocations()->InAt(0);
655 if (lhs.IsRegister()) {
656 __ cmpl(lhs.As<Register>(), Immediate(0));
657 } else {
658 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
659 }
660 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
661 } else {
662 __ j(X86Condition(cond->AsCondition()->GetCondition()),
663 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
664 }
665 } else {
666 Location lhs = cond->GetLocations()->InAt(0);
667 Location rhs = cond->GetLocations()->InAt(1);
668 // LHS is guaranteed to be in a register (see
669 // LocationsBuilderX86::VisitCondition).
670 if (rhs.IsRegister()) {
671 __ cmpl(lhs.As<Register>(), rhs.As<Register>());
672 } else if (rhs.IsConstant()) {
673 HIntConstant* instruction = rhs.GetConstant()->AsIntConstant();
674 Immediate imm(instruction->AsIntConstant()->GetValue());
675 __ cmpl(lhs.As<Register>(), imm);
676 } else {
677 __ cmpl(lhs.As<Register>(), Address(ESP, rhs.GetStackIndex()));
678 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100679 __ j(X86Condition(cond->AsCondition()->GetCondition()),
680 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700681 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100682 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100683 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
684 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700685 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000686 }
687}
688
689void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000690 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000691}
692
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000693void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
694 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000695}
696
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000697void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100698 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000699}
700
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000701void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100702 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000703}
704
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100705void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100706 LocationSummary* locations =
707 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100708 switch (store->InputAt(1)->GetType()) {
709 case Primitive::kPrimBoolean:
710 case Primitive::kPrimByte:
711 case Primitive::kPrimChar:
712 case Primitive::kPrimShort:
713 case Primitive::kPrimInt:
714 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100715 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100716 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
717 break;
718
719 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100720 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100721 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
722 break;
723
724 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100725 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100726 }
727 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000728}
729
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000730void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000731}
732
Dave Allison20dfc792014-06-16 20:44:29 -0700733void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100734 LocationSummary* locations =
735 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100736 locations->SetInAt(0, Location::RequiresRegister());
737 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100738 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100739 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100740 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000741}
742
Dave Allison20dfc792014-06-16 20:44:29 -0700743void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
744 if (comp->NeedsMaterialization()) {
745 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100746 Register reg = locations->Out().As<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100747 // Clear register: setcc only sets the low byte.
748 __ xorl(reg, reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700749 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100750 __ cmpl(locations->InAt(0).As<Register>(),
751 locations->InAt(1).As<Register>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100752 } else if (locations->InAt(1).IsConstant()) {
753 HConstant* instruction = locations->InAt(1).GetConstant();
754 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100755 __ cmpl(locations->InAt(0).As<Register>(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700756 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100757 __ cmpl(locations->InAt(0).As<Register>(),
Dave Allison20dfc792014-06-16 20:44:29 -0700758 Address(ESP, locations->InAt(1).GetStackIndex()));
759 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100760 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100761 }
Dave Allison20dfc792014-06-16 20:44:29 -0700762}
763
764void LocationsBuilderX86::VisitEqual(HEqual* comp) {
765 VisitCondition(comp);
766}
767
768void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
769 VisitCondition(comp);
770}
771
772void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
773 VisitCondition(comp);
774}
775
776void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
777 VisitCondition(comp);
778}
779
780void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
781 VisitCondition(comp);
782}
783
784void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
785 VisitCondition(comp);
786}
787
788void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
789 VisitCondition(comp);
790}
791
792void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
793 VisitCondition(comp);
794}
795
796void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
797 VisitCondition(comp);
798}
799
800void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
801 VisitCondition(comp);
802}
803
804void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
805 VisitCondition(comp);
806}
807
808void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
809 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000810}
811
812void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100813 LocationSummary* locations =
814 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100815 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000816}
817
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000818void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100819 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000820}
821
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100822void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100823 LocationSummary* locations =
824 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100825 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100826}
827
828void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
829 // Will be generated at use site.
830}
831
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100832void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
833 LocationSummary* locations =
834 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
835 locations->SetOut(Location::ConstantLocation(constant));
836}
837
838void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
839 // Will be generated at use site.
840}
841
842void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
843 LocationSummary* locations =
844 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
845 locations->SetOut(Location::ConstantLocation(constant));
846}
847
848void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
849 // Will be generated at use site.
850}
851
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000852void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000853 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000854}
855
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000856void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
857 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000858 __ ret();
859}
860
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000861void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100862 LocationSummary* locations =
863 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100864 switch (ret->InputAt(0)->GetType()) {
865 case Primitive::kPrimBoolean:
866 case Primitive::kPrimByte:
867 case Primitive::kPrimChar:
868 case Primitive::kPrimShort:
869 case Primitive::kPrimInt:
870 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100871 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100872 break;
873
874 case Primitive::kPrimLong:
875 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100876 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100877 break;
878
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100879 case Primitive::kPrimFloat:
880 case Primitive::kPrimDouble:
881 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100882 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100883 break;
884
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100885 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100886 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100887 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000888}
889
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000890void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100891 if (kIsDebugBuild) {
892 switch (ret->InputAt(0)->GetType()) {
893 case Primitive::kPrimBoolean:
894 case Primitive::kPrimByte:
895 case Primitive::kPrimChar:
896 case Primitive::kPrimShort:
897 case Primitive::kPrimInt:
898 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100899 DCHECK_EQ(ret->GetLocations()->InAt(0).As<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100900 break;
901
902 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100903 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
904 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100905 break;
906
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100907 case Primitive::kPrimFloat:
908 case Primitive::kPrimDouble:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100909 DCHECK_EQ(ret->GetLocations()->InAt(0).As<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100910 break;
911
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100912 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100913 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100914 }
915 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000916 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000917 __ ret();
918}
919
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000920void LocationsBuilderX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100921 HandleInvoke(invoke);
922}
923
924void InstructionCodeGeneratorX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100925 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100926
927 // TODO: Implement all kinds of calls:
928 // 1) boot -> boot
929 // 2) app -> boot
930 // 3) app -> app
931 //
932 // Currently we implement the app -> app logic, which looks up in the resolve cache.
933
934 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100935 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100936 // temp = temp->dex_cache_resolved_methods_;
937 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
938 // temp = temp[index_in_cache]
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100939 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetIndexInDexCache())));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100940 // (temp + offset_of_quick_compiled_code)()
941 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
942
943 DCHECK(!codegen_->IsLeafMethod());
944 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
945}
946
947void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
948 HandleInvoke(invoke);
949}
950
951void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100952 LocationSummary* locations =
953 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100954 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100955
956 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100957 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100958 HInstruction* input = invoke->InputAt(i);
959 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
960 }
961
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100962 switch (invoke->GetType()) {
963 case Primitive::kPrimBoolean:
964 case Primitive::kPrimByte:
965 case Primitive::kPrimChar:
966 case Primitive::kPrimShort:
967 case Primitive::kPrimInt:
968 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100969 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100970 break;
971
972 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100973 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100974 break;
975
976 case Primitive::kPrimVoid:
977 break;
978
979 case Primitive::kPrimDouble:
980 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100981 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100982 break;
983 }
984
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000985 invoke->SetLocations(locations);
986}
987
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100988void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100989 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100990 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
991 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
992 LocationSummary* locations = invoke->GetLocations();
993 Location receiver = locations->InAt(0);
994 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
995 // temp = object->GetClass();
996 if (receiver.IsStackSlot()) {
997 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
998 __ movl(temp, Address(temp, class_offset));
999 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001000 __ movl(temp, Address(receiver.As<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001001 }
1002 // temp = temp->GetMethodAt(method_offset);
1003 __ movl(temp, Address(temp, method_offset));
1004 // call temp->GetEntryPoint();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001005 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
1006
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001007 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001008 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001009}
1010
Roland Levillain88cb1752014-10-20 16:36:47 +01001011void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1012 LocationSummary* locations =
1013 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1014 switch (neg->GetResultType()) {
1015 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001016 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001017 locations->SetInAt(0, Location::RequiresRegister());
1018 locations->SetOut(Location::SameAsFirstInput());
1019 break;
1020
Roland Levillain88cb1752014-10-20 16:36:47 +01001021 case Primitive::kPrimFloat:
1022 case Primitive::kPrimDouble:
1023 LOG(FATAL) << "Not yet implemented neg type " << neg->GetResultType();
1024 break;
1025
1026 default:
1027 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1028 }
1029}
1030
1031void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1032 LocationSummary* locations = neg->GetLocations();
1033 Location out = locations->Out();
1034 Location in = locations->InAt(0);
1035 switch (neg->GetResultType()) {
1036 case Primitive::kPrimInt:
1037 DCHECK(in.IsRegister());
1038 __ negl(out.As<Register>());
1039 break;
1040
1041 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001042 DCHECK(in.IsRegisterPair());
1043 __ negl(out.AsRegisterPairLow<Register>());
1044 // Negation is similar to subtraction from zero. The least
1045 // significant byte triggers a borrow when it is different from
1046 // zero; to take it into account, add 1 to the most significant
1047 // byte if the carry flag (CF) is set to 1 after the first NEGL
1048 // operation.
1049 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1050 __ negl(out.AsRegisterPairHigh<Register>());
1051 break;
1052
Roland Levillain88cb1752014-10-20 16:36:47 +01001053 case Primitive::kPrimFloat:
1054 case Primitive::kPrimDouble:
1055 LOG(FATAL) << "Not yet implemented neg type " << neg->GetResultType();
1056 break;
1057
1058 default:
1059 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1060 }
1061}
1062
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001063void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001064 LocationSummary* locations =
1065 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001066 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001067 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001068 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001069 locations->SetInAt(0, Location::RequiresRegister());
1070 locations->SetInAt(1, Location::Any());
1071 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001072 break;
1073 }
1074
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001075 case Primitive::kPrimFloat:
1076 case Primitive::kPrimDouble: {
1077 locations->SetInAt(0, Location::RequiresFpuRegister());
1078 locations->SetInAt(1, Location::Any());
1079 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001080 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001081 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001082
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001083 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001084 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1085 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001086 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001087}
1088
1089void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1090 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001091 Location first = locations->InAt(0);
1092 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001093 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001094 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001095 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001096 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001097 __ addl(first.As<Register>(), second.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001098 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001099 __ addl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001100 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001101 __ addl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001102 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001103 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001104 }
1105
1106 case Primitive::kPrimLong: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001107 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001108 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1109 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001110 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001111 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1112 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001113 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001114 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001115 break;
1116 }
1117
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001118 case Primitive::kPrimFloat: {
1119 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001120 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001121 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001122 __ addss(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001123 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001124 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001125 }
1126
1127 case Primitive::kPrimDouble: {
1128 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001129 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001130 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001131 __ addsd(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001132 }
1133 break;
1134 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001135
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001136 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001137 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001138 }
1139}
1140
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001141void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001142 LocationSummary* locations =
1143 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001144 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001145 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001146 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001147 locations->SetInAt(0, Location::RequiresRegister());
1148 locations->SetInAt(1, Location::Any());
1149 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001150 break;
1151 }
Calin Juravle11351682014-10-23 15:38:15 +01001152 case Primitive::kPrimFloat:
1153 case Primitive::kPrimDouble: {
1154 locations->SetInAt(0, Location::RequiresFpuRegister());
1155 locations->SetInAt(1, Location::RequiresFpuRegister());
1156 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001157 break;
Calin Juravle11351682014-10-23 15:38:15 +01001158 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001159
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001160 default:
Calin Juravle11351682014-10-23 15:38:15 +01001161 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001162 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001163}
1164
1165void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
1166 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001167 Location first = locations->InAt(0);
1168 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001169 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001170 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001171 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001172 if (second.IsRegister()) {
Calin Juravle11351682014-10-23 15:38:15 +01001173 __ subl(first.As<Register>(), second.As<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001174 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001175 __ subl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001176 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001177 __ subl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001178 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001179 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001180 }
1181
1182 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001183 if (second.IsRegister()) {
Calin Juravle11351682014-10-23 15:38:15 +01001184 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1185 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001186 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001187 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001188 __ sbbl(first.AsRegisterPairHigh<Register>(),
1189 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001190 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001191 break;
1192 }
1193
Calin Juravle11351682014-10-23 15:38:15 +01001194 case Primitive::kPrimFloat: {
1195 __ subss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001196 break;
Calin Juravle11351682014-10-23 15:38:15 +01001197 }
1198
1199 case Primitive::kPrimDouble: {
1200 __ subsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1201 break;
1202 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001203
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001204 default:
Calin Juravle11351682014-10-23 15:38:15 +01001205 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001206 }
1207}
1208
Calin Juravle34bacdf2014-10-07 20:23:36 +01001209void LocationsBuilderX86::VisitMul(HMul* mul) {
1210 LocationSummary* locations =
1211 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1212 switch (mul->GetResultType()) {
1213 case Primitive::kPrimInt:
1214 locations->SetInAt(0, Location::RequiresRegister());
1215 locations->SetInAt(1, Location::Any());
1216 locations->SetOut(Location::SameAsFirstInput());
1217 break;
1218 case Primitive::kPrimLong: {
1219 locations->SetInAt(0, Location::RequiresRegister());
1220 // TODO: Currently this handles only stack operands:
1221 // - we don't have enough registers because we currently use Quick ABI.
1222 // - by the time we have a working register allocator we will probably change the ABI
1223 // and fix the above.
1224 // - we don't have a way yet to request operands on stack but the base line compiler
1225 // will leave the operands on the stack with Any().
1226 locations->SetInAt(1, Location::Any());
1227 locations->SetOut(Location::SameAsFirstInput());
1228 // Needed for imul on 32bits with 64bits output.
1229 locations->AddTemp(Location::RegisterLocation(EAX));
1230 locations->AddTemp(Location::RegisterLocation(EDX));
1231 break;
1232 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001233 case Primitive::kPrimFloat:
1234 case Primitive::kPrimDouble: {
1235 locations->SetInAt(0, Location::RequiresFpuRegister());
1236 locations->SetInAt(1, Location::RequiresFpuRegister());
1237 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001238 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001239 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001240
1241 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001242 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001243 }
1244}
1245
1246void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
1247 LocationSummary* locations = mul->GetLocations();
1248 Location first = locations->InAt(0);
1249 Location second = locations->InAt(1);
1250 DCHECK(first.Equals(locations->Out()));
1251
1252 switch (mul->GetResultType()) {
1253 case Primitive::kPrimInt: {
1254 if (second.IsRegister()) {
1255 __ imull(first.As<Register>(), second.As<Register>());
1256 } else if (second.IsConstant()) {
1257 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1258 __ imull(first.As<Register>(), imm);
1259 } else {
1260 DCHECK(second.IsStackSlot());
1261 __ imull(first.As<Register>(), Address(ESP, second.GetStackIndex()));
1262 }
1263 break;
1264 }
1265
1266 case Primitive::kPrimLong: {
1267 DCHECK(second.IsDoubleStackSlot());
1268
1269 Register in1_hi = first.AsRegisterPairHigh<Register>();
1270 Register in1_lo = first.AsRegisterPairLow<Register>();
1271 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
1272 Address in2_lo(ESP, second.GetStackIndex());
1273 Register eax = locations->GetTemp(0).As<Register>();
1274 Register edx = locations->GetTemp(1).As<Register>();
1275
1276 DCHECK_EQ(EAX, eax);
1277 DCHECK_EQ(EDX, edx);
1278
1279 // input: in1 - 64 bits, in2 - 64 bits
1280 // output: in1
1281 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
1282 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
1283 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
1284
1285 __ movl(eax, in2_hi);
1286 // eax <- in1.lo * in2.hi
1287 __ imull(eax, in1_lo);
1288 // in1.hi <- in1.hi * in2.lo
1289 __ imull(in1_hi, in2_lo);
1290 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
1291 __ addl(in1_hi, eax);
1292 // move in1_lo to eax to prepare for double precision
1293 __ movl(eax, in1_lo);
1294 // edx:eax <- in1.lo * in2.lo
1295 __ mull(in2_lo);
1296 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
1297 __ addl(in1_hi, edx);
1298 // in1.lo <- (in1.lo * in2.lo)[31:0];
1299 __ movl(in1_lo, eax);
1300
1301 break;
1302 }
1303
Calin Juravleb5bfa962014-10-21 18:02:24 +01001304 case Primitive::kPrimFloat: {
1305 __ mulss(first.As<XmmRegister>(), second.As<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001306 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001307 }
1308
1309 case Primitive::kPrimDouble: {
1310 __ mulsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1311 break;
1312 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001313
1314 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001315 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001316 }
1317}
1318
Calin Juravle7c4954d2014-10-28 16:57:40 +00001319void LocationsBuilderX86::VisitDiv(HDiv* div) {
1320 LocationSummary* locations =
1321 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1322 switch (div->GetResultType()) {
1323 case Primitive::kPrimInt:
1324 case Primitive::kPrimLong: {
1325 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1326 break;
1327 }
1328 case Primitive::kPrimFloat:
1329 case Primitive::kPrimDouble: {
1330 locations->SetInAt(0, Location::RequiresFpuRegister());
1331 locations->SetInAt(1, Location::RequiresFpuRegister());
1332 locations->SetOut(Location::SameAsFirstInput());
1333 break;
1334 }
1335
1336 default:
1337 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1338 }
1339}
1340
1341void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
1342 LocationSummary* locations = div->GetLocations();
1343 Location first = locations->InAt(0);
1344 Location second = locations->InAt(1);
1345 DCHECK(first.Equals(locations->Out()));
1346
1347 switch (div->GetResultType()) {
1348 case Primitive::kPrimInt:
1349 case Primitive::kPrimLong: {
1350 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1351 break;
1352 }
1353
1354 case Primitive::kPrimFloat: {
1355 __ divss(first.As<XmmRegister>(), second.As<XmmRegister>());
1356 break;
1357 }
1358
1359 case Primitive::kPrimDouble: {
1360 __ divsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1361 break;
1362 }
1363
1364 default:
1365 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1366 }
1367}
1368
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001369void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001370 LocationSummary* locations =
1371 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001372 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001373 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001374 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1375 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001376}
1377
1378void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
1379 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001380 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001381 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001382
Nicolas Geoffray707c8092014-04-04 10:50:14 +01001383 __ fs()->call(
1384 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001385
Nicolas Geoffray39468442014-09-02 15:17:15 +01001386 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001387 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001388}
1389
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001390void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
1391 LocationSummary* locations =
1392 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
1393 locations->SetOut(Location::RegisterLocation(EAX));
1394 InvokeRuntimeCallingConvention calling_convention;
1395 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1396 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1397 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1398}
1399
1400void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
1401 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001402 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001403 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
1404
1405 __ fs()->call(
1406 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocArrayWithAccessCheck)));
1407
1408 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
1409 DCHECK(!codegen_->IsLeafMethod());
1410}
1411
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001412void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001413 LocationSummary* locations =
1414 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001415 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1416 if (location.IsStackSlot()) {
1417 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1418 } else if (location.IsDoubleStackSlot()) {
1419 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001420 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001421 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001422}
1423
1424void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001425}
1426
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001427void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001428 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001429 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001430 locations->SetInAt(0, Location::RequiresRegister());
1431 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001432}
1433
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001434void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
1435 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01001436 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001437 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01001438 DCHECK(in.Equals(out));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001439 switch (not_->InputAt(0)->GetType()) {
1440 case Primitive::kPrimBoolean:
1441 __ xorl(out.As<Register>(), Immediate(1));
1442 break;
1443
1444 case Primitive::kPrimInt:
1445 __ notl(out.As<Register>());
1446 break;
1447
1448 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01001449 __ notl(out.AsRegisterPairLow<Register>());
1450 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001451 break;
1452
1453 default:
1454 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
1455 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001456}
1457
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001458void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001459 LocationSummary* locations =
1460 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001461 locations->SetInAt(0, Location::RequiresRegister());
1462 locations->SetInAt(1, Location::Any());
1463 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001464}
1465
1466void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
1467 Label greater, done;
1468 LocationSummary* locations = compare->GetLocations();
1469 switch (compare->InputAt(0)->GetType()) {
1470 case Primitive::kPrimLong: {
1471 Label less, greater, done;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001472 Register output = locations->Out().As<Register>();
1473 Location left = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001474 Location right = locations->InAt(1);
1475 if (right.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001476 __ cmpl(left.AsRegisterPairHigh<Register>(), right.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001477 } else {
1478 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001479 __ cmpl(left.AsRegisterPairHigh<Register>(),
1480 Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001481 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001482 __ j(kLess, &less); // Signed compare.
1483 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001484 if (right.IsRegisterPair()) {
1485 __ cmpl(left.AsRegisterPairLow<Register>(), right.AsRegisterPairLow<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001486 } else {
1487 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001488 __ cmpl(left.AsRegisterPairLow<Register>(), Address(ESP, right.GetStackIndex()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001489 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001490 __ movl(output, Immediate(0));
1491 __ j(kEqual, &done);
1492 __ j(kBelow, &less); // Unsigned compare.
1493
1494 __ Bind(&greater);
1495 __ movl(output, Immediate(1));
1496 __ jmp(&done);
1497
1498 __ Bind(&less);
1499 __ movl(output, Immediate(-1));
1500
1501 __ Bind(&done);
1502 break;
1503 }
1504 default:
1505 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
1506 }
1507}
1508
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001509void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001510 LocationSummary* locations =
1511 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001512 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1513 locations->SetInAt(i, Location::Any());
1514 }
1515 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001516}
1517
1518void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001519 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001520}
1521
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001522void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001523 LocationSummary* locations =
1524 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001525 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001526 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001527 bool is_object_type = field_type == Primitive::kPrimNot;
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001528 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
1529 || (field_type == Primitive::kPrimByte);
1530 // The register allocator does not support multiple
1531 // inputs that die at entry with one in a specific register.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001532 if (is_byte_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001533 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001534 locations->SetInAt(1, Location::RegisterLocation(EAX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001535 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001536 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001537 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001538 // Temporary registers for the write barrier.
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001539 if (is_object_type) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001540 locations->AddTemp(Location::RequiresRegister());
1541 // Ensure the card is in a byte register.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001542 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001543 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001544}
1545
1546void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1547 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001548 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001549 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001550 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001551
1552 switch (field_type) {
1553 case Primitive::kPrimBoolean:
1554 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001555 ByteRegister value = locations->InAt(1).As<ByteRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001556 __ movb(Address(obj, offset), value);
1557 break;
1558 }
1559
1560 case Primitive::kPrimShort:
1561 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001562 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001563 __ movw(Address(obj, offset), value);
1564 break;
1565 }
1566
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001567 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001568 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001569 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001570 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001571
1572 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001573 Register temp = locations->GetTemp(0).As<Register>();
1574 Register card = locations->GetTemp(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001575 codegen_->MarkGCCard(temp, card, obj, value);
1576 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001577 break;
1578 }
1579
1580 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001581 Location value = locations->InAt(1);
1582 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
1583 __ movl(Address(obj, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001584 break;
1585 }
1586
1587 case Primitive::kPrimFloat:
1588 case Primitive::kPrimDouble:
1589 LOG(FATAL) << "Unimplemented register type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001590 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001591 case Primitive::kPrimVoid:
1592 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001593 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001594 }
1595}
1596
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001597void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
1598 Label is_null;
1599 __ testl(value, value);
1600 __ j(kEqual, &is_null);
1601 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
1602 __ movl(temp, object);
1603 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
1604 __ movb(Address(temp, card, TIMES_1, 0),
1605 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
1606 __ Bind(&is_null);
1607}
1608
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001609void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001610 LocationSummary* locations =
1611 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001612 locations->SetInAt(0, Location::RequiresRegister());
1613 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001614}
1615
1616void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1617 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001618 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001619 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
1620
1621 switch (instruction->GetType()) {
1622 case Primitive::kPrimBoolean: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001623 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001624 __ movzxb(out, Address(obj, offset));
1625 break;
1626 }
1627
1628 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001629 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001630 __ movsxb(out, Address(obj, offset));
1631 break;
1632 }
1633
1634 case Primitive::kPrimShort: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001635 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001636 __ movsxw(out, Address(obj, offset));
1637 break;
1638 }
1639
1640 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001641 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001642 __ movzxw(out, Address(obj, offset));
1643 break;
1644 }
1645
1646 case Primitive::kPrimInt:
1647 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001648 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001649 __ movl(out, Address(obj, offset));
1650 break;
1651 }
1652
1653 case Primitive::kPrimLong: {
1654 // TODO: support volatile.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001655 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(obj, offset));
1656 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(obj, kX86WordSize + offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001657 break;
1658 }
1659
1660 case Primitive::kPrimFloat:
1661 case Primitive::kPrimDouble:
1662 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001663 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001664 case Primitive::kPrimVoid:
1665 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001666 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001667 }
1668}
1669
1670void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001671 LocationSummary* locations =
1672 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001673 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001674 if (instruction->HasUses()) {
1675 locations->SetOut(Location::SameAsFirstInput());
1676 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001677}
1678
1679void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001680 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001681 codegen_->AddSlowPath(slow_path);
1682
1683 LocationSummary* locations = instruction->GetLocations();
1684 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001685
1686 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001687 __ cmpl(obj.As<Register>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001688 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001689 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001690 } else {
1691 DCHECK(obj.IsConstant()) << obj;
1692 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
1693 __ jmp(slow_path->GetEntryLabel());
1694 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001695 }
1696 __ j(kEqual, slow_path->GetEntryLabel());
1697}
1698
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001699void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001700 LocationSummary* locations =
1701 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001702 locations->SetInAt(0, Location::RequiresRegister());
1703 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1704 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001705}
1706
1707void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
1708 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001709 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001710 Location index = locations->InAt(1);
1711
1712 switch (instruction->GetType()) {
1713 case Primitive::kPrimBoolean: {
1714 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001715 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001716 if (index.IsConstant()) {
1717 __ movzxb(out, Address(obj,
1718 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1719 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001720 __ movzxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001721 }
1722 break;
1723 }
1724
1725 case Primitive::kPrimByte: {
1726 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001727 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001728 if (index.IsConstant()) {
1729 __ movsxb(out, Address(obj,
1730 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1731 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001732 __ movsxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001733 }
1734 break;
1735 }
1736
1737 case Primitive::kPrimShort: {
1738 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001739 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001740 if (index.IsConstant()) {
1741 __ movsxw(out, Address(obj,
1742 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1743 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001744 __ movsxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001745 }
1746 break;
1747 }
1748
1749 case Primitive::kPrimChar: {
1750 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001751 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001752 if (index.IsConstant()) {
1753 __ movzxw(out, Address(obj,
1754 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1755 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001756 __ movzxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001757 }
1758 break;
1759 }
1760
1761 case Primitive::kPrimInt:
1762 case Primitive::kPrimNot: {
1763 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001764 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001765 if (index.IsConstant()) {
1766 __ movl(out, Address(obj,
1767 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
1768 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001769 __ movl(out, Address(obj, index.As<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001770 }
1771 break;
1772 }
1773
1774 case Primitive::kPrimLong: {
1775 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001776 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001777 if (index.IsConstant()) {
1778 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001779 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
1780 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001781 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001782 __ movl(out.AsRegisterPairLow<Register>(),
1783 Address(obj, index.As<Register>(), TIMES_8, data_offset));
1784 __ movl(out.AsRegisterPairHigh<Register>(),
1785 Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001786 }
1787 break;
1788 }
1789
1790 case Primitive::kPrimFloat:
1791 case Primitive::kPrimDouble:
1792 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001793 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001794 case Primitive::kPrimVoid:
1795 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001796 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001797 }
1798}
1799
1800void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001801 Primitive::Type value_type = instruction->GetComponentType();
1802 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1803 instruction,
1804 value_type == Primitive::kPrimNot ? LocationSummary::kCall : LocationSummary::kNoCall);
1805
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001806 if (value_type == Primitive::kPrimNot) {
1807 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001808 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1809 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1810 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001811 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001812 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
1813 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001814 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001815 // In case of a byte operation, the register allocator does not support multiple
1816 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001817 locations->SetInAt(0, Location::RequiresRegister());
1818 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001819 if (is_byte_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001820 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001821 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001822 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001823 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001824 }
1825 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001826}
1827
1828void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
1829 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001830 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001831 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001832 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001833 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001834
1835 switch (value_type) {
1836 case Primitive::kPrimBoolean:
1837 case Primitive::kPrimByte: {
1838 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001839 if (index.IsConstant()) {
1840 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001841 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001842 __ movb(Address(obj, offset), value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001843 } else {
1844 __ movb(Address(obj, offset),
1845 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1846 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001847 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001848 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001849 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
1850 value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001851 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001852 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001853 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1854 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001855 }
1856 break;
1857 }
1858
1859 case Primitive::kPrimShort:
1860 case Primitive::kPrimChar: {
1861 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001862 if (index.IsConstant()) {
1863 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001864 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001865 __ movw(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001866 } else {
1867 __ movw(Address(obj, offset),
1868 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1869 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001870 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001871 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001872 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
1873 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001874 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001875 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001876 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1877 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001878 }
1879 break;
1880 }
1881
1882 case Primitive::kPrimInt: {
1883 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001884 if (index.IsConstant()) {
1885 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001886 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001887 __ movl(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001888 } else {
1889 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1890 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001891 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001892 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001893 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
1894 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001895 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001896 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001897 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1898 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001899 }
1900 break;
1901 }
1902
1903 case Primitive::kPrimNot: {
1904 DCHECK(!codegen_->IsLeafMethod());
1905 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001906 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001907 break;
1908 }
1909
1910 case Primitive::kPrimLong: {
1911 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001912 if (index.IsConstant()) {
1913 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001914 if (value.IsRegisterPair()) {
1915 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
1916 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001917 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001918 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001919 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
1920 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
1921 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
1922 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001923 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001924 if (value.IsRegisterPair()) {
1925 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
1926 value.AsRegisterPairLow<Register>());
1927 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
1928 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001929 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001930 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001931 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001932 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001933 Immediate(Low32Bits(val)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001934 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001935 Immediate(High32Bits(val)));
1936 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001937 }
1938 break;
1939 }
1940
1941 case Primitive::kPrimFloat:
1942 case Primitive::kPrimDouble:
1943 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001944 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001945 case Primitive::kPrimVoid:
1946 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001947 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001948 }
1949}
1950
1951void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
1952 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001953 locations->SetInAt(0, Location::RequiresRegister());
1954 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001955 instruction->SetLocations(locations);
1956}
1957
1958void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
1959 LocationSummary* locations = instruction->GetLocations();
1960 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001961 Register obj = locations->InAt(0).As<Register>();
1962 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001963 __ movl(out, Address(obj, offset));
1964}
1965
1966void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001967 LocationSummary* locations =
1968 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001969 locations->SetInAt(0, Location::RequiresRegister());
1970 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001971 if (instruction->HasUses()) {
1972 locations->SetOut(Location::SameAsFirstInput());
1973 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001974}
1975
1976void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
1977 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001978 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01001979 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001980 codegen_->AddSlowPath(slow_path);
1981
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001982 Register index = locations->InAt(0).As<Register>();
1983 Register length = locations->InAt(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001984
1985 __ cmpl(index, length);
1986 __ j(kAboveEqual, slow_path->GetEntryLabel());
1987}
1988
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001989void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
1990 temp->SetLocations(nullptr);
1991}
1992
1993void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
1994 // Nothing to do, this is driven by the code generator.
1995}
1996
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001997void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001998 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001999}
2000
2001void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002002 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2003}
2004
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002005void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
2006 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2007}
2008
2009void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002010 HBasicBlock* block = instruction->GetBlock();
2011 if (block->GetLoopInformation() != nullptr) {
2012 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2013 // The back edge will generate the suspend check.
2014 return;
2015 }
2016 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2017 // The goto will generate the suspend check.
2018 return;
2019 }
2020 GenerateSuspendCheck(instruction, nullptr);
2021}
2022
2023void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
2024 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002025 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002026 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002027 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002028 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002029 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002030 if (successor == nullptr) {
2031 __ j(kNotEqual, slow_path->GetEntryLabel());
2032 __ Bind(slow_path->GetReturnLabel());
2033 } else {
2034 __ j(kEqual, codegen_->GetLabelOf(successor));
2035 __ jmp(slow_path->GetEntryLabel());
2036 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002037}
2038
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002039X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
2040 return codegen_->GetAssembler();
2041}
2042
2043void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
2044 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002045 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002046 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
2047 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
2048 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
2049}
2050
2051void ParallelMoveResolverX86::EmitMove(size_t index) {
2052 MoveOperands* move = moves_.Get(index);
2053 Location source = move->GetSource();
2054 Location destination = move->GetDestination();
2055
2056 if (source.IsRegister()) {
2057 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002058 __ movl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002059 } else {
2060 DCHECK(destination.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002061 __ movl(Address(ESP, destination.GetStackIndex()), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002062 }
2063 } else if (source.IsStackSlot()) {
2064 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002065 __ movl(destination.As<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002066 } else {
2067 DCHECK(destination.IsStackSlot());
2068 MoveMemoryToMemory(destination.GetStackIndex(),
2069 source.GetStackIndex());
2070 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002071 } else if (source.IsConstant()) {
2072 HIntConstant* instruction = source.GetConstant()->AsIntConstant();
2073 Immediate imm(instruction->AsIntConstant()->GetValue());
2074 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002075 __ movl(destination.As<Register>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002076 } else {
2077 __ movl(Address(ESP, destination.GetStackIndex()), imm);
2078 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002079 } else {
2080 LOG(FATAL) << "Unimplemented";
2081 }
2082}
2083
2084void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002085 Register suggested_scratch = reg == EAX ? EBX : EAX;
2086 ScratchRegisterScope ensure_scratch(
2087 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
2088
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002089 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
2090 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
2091 __ movl(Address(ESP, mem + stack_offset), reg);
2092 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
2093}
2094
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002095void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
2096 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002097 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
2098
2099 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002100 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002101 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
2102
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002103 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
2104 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
2105 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
2106 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
2107 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
2108 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
2109}
2110
2111void ParallelMoveResolverX86::EmitSwap(size_t index) {
2112 MoveOperands* move = moves_.Get(index);
2113 Location source = move->GetSource();
2114 Location destination = move->GetDestination();
2115
2116 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002117 __ xchgl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002118 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002119 Exchange(source.As<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002120 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002121 Exchange(destination.As<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002122 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
2123 Exchange(destination.GetStackIndex(), source.GetStackIndex());
2124 } else {
2125 LOG(FATAL) << "Unimplemented";
2126 }
2127}
2128
2129void ParallelMoveResolverX86::SpillScratch(int reg) {
2130 __ pushl(static_cast<Register>(reg));
2131}
2132
2133void ParallelMoveResolverX86::RestoreScratch(int reg) {
2134 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002135}
2136
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002137void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
2138 LocationSummary* locations =
2139 new (GetGraph()->GetArena()) LocationSummary(cls, LocationSummary::kNoCall);
2140 locations->SetOut(Location::RequiresRegister());
2141}
2142
2143void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
2144 Register out = cls->GetLocations()->Out().As<Register>();
2145 if (cls->IsReferrersClass()) {
2146 codegen_->LoadCurrentMethod(out);
2147 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
2148 } else {
2149 codegen_->LoadCurrentMethod(out);
2150 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
2151 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
2152 }
2153}
2154
2155void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
2156 LocationSummary* locations =
2157 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2158 locations->SetInAt(0, Location::RequiresRegister());
2159 if (check->HasUses()) {
2160 locations->SetOut(Location::SameAsFirstInput());
2161 }
2162}
2163
2164void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
2165 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) ClinitCheckSlowPathX86(check);
2166 codegen_->AddSlowPath(slow_path);
2167
2168 LocationSummary* locations = check->GetLocations();
2169 // We remove the class as a live register, we know it's null or unused in the slow path.
2170 RegisterSet* register_set = locations->GetLiveRegisters();
2171 register_set->Remove(locations->InAt(0));
2172
2173 Register class_reg = locations->InAt(0).As<Register>();
2174 __ testl(class_reg, class_reg);
2175 __ j(kEqual, slow_path->GetEntryLabel());
2176 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
2177 Immediate(mirror::Class::kStatusInitialized));
2178 __ j(kLess, slow_path->GetEntryLabel());
2179 __ Bind(slow_path->GetExitLabel());
2180 // No need for memory fence, thanks to the X86 memory model.
2181}
2182
2183void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2184 LocationSummary* locations =
2185 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2186 locations->SetInAt(0, Location::RequiresRegister());
2187 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2188}
2189
2190void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2191 LocationSummary* locations = instruction->GetLocations();
2192 Register cls = locations->InAt(0).As<Register>();
2193 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2194
2195 switch (instruction->GetType()) {
2196 case Primitive::kPrimBoolean: {
2197 Register out = locations->Out().As<Register>();
2198 __ movzxb(out, Address(cls, offset));
2199 break;
2200 }
2201
2202 case Primitive::kPrimByte: {
2203 Register out = locations->Out().As<Register>();
2204 __ movsxb(out, Address(cls, offset));
2205 break;
2206 }
2207
2208 case Primitive::kPrimShort: {
2209 Register out = locations->Out().As<Register>();
2210 __ movsxw(out, Address(cls, offset));
2211 break;
2212 }
2213
2214 case Primitive::kPrimChar: {
2215 Register out = locations->Out().As<Register>();
2216 __ movzxw(out, Address(cls, offset));
2217 break;
2218 }
2219
2220 case Primitive::kPrimInt:
2221 case Primitive::kPrimNot: {
2222 Register out = locations->Out().As<Register>();
2223 __ movl(out, Address(cls, offset));
2224 break;
2225 }
2226
2227 case Primitive::kPrimLong: {
2228 // TODO: support volatile.
2229 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(cls, offset));
2230 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(cls, kX86WordSize + offset));
2231 break;
2232 }
2233
2234 case Primitive::kPrimFloat:
2235 case Primitive::kPrimDouble:
2236 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
2237 UNREACHABLE();
2238 case Primitive::kPrimVoid:
2239 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2240 UNREACHABLE();
2241 }
2242}
2243
2244void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2245 LocationSummary* locations =
2246 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2247 locations->SetInAt(0, Location::RequiresRegister());
2248 Primitive::Type field_type = instruction->GetFieldType();
2249 bool is_object_type = field_type == Primitive::kPrimNot;
2250 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
2251 || (field_type == Primitive::kPrimByte);
2252 // The register allocator does not support multiple
2253 // inputs that die at entry with one in a specific register.
2254 if (is_byte_type) {
2255 // Ensure the value is in a byte register.
2256 locations->SetInAt(1, Location::RegisterLocation(EAX));
2257 } else {
2258 locations->SetInAt(1, Location::RequiresRegister());
2259 }
2260 // Temporary registers for the write barrier.
2261 if (is_object_type) {
2262 locations->AddTemp(Location::RequiresRegister());
2263 // Ensure the card is in a byte register.
2264 locations->AddTemp(Location::RegisterLocation(ECX));
2265 }
2266}
2267
2268void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2269 LocationSummary* locations = instruction->GetLocations();
2270 Register cls = locations->InAt(0).As<Register>();
2271 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2272 Primitive::Type field_type = instruction->GetFieldType();
2273
2274 switch (field_type) {
2275 case Primitive::kPrimBoolean:
2276 case Primitive::kPrimByte: {
2277 ByteRegister value = locations->InAt(1).As<ByteRegister>();
2278 __ movb(Address(cls, offset), value);
2279 break;
2280 }
2281
2282 case Primitive::kPrimShort:
2283 case Primitive::kPrimChar: {
2284 Register value = locations->InAt(1).As<Register>();
2285 __ movw(Address(cls, offset), value);
2286 break;
2287 }
2288
2289 case Primitive::kPrimInt:
2290 case Primitive::kPrimNot: {
2291 Register value = locations->InAt(1).As<Register>();
2292 __ movl(Address(cls, offset), value);
2293
2294 if (field_type == Primitive::kPrimNot) {
2295 Register temp = locations->GetTemp(0).As<Register>();
2296 Register card = locations->GetTemp(1).As<Register>();
2297 codegen_->MarkGCCard(temp, card, cls, value);
2298 }
2299 break;
2300 }
2301
2302 case Primitive::kPrimLong: {
2303 Location value = locations->InAt(1);
2304 __ movl(Address(cls, offset), value.AsRegisterPairLow<Register>());
2305 __ movl(Address(cls, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
2306 break;
2307 }
2308
2309 case Primitive::kPrimFloat:
2310 case Primitive::kPrimDouble:
2311 LOG(FATAL) << "Unimplemented register type " << field_type;
2312 UNREACHABLE();
2313 case Primitive::kPrimVoid:
2314 LOG(FATAL) << "Unreachable type " << field_type;
2315 UNREACHABLE();
2316 }
2317}
2318
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002319} // namespace x86
2320} // namespace art