blob: d41d5a00a8e29ded9ecd93d6fcf53717feacedb1 [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 Geoffrayb5f62b32014-10-30 10:58:41 +0000186class LoadStringSlowPathX86 : public SlowPathCodeX86 {
187 public:
188 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
189
190 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
191 LocationSummary* locations = instruction_->GetLocations();
192 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
193
194 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
195 __ Bind(GetEntryLabel());
196 codegen->SaveLiveRegisters(locations);
197
198 InvokeRuntimeCallingConvention calling_convention;
199 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(0));
200 __ movl(calling_convention.GetRegisterAt(1), Immediate(instruction_->GetStringIndex()));
201 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pResolveString)));
202 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
203 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
204 codegen->RestoreLiveRegisters(locations);
205
206 __ jmp(GetExitLabel());
207 }
208
209 private:
210 HLoadString* const instruction_;
211
212 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
213};
214
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100215#undef __
216#define __ reinterpret_cast<X86Assembler*>(GetAssembler())->
217
Dave Allison20dfc792014-06-16 20:44:29 -0700218inline Condition X86Condition(IfCondition cond) {
219 switch (cond) {
220 case kCondEQ: return kEqual;
221 case kCondNE: return kNotEqual;
222 case kCondLT: return kLess;
223 case kCondLE: return kLessEqual;
224 case kCondGT: return kGreater;
225 case kCondGE: return kGreaterEqual;
226 default:
227 LOG(FATAL) << "Unknown if condition";
228 }
229 return kEqual;
230}
231
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100232void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
233 stream << X86ManagedRegister::FromCpuRegister(Register(reg));
234}
235
236void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
237 stream << X86ManagedRegister::FromXmmRegister(XmmRegister(reg));
238}
239
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100240size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
241 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
242 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100243}
244
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100245size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
246 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
247 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100248}
249
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100250CodeGeneratorX86::CodeGeneratorX86(HGraph* graph)
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100251 : CodeGenerator(graph, kNumberOfCpuRegisters, kNumberOfXmmRegisters, kNumberOfRegisterPairs),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100252 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100253 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100254 instruction_visitor_(graph, this),
255 move_resolver_(graph->GetArena(), this) {}
256
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100257size_t CodeGeneratorX86::FrameEntrySpillSize() const {
258 return kNumberOfPushedRegistersAtEntry * kX86WordSize;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100259}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100260
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100261Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100262 switch (type) {
263 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100264 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100265 X86ManagedRegister pair =
266 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100267 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
268 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100269 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
270 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100271 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100272 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100273 }
274
275 case Primitive::kPrimByte:
276 case Primitive::kPrimBoolean:
277 case Primitive::kPrimChar:
278 case Primitive::kPrimShort:
279 case Primitive::kPrimInt:
280 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100281 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100282 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100283 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100284 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
285 X86ManagedRegister current =
286 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
287 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100288 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100289 }
290 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100291 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100292 }
293
294 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100295 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100296 return Location::FpuRegisterLocation(
297 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100298 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100299
300 case Primitive::kPrimVoid:
301 LOG(FATAL) << "Unreachable type " << type;
302 }
303
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100304 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100305}
306
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100307void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100308 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100309 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100310
311 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100312 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100313
314 // TODO: We currently don't use Quick's callee saved registers.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100315 blocked_core_registers_[EBP] = true;
316 blocked_core_registers_[ESI] = true;
317 blocked_core_registers_[EDI] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100318
319 UpdateBlockedPairRegisters();
320}
321
322void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
323 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
324 X86ManagedRegister current =
325 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
326 if (blocked_core_registers_[current.AsRegisterPairLow()]
327 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
328 blocked_register_pairs_[i] = true;
329 }
330 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100331}
332
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100333InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
334 : HGraphVisitor(graph),
335 assembler_(codegen->GetAssembler()),
336 codegen_(codegen) {}
337
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000338void CodeGeneratorX86::GenerateFrameEntry() {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000339 // Create a fake register to mimic Quick.
340 static const int kFakeReturnRegister = 8;
341 core_spill_mask_ |= (1 << kFakeReturnRegister);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000342
Dave Allison648d7112014-07-25 16:15:27 -0700343 bool skip_overflow_check = IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100344 if (!skip_overflow_check && !kExplicitStackOverflowCheck) {
345 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100346 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100347 }
348
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100349 // The return PC has already been pushed on the stack.
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100350 __ subl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100351
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100352 if (!skip_overflow_check && kExplicitStackOverflowCheck) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100353 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathX86();
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100354 AddSlowPath(slow_path);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100355
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100356 __ fs()->cmpl(ESP, Address::Absolute(Thread::StackEndOffset<kX86WordSize>()));
357 __ j(kLess, slow_path->GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100358 }
359
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100360 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000361}
362
363void CodeGeneratorX86::GenerateFrameExit() {
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100364 __ addl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000365}
366
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100367void CodeGeneratorX86::Bind(HBasicBlock* block) {
368 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000369}
370
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100371void CodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100372 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000373}
374
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100375Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
376 switch (load->GetType()) {
377 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100378 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100379 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
380 break;
381
382 case Primitive::kPrimInt:
383 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100384 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100385 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100386
387 case Primitive::kPrimBoolean:
388 case Primitive::kPrimByte:
389 case Primitive::kPrimChar:
390 case Primitive::kPrimShort:
391 case Primitive::kPrimVoid:
392 LOG(FATAL) << "Unexpected type " << load->GetType();
393 }
394
395 LOG(FATAL) << "Unreachable";
396 return Location();
397}
398
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100399Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
400 switch (type) {
401 case Primitive::kPrimBoolean:
402 case Primitive::kPrimByte:
403 case Primitive::kPrimChar:
404 case Primitive::kPrimShort:
405 case Primitive::kPrimInt:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100406 case Primitive::kPrimFloat:
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100407 case Primitive::kPrimNot: {
408 uint32_t index = gp_index_++;
409 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100410 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100411 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100412 return Location::StackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100413 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100414 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100415
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100416 case Primitive::kPrimLong:
417 case Primitive::kPrimDouble: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100418 uint32_t index = gp_index_;
419 gp_index_ += 2;
420 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100421 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
422 calling_convention.GetRegisterPairAt(index));
423 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100424 } else if (index + 1 == calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000425 // On X86, the register index and stack index of a quick parameter is the same, since
426 // we are passing floating pointer values in core registers.
427 return Location::QuickParameter(index, index);
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100428 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100429 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100430 }
431 }
432
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100433 case Primitive::kPrimVoid:
434 LOG(FATAL) << "Unexpected parameter type " << type;
435 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100436 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100437 return Location();
438}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100439
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100440void CodeGeneratorX86::Move32(Location destination, Location source) {
441 if (source.Equals(destination)) {
442 return;
443 }
444 if (destination.IsRegister()) {
445 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100446 __ movl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100447 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100448 __ movd(destination.As<Register>(), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100449 } else {
450 DCHECK(source.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100451 __ movl(destination.As<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100452 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100453 } else if (destination.IsFpuRegister()) {
454 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100455 __ movd(destination.As<XmmRegister>(), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100456 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100457 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100458 } else {
459 DCHECK(source.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100460 __ movss(destination.As<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100461 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100462 } else {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100463 DCHECK(destination.IsStackSlot());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100464 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100465 __ movl(Address(ESP, destination.GetStackIndex()), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100466 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100467 __ movss(Address(ESP, destination.GetStackIndex()), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100468 } else {
469 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100470 __ pushl(Address(ESP, source.GetStackIndex()));
471 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100472 }
473 }
474}
475
476void CodeGeneratorX86::Move64(Location destination, Location source) {
477 if (source.Equals(destination)) {
478 return;
479 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100480 if (destination.IsRegisterPair()) {
481 if (source.IsRegisterPair()) {
482 __ movl(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
483 __ movl(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100484 } else if (source.IsFpuRegister()) {
485 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100486 } else if (source.IsQuickParameter()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000487 uint16_t register_index = source.GetQuickParameterRegisterIndex();
488 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100489 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100490 __ movl(destination.AsRegisterPairLow<Register>(),
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000491 calling_convention.GetRegisterAt(register_index));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100492 __ movl(destination.AsRegisterPairHigh<Register>(), Address(ESP,
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000493 calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100494 } else {
495 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100496 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
497 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100498 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
499 }
500 } else if (destination.IsQuickParameter()) {
501 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000502 uint16_t register_index = destination.GetQuickParameterRegisterIndex();
503 uint16_t stack_index = destination.GetQuickParameterStackIndex();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100504 if (source.IsRegister()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000505 __ movl(calling_convention.GetRegisterAt(register_index), source.AsRegisterPairLow<Register>());
506 __ movl(Address(ESP, calling_convention.GetStackOffsetOf(stack_index + 1)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100507 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100508 } else if (source.IsFpuRegister()) {
509 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100510 } else {
511 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000512 __ movl(calling_convention.GetRegisterAt(register_index),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100513 Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100514 __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000515 __ popl(Address(ESP, calling_convention.GetStackOffsetOf(stack_index + 1)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100516 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100517 } else if (destination.IsFpuRegister()) {
518 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100519 __ movsd(destination.As<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100520 } else {
521 LOG(FATAL) << "Unimplemented";
522 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100523 } else {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100524 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100525 if (source.IsRegisterPair()) {
526 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100527 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100528 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100529 } else if (source.IsQuickParameter()) {
530 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000531 uint16_t register_index = source.GetQuickParameterRegisterIndex();
532 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100533 __ movl(Address(ESP, destination.GetStackIndex()),
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000534 calling_convention.GetRegisterAt(register_index));
535 DCHECK_EQ(calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100536 static_cast<size_t>(destination.GetHighStackIndex(kX86WordSize)));
537 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100538 __ movsd(Address(ESP, destination.GetStackIndex()), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100539 } else {
540 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100541 __ pushl(Address(ESP, source.GetStackIndex()));
542 __ popl(Address(ESP, destination.GetStackIndex()));
543 __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize)));
544 __ popl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100545 }
546 }
547}
548
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100549void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Roland Levillain476df552014-10-09 17:51:36 +0100550 if (instruction->IsIntConstant()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100551 Immediate imm(instruction->AsIntConstant()->GetValue());
552 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100553 __ movl(location.As<Register>(), imm);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100554 } else if (location.IsStackSlot()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100555 __ movl(Address(ESP, location.GetStackIndex()), imm);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100556 } else {
557 DCHECK(location.IsConstant());
558 DCHECK_EQ(location.GetConstant(), instruction);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100559 }
Roland Levillain476df552014-10-09 17:51:36 +0100560 } else if (instruction->IsLongConstant()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100561 int64_t value = instruction->AsLongConstant()->GetValue();
562 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100563 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
564 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100565 } else if (location.IsDoubleStackSlot()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100566 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
567 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100568 } else {
569 DCHECK(location.IsConstant());
570 DCHECK_EQ(location.GetConstant(), instruction);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100571 }
Roland Levillain476df552014-10-09 17:51:36 +0100572 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100573 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100574 switch (instruction->GetType()) {
575 case Primitive::kPrimBoolean:
576 case Primitive::kPrimByte:
577 case Primitive::kPrimChar:
578 case Primitive::kPrimShort:
579 case Primitive::kPrimInt:
580 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100581 case Primitive::kPrimFloat:
582 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100583 break;
584
585 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100586 case Primitive::kPrimDouble:
587 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100588 break;
589
590 default:
591 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
592 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000593 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100594 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100595 switch (instruction->GetType()) {
596 case Primitive::kPrimBoolean:
597 case Primitive::kPrimByte:
598 case Primitive::kPrimChar:
599 case Primitive::kPrimShort:
600 case Primitive::kPrimInt:
601 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100602 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100603 Move32(location, instruction->GetLocations()->Out());
604 break;
605
606 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100607 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100608 Move64(location, instruction->GetLocations()->Out());
609 break;
610
611 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100612 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100613 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000614 }
615}
616
617void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000618 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000619}
620
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000621void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000622 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100623 DCHECK(!successor->IsExitBlock());
624
625 HBasicBlock* block = got->GetBlock();
626 HInstruction* previous = got->GetPrevious();
627
628 HLoopInformation* info = block->GetLoopInformation();
629 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
630 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
631 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
632 return;
633 }
634
635 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
636 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
637 }
638 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000639 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000640 }
641}
642
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000643void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000644 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000645}
646
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000647void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000648 if (kIsDebugBuild) {
649 __ Comment("Unreachable");
650 __ int3();
651 }
652}
653
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000654void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100655 LocationSummary* locations =
656 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100657 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100658 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100659 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100660 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000661}
662
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000663void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700664 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100665 if (cond->IsIntConstant()) {
666 // Constant condition, statically compared against 1.
667 int32_t cond_value = cond->AsIntConstant()->GetValue();
668 if (cond_value == 1) {
669 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
670 if_instr->IfTrueSuccessor())) {
671 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100672 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100673 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100674 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100675 DCHECK_EQ(cond_value, 0);
676 }
677 } else {
678 bool materialized =
679 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
680 // Moves do not affect the eflags register, so if the condition is
681 // evaluated just before the if, we don't need to evaluate it
682 // again.
683 bool eflags_set = cond->IsCondition()
684 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
685 if (materialized) {
686 if (!eflags_set) {
687 // Materialized condition, compare against 0.
688 Location lhs = if_instr->GetLocations()->InAt(0);
689 if (lhs.IsRegister()) {
690 __ cmpl(lhs.As<Register>(), Immediate(0));
691 } else {
692 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
693 }
694 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
695 } else {
696 __ j(X86Condition(cond->AsCondition()->GetCondition()),
697 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
698 }
699 } else {
700 Location lhs = cond->GetLocations()->InAt(0);
701 Location rhs = cond->GetLocations()->InAt(1);
702 // LHS is guaranteed to be in a register (see
703 // LocationsBuilderX86::VisitCondition).
704 if (rhs.IsRegister()) {
705 __ cmpl(lhs.As<Register>(), rhs.As<Register>());
706 } else if (rhs.IsConstant()) {
707 HIntConstant* instruction = rhs.GetConstant()->AsIntConstant();
708 Immediate imm(instruction->AsIntConstant()->GetValue());
709 __ cmpl(lhs.As<Register>(), imm);
710 } else {
711 __ cmpl(lhs.As<Register>(), Address(ESP, rhs.GetStackIndex()));
712 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100713 __ j(X86Condition(cond->AsCondition()->GetCondition()),
714 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700715 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100716 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100717 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
718 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700719 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000720 }
721}
722
723void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000724 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000725}
726
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000727void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
728 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000729}
730
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000731void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100732 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000733}
734
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000735void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100736 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000737}
738
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100739void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100740 LocationSummary* locations =
741 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100742 switch (store->InputAt(1)->GetType()) {
743 case Primitive::kPrimBoolean:
744 case Primitive::kPrimByte:
745 case Primitive::kPrimChar:
746 case Primitive::kPrimShort:
747 case Primitive::kPrimInt:
748 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100749 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100750 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
751 break;
752
753 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100754 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100755 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
756 break;
757
758 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100759 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100760 }
761 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000762}
763
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000764void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000765}
766
Dave Allison20dfc792014-06-16 20:44:29 -0700767void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100768 LocationSummary* locations =
769 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100770 locations->SetInAt(0, Location::RequiresRegister());
771 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100772 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100773 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100774 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000775}
776
Dave Allison20dfc792014-06-16 20:44:29 -0700777void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
778 if (comp->NeedsMaterialization()) {
779 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100780 Register reg = locations->Out().As<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100781 // Clear register: setcc only sets the low byte.
782 __ xorl(reg, reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700783 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100784 __ cmpl(locations->InAt(0).As<Register>(),
785 locations->InAt(1).As<Register>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100786 } else if (locations->InAt(1).IsConstant()) {
787 HConstant* instruction = locations->InAt(1).GetConstant();
788 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100789 __ cmpl(locations->InAt(0).As<Register>(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700790 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100791 __ cmpl(locations->InAt(0).As<Register>(),
Dave Allison20dfc792014-06-16 20:44:29 -0700792 Address(ESP, locations->InAt(1).GetStackIndex()));
793 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100794 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100795 }
Dave Allison20dfc792014-06-16 20:44:29 -0700796}
797
798void LocationsBuilderX86::VisitEqual(HEqual* comp) {
799 VisitCondition(comp);
800}
801
802void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
803 VisitCondition(comp);
804}
805
806void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
807 VisitCondition(comp);
808}
809
810void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
811 VisitCondition(comp);
812}
813
814void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
815 VisitCondition(comp);
816}
817
818void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
819 VisitCondition(comp);
820}
821
822void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
823 VisitCondition(comp);
824}
825
826void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
827 VisitCondition(comp);
828}
829
830void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
831 VisitCondition(comp);
832}
833
834void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
835 VisitCondition(comp);
836}
837
838void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
839 VisitCondition(comp);
840}
841
842void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
843 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000844}
845
846void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100847 LocationSummary* locations =
848 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100849 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000850}
851
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000852void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100853 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000854}
855
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100856void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100857 LocationSummary* locations =
858 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100859 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100860}
861
862void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
863 // Will be generated at use site.
864}
865
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100866void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
867 LocationSummary* locations =
868 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
869 locations->SetOut(Location::ConstantLocation(constant));
870}
871
872void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
873 // Will be generated at use site.
874}
875
876void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
877 LocationSummary* locations =
878 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
879 locations->SetOut(Location::ConstantLocation(constant));
880}
881
882void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
883 // Will be generated at use site.
884}
885
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000886void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000887 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000888}
889
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000890void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
891 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000892 __ ret();
893}
894
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000895void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100896 LocationSummary* locations =
897 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100898 switch (ret->InputAt(0)->GetType()) {
899 case Primitive::kPrimBoolean:
900 case Primitive::kPrimByte:
901 case Primitive::kPrimChar:
902 case Primitive::kPrimShort:
903 case Primitive::kPrimInt:
904 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100905 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100906 break;
907
908 case Primitive::kPrimLong:
909 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100910 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100911 break;
912
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100913 case Primitive::kPrimFloat:
914 case Primitive::kPrimDouble:
915 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100916 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100917 break;
918
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100919 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100920 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100921 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000922}
923
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000924void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100925 if (kIsDebugBuild) {
926 switch (ret->InputAt(0)->GetType()) {
927 case Primitive::kPrimBoolean:
928 case Primitive::kPrimByte:
929 case Primitive::kPrimChar:
930 case Primitive::kPrimShort:
931 case Primitive::kPrimInt:
932 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100933 DCHECK_EQ(ret->GetLocations()->InAt(0).As<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100934 break;
935
936 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100937 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
938 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100939 break;
940
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100941 case Primitive::kPrimFloat:
942 case Primitive::kPrimDouble:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100943 DCHECK_EQ(ret->GetLocations()->InAt(0).As<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100944 break;
945
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100946 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100947 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100948 }
949 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000950 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000951 __ ret();
952}
953
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000954void LocationsBuilderX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100955 HandleInvoke(invoke);
956}
957
958void InstructionCodeGeneratorX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100959 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100960
961 // TODO: Implement all kinds of calls:
962 // 1) boot -> boot
963 // 2) app -> boot
964 // 3) app -> app
965 //
966 // Currently we implement the app -> app logic, which looks up in the resolve cache.
967
968 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100969 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100970 // temp = temp->dex_cache_resolved_methods_;
971 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
972 // temp = temp[index_in_cache]
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100973 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetIndexInDexCache())));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100974 // (temp + offset_of_quick_compiled_code)()
975 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
976
977 DCHECK(!codegen_->IsLeafMethod());
978 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
979}
980
981void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
982 HandleInvoke(invoke);
983}
984
985void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100986 LocationSummary* locations =
987 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100988 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100989
990 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100991 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100992 HInstruction* input = invoke->InputAt(i);
993 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
994 }
995
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100996 switch (invoke->GetType()) {
997 case Primitive::kPrimBoolean:
998 case Primitive::kPrimByte:
999 case Primitive::kPrimChar:
1000 case Primitive::kPrimShort:
1001 case Primitive::kPrimInt:
1002 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001003 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001004 break;
1005
1006 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001007 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001008 break;
1009
1010 case Primitive::kPrimVoid:
1011 break;
1012
1013 case Primitive::kPrimDouble:
1014 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001015 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001016 break;
1017 }
1018
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001019 invoke->SetLocations(locations);
1020}
1021
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001022void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001023 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001024 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1025 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1026 LocationSummary* locations = invoke->GetLocations();
1027 Location receiver = locations->InAt(0);
1028 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1029 // temp = object->GetClass();
1030 if (receiver.IsStackSlot()) {
1031 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1032 __ movl(temp, Address(temp, class_offset));
1033 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001034 __ movl(temp, Address(receiver.As<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001035 }
1036 // temp = temp->GetMethodAt(method_offset);
1037 __ movl(temp, Address(temp, method_offset));
1038 // call temp->GetEntryPoint();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001039 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
1040
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001041 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001042 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001043}
1044
Roland Levillain88cb1752014-10-20 16:36:47 +01001045void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1046 LocationSummary* locations =
1047 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1048 switch (neg->GetResultType()) {
1049 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001050 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001051 locations->SetInAt(0, Location::RequiresRegister());
1052 locations->SetOut(Location::SameAsFirstInput());
1053 break;
1054
Roland Levillain88cb1752014-10-20 16:36:47 +01001055 case Primitive::kPrimFloat:
1056 case Primitive::kPrimDouble:
1057 LOG(FATAL) << "Not yet implemented neg type " << neg->GetResultType();
1058 break;
1059
1060 default:
1061 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1062 }
1063}
1064
1065void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1066 LocationSummary* locations = neg->GetLocations();
1067 Location out = locations->Out();
1068 Location in = locations->InAt(0);
1069 switch (neg->GetResultType()) {
1070 case Primitive::kPrimInt:
1071 DCHECK(in.IsRegister());
1072 __ negl(out.As<Register>());
1073 break;
1074
1075 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001076 DCHECK(in.IsRegisterPair());
1077 __ negl(out.AsRegisterPairLow<Register>());
1078 // Negation is similar to subtraction from zero. The least
1079 // significant byte triggers a borrow when it is different from
1080 // zero; to take it into account, add 1 to the most significant
1081 // byte if the carry flag (CF) is set to 1 after the first NEGL
1082 // operation.
1083 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1084 __ negl(out.AsRegisterPairHigh<Register>());
1085 break;
1086
Roland Levillain88cb1752014-10-20 16:36:47 +01001087 case Primitive::kPrimFloat:
1088 case Primitive::kPrimDouble:
1089 LOG(FATAL) << "Not yet implemented neg type " << neg->GetResultType();
1090 break;
1091
1092 default:
1093 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1094 }
1095}
1096
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001097void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001098 LocationSummary* locations =
1099 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001100 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001101 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001102 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001103 locations->SetInAt(0, Location::RequiresRegister());
1104 locations->SetInAt(1, Location::Any());
1105 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001106 break;
1107 }
1108
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001109 case Primitive::kPrimFloat:
1110 case Primitive::kPrimDouble: {
1111 locations->SetInAt(0, Location::RequiresFpuRegister());
1112 locations->SetInAt(1, Location::Any());
1113 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001114 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001115 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001116
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001117 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001118 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1119 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001120 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001121}
1122
1123void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1124 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001125 Location first = locations->InAt(0);
1126 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001127 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001128 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001129 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001130 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001131 __ addl(first.As<Register>(), second.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001132 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001133 __ addl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001134 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001135 __ addl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001136 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001137 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001138 }
1139
1140 case Primitive::kPrimLong: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001141 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001142 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1143 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001144 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001145 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1146 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001147 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001148 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001149 break;
1150 }
1151
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001152 case Primitive::kPrimFloat: {
1153 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001154 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001155 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001156 __ addss(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001157 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001158 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001159 }
1160
1161 case Primitive::kPrimDouble: {
1162 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001163 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001164 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001165 __ addsd(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001166 }
1167 break;
1168 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001169
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001170 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001171 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001172 }
1173}
1174
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001175void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001176 LocationSummary* locations =
1177 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001178 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001179 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001180 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001181 locations->SetInAt(0, Location::RequiresRegister());
1182 locations->SetInAt(1, Location::Any());
1183 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001184 break;
1185 }
Calin Juravle11351682014-10-23 15:38:15 +01001186 case Primitive::kPrimFloat:
1187 case Primitive::kPrimDouble: {
1188 locations->SetInAt(0, Location::RequiresFpuRegister());
1189 locations->SetInAt(1, Location::RequiresFpuRegister());
1190 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001191 break;
Calin Juravle11351682014-10-23 15:38:15 +01001192 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001193
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001194 default:
Calin Juravle11351682014-10-23 15:38:15 +01001195 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001196 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001197}
1198
1199void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
1200 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001201 Location first = locations->InAt(0);
1202 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001203 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001204 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001205 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001206 if (second.IsRegister()) {
Calin Juravle11351682014-10-23 15:38:15 +01001207 __ subl(first.As<Register>(), second.As<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001208 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001209 __ subl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001210 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001211 __ subl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001212 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001213 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001214 }
1215
1216 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001217 if (second.IsRegister()) {
Calin Juravle11351682014-10-23 15:38:15 +01001218 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1219 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001220 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001221 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001222 __ sbbl(first.AsRegisterPairHigh<Register>(),
1223 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001224 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001225 break;
1226 }
1227
Calin Juravle11351682014-10-23 15:38:15 +01001228 case Primitive::kPrimFloat: {
1229 __ subss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001230 break;
Calin Juravle11351682014-10-23 15:38:15 +01001231 }
1232
1233 case Primitive::kPrimDouble: {
1234 __ subsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1235 break;
1236 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001237
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001238 default:
Calin Juravle11351682014-10-23 15:38:15 +01001239 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001240 }
1241}
1242
Calin Juravle34bacdf2014-10-07 20:23:36 +01001243void LocationsBuilderX86::VisitMul(HMul* mul) {
1244 LocationSummary* locations =
1245 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1246 switch (mul->GetResultType()) {
1247 case Primitive::kPrimInt:
1248 locations->SetInAt(0, Location::RequiresRegister());
1249 locations->SetInAt(1, Location::Any());
1250 locations->SetOut(Location::SameAsFirstInput());
1251 break;
1252 case Primitive::kPrimLong: {
1253 locations->SetInAt(0, Location::RequiresRegister());
1254 // TODO: Currently this handles only stack operands:
1255 // - we don't have enough registers because we currently use Quick ABI.
1256 // - by the time we have a working register allocator we will probably change the ABI
1257 // and fix the above.
1258 // - we don't have a way yet to request operands on stack but the base line compiler
1259 // will leave the operands on the stack with Any().
1260 locations->SetInAt(1, Location::Any());
1261 locations->SetOut(Location::SameAsFirstInput());
1262 // Needed for imul on 32bits with 64bits output.
1263 locations->AddTemp(Location::RegisterLocation(EAX));
1264 locations->AddTemp(Location::RegisterLocation(EDX));
1265 break;
1266 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001267 case Primitive::kPrimFloat:
1268 case Primitive::kPrimDouble: {
1269 locations->SetInAt(0, Location::RequiresFpuRegister());
1270 locations->SetInAt(1, Location::RequiresFpuRegister());
1271 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001272 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001273 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001274
1275 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001276 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001277 }
1278}
1279
1280void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
1281 LocationSummary* locations = mul->GetLocations();
1282 Location first = locations->InAt(0);
1283 Location second = locations->InAt(1);
1284 DCHECK(first.Equals(locations->Out()));
1285
1286 switch (mul->GetResultType()) {
1287 case Primitive::kPrimInt: {
1288 if (second.IsRegister()) {
1289 __ imull(first.As<Register>(), second.As<Register>());
1290 } else if (second.IsConstant()) {
1291 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1292 __ imull(first.As<Register>(), imm);
1293 } else {
1294 DCHECK(second.IsStackSlot());
1295 __ imull(first.As<Register>(), Address(ESP, second.GetStackIndex()));
1296 }
1297 break;
1298 }
1299
1300 case Primitive::kPrimLong: {
1301 DCHECK(second.IsDoubleStackSlot());
1302
1303 Register in1_hi = first.AsRegisterPairHigh<Register>();
1304 Register in1_lo = first.AsRegisterPairLow<Register>();
1305 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
1306 Address in2_lo(ESP, second.GetStackIndex());
1307 Register eax = locations->GetTemp(0).As<Register>();
1308 Register edx = locations->GetTemp(1).As<Register>();
1309
1310 DCHECK_EQ(EAX, eax);
1311 DCHECK_EQ(EDX, edx);
1312
1313 // input: in1 - 64 bits, in2 - 64 bits
1314 // output: in1
1315 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
1316 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
1317 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
1318
1319 __ movl(eax, in2_hi);
1320 // eax <- in1.lo * in2.hi
1321 __ imull(eax, in1_lo);
1322 // in1.hi <- in1.hi * in2.lo
1323 __ imull(in1_hi, in2_lo);
1324 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
1325 __ addl(in1_hi, eax);
1326 // move in1_lo to eax to prepare for double precision
1327 __ movl(eax, in1_lo);
1328 // edx:eax <- in1.lo * in2.lo
1329 __ mull(in2_lo);
1330 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
1331 __ addl(in1_hi, edx);
1332 // in1.lo <- (in1.lo * in2.lo)[31:0];
1333 __ movl(in1_lo, eax);
1334
1335 break;
1336 }
1337
Calin Juravleb5bfa962014-10-21 18:02:24 +01001338 case Primitive::kPrimFloat: {
1339 __ mulss(first.As<XmmRegister>(), second.As<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001340 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001341 }
1342
1343 case Primitive::kPrimDouble: {
1344 __ mulsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1345 break;
1346 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001347
1348 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001349 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001350 }
1351}
1352
Calin Juravle7c4954d2014-10-28 16:57:40 +00001353void LocationsBuilderX86::VisitDiv(HDiv* div) {
1354 LocationSummary* locations =
1355 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1356 switch (div->GetResultType()) {
1357 case Primitive::kPrimInt:
1358 case Primitive::kPrimLong: {
1359 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1360 break;
1361 }
1362 case Primitive::kPrimFloat:
1363 case Primitive::kPrimDouble: {
1364 locations->SetInAt(0, Location::RequiresFpuRegister());
1365 locations->SetInAt(1, Location::RequiresFpuRegister());
1366 locations->SetOut(Location::SameAsFirstInput());
1367 break;
1368 }
1369
1370 default:
1371 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1372 }
1373}
1374
1375void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
1376 LocationSummary* locations = div->GetLocations();
1377 Location first = locations->InAt(0);
1378 Location second = locations->InAt(1);
1379 DCHECK(first.Equals(locations->Out()));
1380
1381 switch (div->GetResultType()) {
1382 case Primitive::kPrimInt:
1383 case Primitive::kPrimLong: {
1384 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1385 break;
1386 }
1387
1388 case Primitive::kPrimFloat: {
1389 __ divss(first.As<XmmRegister>(), second.As<XmmRegister>());
1390 break;
1391 }
1392
1393 case Primitive::kPrimDouble: {
1394 __ divsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1395 break;
1396 }
1397
1398 default:
1399 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1400 }
1401}
1402
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001403void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001404 LocationSummary* locations =
1405 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001406 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001407 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001408 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1409 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001410}
1411
1412void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
1413 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001414 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001415 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001416
Nicolas Geoffray707c8092014-04-04 10:50:14 +01001417 __ fs()->call(
1418 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001419
Nicolas Geoffray39468442014-09-02 15:17:15 +01001420 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001421 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001422}
1423
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001424void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
1425 LocationSummary* locations =
1426 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
1427 locations->SetOut(Location::RegisterLocation(EAX));
1428 InvokeRuntimeCallingConvention calling_convention;
1429 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1430 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1431 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1432}
1433
1434void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
1435 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001436 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001437 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
1438
1439 __ fs()->call(
1440 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocArrayWithAccessCheck)));
1441
1442 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
1443 DCHECK(!codegen_->IsLeafMethod());
1444}
1445
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001446void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001447 LocationSummary* locations =
1448 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001449 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1450 if (location.IsStackSlot()) {
1451 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1452 } else if (location.IsDoubleStackSlot()) {
1453 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001454 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001455 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001456}
1457
1458void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001459}
1460
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001461void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001462 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001463 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001464 locations->SetInAt(0, Location::RequiresRegister());
1465 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001466}
1467
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001468void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
1469 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01001470 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001471 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01001472 DCHECK(in.Equals(out));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001473 switch (not_->InputAt(0)->GetType()) {
1474 case Primitive::kPrimBoolean:
1475 __ xorl(out.As<Register>(), Immediate(1));
1476 break;
1477
1478 case Primitive::kPrimInt:
1479 __ notl(out.As<Register>());
1480 break;
1481
1482 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01001483 __ notl(out.AsRegisterPairLow<Register>());
1484 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001485 break;
1486
1487 default:
1488 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
1489 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001490}
1491
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001492void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001493 LocationSummary* locations =
1494 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001495 locations->SetInAt(0, Location::RequiresRegister());
1496 locations->SetInAt(1, Location::Any());
1497 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001498}
1499
1500void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
1501 Label greater, done;
1502 LocationSummary* locations = compare->GetLocations();
1503 switch (compare->InputAt(0)->GetType()) {
1504 case Primitive::kPrimLong: {
1505 Label less, greater, done;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001506 Register output = locations->Out().As<Register>();
1507 Location left = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001508 Location right = locations->InAt(1);
1509 if (right.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001510 __ cmpl(left.AsRegisterPairHigh<Register>(), right.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001511 } else {
1512 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001513 __ cmpl(left.AsRegisterPairHigh<Register>(),
1514 Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001515 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001516 __ j(kLess, &less); // Signed compare.
1517 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001518 if (right.IsRegisterPair()) {
1519 __ cmpl(left.AsRegisterPairLow<Register>(), right.AsRegisterPairLow<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001520 } else {
1521 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001522 __ cmpl(left.AsRegisterPairLow<Register>(), Address(ESP, right.GetStackIndex()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001523 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001524 __ movl(output, Immediate(0));
1525 __ j(kEqual, &done);
1526 __ j(kBelow, &less); // Unsigned compare.
1527
1528 __ Bind(&greater);
1529 __ movl(output, Immediate(1));
1530 __ jmp(&done);
1531
1532 __ Bind(&less);
1533 __ movl(output, Immediate(-1));
1534
1535 __ Bind(&done);
1536 break;
1537 }
1538 default:
1539 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
1540 }
1541}
1542
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001543void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001544 LocationSummary* locations =
1545 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001546 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1547 locations->SetInAt(i, Location::Any());
1548 }
1549 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001550}
1551
1552void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001553 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001554}
1555
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001556void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001557 LocationSummary* locations =
1558 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001559 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001560 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001561 bool is_object_type = field_type == Primitive::kPrimNot;
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001562 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
1563 || (field_type == Primitive::kPrimByte);
1564 // The register allocator does not support multiple
1565 // inputs that die at entry with one in a specific register.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001566 if (is_byte_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001567 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001568 locations->SetInAt(1, Location::RegisterLocation(EAX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001569 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001570 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001571 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001572 // Temporary registers for the write barrier.
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001573 if (is_object_type) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001574 locations->AddTemp(Location::RequiresRegister());
1575 // Ensure the card is in a byte register.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001576 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001577 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001578}
1579
1580void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1581 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001582 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001583 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001584 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001585
1586 switch (field_type) {
1587 case Primitive::kPrimBoolean:
1588 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001589 ByteRegister value = locations->InAt(1).As<ByteRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001590 __ movb(Address(obj, offset), value);
1591 break;
1592 }
1593
1594 case Primitive::kPrimShort:
1595 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001596 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001597 __ movw(Address(obj, offset), value);
1598 break;
1599 }
1600
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001601 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001602 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001603 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001604 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001605
1606 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001607 Register temp = locations->GetTemp(0).As<Register>();
1608 Register card = locations->GetTemp(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001609 codegen_->MarkGCCard(temp, card, obj, value);
1610 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001611 break;
1612 }
1613
1614 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001615 Location value = locations->InAt(1);
1616 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
1617 __ movl(Address(obj, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001618 break;
1619 }
1620
1621 case Primitive::kPrimFloat:
1622 case Primitive::kPrimDouble:
1623 LOG(FATAL) << "Unimplemented register type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001624 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001625 case Primitive::kPrimVoid:
1626 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001627 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001628 }
1629}
1630
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001631void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
1632 Label is_null;
1633 __ testl(value, value);
1634 __ j(kEqual, &is_null);
1635 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
1636 __ movl(temp, object);
1637 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
1638 __ movb(Address(temp, card, TIMES_1, 0),
1639 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
1640 __ Bind(&is_null);
1641}
1642
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001643void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001644 LocationSummary* locations =
1645 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001646 locations->SetInAt(0, Location::RequiresRegister());
1647 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001648}
1649
1650void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1651 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001652 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001653 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
1654
1655 switch (instruction->GetType()) {
1656 case Primitive::kPrimBoolean: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001657 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001658 __ movzxb(out, Address(obj, offset));
1659 break;
1660 }
1661
1662 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001663 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001664 __ movsxb(out, Address(obj, offset));
1665 break;
1666 }
1667
1668 case Primitive::kPrimShort: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001669 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001670 __ movsxw(out, Address(obj, offset));
1671 break;
1672 }
1673
1674 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001675 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001676 __ movzxw(out, Address(obj, offset));
1677 break;
1678 }
1679
1680 case Primitive::kPrimInt:
1681 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001682 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001683 __ movl(out, Address(obj, offset));
1684 break;
1685 }
1686
1687 case Primitive::kPrimLong: {
1688 // TODO: support volatile.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001689 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(obj, offset));
1690 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(obj, kX86WordSize + offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001691 break;
1692 }
1693
1694 case Primitive::kPrimFloat:
1695 case Primitive::kPrimDouble:
1696 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001697 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001698 case Primitive::kPrimVoid:
1699 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001700 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001701 }
1702}
1703
1704void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001705 LocationSummary* locations =
1706 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001707 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001708 if (instruction->HasUses()) {
1709 locations->SetOut(Location::SameAsFirstInput());
1710 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001711}
1712
1713void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001714 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001715 codegen_->AddSlowPath(slow_path);
1716
1717 LocationSummary* locations = instruction->GetLocations();
1718 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001719
1720 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001721 __ cmpl(obj.As<Register>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001722 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001723 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001724 } else {
1725 DCHECK(obj.IsConstant()) << obj;
1726 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
1727 __ jmp(slow_path->GetEntryLabel());
1728 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001729 }
1730 __ j(kEqual, slow_path->GetEntryLabel());
1731}
1732
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001733void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001734 LocationSummary* locations =
1735 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001736 locations->SetInAt(0, Location::RequiresRegister());
1737 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1738 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001739}
1740
1741void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
1742 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001743 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001744 Location index = locations->InAt(1);
1745
1746 switch (instruction->GetType()) {
1747 case Primitive::kPrimBoolean: {
1748 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001749 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001750 if (index.IsConstant()) {
1751 __ movzxb(out, Address(obj,
1752 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1753 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001754 __ movzxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001755 }
1756 break;
1757 }
1758
1759 case Primitive::kPrimByte: {
1760 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001761 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001762 if (index.IsConstant()) {
1763 __ movsxb(out, Address(obj,
1764 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1765 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001766 __ movsxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001767 }
1768 break;
1769 }
1770
1771 case Primitive::kPrimShort: {
1772 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001773 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001774 if (index.IsConstant()) {
1775 __ movsxw(out, Address(obj,
1776 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1777 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001778 __ movsxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001779 }
1780 break;
1781 }
1782
1783 case Primitive::kPrimChar: {
1784 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001785 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001786 if (index.IsConstant()) {
1787 __ movzxw(out, Address(obj,
1788 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1789 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001790 __ movzxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001791 }
1792 break;
1793 }
1794
1795 case Primitive::kPrimInt:
1796 case Primitive::kPrimNot: {
1797 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001798 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001799 if (index.IsConstant()) {
1800 __ movl(out, Address(obj,
1801 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
1802 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001803 __ movl(out, Address(obj, index.As<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001804 }
1805 break;
1806 }
1807
1808 case Primitive::kPrimLong: {
1809 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001810 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001811 if (index.IsConstant()) {
1812 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001813 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
1814 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001815 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001816 __ movl(out.AsRegisterPairLow<Register>(),
1817 Address(obj, index.As<Register>(), TIMES_8, data_offset));
1818 __ movl(out.AsRegisterPairHigh<Register>(),
1819 Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001820 }
1821 break;
1822 }
1823
1824 case Primitive::kPrimFloat:
1825 case Primitive::kPrimDouble:
1826 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001827 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001828 case Primitive::kPrimVoid:
1829 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001830 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001831 }
1832}
1833
1834void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001835 Primitive::Type value_type = instruction->GetComponentType();
1836 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1837 instruction,
1838 value_type == Primitive::kPrimNot ? LocationSummary::kCall : LocationSummary::kNoCall);
1839
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001840 if (value_type == Primitive::kPrimNot) {
1841 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001842 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1843 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1844 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001845 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001846 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
1847 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001848 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001849 // In case of a byte operation, the register allocator does not support multiple
1850 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001851 locations->SetInAt(0, Location::RequiresRegister());
1852 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001853 if (is_byte_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001854 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001855 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001856 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001857 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001858 }
1859 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001860}
1861
1862void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
1863 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001864 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001865 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001866 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001867 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001868
1869 switch (value_type) {
1870 case Primitive::kPrimBoolean:
1871 case Primitive::kPrimByte: {
1872 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001873 if (index.IsConstant()) {
1874 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001875 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001876 __ movb(Address(obj, offset), value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001877 } else {
1878 __ movb(Address(obj, offset),
1879 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1880 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001881 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001882 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001883 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
1884 value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001885 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001886 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001887 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1888 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001889 }
1890 break;
1891 }
1892
1893 case Primitive::kPrimShort:
1894 case Primitive::kPrimChar: {
1895 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001896 if (index.IsConstant()) {
1897 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001898 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001899 __ movw(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001900 } else {
1901 __ movw(Address(obj, offset),
1902 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1903 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001904 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001905 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001906 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
1907 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001908 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001909 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001910 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1911 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001912 }
1913 break;
1914 }
1915
1916 case Primitive::kPrimInt: {
1917 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001918 if (index.IsConstant()) {
1919 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001920 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001921 __ movl(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001922 } else {
1923 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1924 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001925 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001926 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001927 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
1928 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001929 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001930 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001931 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1932 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001933 }
1934 break;
1935 }
1936
1937 case Primitive::kPrimNot: {
1938 DCHECK(!codegen_->IsLeafMethod());
1939 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001940 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001941 break;
1942 }
1943
1944 case Primitive::kPrimLong: {
1945 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001946 if (index.IsConstant()) {
1947 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001948 if (value.IsRegisterPair()) {
1949 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
1950 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001951 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001952 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001953 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
1954 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
1955 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
1956 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001957 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001958 if (value.IsRegisterPair()) {
1959 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
1960 value.AsRegisterPairLow<Register>());
1961 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
1962 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001963 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001964 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001965 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001966 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001967 Immediate(Low32Bits(val)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001968 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001969 Immediate(High32Bits(val)));
1970 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001971 }
1972 break;
1973 }
1974
1975 case Primitive::kPrimFloat:
1976 case Primitive::kPrimDouble:
1977 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001978 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001979 case Primitive::kPrimVoid:
1980 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001981 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001982 }
1983}
1984
1985void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
1986 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001987 locations->SetInAt(0, Location::RequiresRegister());
1988 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001989 instruction->SetLocations(locations);
1990}
1991
1992void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
1993 LocationSummary* locations = instruction->GetLocations();
1994 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001995 Register obj = locations->InAt(0).As<Register>();
1996 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001997 __ movl(out, Address(obj, offset));
1998}
1999
2000void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002001 LocationSummary* locations =
2002 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002003 locations->SetInAt(0, Location::RequiresRegister());
2004 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002005 if (instruction->HasUses()) {
2006 locations->SetOut(Location::SameAsFirstInput());
2007 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002008}
2009
2010void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
2011 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002012 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01002013 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002014 codegen_->AddSlowPath(slow_path);
2015
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002016 Register index = locations->InAt(0).As<Register>();
2017 Register length = locations->InAt(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002018
2019 __ cmpl(index, length);
2020 __ j(kAboveEqual, slow_path->GetEntryLabel());
2021}
2022
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002023void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
2024 temp->SetLocations(nullptr);
2025}
2026
2027void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
2028 // Nothing to do, this is driven by the code generator.
2029}
2030
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002031void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002032 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002033}
2034
2035void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002036 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2037}
2038
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002039void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
2040 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2041}
2042
2043void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002044 HBasicBlock* block = instruction->GetBlock();
2045 if (block->GetLoopInformation() != nullptr) {
2046 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2047 // The back edge will generate the suspend check.
2048 return;
2049 }
2050 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2051 // The goto will generate the suspend check.
2052 return;
2053 }
2054 GenerateSuspendCheck(instruction, nullptr);
2055}
2056
2057void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
2058 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002059 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002060 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002061 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002062 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002063 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002064 if (successor == nullptr) {
2065 __ j(kNotEqual, slow_path->GetEntryLabel());
2066 __ Bind(slow_path->GetReturnLabel());
2067 } else {
2068 __ j(kEqual, codegen_->GetLabelOf(successor));
2069 __ jmp(slow_path->GetEntryLabel());
2070 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002071}
2072
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002073X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
2074 return codegen_->GetAssembler();
2075}
2076
2077void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
2078 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002079 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002080 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
2081 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
2082 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
2083}
2084
2085void ParallelMoveResolverX86::EmitMove(size_t index) {
2086 MoveOperands* move = moves_.Get(index);
2087 Location source = move->GetSource();
2088 Location destination = move->GetDestination();
2089
2090 if (source.IsRegister()) {
2091 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002092 __ movl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002093 } else {
2094 DCHECK(destination.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002095 __ movl(Address(ESP, destination.GetStackIndex()), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002096 }
2097 } else if (source.IsStackSlot()) {
2098 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002099 __ movl(destination.As<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002100 } else {
2101 DCHECK(destination.IsStackSlot());
2102 MoveMemoryToMemory(destination.GetStackIndex(),
2103 source.GetStackIndex());
2104 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002105 } else if (source.IsConstant()) {
2106 HIntConstant* instruction = source.GetConstant()->AsIntConstant();
2107 Immediate imm(instruction->AsIntConstant()->GetValue());
2108 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002109 __ movl(destination.As<Register>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002110 } else {
2111 __ movl(Address(ESP, destination.GetStackIndex()), imm);
2112 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002113 } else {
2114 LOG(FATAL) << "Unimplemented";
2115 }
2116}
2117
2118void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002119 Register suggested_scratch = reg == EAX ? EBX : EAX;
2120 ScratchRegisterScope ensure_scratch(
2121 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
2122
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002123 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
2124 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
2125 __ movl(Address(ESP, mem + stack_offset), reg);
2126 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
2127}
2128
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002129void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
2130 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002131 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
2132
2133 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002134 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002135 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
2136
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002137 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
2138 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
2139 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
2140 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
2141 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
2142 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
2143}
2144
2145void ParallelMoveResolverX86::EmitSwap(size_t index) {
2146 MoveOperands* move = moves_.Get(index);
2147 Location source = move->GetSource();
2148 Location destination = move->GetDestination();
2149
2150 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002151 __ xchgl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002152 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002153 Exchange(source.As<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002154 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002155 Exchange(destination.As<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002156 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
2157 Exchange(destination.GetStackIndex(), source.GetStackIndex());
2158 } else {
2159 LOG(FATAL) << "Unimplemented";
2160 }
2161}
2162
2163void ParallelMoveResolverX86::SpillScratch(int reg) {
2164 __ pushl(static_cast<Register>(reg));
2165}
2166
2167void ParallelMoveResolverX86::RestoreScratch(int reg) {
2168 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002169}
2170
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002171void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
2172 LocationSummary* locations =
2173 new (GetGraph()->GetArena()) LocationSummary(cls, LocationSummary::kNoCall);
2174 locations->SetOut(Location::RequiresRegister());
2175}
2176
2177void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
2178 Register out = cls->GetLocations()->Out().As<Register>();
2179 if (cls->IsReferrersClass()) {
2180 codegen_->LoadCurrentMethod(out);
2181 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
2182 } else {
2183 codegen_->LoadCurrentMethod(out);
2184 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
2185 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
2186 }
2187}
2188
2189void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
2190 LocationSummary* locations =
2191 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2192 locations->SetInAt(0, Location::RequiresRegister());
2193 if (check->HasUses()) {
2194 locations->SetOut(Location::SameAsFirstInput());
2195 }
2196}
2197
2198void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
2199 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) ClinitCheckSlowPathX86(check);
2200 codegen_->AddSlowPath(slow_path);
2201
2202 LocationSummary* locations = check->GetLocations();
2203 // We remove the class as a live register, we know it's null or unused in the slow path.
2204 RegisterSet* register_set = locations->GetLiveRegisters();
2205 register_set->Remove(locations->InAt(0));
2206
2207 Register class_reg = locations->InAt(0).As<Register>();
2208 __ testl(class_reg, class_reg);
2209 __ j(kEqual, slow_path->GetEntryLabel());
2210 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
2211 Immediate(mirror::Class::kStatusInitialized));
2212 __ j(kLess, slow_path->GetEntryLabel());
2213 __ Bind(slow_path->GetExitLabel());
2214 // No need for memory fence, thanks to the X86 memory model.
2215}
2216
2217void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2218 LocationSummary* locations =
2219 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2220 locations->SetInAt(0, Location::RequiresRegister());
2221 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2222}
2223
2224void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2225 LocationSummary* locations = instruction->GetLocations();
2226 Register cls = locations->InAt(0).As<Register>();
2227 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2228
2229 switch (instruction->GetType()) {
2230 case Primitive::kPrimBoolean: {
2231 Register out = locations->Out().As<Register>();
2232 __ movzxb(out, Address(cls, offset));
2233 break;
2234 }
2235
2236 case Primitive::kPrimByte: {
2237 Register out = locations->Out().As<Register>();
2238 __ movsxb(out, Address(cls, offset));
2239 break;
2240 }
2241
2242 case Primitive::kPrimShort: {
2243 Register out = locations->Out().As<Register>();
2244 __ movsxw(out, Address(cls, offset));
2245 break;
2246 }
2247
2248 case Primitive::kPrimChar: {
2249 Register out = locations->Out().As<Register>();
2250 __ movzxw(out, Address(cls, offset));
2251 break;
2252 }
2253
2254 case Primitive::kPrimInt:
2255 case Primitive::kPrimNot: {
2256 Register out = locations->Out().As<Register>();
2257 __ movl(out, Address(cls, offset));
2258 break;
2259 }
2260
2261 case Primitive::kPrimLong: {
2262 // TODO: support volatile.
2263 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(cls, offset));
2264 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(cls, kX86WordSize + offset));
2265 break;
2266 }
2267
2268 case Primitive::kPrimFloat:
2269 case Primitive::kPrimDouble:
2270 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
2271 UNREACHABLE();
2272 case Primitive::kPrimVoid:
2273 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2274 UNREACHABLE();
2275 }
2276}
2277
2278void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2279 LocationSummary* locations =
2280 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2281 locations->SetInAt(0, Location::RequiresRegister());
2282 Primitive::Type field_type = instruction->GetFieldType();
2283 bool is_object_type = field_type == Primitive::kPrimNot;
2284 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
2285 || (field_type == Primitive::kPrimByte);
2286 // The register allocator does not support multiple
2287 // inputs that die at entry with one in a specific register.
2288 if (is_byte_type) {
2289 // Ensure the value is in a byte register.
2290 locations->SetInAt(1, Location::RegisterLocation(EAX));
2291 } else {
2292 locations->SetInAt(1, Location::RequiresRegister());
2293 }
2294 // Temporary registers for the write barrier.
2295 if (is_object_type) {
2296 locations->AddTemp(Location::RequiresRegister());
2297 // Ensure the card is in a byte register.
2298 locations->AddTemp(Location::RegisterLocation(ECX));
2299 }
2300}
2301
2302void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2303 LocationSummary* locations = instruction->GetLocations();
2304 Register cls = locations->InAt(0).As<Register>();
2305 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2306 Primitive::Type field_type = instruction->GetFieldType();
2307
2308 switch (field_type) {
2309 case Primitive::kPrimBoolean:
2310 case Primitive::kPrimByte: {
2311 ByteRegister value = locations->InAt(1).As<ByteRegister>();
2312 __ movb(Address(cls, offset), value);
2313 break;
2314 }
2315
2316 case Primitive::kPrimShort:
2317 case Primitive::kPrimChar: {
2318 Register value = locations->InAt(1).As<Register>();
2319 __ movw(Address(cls, offset), value);
2320 break;
2321 }
2322
2323 case Primitive::kPrimInt:
2324 case Primitive::kPrimNot: {
2325 Register value = locations->InAt(1).As<Register>();
2326 __ movl(Address(cls, offset), value);
2327
2328 if (field_type == Primitive::kPrimNot) {
2329 Register temp = locations->GetTemp(0).As<Register>();
2330 Register card = locations->GetTemp(1).As<Register>();
2331 codegen_->MarkGCCard(temp, card, cls, value);
2332 }
2333 break;
2334 }
2335
2336 case Primitive::kPrimLong: {
2337 Location value = locations->InAt(1);
2338 __ movl(Address(cls, offset), value.AsRegisterPairLow<Register>());
2339 __ movl(Address(cls, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
2340 break;
2341 }
2342
2343 case Primitive::kPrimFloat:
2344 case Primitive::kPrimDouble:
2345 LOG(FATAL) << "Unimplemented register type " << field_type;
2346 UNREACHABLE();
2347 case Primitive::kPrimVoid:
2348 LOG(FATAL) << "Unreachable type " << field_type;
2349 UNREACHABLE();
2350 }
2351}
2352
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002353void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
2354 LocationSummary* locations =
2355 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
2356 locations->SetOut(Location::RequiresRegister());
2357}
2358
2359void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
2360 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
2361 codegen_->AddSlowPath(slow_path);
2362
2363 Register out = load->GetLocations()->Out().As<Register>();
2364 codegen_->LoadCurrentMethod(out);
2365 __ movl(out, Address(out, mirror::ArtMethod::DexCacheStringsOffset().Int32Value()));
2366 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
2367 __ testl(out, out);
2368 __ j(kEqual, slow_path->GetEntryLabel());
2369 __ Bind(slow_path->GetExitLabel());
2370}
2371
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002372} // namespace x86
2373} // namespace art