blob: ac328c319c9504eb003e0c61aefa7c9d46ea2111 [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) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700648 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000649 if (kIsDebugBuild) {
650 __ Comment("Unreachable");
651 __ int3();
652 }
653}
654
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000655void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100656 LocationSummary* locations =
657 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100658 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100659 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100660 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100661 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000662}
663
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000664void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700665 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100666 if (cond->IsIntConstant()) {
667 // Constant condition, statically compared against 1.
668 int32_t cond_value = cond->AsIntConstant()->GetValue();
669 if (cond_value == 1) {
670 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
671 if_instr->IfTrueSuccessor())) {
672 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100673 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100674 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100675 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100676 DCHECK_EQ(cond_value, 0);
677 }
678 } else {
679 bool materialized =
680 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
681 // Moves do not affect the eflags register, so if the condition is
682 // evaluated just before the if, we don't need to evaluate it
683 // again.
684 bool eflags_set = cond->IsCondition()
685 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
686 if (materialized) {
687 if (!eflags_set) {
688 // Materialized condition, compare against 0.
689 Location lhs = if_instr->GetLocations()->InAt(0);
690 if (lhs.IsRegister()) {
691 __ cmpl(lhs.As<Register>(), Immediate(0));
692 } else {
693 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
694 }
695 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
696 } else {
697 __ j(X86Condition(cond->AsCondition()->GetCondition()),
698 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
699 }
700 } else {
701 Location lhs = cond->GetLocations()->InAt(0);
702 Location rhs = cond->GetLocations()->InAt(1);
703 // LHS is guaranteed to be in a register (see
704 // LocationsBuilderX86::VisitCondition).
705 if (rhs.IsRegister()) {
706 __ cmpl(lhs.As<Register>(), rhs.As<Register>());
707 } else if (rhs.IsConstant()) {
708 HIntConstant* instruction = rhs.GetConstant()->AsIntConstant();
709 Immediate imm(instruction->AsIntConstant()->GetValue());
710 __ cmpl(lhs.As<Register>(), imm);
711 } else {
712 __ cmpl(lhs.As<Register>(), Address(ESP, rhs.GetStackIndex()));
713 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100714 __ j(X86Condition(cond->AsCondition()->GetCondition()),
715 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700716 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100717 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100718 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
719 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700720 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000721 }
722}
723
724void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000725 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000726}
727
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000728void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
729 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000730}
731
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000732void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100733 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000734}
735
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000736void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100737 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700738 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000739}
740
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100741void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100742 LocationSummary* locations =
743 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100744 switch (store->InputAt(1)->GetType()) {
745 case Primitive::kPrimBoolean:
746 case Primitive::kPrimByte:
747 case Primitive::kPrimChar:
748 case Primitive::kPrimShort:
749 case Primitive::kPrimInt:
750 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100751 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100752 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
753 break;
754
755 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100756 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100757 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
758 break;
759
760 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100761 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100762 }
763 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000764}
765
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000766void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700767 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000768}
769
Dave Allison20dfc792014-06-16 20:44:29 -0700770void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100771 LocationSummary* locations =
772 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100773 locations->SetInAt(0, Location::RequiresRegister());
774 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100775 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100776 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100777 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000778}
779
Dave Allison20dfc792014-06-16 20:44:29 -0700780void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
781 if (comp->NeedsMaterialization()) {
782 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100783 Register reg = locations->Out().As<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100784 // Clear register: setcc only sets the low byte.
785 __ xorl(reg, reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700786 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100787 __ cmpl(locations->InAt(0).As<Register>(),
788 locations->InAt(1).As<Register>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100789 } else if (locations->InAt(1).IsConstant()) {
790 HConstant* instruction = locations->InAt(1).GetConstant();
791 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100792 __ cmpl(locations->InAt(0).As<Register>(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700793 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100794 __ cmpl(locations->InAt(0).As<Register>(),
Dave Allison20dfc792014-06-16 20:44:29 -0700795 Address(ESP, locations->InAt(1).GetStackIndex()));
796 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100797 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100798 }
Dave Allison20dfc792014-06-16 20:44:29 -0700799}
800
801void LocationsBuilderX86::VisitEqual(HEqual* comp) {
802 VisitCondition(comp);
803}
804
805void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
806 VisitCondition(comp);
807}
808
809void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
810 VisitCondition(comp);
811}
812
813void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
814 VisitCondition(comp);
815}
816
817void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
818 VisitCondition(comp);
819}
820
821void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
822 VisitCondition(comp);
823}
824
825void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
826 VisitCondition(comp);
827}
828
829void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
830 VisitCondition(comp);
831}
832
833void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
834 VisitCondition(comp);
835}
836
837void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
838 VisitCondition(comp);
839}
840
841void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
842 VisitCondition(comp);
843}
844
845void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
846 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000847}
848
849void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100850 LocationSummary* locations =
851 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100852 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000853}
854
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000855void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100856 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700857 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000858}
859
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100860void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100861 LocationSummary* locations =
862 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100863 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100864}
865
866void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
867 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700868 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100869}
870
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100871void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
872 LocationSummary* locations =
873 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
874 locations->SetOut(Location::ConstantLocation(constant));
875}
876
877void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
878 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700879 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100880}
881
882void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
883 LocationSummary* locations =
884 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
885 locations->SetOut(Location::ConstantLocation(constant));
886}
887
888void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
889 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700890 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100891}
892
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000893void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000894 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000895}
896
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000897void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700898 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000899 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000900 __ ret();
901}
902
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000903void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100904 LocationSummary* locations =
905 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100906 switch (ret->InputAt(0)->GetType()) {
907 case Primitive::kPrimBoolean:
908 case Primitive::kPrimByte:
909 case Primitive::kPrimChar:
910 case Primitive::kPrimShort:
911 case Primitive::kPrimInt:
912 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100913 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100914 break;
915
916 case Primitive::kPrimLong:
917 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100918 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100919 break;
920
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100921 case Primitive::kPrimFloat:
922 case Primitive::kPrimDouble:
923 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100924 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100925 break;
926
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100927 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100928 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100929 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000930}
931
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000932void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100933 if (kIsDebugBuild) {
934 switch (ret->InputAt(0)->GetType()) {
935 case Primitive::kPrimBoolean:
936 case Primitive::kPrimByte:
937 case Primitive::kPrimChar:
938 case Primitive::kPrimShort:
939 case Primitive::kPrimInt:
940 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100941 DCHECK_EQ(ret->GetLocations()->InAt(0).As<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100942 break;
943
944 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100945 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
946 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100947 break;
948
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100949 case Primitive::kPrimFloat:
950 case Primitive::kPrimDouble:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100951 DCHECK_EQ(ret->GetLocations()->InAt(0).As<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100952 break;
953
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100954 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100955 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100956 }
957 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000958 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000959 __ ret();
960}
961
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000962void LocationsBuilderX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100963 HandleInvoke(invoke);
964}
965
966void InstructionCodeGeneratorX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100967 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100968
969 // TODO: Implement all kinds of calls:
970 // 1) boot -> boot
971 // 2) app -> boot
972 // 3) app -> app
973 //
974 // Currently we implement the app -> app logic, which looks up in the resolve cache.
975
976 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100977 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100978 // temp = temp->dex_cache_resolved_methods_;
979 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
980 // temp = temp[index_in_cache]
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100981 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetIndexInDexCache())));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100982 // (temp + offset_of_quick_compiled_code)()
983 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
984
985 DCHECK(!codegen_->IsLeafMethod());
986 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
987}
988
989void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
990 HandleInvoke(invoke);
991}
992
993void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100994 LocationSummary* locations =
995 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100996 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100997
998 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100999 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001000 HInstruction* input = invoke->InputAt(i);
1001 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1002 }
1003
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001004 switch (invoke->GetType()) {
1005 case Primitive::kPrimBoolean:
1006 case Primitive::kPrimByte:
1007 case Primitive::kPrimChar:
1008 case Primitive::kPrimShort:
1009 case Primitive::kPrimInt:
1010 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001011 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001012 break;
1013
1014 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001015 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001016 break;
1017
1018 case Primitive::kPrimVoid:
1019 break;
1020
1021 case Primitive::kPrimDouble:
1022 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001023 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001024 break;
1025 }
1026
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001027 invoke->SetLocations(locations);
1028}
1029
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001030void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001031 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001032 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1033 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1034 LocationSummary* locations = invoke->GetLocations();
1035 Location receiver = locations->InAt(0);
1036 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1037 // temp = object->GetClass();
1038 if (receiver.IsStackSlot()) {
1039 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1040 __ movl(temp, Address(temp, class_offset));
1041 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001042 __ movl(temp, Address(receiver.As<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001043 }
1044 // temp = temp->GetMethodAt(method_offset);
1045 __ movl(temp, Address(temp, method_offset));
1046 // call temp->GetEntryPoint();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001047 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
1048
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001049 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001050 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001051}
1052
Roland Levillain88cb1752014-10-20 16:36:47 +01001053void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1054 LocationSummary* locations =
1055 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1056 switch (neg->GetResultType()) {
1057 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001058 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001059 locations->SetInAt(0, Location::RequiresRegister());
1060 locations->SetOut(Location::SameAsFirstInput());
1061 break;
1062
Roland Levillain88cb1752014-10-20 16:36:47 +01001063 case Primitive::kPrimFloat:
1064 case Primitive::kPrimDouble:
1065 LOG(FATAL) << "Not yet implemented neg type " << neg->GetResultType();
1066 break;
1067
1068 default:
1069 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1070 }
1071}
1072
1073void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1074 LocationSummary* locations = neg->GetLocations();
1075 Location out = locations->Out();
1076 Location in = locations->InAt(0);
1077 switch (neg->GetResultType()) {
1078 case Primitive::kPrimInt:
1079 DCHECK(in.IsRegister());
1080 __ negl(out.As<Register>());
1081 break;
1082
1083 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001084 DCHECK(in.IsRegisterPair());
1085 __ negl(out.AsRegisterPairLow<Register>());
1086 // Negation is similar to subtraction from zero. The least
1087 // significant byte triggers a borrow when it is different from
1088 // zero; to take it into account, add 1 to the most significant
1089 // byte if the carry flag (CF) is set to 1 after the first NEGL
1090 // operation.
1091 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1092 __ negl(out.AsRegisterPairHigh<Register>());
1093 break;
1094
Roland Levillain88cb1752014-10-20 16:36:47 +01001095 case Primitive::kPrimFloat:
1096 case Primitive::kPrimDouble:
1097 LOG(FATAL) << "Not yet implemented neg type " << neg->GetResultType();
1098 break;
1099
1100 default:
1101 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1102 }
1103}
1104
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001105void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001106 LocationSummary* locations =
1107 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001108 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001109 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001110 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001111 locations->SetInAt(0, Location::RequiresRegister());
1112 locations->SetInAt(1, Location::Any());
1113 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001114 break;
1115 }
1116
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001117 case Primitive::kPrimFloat:
1118 case Primitive::kPrimDouble: {
1119 locations->SetInAt(0, Location::RequiresFpuRegister());
1120 locations->SetInAt(1, Location::Any());
1121 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001122 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001123 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001124
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001125 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001126 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1127 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001128 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001129}
1130
1131void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1132 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001133 Location first = locations->InAt(0);
1134 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001135 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001136 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001137 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001138 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001139 __ addl(first.As<Register>(), second.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001140 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001141 __ addl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001142 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001143 __ addl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001144 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001145 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001146 }
1147
1148 case Primitive::kPrimLong: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001149 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001150 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1151 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001152 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001153 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1154 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001155 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001156 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001157 break;
1158 }
1159
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001160 case Primitive::kPrimFloat: {
1161 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001162 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001163 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001164 __ addss(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001165 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001166 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001167 }
1168
1169 case Primitive::kPrimDouble: {
1170 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001171 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001172 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001173 __ addsd(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001174 }
1175 break;
1176 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001177
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001178 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001179 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001180 }
1181}
1182
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001183void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001184 LocationSummary* locations =
1185 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001186 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001187 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001188 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001189 locations->SetInAt(0, Location::RequiresRegister());
1190 locations->SetInAt(1, Location::Any());
1191 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001192 break;
1193 }
Calin Juravle11351682014-10-23 15:38:15 +01001194 case Primitive::kPrimFloat:
1195 case Primitive::kPrimDouble: {
1196 locations->SetInAt(0, Location::RequiresFpuRegister());
1197 locations->SetInAt(1, Location::RequiresFpuRegister());
1198 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001199 break;
Calin Juravle11351682014-10-23 15:38:15 +01001200 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001201
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001202 default:
Calin Juravle11351682014-10-23 15:38:15 +01001203 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001204 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001205}
1206
1207void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
1208 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001209 Location first = locations->InAt(0);
1210 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001211 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001212 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001213 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001214 if (second.IsRegister()) {
Calin Juravle11351682014-10-23 15:38:15 +01001215 __ subl(first.As<Register>(), second.As<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001216 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001217 __ subl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001218 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001219 __ subl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001220 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001221 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001222 }
1223
1224 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001225 if (second.IsRegister()) {
Calin Juravle11351682014-10-23 15:38:15 +01001226 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1227 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001228 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001229 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001230 __ sbbl(first.AsRegisterPairHigh<Register>(),
1231 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001232 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001233 break;
1234 }
1235
Calin Juravle11351682014-10-23 15:38:15 +01001236 case Primitive::kPrimFloat: {
1237 __ subss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001238 break;
Calin Juravle11351682014-10-23 15:38:15 +01001239 }
1240
1241 case Primitive::kPrimDouble: {
1242 __ subsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1243 break;
1244 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001245
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001246 default:
Calin Juravle11351682014-10-23 15:38:15 +01001247 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001248 }
1249}
1250
Calin Juravle34bacdf2014-10-07 20:23:36 +01001251void LocationsBuilderX86::VisitMul(HMul* mul) {
1252 LocationSummary* locations =
1253 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1254 switch (mul->GetResultType()) {
1255 case Primitive::kPrimInt:
1256 locations->SetInAt(0, Location::RequiresRegister());
1257 locations->SetInAt(1, Location::Any());
1258 locations->SetOut(Location::SameAsFirstInput());
1259 break;
1260 case Primitive::kPrimLong: {
1261 locations->SetInAt(0, Location::RequiresRegister());
1262 // TODO: Currently this handles only stack operands:
1263 // - we don't have enough registers because we currently use Quick ABI.
1264 // - by the time we have a working register allocator we will probably change the ABI
1265 // and fix the above.
1266 // - we don't have a way yet to request operands on stack but the base line compiler
1267 // will leave the operands on the stack with Any().
1268 locations->SetInAt(1, Location::Any());
1269 locations->SetOut(Location::SameAsFirstInput());
1270 // Needed for imul on 32bits with 64bits output.
1271 locations->AddTemp(Location::RegisterLocation(EAX));
1272 locations->AddTemp(Location::RegisterLocation(EDX));
1273 break;
1274 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001275 case Primitive::kPrimFloat:
1276 case Primitive::kPrimDouble: {
1277 locations->SetInAt(0, Location::RequiresFpuRegister());
1278 locations->SetInAt(1, Location::RequiresFpuRegister());
1279 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001280 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001281 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001282
1283 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001284 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001285 }
1286}
1287
1288void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
1289 LocationSummary* locations = mul->GetLocations();
1290 Location first = locations->InAt(0);
1291 Location second = locations->InAt(1);
1292 DCHECK(first.Equals(locations->Out()));
1293
1294 switch (mul->GetResultType()) {
1295 case Primitive::kPrimInt: {
1296 if (second.IsRegister()) {
1297 __ imull(first.As<Register>(), second.As<Register>());
1298 } else if (second.IsConstant()) {
1299 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1300 __ imull(first.As<Register>(), imm);
1301 } else {
1302 DCHECK(second.IsStackSlot());
1303 __ imull(first.As<Register>(), Address(ESP, second.GetStackIndex()));
1304 }
1305 break;
1306 }
1307
1308 case Primitive::kPrimLong: {
1309 DCHECK(second.IsDoubleStackSlot());
1310
1311 Register in1_hi = first.AsRegisterPairHigh<Register>();
1312 Register in1_lo = first.AsRegisterPairLow<Register>();
1313 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
1314 Address in2_lo(ESP, second.GetStackIndex());
1315 Register eax = locations->GetTemp(0).As<Register>();
1316 Register edx = locations->GetTemp(1).As<Register>();
1317
1318 DCHECK_EQ(EAX, eax);
1319 DCHECK_EQ(EDX, edx);
1320
1321 // input: in1 - 64 bits, in2 - 64 bits
1322 // output: in1
1323 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
1324 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
1325 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
1326
1327 __ movl(eax, in2_hi);
1328 // eax <- in1.lo * in2.hi
1329 __ imull(eax, in1_lo);
1330 // in1.hi <- in1.hi * in2.lo
1331 __ imull(in1_hi, in2_lo);
1332 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
1333 __ addl(in1_hi, eax);
1334 // move in1_lo to eax to prepare for double precision
1335 __ movl(eax, in1_lo);
1336 // edx:eax <- in1.lo * in2.lo
1337 __ mull(in2_lo);
1338 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
1339 __ addl(in1_hi, edx);
1340 // in1.lo <- (in1.lo * in2.lo)[31:0];
1341 __ movl(in1_lo, eax);
1342
1343 break;
1344 }
1345
Calin Juravleb5bfa962014-10-21 18:02:24 +01001346 case Primitive::kPrimFloat: {
1347 __ mulss(first.As<XmmRegister>(), second.As<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001348 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001349 }
1350
1351 case Primitive::kPrimDouble: {
1352 __ mulsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1353 break;
1354 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001355
1356 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001357 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001358 }
1359}
1360
Calin Juravle7c4954d2014-10-28 16:57:40 +00001361void LocationsBuilderX86::VisitDiv(HDiv* div) {
1362 LocationSummary* locations =
1363 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1364 switch (div->GetResultType()) {
1365 case Primitive::kPrimInt:
1366 case Primitive::kPrimLong: {
1367 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1368 break;
1369 }
1370 case Primitive::kPrimFloat:
1371 case Primitive::kPrimDouble: {
1372 locations->SetInAt(0, Location::RequiresFpuRegister());
1373 locations->SetInAt(1, Location::RequiresFpuRegister());
1374 locations->SetOut(Location::SameAsFirstInput());
1375 break;
1376 }
1377
1378 default:
1379 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1380 }
1381}
1382
1383void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
1384 LocationSummary* locations = div->GetLocations();
1385 Location first = locations->InAt(0);
1386 Location second = locations->InAt(1);
1387 DCHECK(first.Equals(locations->Out()));
1388
1389 switch (div->GetResultType()) {
1390 case Primitive::kPrimInt:
1391 case Primitive::kPrimLong: {
1392 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1393 break;
1394 }
1395
1396 case Primitive::kPrimFloat: {
1397 __ divss(first.As<XmmRegister>(), second.As<XmmRegister>());
1398 break;
1399 }
1400
1401 case Primitive::kPrimDouble: {
1402 __ divsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1403 break;
1404 }
1405
1406 default:
1407 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1408 }
1409}
1410
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001411void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001412 LocationSummary* locations =
1413 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001414 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001415 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001416 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1417 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001418}
1419
1420void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
1421 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001422 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001423 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001424
Nicolas Geoffray707c8092014-04-04 10:50:14 +01001425 __ fs()->call(
1426 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001427
Nicolas Geoffray39468442014-09-02 15:17:15 +01001428 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001429 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001430}
1431
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001432void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
1433 LocationSummary* locations =
1434 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
1435 locations->SetOut(Location::RegisterLocation(EAX));
1436 InvokeRuntimeCallingConvention calling_convention;
1437 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1438 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1439 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1440}
1441
1442void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
1443 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001444 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001445 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
1446
1447 __ fs()->call(
1448 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocArrayWithAccessCheck)));
1449
1450 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
1451 DCHECK(!codegen_->IsLeafMethod());
1452}
1453
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001454void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001455 LocationSummary* locations =
1456 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001457 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1458 if (location.IsStackSlot()) {
1459 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1460 } else if (location.IsDoubleStackSlot()) {
1461 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001462 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001463 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001464}
1465
1466void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001467 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001468}
1469
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001470void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001471 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001472 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001473 locations->SetInAt(0, Location::RequiresRegister());
1474 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001475}
1476
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001477void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
1478 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01001479 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001480 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01001481 DCHECK(in.Equals(out));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001482 switch (not_->InputAt(0)->GetType()) {
1483 case Primitive::kPrimBoolean:
1484 __ xorl(out.As<Register>(), Immediate(1));
1485 break;
1486
1487 case Primitive::kPrimInt:
1488 __ notl(out.As<Register>());
1489 break;
1490
1491 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01001492 __ notl(out.AsRegisterPairLow<Register>());
1493 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001494 break;
1495
1496 default:
1497 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
1498 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001499}
1500
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001501void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001502 LocationSummary* locations =
1503 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001504 locations->SetInAt(0, Location::RequiresRegister());
1505 locations->SetInAt(1, Location::Any());
1506 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001507}
1508
1509void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
1510 Label greater, done;
1511 LocationSummary* locations = compare->GetLocations();
1512 switch (compare->InputAt(0)->GetType()) {
1513 case Primitive::kPrimLong: {
1514 Label less, greater, done;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001515 Register output = locations->Out().As<Register>();
1516 Location left = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001517 Location right = locations->InAt(1);
1518 if (right.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001519 __ cmpl(left.AsRegisterPairHigh<Register>(), right.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001520 } else {
1521 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001522 __ cmpl(left.AsRegisterPairHigh<Register>(),
1523 Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001524 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001525 __ j(kLess, &less); // Signed compare.
1526 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001527 if (right.IsRegisterPair()) {
1528 __ cmpl(left.AsRegisterPairLow<Register>(), right.AsRegisterPairLow<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001529 } else {
1530 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001531 __ cmpl(left.AsRegisterPairLow<Register>(), Address(ESP, right.GetStackIndex()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001532 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001533 __ movl(output, Immediate(0));
1534 __ j(kEqual, &done);
1535 __ j(kBelow, &less); // Unsigned compare.
1536
1537 __ Bind(&greater);
1538 __ movl(output, Immediate(1));
1539 __ jmp(&done);
1540
1541 __ Bind(&less);
1542 __ movl(output, Immediate(-1));
1543
1544 __ Bind(&done);
1545 break;
1546 }
1547 default:
1548 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
1549 }
1550}
1551
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001552void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001553 LocationSummary* locations =
1554 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001555 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1556 locations->SetInAt(i, Location::Any());
1557 }
1558 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001559}
1560
1561void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001562 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001563 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001564}
1565
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001566void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001567 LocationSummary* locations =
1568 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001569 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001570 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001571 bool is_object_type = field_type == Primitive::kPrimNot;
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001572 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
1573 || (field_type == Primitive::kPrimByte);
1574 // The register allocator does not support multiple
1575 // inputs that die at entry with one in a specific register.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001576 if (is_byte_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001577 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001578 locations->SetInAt(1, Location::RegisterLocation(EAX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001579 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001580 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001581 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001582 // Temporary registers for the write barrier.
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001583 if (is_object_type) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001584 locations->AddTemp(Location::RequiresRegister());
1585 // Ensure the card is in a byte register.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001586 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001587 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001588}
1589
1590void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1591 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001592 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001593 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001594 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001595
1596 switch (field_type) {
1597 case Primitive::kPrimBoolean:
1598 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001599 ByteRegister value = locations->InAt(1).As<ByteRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001600 __ movb(Address(obj, offset), value);
1601 break;
1602 }
1603
1604 case Primitive::kPrimShort:
1605 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001606 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001607 __ movw(Address(obj, offset), value);
1608 break;
1609 }
1610
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001611 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001612 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001613 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001614 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001615
1616 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001617 Register temp = locations->GetTemp(0).As<Register>();
1618 Register card = locations->GetTemp(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001619 codegen_->MarkGCCard(temp, card, obj, value);
1620 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001621 break;
1622 }
1623
1624 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001625 Location value = locations->InAt(1);
1626 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
1627 __ movl(Address(obj, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001628 break;
1629 }
1630
1631 case Primitive::kPrimFloat:
1632 case Primitive::kPrimDouble:
1633 LOG(FATAL) << "Unimplemented register type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001634 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001635 case Primitive::kPrimVoid:
1636 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001637 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001638 }
1639}
1640
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001641void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
1642 Label is_null;
1643 __ testl(value, value);
1644 __ j(kEqual, &is_null);
1645 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
1646 __ movl(temp, object);
1647 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
1648 __ movb(Address(temp, card, TIMES_1, 0),
1649 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
1650 __ Bind(&is_null);
1651}
1652
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001653void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001654 LocationSummary* locations =
1655 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001656 locations->SetInAt(0, Location::RequiresRegister());
1657 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001658}
1659
1660void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1661 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001662 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001663 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
1664
1665 switch (instruction->GetType()) {
1666 case Primitive::kPrimBoolean: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001667 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001668 __ movzxb(out, Address(obj, offset));
1669 break;
1670 }
1671
1672 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001673 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001674 __ movsxb(out, Address(obj, offset));
1675 break;
1676 }
1677
1678 case Primitive::kPrimShort: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001679 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001680 __ movsxw(out, Address(obj, offset));
1681 break;
1682 }
1683
1684 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001685 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001686 __ movzxw(out, Address(obj, offset));
1687 break;
1688 }
1689
1690 case Primitive::kPrimInt:
1691 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001692 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001693 __ movl(out, Address(obj, offset));
1694 break;
1695 }
1696
1697 case Primitive::kPrimLong: {
1698 // TODO: support volatile.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001699 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(obj, offset));
1700 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(obj, kX86WordSize + offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001701 break;
1702 }
1703
1704 case Primitive::kPrimFloat:
1705 case Primitive::kPrimDouble:
1706 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001707 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001708 case Primitive::kPrimVoid:
1709 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001710 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001711 }
1712}
1713
1714void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001715 LocationSummary* locations =
1716 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001717 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001718 if (instruction->HasUses()) {
1719 locations->SetOut(Location::SameAsFirstInput());
1720 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001721}
1722
1723void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001724 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001725 codegen_->AddSlowPath(slow_path);
1726
1727 LocationSummary* locations = instruction->GetLocations();
1728 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001729
1730 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001731 __ cmpl(obj.As<Register>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001732 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001733 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001734 } else {
1735 DCHECK(obj.IsConstant()) << obj;
1736 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
1737 __ jmp(slow_path->GetEntryLabel());
1738 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001739 }
1740 __ j(kEqual, slow_path->GetEntryLabel());
1741}
1742
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001743void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001744 LocationSummary* locations =
1745 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001746 locations->SetInAt(0, Location::RequiresRegister());
1747 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1748 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001749}
1750
1751void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
1752 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001753 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001754 Location index = locations->InAt(1);
1755
1756 switch (instruction->GetType()) {
1757 case Primitive::kPrimBoolean: {
1758 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001759 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001760 if (index.IsConstant()) {
1761 __ movzxb(out, Address(obj,
1762 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1763 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001764 __ movzxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001765 }
1766 break;
1767 }
1768
1769 case Primitive::kPrimByte: {
1770 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001771 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001772 if (index.IsConstant()) {
1773 __ movsxb(out, Address(obj,
1774 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1775 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001776 __ movsxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001777 }
1778 break;
1779 }
1780
1781 case Primitive::kPrimShort: {
1782 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001783 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001784 if (index.IsConstant()) {
1785 __ movsxw(out, Address(obj,
1786 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1787 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001788 __ movsxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001789 }
1790 break;
1791 }
1792
1793 case Primitive::kPrimChar: {
1794 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001795 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001796 if (index.IsConstant()) {
1797 __ movzxw(out, Address(obj,
1798 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1799 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001800 __ movzxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001801 }
1802 break;
1803 }
1804
1805 case Primitive::kPrimInt:
1806 case Primitive::kPrimNot: {
1807 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001808 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001809 if (index.IsConstant()) {
1810 __ movl(out, Address(obj,
1811 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
1812 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001813 __ movl(out, Address(obj, index.As<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001814 }
1815 break;
1816 }
1817
1818 case Primitive::kPrimLong: {
1819 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001820 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001821 if (index.IsConstant()) {
1822 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001823 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
1824 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001825 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001826 __ movl(out.AsRegisterPairLow<Register>(),
1827 Address(obj, index.As<Register>(), TIMES_8, data_offset));
1828 __ movl(out.AsRegisterPairHigh<Register>(),
1829 Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001830 }
1831 break;
1832 }
1833
1834 case Primitive::kPrimFloat:
1835 case Primitive::kPrimDouble:
1836 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001837 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001838 case Primitive::kPrimVoid:
1839 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001840 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001841 }
1842}
1843
1844void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001845 Primitive::Type value_type = instruction->GetComponentType();
1846 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1847 instruction,
1848 value_type == Primitive::kPrimNot ? LocationSummary::kCall : LocationSummary::kNoCall);
1849
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001850 if (value_type == Primitive::kPrimNot) {
1851 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001852 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1853 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1854 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001855 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001856 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
1857 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001858 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001859 // In case of a byte operation, the register allocator does not support multiple
1860 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001861 locations->SetInAt(0, Location::RequiresRegister());
1862 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001863 if (is_byte_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001864 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001865 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001866 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001867 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001868 }
1869 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001870}
1871
1872void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
1873 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001874 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001875 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001876 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001877 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001878
1879 switch (value_type) {
1880 case Primitive::kPrimBoolean:
1881 case Primitive::kPrimByte: {
1882 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001883 if (index.IsConstant()) {
1884 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001885 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001886 __ movb(Address(obj, offset), value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001887 } else {
1888 __ movb(Address(obj, offset),
1889 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1890 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001891 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001892 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001893 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
1894 value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001895 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001896 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001897 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1898 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001899 }
1900 break;
1901 }
1902
1903 case Primitive::kPrimShort:
1904 case Primitive::kPrimChar: {
1905 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001906 if (index.IsConstant()) {
1907 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001908 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001909 __ movw(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001910 } else {
1911 __ movw(Address(obj, offset),
1912 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1913 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001914 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001915 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001916 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
1917 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001918 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001919 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001920 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1921 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001922 }
1923 break;
1924 }
1925
1926 case Primitive::kPrimInt: {
1927 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001928 if (index.IsConstant()) {
1929 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001930 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001931 __ movl(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001932 } else {
1933 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1934 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001935 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001936 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001937 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
1938 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001939 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001940 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001941 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1942 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001943 }
1944 break;
1945 }
1946
1947 case Primitive::kPrimNot: {
1948 DCHECK(!codegen_->IsLeafMethod());
1949 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001950 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001951 break;
1952 }
1953
1954 case Primitive::kPrimLong: {
1955 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001956 if (index.IsConstant()) {
1957 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001958 if (value.IsRegisterPair()) {
1959 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
1960 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001961 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001962 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001963 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
1964 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
1965 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
1966 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001967 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001968 if (value.IsRegisterPair()) {
1969 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
1970 value.AsRegisterPairLow<Register>());
1971 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
1972 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001973 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001974 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001975 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001976 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001977 Immediate(Low32Bits(val)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001978 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001979 Immediate(High32Bits(val)));
1980 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001981 }
1982 break;
1983 }
1984
1985 case Primitive::kPrimFloat:
1986 case Primitive::kPrimDouble:
1987 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001988 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001989 case Primitive::kPrimVoid:
1990 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001991 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001992 }
1993}
1994
1995void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
1996 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001997 locations->SetInAt(0, Location::RequiresRegister());
1998 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001999 instruction->SetLocations(locations);
2000}
2001
2002void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
2003 LocationSummary* locations = instruction->GetLocations();
2004 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002005 Register obj = locations->InAt(0).As<Register>();
2006 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002007 __ movl(out, Address(obj, offset));
2008}
2009
2010void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002011 LocationSummary* locations =
2012 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002013 locations->SetInAt(0, Location::RequiresRegister());
2014 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002015 if (instruction->HasUses()) {
2016 locations->SetOut(Location::SameAsFirstInput());
2017 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002018}
2019
2020void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
2021 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002022 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01002023 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002024 codegen_->AddSlowPath(slow_path);
2025
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002026 Register index = locations->InAt(0).As<Register>();
2027 Register length = locations->InAt(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002028
2029 __ cmpl(index, length);
2030 __ j(kAboveEqual, slow_path->GetEntryLabel());
2031}
2032
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002033void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
2034 temp->SetLocations(nullptr);
2035}
2036
2037void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
2038 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002039 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002040}
2041
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002042void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002043 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002044 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002045}
2046
2047void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002048 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2049}
2050
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002051void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
2052 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2053}
2054
2055void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002056 HBasicBlock* block = instruction->GetBlock();
2057 if (block->GetLoopInformation() != nullptr) {
2058 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2059 // The back edge will generate the suspend check.
2060 return;
2061 }
2062 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2063 // The goto will generate the suspend check.
2064 return;
2065 }
2066 GenerateSuspendCheck(instruction, nullptr);
2067}
2068
2069void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
2070 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002071 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002072 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002073 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002074 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002075 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002076 if (successor == nullptr) {
2077 __ j(kNotEqual, slow_path->GetEntryLabel());
2078 __ Bind(slow_path->GetReturnLabel());
2079 } else {
2080 __ j(kEqual, codegen_->GetLabelOf(successor));
2081 __ jmp(slow_path->GetEntryLabel());
2082 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002083}
2084
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002085X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
2086 return codegen_->GetAssembler();
2087}
2088
2089void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
2090 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002091 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002092 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
2093 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
2094 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
2095}
2096
2097void ParallelMoveResolverX86::EmitMove(size_t index) {
2098 MoveOperands* move = moves_.Get(index);
2099 Location source = move->GetSource();
2100 Location destination = move->GetDestination();
2101
2102 if (source.IsRegister()) {
2103 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002104 __ movl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002105 } else {
2106 DCHECK(destination.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002107 __ movl(Address(ESP, destination.GetStackIndex()), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002108 }
2109 } else if (source.IsStackSlot()) {
2110 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002111 __ movl(destination.As<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002112 } else {
2113 DCHECK(destination.IsStackSlot());
2114 MoveMemoryToMemory(destination.GetStackIndex(),
2115 source.GetStackIndex());
2116 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002117 } else if (source.IsConstant()) {
2118 HIntConstant* instruction = source.GetConstant()->AsIntConstant();
2119 Immediate imm(instruction->AsIntConstant()->GetValue());
2120 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002121 __ movl(destination.As<Register>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002122 } else {
2123 __ movl(Address(ESP, destination.GetStackIndex()), imm);
2124 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002125 } else {
2126 LOG(FATAL) << "Unimplemented";
2127 }
2128}
2129
2130void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002131 Register suggested_scratch = reg == EAX ? EBX : EAX;
2132 ScratchRegisterScope ensure_scratch(
2133 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
2134
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002135 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
2136 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
2137 __ movl(Address(ESP, mem + stack_offset), reg);
2138 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
2139}
2140
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002141void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
2142 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002143 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
2144
2145 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002146 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002147 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
2148
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002149 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
2150 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
2151 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
2152 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
2153 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
2154 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
2155}
2156
2157void ParallelMoveResolverX86::EmitSwap(size_t index) {
2158 MoveOperands* move = moves_.Get(index);
2159 Location source = move->GetSource();
2160 Location destination = move->GetDestination();
2161
2162 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002163 __ xchgl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002164 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002165 Exchange(source.As<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002166 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002167 Exchange(destination.As<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002168 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
2169 Exchange(destination.GetStackIndex(), source.GetStackIndex());
2170 } else {
2171 LOG(FATAL) << "Unimplemented";
2172 }
2173}
2174
2175void ParallelMoveResolverX86::SpillScratch(int reg) {
2176 __ pushl(static_cast<Register>(reg));
2177}
2178
2179void ParallelMoveResolverX86::RestoreScratch(int reg) {
2180 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002181}
2182
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002183void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
2184 LocationSummary* locations =
2185 new (GetGraph()->GetArena()) LocationSummary(cls, LocationSummary::kNoCall);
2186 locations->SetOut(Location::RequiresRegister());
2187}
2188
2189void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
2190 Register out = cls->GetLocations()->Out().As<Register>();
2191 if (cls->IsReferrersClass()) {
2192 codegen_->LoadCurrentMethod(out);
2193 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
2194 } else {
2195 codegen_->LoadCurrentMethod(out);
2196 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
2197 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
2198 }
2199}
2200
2201void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
2202 LocationSummary* locations =
2203 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2204 locations->SetInAt(0, Location::RequiresRegister());
2205 if (check->HasUses()) {
2206 locations->SetOut(Location::SameAsFirstInput());
2207 }
2208}
2209
2210void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
2211 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) ClinitCheckSlowPathX86(check);
2212 codegen_->AddSlowPath(slow_path);
2213
2214 LocationSummary* locations = check->GetLocations();
2215 // We remove the class as a live register, we know it's null or unused in the slow path.
2216 RegisterSet* register_set = locations->GetLiveRegisters();
2217 register_set->Remove(locations->InAt(0));
2218
2219 Register class_reg = locations->InAt(0).As<Register>();
2220 __ testl(class_reg, class_reg);
2221 __ j(kEqual, slow_path->GetEntryLabel());
2222 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
2223 Immediate(mirror::Class::kStatusInitialized));
2224 __ j(kLess, slow_path->GetEntryLabel());
2225 __ Bind(slow_path->GetExitLabel());
2226 // No need for memory fence, thanks to the X86 memory model.
2227}
2228
2229void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2230 LocationSummary* locations =
2231 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2232 locations->SetInAt(0, Location::RequiresRegister());
2233 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2234}
2235
2236void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2237 LocationSummary* locations = instruction->GetLocations();
2238 Register cls = locations->InAt(0).As<Register>();
2239 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2240
2241 switch (instruction->GetType()) {
2242 case Primitive::kPrimBoolean: {
2243 Register out = locations->Out().As<Register>();
2244 __ movzxb(out, Address(cls, offset));
2245 break;
2246 }
2247
2248 case Primitive::kPrimByte: {
2249 Register out = locations->Out().As<Register>();
2250 __ movsxb(out, Address(cls, offset));
2251 break;
2252 }
2253
2254 case Primitive::kPrimShort: {
2255 Register out = locations->Out().As<Register>();
2256 __ movsxw(out, Address(cls, offset));
2257 break;
2258 }
2259
2260 case Primitive::kPrimChar: {
2261 Register out = locations->Out().As<Register>();
2262 __ movzxw(out, Address(cls, offset));
2263 break;
2264 }
2265
2266 case Primitive::kPrimInt:
2267 case Primitive::kPrimNot: {
2268 Register out = locations->Out().As<Register>();
2269 __ movl(out, Address(cls, offset));
2270 break;
2271 }
2272
2273 case Primitive::kPrimLong: {
2274 // TODO: support volatile.
2275 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(cls, offset));
2276 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(cls, kX86WordSize + offset));
2277 break;
2278 }
2279
2280 case Primitive::kPrimFloat:
2281 case Primitive::kPrimDouble:
2282 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
2283 UNREACHABLE();
2284 case Primitive::kPrimVoid:
2285 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2286 UNREACHABLE();
2287 }
2288}
2289
2290void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2291 LocationSummary* locations =
2292 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2293 locations->SetInAt(0, Location::RequiresRegister());
2294 Primitive::Type field_type = instruction->GetFieldType();
2295 bool is_object_type = field_type == Primitive::kPrimNot;
2296 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
2297 || (field_type == Primitive::kPrimByte);
2298 // The register allocator does not support multiple
2299 // inputs that die at entry with one in a specific register.
2300 if (is_byte_type) {
2301 // Ensure the value is in a byte register.
2302 locations->SetInAt(1, Location::RegisterLocation(EAX));
2303 } else {
2304 locations->SetInAt(1, Location::RequiresRegister());
2305 }
2306 // Temporary registers for the write barrier.
2307 if (is_object_type) {
2308 locations->AddTemp(Location::RequiresRegister());
2309 // Ensure the card is in a byte register.
2310 locations->AddTemp(Location::RegisterLocation(ECX));
2311 }
2312}
2313
2314void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2315 LocationSummary* locations = instruction->GetLocations();
2316 Register cls = locations->InAt(0).As<Register>();
2317 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2318 Primitive::Type field_type = instruction->GetFieldType();
2319
2320 switch (field_type) {
2321 case Primitive::kPrimBoolean:
2322 case Primitive::kPrimByte: {
2323 ByteRegister value = locations->InAt(1).As<ByteRegister>();
2324 __ movb(Address(cls, offset), value);
2325 break;
2326 }
2327
2328 case Primitive::kPrimShort:
2329 case Primitive::kPrimChar: {
2330 Register value = locations->InAt(1).As<Register>();
2331 __ movw(Address(cls, offset), value);
2332 break;
2333 }
2334
2335 case Primitive::kPrimInt:
2336 case Primitive::kPrimNot: {
2337 Register value = locations->InAt(1).As<Register>();
2338 __ movl(Address(cls, offset), value);
2339
2340 if (field_type == Primitive::kPrimNot) {
2341 Register temp = locations->GetTemp(0).As<Register>();
2342 Register card = locations->GetTemp(1).As<Register>();
2343 codegen_->MarkGCCard(temp, card, cls, value);
2344 }
2345 break;
2346 }
2347
2348 case Primitive::kPrimLong: {
2349 Location value = locations->InAt(1);
2350 __ movl(Address(cls, offset), value.AsRegisterPairLow<Register>());
2351 __ movl(Address(cls, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
2352 break;
2353 }
2354
2355 case Primitive::kPrimFloat:
2356 case Primitive::kPrimDouble:
2357 LOG(FATAL) << "Unimplemented register type " << field_type;
2358 UNREACHABLE();
2359 case Primitive::kPrimVoid:
2360 LOG(FATAL) << "Unreachable type " << field_type;
2361 UNREACHABLE();
2362 }
2363}
2364
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002365void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
2366 LocationSummary* locations =
2367 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
2368 locations->SetOut(Location::RequiresRegister());
2369}
2370
2371void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
2372 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
2373 codegen_->AddSlowPath(slow_path);
2374
2375 Register out = load->GetLocations()->Out().As<Register>();
2376 codegen_->LoadCurrentMethod(out);
2377 __ movl(out, Address(out, mirror::ArtMethod::DexCacheStringsOffset().Int32Value()));
2378 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
2379 __ testl(out, out);
2380 __ j(kEqual, slow_path->GetEntryLabel());
2381 __ Bind(slow_path->GetExitLabel());
2382}
2383
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002384} // namespace x86
2385} // namespace art