blob: 1e37909be9d66996e0a00c59151fb7f4b58798ea [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 Geoffrayb5f62b32014-10-30 10:58:41 +0000160class LoadStringSlowPathX86 : public SlowPathCodeX86 {
161 public:
162 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
163
164 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
165 LocationSummary* locations = instruction_->GetLocations();
166 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
167
168 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
169 __ Bind(GetEntryLabel());
170 codegen->SaveLiveRegisters(locations);
171
172 InvokeRuntimeCallingConvention calling_convention;
173 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(0));
174 __ movl(calling_convention.GetRegisterAt(1), Immediate(instruction_->GetStringIndex()));
175 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pResolveString)));
176 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
177 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
178 codegen->RestoreLiveRegisters(locations);
179
180 __ jmp(GetExitLabel());
181 }
182
183 private:
184 HLoadString* const instruction_;
185
186 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
187};
188
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000189class LoadClassSlowPathX86 : public SlowPathCodeX86 {
190 public:
191 LoadClassSlowPathX86(HLoadClass* cls,
192 HInstruction* at,
193 uint32_t dex_pc,
194 bool do_clinit)
195 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
196 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
197 }
198
199 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
200 LocationSummary* locations = at_->GetLocations();
201 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
202 __ Bind(GetEntryLabel());
203 codegen->SaveLiveRegisters(locations);
204
205 InvokeRuntimeCallingConvention calling_convention;
206 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
207 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
208 __ fs()->call(Address::Absolute(do_clinit_
209 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeStaticStorage)
210 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeType)));
211 codegen->RecordPcInfo(at_, dex_pc_);
212
213 // Move the class to the desired location.
214 if (locations->Out().IsValid()) {
215 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
216 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
217 }
218 codegen->RestoreLiveRegisters(locations);
219 __ jmp(GetExitLabel());
220 }
221
222 private:
223 // The class this slow path will load.
224 HLoadClass* const cls_;
225
226 // The instruction where this slow path is happening.
227 // (Might be the load class or an initialization check).
228 HInstruction* const at_;
229
230 // The dex PC of `at_`.
231 const uint32_t dex_pc_;
232
233 // Whether to initialize the class.
234 const bool do_clinit_;
235
236 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
237};
238
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100239#undef __
240#define __ reinterpret_cast<X86Assembler*>(GetAssembler())->
241
Dave Allison20dfc792014-06-16 20:44:29 -0700242inline Condition X86Condition(IfCondition cond) {
243 switch (cond) {
244 case kCondEQ: return kEqual;
245 case kCondNE: return kNotEqual;
246 case kCondLT: return kLess;
247 case kCondLE: return kLessEqual;
248 case kCondGT: return kGreater;
249 case kCondGE: return kGreaterEqual;
250 default:
251 LOG(FATAL) << "Unknown if condition";
252 }
253 return kEqual;
254}
255
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100256void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
257 stream << X86ManagedRegister::FromCpuRegister(Register(reg));
258}
259
260void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
261 stream << X86ManagedRegister::FromXmmRegister(XmmRegister(reg));
262}
263
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100264size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
265 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
266 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100267}
268
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100269size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
270 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
271 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100272}
273
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100274CodeGeneratorX86::CodeGeneratorX86(HGraph* graph)
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100275 : CodeGenerator(graph, kNumberOfCpuRegisters, kNumberOfXmmRegisters, kNumberOfRegisterPairs),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100276 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100277 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100278 instruction_visitor_(graph, this),
279 move_resolver_(graph->GetArena(), this) {}
280
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100281size_t CodeGeneratorX86::FrameEntrySpillSize() const {
282 return kNumberOfPushedRegistersAtEntry * kX86WordSize;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100283}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100284
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100285Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100286 switch (type) {
287 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100288 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100289 X86ManagedRegister pair =
290 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100291 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
292 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100293 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
294 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100295 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100296 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100297 }
298
299 case Primitive::kPrimByte:
300 case Primitive::kPrimBoolean:
301 case Primitive::kPrimChar:
302 case Primitive::kPrimShort:
303 case Primitive::kPrimInt:
304 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100305 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100306 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100307 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100308 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
309 X86ManagedRegister current =
310 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
311 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100312 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100313 }
314 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100315 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100316 }
317
318 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100319 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100320 return Location::FpuRegisterLocation(
321 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100322 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100323
324 case Primitive::kPrimVoid:
325 LOG(FATAL) << "Unreachable type " << type;
326 }
327
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100328 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100329}
330
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100331void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100332 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100333 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100334
335 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100336 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100337
338 // TODO: We currently don't use Quick's callee saved registers.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100339 blocked_core_registers_[EBP] = true;
340 blocked_core_registers_[ESI] = true;
341 blocked_core_registers_[EDI] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100342
343 UpdateBlockedPairRegisters();
344}
345
346void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
347 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
348 X86ManagedRegister current =
349 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
350 if (blocked_core_registers_[current.AsRegisterPairLow()]
351 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
352 blocked_register_pairs_[i] = true;
353 }
354 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100355}
356
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100357InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
358 : HGraphVisitor(graph),
359 assembler_(codegen->GetAssembler()),
360 codegen_(codegen) {}
361
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000362void CodeGeneratorX86::GenerateFrameEntry() {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000363 // Create a fake register to mimic Quick.
364 static const int kFakeReturnRegister = 8;
365 core_spill_mask_ |= (1 << kFakeReturnRegister);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000366
Dave Allison648d7112014-07-25 16:15:27 -0700367 bool skip_overflow_check = IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100368 if (!skip_overflow_check && !kExplicitStackOverflowCheck) {
369 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100370 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100371 }
372
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100373 // The return PC has already been pushed on the stack.
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100374 __ subl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100375
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100376 if (!skip_overflow_check && kExplicitStackOverflowCheck) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100377 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathX86();
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100378 AddSlowPath(slow_path);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100379
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100380 __ fs()->cmpl(ESP, Address::Absolute(Thread::StackEndOffset<kX86WordSize>()));
381 __ j(kLess, slow_path->GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100382 }
383
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100384 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000385}
386
387void CodeGeneratorX86::GenerateFrameExit() {
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100388 __ addl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000389}
390
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100391void CodeGeneratorX86::Bind(HBasicBlock* block) {
392 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000393}
394
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100395void CodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100396 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000397}
398
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100399Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
400 switch (load->GetType()) {
401 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100402 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100403 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
404 break;
405
406 case Primitive::kPrimInt:
407 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100408 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100409 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100410
411 case Primitive::kPrimBoolean:
412 case Primitive::kPrimByte:
413 case Primitive::kPrimChar:
414 case Primitive::kPrimShort:
415 case Primitive::kPrimVoid:
416 LOG(FATAL) << "Unexpected type " << load->GetType();
417 }
418
419 LOG(FATAL) << "Unreachable";
420 return Location();
421}
422
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100423Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
424 switch (type) {
425 case Primitive::kPrimBoolean:
426 case Primitive::kPrimByte:
427 case Primitive::kPrimChar:
428 case Primitive::kPrimShort:
429 case Primitive::kPrimInt:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100430 case Primitive::kPrimFloat:
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100431 case Primitive::kPrimNot: {
432 uint32_t index = gp_index_++;
433 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100434 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100435 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100436 return Location::StackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100437 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100438 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100439
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100440 case Primitive::kPrimLong:
441 case Primitive::kPrimDouble: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100442 uint32_t index = gp_index_;
443 gp_index_ += 2;
444 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100445 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
446 calling_convention.GetRegisterPairAt(index));
447 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100448 } else if (index + 1 == calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000449 // On X86, the register index and stack index of a quick parameter is the same, since
450 // we are passing floating pointer values in core registers.
451 return Location::QuickParameter(index, index);
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100452 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100453 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100454 }
455 }
456
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100457 case Primitive::kPrimVoid:
458 LOG(FATAL) << "Unexpected parameter type " << type;
459 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100460 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100461 return Location();
462}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100463
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100464void CodeGeneratorX86::Move32(Location destination, Location source) {
465 if (source.Equals(destination)) {
466 return;
467 }
468 if (destination.IsRegister()) {
469 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100470 __ movl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100471 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100472 __ movd(destination.As<Register>(), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100473 } else {
474 DCHECK(source.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100475 __ movl(destination.As<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100476 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100477 } else if (destination.IsFpuRegister()) {
478 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100479 __ movd(destination.As<XmmRegister>(), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100480 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100481 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100482 } else {
483 DCHECK(source.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100484 __ movss(destination.As<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100485 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100486 } else {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100487 DCHECK(destination.IsStackSlot());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100488 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100489 __ movl(Address(ESP, destination.GetStackIndex()), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100490 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100491 __ movss(Address(ESP, destination.GetStackIndex()), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100492 } else {
493 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100494 __ pushl(Address(ESP, source.GetStackIndex()));
495 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100496 }
497 }
498}
499
500void CodeGeneratorX86::Move64(Location destination, Location source) {
501 if (source.Equals(destination)) {
502 return;
503 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100504 if (destination.IsRegisterPair()) {
505 if (source.IsRegisterPair()) {
506 __ movl(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
507 __ movl(destination.AsRegisterPairHigh<Register>(), 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 if (source.IsQuickParameter()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000511 uint16_t register_index = source.GetQuickParameterRegisterIndex();
512 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100513 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100514 __ movl(destination.AsRegisterPairLow<Register>(),
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000515 calling_convention.GetRegisterAt(register_index));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100516 __ movl(destination.AsRegisterPairHigh<Register>(), Address(ESP,
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000517 calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100518 } else {
519 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100520 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
521 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100522 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
523 }
524 } else if (destination.IsQuickParameter()) {
525 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000526 uint16_t register_index = destination.GetQuickParameterRegisterIndex();
527 uint16_t stack_index = destination.GetQuickParameterStackIndex();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100528 if (source.IsRegister()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000529 __ movl(calling_convention.GetRegisterAt(register_index), source.AsRegisterPairLow<Register>());
530 __ movl(Address(ESP, calling_convention.GetStackOffsetOf(stack_index + 1)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100531 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100532 } else if (source.IsFpuRegister()) {
533 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100534 } else {
535 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000536 __ movl(calling_convention.GetRegisterAt(register_index),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100537 Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100538 __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000539 __ popl(Address(ESP, calling_convention.GetStackOffsetOf(stack_index + 1)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100540 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100541 } else if (destination.IsFpuRegister()) {
542 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100543 __ movsd(destination.As<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100544 } else {
545 LOG(FATAL) << "Unimplemented";
546 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100547 } else {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100548 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100549 if (source.IsRegisterPair()) {
550 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100551 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100552 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100553 } else if (source.IsQuickParameter()) {
554 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000555 uint16_t register_index = source.GetQuickParameterRegisterIndex();
556 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100557 __ movl(Address(ESP, destination.GetStackIndex()),
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000558 calling_convention.GetRegisterAt(register_index));
559 DCHECK_EQ(calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100560 static_cast<size_t>(destination.GetHighStackIndex(kX86WordSize)));
561 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100562 __ movsd(Address(ESP, destination.GetStackIndex()), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100563 } else {
564 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100565 __ pushl(Address(ESP, source.GetStackIndex()));
566 __ popl(Address(ESP, destination.GetStackIndex()));
567 __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize)));
568 __ popl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100569 }
570 }
571}
572
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100573void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Roland Levillain476df552014-10-09 17:51:36 +0100574 if (instruction->IsIntConstant()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100575 Immediate imm(instruction->AsIntConstant()->GetValue());
576 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100577 __ movl(location.As<Register>(), imm);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100578 } else if (location.IsStackSlot()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100579 __ movl(Address(ESP, location.GetStackIndex()), imm);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100580 } else {
581 DCHECK(location.IsConstant());
582 DCHECK_EQ(location.GetConstant(), instruction);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100583 }
Roland Levillain476df552014-10-09 17:51:36 +0100584 } else if (instruction->IsLongConstant()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100585 int64_t value = instruction->AsLongConstant()->GetValue();
586 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100587 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
588 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100589 } else if (location.IsDoubleStackSlot()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100590 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
591 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100592 } else {
593 DCHECK(location.IsConstant());
594 DCHECK_EQ(location.GetConstant(), instruction);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100595 }
Roland Levillain476df552014-10-09 17:51:36 +0100596 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100597 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100598 switch (instruction->GetType()) {
599 case Primitive::kPrimBoolean:
600 case Primitive::kPrimByte:
601 case Primitive::kPrimChar:
602 case Primitive::kPrimShort:
603 case Primitive::kPrimInt:
604 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100605 case Primitive::kPrimFloat:
606 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100607 break;
608
609 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100610 case Primitive::kPrimDouble:
611 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100612 break;
613
614 default:
615 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
616 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000617 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100618 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100619 switch (instruction->GetType()) {
620 case Primitive::kPrimBoolean:
621 case Primitive::kPrimByte:
622 case Primitive::kPrimChar:
623 case Primitive::kPrimShort:
624 case Primitive::kPrimInt:
625 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100626 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100627 Move32(location, instruction->GetLocations()->Out());
628 break;
629
630 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100631 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100632 Move64(location, instruction->GetLocations()->Out());
633 break;
634
635 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100636 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100637 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000638 }
639}
640
641void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000642 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000643}
644
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000645void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000646 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100647 DCHECK(!successor->IsExitBlock());
648
649 HBasicBlock* block = got->GetBlock();
650 HInstruction* previous = got->GetPrevious();
651
652 HLoopInformation* info = block->GetLoopInformation();
653 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
654 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
655 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
656 return;
657 }
658
659 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
660 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
661 }
662 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000663 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000664 }
665}
666
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000667void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000668 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000669}
670
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000671void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700672 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000673 if (kIsDebugBuild) {
674 __ Comment("Unreachable");
675 __ int3();
676 }
677}
678
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000679void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100680 LocationSummary* locations =
681 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100682 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100683 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100684 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100685 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000686}
687
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000688void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700689 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100690 if (cond->IsIntConstant()) {
691 // Constant condition, statically compared against 1.
692 int32_t cond_value = cond->AsIntConstant()->GetValue();
693 if (cond_value == 1) {
694 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
695 if_instr->IfTrueSuccessor())) {
696 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100697 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100698 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100699 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100700 DCHECK_EQ(cond_value, 0);
701 }
702 } else {
703 bool materialized =
704 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
705 // Moves do not affect the eflags register, so if the condition is
706 // evaluated just before the if, we don't need to evaluate it
707 // again.
708 bool eflags_set = cond->IsCondition()
709 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
710 if (materialized) {
711 if (!eflags_set) {
712 // Materialized condition, compare against 0.
713 Location lhs = if_instr->GetLocations()->InAt(0);
714 if (lhs.IsRegister()) {
715 __ cmpl(lhs.As<Register>(), Immediate(0));
716 } else {
717 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
718 }
719 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
720 } else {
721 __ j(X86Condition(cond->AsCondition()->GetCondition()),
722 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
723 }
724 } else {
725 Location lhs = cond->GetLocations()->InAt(0);
726 Location rhs = cond->GetLocations()->InAt(1);
727 // LHS is guaranteed to be in a register (see
728 // LocationsBuilderX86::VisitCondition).
729 if (rhs.IsRegister()) {
730 __ cmpl(lhs.As<Register>(), rhs.As<Register>());
731 } else if (rhs.IsConstant()) {
732 HIntConstant* instruction = rhs.GetConstant()->AsIntConstant();
733 Immediate imm(instruction->AsIntConstant()->GetValue());
734 __ cmpl(lhs.As<Register>(), imm);
735 } else {
736 __ cmpl(lhs.As<Register>(), Address(ESP, rhs.GetStackIndex()));
737 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100738 __ j(X86Condition(cond->AsCondition()->GetCondition()),
739 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700740 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100741 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100742 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
743 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700744 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000745 }
746}
747
748void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000749 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000750}
751
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000752void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
753 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000754}
755
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000756void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100757 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000758}
759
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000760void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100761 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700762 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000763}
764
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100765void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100766 LocationSummary* locations =
767 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100768 switch (store->InputAt(1)->GetType()) {
769 case Primitive::kPrimBoolean:
770 case Primitive::kPrimByte:
771 case Primitive::kPrimChar:
772 case Primitive::kPrimShort:
773 case Primitive::kPrimInt:
774 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100775 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100776 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
777 break;
778
779 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100780 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100781 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
782 break;
783
784 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100785 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100786 }
787 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000788}
789
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000790void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700791 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000792}
793
Dave Allison20dfc792014-06-16 20:44:29 -0700794void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100795 LocationSummary* locations =
796 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100797 locations->SetInAt(0, Location::RequiresRegister());
798 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100799 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100800 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100801 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000802}
803
Dave Allison20dfc792014-06-16 20:44:29 -0700804void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
805 if (comp->NeedsMaterialization()) {
806 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100807 Register reg = locations->Out().As<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100808 // Clear register: setcc only sets the low byte.
809 __ xorl(reg, reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700810 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100811 __ cmpl(locations->InAt(0).As<Register>(),
812 locations->InAt(1).As<Register>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100813 } else if (locations->InAt(1).IsConstant()) {
814 HConstant* instruction = locations->InAt(1).GetConstant();
815 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100816 __ cmpl(locations->InAt(0).As<Register>(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700817 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100818 __ cmpl(locations->InAt(0).As<Register>(),
Dave Allison20dfc792014-06-16 20:44:29 -0700819 Address(ESP, locations->InAt(1).GetStackIndex()));
820 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100821 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100822 }
Dave Allison20dfc792014-06-16 20:44:29 -0700823}
824
825void LocationsBuilderX86::VisitEqual(HEqual* comp) {
826 VisitCondition(comp);
827}
828
829void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
830 VisitCondition(comp);
831}
832
833void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
834 VisitCondition(comp);
835}
836
837void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
838 VisitCondition(comp);
839}
840
841void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
842 VisitCondition(comp);
843}
844
845void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
846 VisitCondition(comp);
847}
848
849void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
850 VisitCondition(comp);
851}
852
853void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
854 VisitCondition(comp);
855}
856
857void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
858 VisitCondition(comp);
859}
860
861void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
862 VisitCondition(comp);
863}
864
865void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
866 VisitCondition(comp);
867}
868
869void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
870 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000871}
872
873void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100874 LocationSummary* locations =
875 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100876 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000877}
878
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000879void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100880 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700881 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000882}
883
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100884void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100885 LocationSummary* locations =
886 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100887 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100888}
889
890void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
891 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700892 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100893}
894
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100895void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
896 LocationSummary* locations =
897 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
898 locations->SetOut(Location::ConstantLocation(constant));
899}
900
901void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
902 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700903 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100904}
905
906void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
907 LocationSummary* locations =
908 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
909 locations->SetOut(Location::ConstantLocation(constant));
910}
911
912void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
913 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700914 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100915}
916
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000917void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000918 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000919}
920
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000921void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700922 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000923 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000924 __ ret();
925}
926
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000927void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100928 LocationSummary* locations =
929 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100930 switch (ret->InputAt(0)->GetType()) {
931 case Primitive::kPrimBoolean:
932 case Primitive::kPrimByte:
933 case Primitive::kPrimChar:
934 case Primitive::kPrimShort:
935 case Primitive::kPrimInt:
936 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100937 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100938 break;
939
940 case Primitive::kPrimLong:
941 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100942 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100943 break;
944
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100945 case Primitive::kPrimFloat:
946 case Primitive::kPrimDouble:
947 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100948 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100949 break;
950
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100951 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100952 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100953 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000954}
955
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000956void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100957 if (kIsDebugBuild) {
958 switch (ret->InputAt(0)->GetType()) {
959 case Primitive::kPrimBoolean:
960 case Primitive::kPrimByte:
961 case Primitive::kPrimChar:
962 case Primitive::kPrimShort:
963 case Primitive::kPrimInt:
964 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100965 DCHECK_EQ(ret->GetLocations()->InAt(0).As<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100966 break;
967
968 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100969 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
970 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100971 break;
972
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100973 case Primitive::kPrimFloat:
974 case Primitive::kPrimDouble:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100975 DCHECK_EQ(ret->GetLocations()->InAt(0).As<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100976 break;
977
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100978 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100979 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100980 }
981 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000982 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000983 __ ret();
984}
985
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000986void LocationsBuilderX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100987 HandleInvoke(invoke);
988}
989
990void InstructionCodeGeneratorX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100991 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100992
993 // TODO: Implement all kinds of calls:
994 // 1) boot -> boot
995 // 2) app -> boot
996 // 3) app -> app
997 //
998 // Currently we implement the app -> app logic, which looks up in the resolve cache.
999
1000 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001001 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001002 // temp = temp->dex_cache_resolved_methods_;
1003 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
1004 // temp = temp[index_in_cache]
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001005 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetIndexInDexCache())));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001006 // (temp + offset_of_quick_compiled_code)()
1007 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
1008
1009 DCHECK(!codegen_->IsLeafMethod());
1010 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1011}
1012
1013void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1014 HandleInvoke(invoke);
1015}
1016
1017void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001018 LocationSummary* locations =
1019 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001020 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001021
1022 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001023 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001024 HInstruction* input = invoke->InputAt(i);
1025 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1026 }
1027
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001028 switch (invoke->GetType()) {
1029 case Primitive::kPrimBoolean:
1030 case Primitive::kPrimByte:
1031 case Primitive::kPrimChar:
1032 case Primitive::kPrimShort:
1033 case Primitive::kPrimInt:
1034 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001035 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001036 break;
1037
1038 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001039 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001040 break;
1041
1042 case Primitive::kPrimVoid:
1043 break;
1044
1045 case Primitive::kPrimDouble:
1046 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001047 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001048 break;
1049 }
1050
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001051 invoke->SetLocations(locations);
1052}
1053
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001054void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001055 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001056 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1057 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1058 LocationSummary* locations = invoke->GetLocations();
1059 Location receiver = locations->InAt(0);
1060 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1061 // temp = object->GetClass();
1062 if (receiver.IsStackSlot()) {
1063 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1064 __ movl(temp, Address(temp, class_offset));
1065 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001066 __ movl(temp, Address(receiver.As<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001067 }
1068 // temp = temp->GetMethodAt(method_offset);
1069 __ movl(temp, Address(temp, method_offset));
1070 // call temp->GetEntryPoint();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001071 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
1072
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001073 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001074 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001075}
1076
Roland Levillain88cb1752014-10-20 16:36:47 +01001077void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1078 LocationSummary* locations =
1079 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1080 switch (neg->GetResultType()) {
1081 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001082 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001083 locations->SetInAt(0, Location::RequiresRegister());
1084 locations->SetOut(Location::SameAsFirstInput());
1085 break;
1086
Roland Levillain88cb1752014-10-20 16:36:47 +01001087 case Primitive::kPrimFloat:
1088 case Primitive::kPrimDouble:
1089 LOG(FATAL) << "Not yet implemented neg type " << neg->GetResultType();
1090 break;
1091
1092 default:
1093 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1094 }
1095}
1096
1097void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1098 LocationSummary* locations = neg->GetLocations();
1099 Location out = locations->Out();
1100 Location in = locations->InAt(0);
1101 switch (neg->GetResultType()) {
1102 case Primitive::kPrimInt:
1103 DCHECK(in.IsRegister());
1104 __ negl(out.As<Register>());
1105 break;
1106
1107 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001108 DCHECK(in.IsRegisterPair());
1109 __ negl(out.AsRegisterPairLow<Register>());
1110 // Negation is similar to subtraction from zero. The least
1111 // significant byte triggers a borrow when it is different from
1112 // zero; to take it into account, add 1 to the most significant
1113 // byte if the carry flag (CF) is set to 1 after the first NEGL
1114 // operation.
1115 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1116 __ negl(out.AsRegisterPairHigh<Register>());
1117 break;
1118
Roland Levillain88cb1752014-10-20 16:36:47 +01001119 case Primitive::kPrimFloat:
1120 case Primitive::kPrimDouble:
1121 LOG(FATAL) << "Not yet implemented neg type " << neg->GetResultType();
1122 break;
1123
1124 default:
1125 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1126 }
1127}
1128
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001129void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001130 LocationSummary* locations =
1131 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001132 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001133 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001134 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001135 locations->SetInAt(0, Location::RequiresRegister());
1136 locations->SetInAt(1, Location::Any());
1137 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001138 break;
1139 }
1140
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001141 case Primitive::kPrimFloat:
1142 case Primitive::kPrimDouble: {
1143 locations->SetInAt(0, Location::RequiresFpuRegister());
1144 locations->SetInAt(1, Location::Any());
1145 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001146 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001147 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001148
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001149 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001150 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1151 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001152 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001153}
1154
1155void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1156 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001157 Location first = locations->InAt(0);
1158 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001159 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001160 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001161 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001162 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001163 __ addl(first.As<Register>(), second.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001164 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001165 __ addl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001166 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001167 __ addl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001168 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001169 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001170 }
1171
1172 case Primitive::kPrimLong: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001173 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001174 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1175 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001176 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001177 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1178 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001179 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001180 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001181 break;
1182 }
1183
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001184 case Primitive::kPrimFloat: {
1185 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001186 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001187 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001188 __ addss(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001189 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001190 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001191 }
1192
1193 case Primitive::kPrimDouble: {
1194 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001195 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001196 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001197 __ addsd(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001198 }
1199 break;
1200 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001201
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001202 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001203 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001204 }
1205}
1206
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001207void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001208 LocationSummary* locations =
1209 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001210 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001211 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001212 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001213 locations->SetInAt(0, Location::RequiresRegister());
1214 locations->SetInAt(1, Location::Any());
1215 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001216 break;
1217 }
Calin Juravle11351682014-10-23 15:38:15 +01001218 case Primitive::kPrimFloat:
1219 case Primitive::kPrimDouble: {
1220 locations->SetInAt(0, Location::RequiresFpuRegister());
1221 locations->SetInAt(1, Location::RequiresFpuRegister());
1222 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001223 break;
Calin Juravle11351682014-10-23 15:38:15 +01001224 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001225
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001226 default:
Calin Juravle11351682014-10-23 15:38:15 +01001227 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001228 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001229}
1230
1231void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
1232 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001233 Location first = locations->InAt(0);
1234 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001235 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001236 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001237 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001238 if (second.IsRegister()) {
Calin Juravle11351682014-10-23 15:38:15 +01001239 __ subl(first.As<Register>(), second.As<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001240 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001241 __ subl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001242 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001243 __ subl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001244 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001245 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001246 }
1247
1248 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001249 if (second.IsRegister()) {
Calin Juravle11351682014-10-23 15:38:15 +01001250 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1251 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001252 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001253 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001254 __ sbbl(first.AsRegisterPairHigh<Register>(),
1255 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001256 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001257 break;
1258 }
1259
Calin Juravle11351682014-10-23 15:38:15 +01001260 case Primitive::kPrimFloat: {
1261 __ subss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001262 break;
Calin Juravle11351682014-10-23 15:38:15 +01001263 }
1264
1265 case Primitive::kPrimDouble: {
1266 __ subsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1267 break;
1268 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001269
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001270 default:
Calin Juravle11351682014-10-23 15:38:15 +01001271 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001272 }
1273}
1274
Calin Juravle34bacdf2014-10-07 20:23:36 +01001275void LocationsBuilderX86::VisitMul(HMul* mul) {
1276 LocationSummary* locations =
1277 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1278 switch (mul->GetResultType()) {
1279 case Primitive::kPrimInt:
1280 locations->SetInAt(0, Location::RequiresRegister());
1281 locations->SetInAt(1, Location::Any());
1282 locations->SetOut(Location::SameAsFirstInput());
1283 break;
1284 case Primitive::kPrimLong: {
1285 locations->SetInAt(0, Location::RequiresRegister());
1286 // TODO: Currently this handles only stack operands:
1287 // - we don't have enough registers because we currently use Quick ABI.
1288 // - by the time we have a working register allocator we will probably change the ABI
1289 // and fix the above.
1290 // - we don't have a way yet to request operands on stack but the base line compiler
1291 // will leave the operands on the stack with Any().
1292 locations->SetInAt(1, Location::Any());
1293 locations->SetOut(Location::SameAsFirstInput());
1294 // Needed for imul on 32bits with 64bits output.
1295 locations->AddTemp(Location::RegisterLocation(EAX));
1296 locations->AddTemp(Location::RegisterLocation(EDX));
1297 break;
1298 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001299 case Primitive::kPrimFloat:
1300 case Primitive::kPrimDouble: {
1301 locations->SetInAt(0, Location::RequiresFpuRegister());
1302 locations->SetInAt(1, Location::RequiresFpuRegister());
1303 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001304 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001305 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001306
1307 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001308 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001309 }
1310}
1311
1312void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
1313 LocationSummary* locations = mul->GetLocations();
1314 Location first = locations->InAt(0);
1315 Location second = locations->InAt(1);
1316 DCHECK(first.Equals(locations->Out()));
1317
1318 switch (mul->GetResultType()) {
1319 case Primitive::kPrimInt: {
1320 if (second.IsRegister()) {
1321 __ imull(first.As<Register>(), second.As<Register>());
1322 } else if (second.IsConstant()) {
1323 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1324 __ imull(first.As<Register>(), imm);
1325 } else {
1326 DCHECK(second.IsStackSlot());
1327 __ imull(first.As<Register>(), Address(ESP, second.GetStackIndex()));
1328 }
1329 break;
1330 }
1331
1332 case Primitive::kPrimLong: {
1333 DCHECK(second.IsDoubleStackSlot());
1334
1335 Register in1_hi = first.AsRegisterPairHigh<Register>();
1336 Register in1_lo = first.AsRegisterPairLow<Register>();
1337 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
1338 Address in2_lo(ESP, second.GetStackIndex());
1339 Register eax = locations->GetTemp(0).As<Register>();
1340 Register edx = locations->GetTemp(1).As<Register>();
1341
1342 DCHECK_EQ(EAX, eax);
1343 DCHECK_EQ(EDX, edx);
1344
1345 // input: in1 - 64 bits, in2 - 64 bits
1346 // output: in1
1347 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
1348 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
1349 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
1350
1351 __ movl(eax, in2_hi);
1352 // eax <- in1.lo * in2.hi
1353 __ imull(eax, in1_lo);
1354 // in1.hi <- in1.hi * in2.lo
1355 __ imull(in1_hi, in2_lo);
1356 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
1357 __ addl(in1_hi, eax);
1358 // move in1_lo to eax to prepare for double precision
1359 __ movl(eax, in1_lo);
1360 // edx:eax <- in1.lo * in2.lo
1361 __ mull(in2_lo);
1362 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
1363 __ addl(in1_hi, edx);
1364 // in1.lo <- (in1.lo * in2.lo)[31:0];
1365 __ movl(in1_lo, eax);
1366
1367 break;
1368 }
1369
Calin Juravleb5bfa962014-10-21 18:02:24 +01001370 case Primitive::kPrimFloat: {
1371 __ mulss(first.As<XmmRegister>(), second.As<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001372 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001373 }
1374
1375 case Primitive::kPrimDouble: {
1376 __ mulsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1377 break;
1378 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001379
1380 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001381 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001382 }
1383}
1384
Calin Juravle7c4954d2014-10-28 16:57:40 +00001385void LocationsBuilderX86::VisitDiv(HDiv* div) {
1386 LocationSummary* locations =
1387 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1388 switch (div->GetResultType()) {
1389 case Primitive::kPrimInt:
1390 case Primitive::kPrimLong: {
1391 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1392 break;
1393 }
1394 case Primitive::kPrimFloat:
1395 case Primitive::kPrimDouble: {
1396 locations->SetInAt(0, Location::RequiresFpuRegister());
1397 locations->SetInAt(1, Location::RequiresFpuRegister());
1398 locations->SetOut(Location::SameAsFirstInput());
1399 break;
1400 }
1401
1402 default:
1403 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1404 }
1405}
1406
1407void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
1408 LocationSummary* locations = div->GetLocations();
1409 Location first = locations->InAt(0);
1410 Location second = locations->InAt(1);
1411 DCHECK(first.Equals(locations->Out()));
1412
1413 switch (div->GetResultType()) {
1414 case Primitive::kPrimInt:
1415 case Primitive::kPrimLong: {
1416 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1417 break;
1418 }
1419
1420 case Primitive::kPrimFloat: {
1421 __ divss(first.As<XmmRegister>(), second.As<XmmRegister>());
1422 break;
1423 }
1424
1425 case Primitive::kPrimDouble: {
1426 __ divsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1427 break;
1428 }
1429
1430 default:
1431 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1432 }
1433}
1434
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001435void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001436 LocationSummary* locations =
1437 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001438 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001439 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001440 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1441 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001442}
1443
1444void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
1445 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001446 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001447 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001448
Nicolas Geoffray707c8092014-04-04 10:50:14 +01001449 __ fs()->call(
1450 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001451
Nicolas Geoffray39468442014-09-02 15:17:15 +01001452 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001453 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001454}
1455
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001456void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
1457 LocationSummary* locations =
1458 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
1459 locations->SetOut(Location::RegisterLocation(EAX));
1460 InvokeRuntimeCallingConvention calling_convention;
1461 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1462 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1463 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1464}
1465
1466void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
1467 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001468 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001469 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
1470
1471 __ fs()->call(
1472 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocArrayWithAccessCheck)));
1473
1474 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
1475 DCHECK(!codegen_->IsLeafMethod());
1476}
1477
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001478void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001479 LocationSummary* locations =
1480 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001481 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1482 if (location.IsStackSlot()) {
1483 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1484 } else if (location.IsDoubleStackSlot()) {
1485 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001486 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001487 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001488}
1489
1490void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001491 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001492}
1493
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001494void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001495 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001496 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001497 locations->SetInAt(0, Location::RequiresRegister());
1498 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001499}
1500
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001501void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
1502 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01001503 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001504 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01001505 DCHECK(in.Equals(out));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001506 switch (not_->InputAt(0)->GetType()) {
1507 case Primitive::kPrimBoolean:
1508 __ xorl(out.As<Register>(), Immediate(1));
1509 break;
1510
1511 case Primitive::kPrimInt:
1512 __ notl(out.As<Register>());
1513 break;
1514
1515 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01001516 __ notl(out.AsRegisterPairLow<Register>());
1517 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001518 break;
1519
1520 default:
1521 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
1522 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001523}
1524
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001525void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001526 LocationSummary* locations =
1527 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001528 locations->SetInAt(0, Location::RequiresRegister());
1529 locations->SetInAt(1, Location::Any());
1530 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001531}
1532
1533void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001534 LocationSummary* locations = compare->GetLocations();
1535 switch (compare->InputAt(0)->GetType()) {
1536 case Primitive::kPrimLong: {
1537 Label less, greater, done;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001538 Register output = locations->Out().As<Register>();
1539 Location left = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001540 Location right = locations->InAt(1);
1541 if (right.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001542 __ cmpl(left.AsRegisterPairHigh<Register>(), right.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001543 } else {
1544 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001545 __ cmpl(left.AsRegisterPairHigh<Register>(),
1546 Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001547 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001548 __ j(kLess, &less); // Signed compare.
1549 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001550 if (right.IsRegisterPair()) {
1551 __ cmpl(left.AsRegisterPairLow<Register>(), right.AsRegisterPairLow<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001552 } else {
1553 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001554 __ cmpl(left.AsRegisterPairLow<Register>(), Address(ESP, right.GetStackIndex()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001555 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001556 __ movl(output, Immediate(0));
1557 __ j(kEqual, &done);
1558 __ j(kBelow, &less); // Unsigned compare.
1559
1560 __ Bind(&greater);
1561 __ movl(output, Immediate(1));
1562 __ jmp(&done);
1563
1564 __ Bind(&less);
1565 __ movl(output, Immediate(-1));
1566
1567 __ Bind(&done);
1568 break;
1569 }
1570 default:
1571 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
1572 }
1573}
1574
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001575void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001576 LocationSummary* locations =
1577 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001578 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1579 locations->SetInAt(i, Location::Any());
1580 }
1581 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001582}
1583
1584void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001585 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001586 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001587}
1588
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001589void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001590 LocationSummary* locations =
1591 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001592 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001593 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001594 bool is_object_type = field_type == Primitive::kPrimNot;
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001595 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
1596 || (field_type == Primitive::kPrimByte);
1597 // The register allocator does not support multiple
1598 // inputs that die at entry with one in a specific register.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001599 if (is_byte_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001600 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001601 locations->SetInAt(1, Location::RegisterLocation(EAX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001602 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001603 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001604 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001605 // Temporary registers for the write barrier.
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001606 if (is_object_type) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001607 locations->AddTemp(Location::RequiresRegister());
1608 // Ensure the card is in a byte register.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001609 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001610 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001611}
1612
1613void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1614 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001615 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001616 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001617 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001618
1619 switch (field_type) {
1620 case Primitive::kPrimBoolean:
1621 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001622 ByteRegister value = locations->InAt(1).As<ByteRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001623 __ movb(Address(obj, offset), value);
1624 break;
1625 }
1626
1627 case Primitive::kPrimShort:
1628 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001629 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001630 __ movw(Address(obj, offset), value);
1631 break;
1632 }
1633
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001634 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001635 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001636 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001637 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001638
1639 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001640 Register temp = locations->GetTemp(0).As<Register>();
1641 Register card = locations->GetTemp(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001642 codegen_->MarkGCCard(temp, card, obj, value);
1643 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001644 break;
1645 }
1646
1647 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001648 Location value = locations->InAt(1);
1649 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
1650 __ movl(Address(obj, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001651 break;
1652 }
1653
1654 case Primitive::kPrimFloat:
1655 case Primitive::kPrimDouble:
1656 LOG(FATAL) << "Unimplemented register type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001657 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001658 case Primitive::kPrimVoid:
1659 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001660 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001661 }
1662}
1663
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001664void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
1665 Label is_null;
1666 __ testl(value, value);
1667 __ j(kEqual, &is_null);
1668 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
1669 __ movl(temp, object);
1670 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
1671 __ movb(Address(temp, card, TIMES_1, 0),
1672 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
1673 __ Bind(&is_null);
1674}
1675
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001676void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001677 LocationSummary* locations =
1678 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001679 locations->SetInAt(0, Location::RequiresRegister());
1680 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001681}
1682
1683void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1684 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001685 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001686 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
1687
1688 switch (instruction->GetType()) {
1689 case Primitive::kPrimBoolean: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001690 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001691 __ movzxb(out, Address(obj, offset));
1692 break;
1693 }
1694
1695 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001696 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001697 __ movsxb(out, Address(obj, offset));
1698 break;
1699 }
1700
1701 case Primitive::kPrimShort: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001702 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001703 __ movsxw(out, Address(obj, offset));
1704 break;
1705 }
1706
1707 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001708 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001709 __ movzxw(out, Address(obj, offset));
1710 break;
1711 }
1712
1713 case Primitive::kPrimInt:
1714 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001715 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001716 __ movl(out, Address(obj, offset));
1717 break;
1718 }
1719
1720 case Primitive::kPrimLong: {
1721 // TODO: support volatile.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001722 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(obj, offset));
1723 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(obj, kX86WordSize + offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001724 break;
1725 }
1726
1727 case Primitive::kPrimFloat:
1728 case Primitive::kPrimDouble:
1729 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001730 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001731 case Primitive::kPrimVoid:
1732 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001733 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001734 }
1735}
1736
1737void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001738 LocationSummary* locations =
1739 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001740 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001741 if (instruction->HasUses()) {
1742 locations->SetOut(Location::SameAsFirstInput());
1743 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001744}
1745
1746void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001747 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001748 codegen_->AddSlowPath(slow_path);
1749
1750 LocationSummary* locations = instruction->GetLocations();
1751 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001752
1753 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001754 __ cmpl(obj.As<Register>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001755 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001756 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001757 } else {
1758 DCHECK(obj.IsConstant()) << obj;
1759 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
1760 __ jmp(slow_path->GetEntryLabel());
1761 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001762 }
1763 __ j(kEqual, slow_path->GetEntryLabel());
1764}
1765
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001766void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001767 LocationSummary* locations =
1768 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001769 locations->SetInAt(0, Location::RequiresRegister());
1770 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1771 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001772}
1773
1774void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
1775 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001776 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001777 Location index = locations->InAt(1);
1778
1779 switch (instruction->GetType()) {
1780 case Primitive::kPrimBoolean: {
1781 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001782 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001783 if (index.IsConstant()) {
1784 __ movzxb(out, Address(obj,
1785 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1786 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001787 __ movzxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001788 }
1789 break;
1790 }
1791
1792 case Primitive::kPrimByte: {
1793 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001794 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001795 if (index.IsConstant()) {
1796 __ movsxb(out, Address(obj,
1797 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1798 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001799 __ movsxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001800 }
1801 break;
1802 }
1803
1804 case Primitive::kPrimShort: {
1805 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001806 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001807 if (index.IsConstant()) {
1808 __ movsxw(out, Address(obj,
1809 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1810 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001811 __ movsxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001812 }
1813 break;
1814 }
1815
1816 case Primitive::kPrimChar: {
1817 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001818 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001819 if (index.IsConstant()) {
1820 __ movzxw(out, Address(obj,
1821 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1822 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001823 __ movzxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001824 }
1825 break;
1826 }
1827
1828 case Primitive::kPrimInt:
1829 case Primitive::kPrimNot: {
1830 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001831 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001832 if (index.IsConstant()) {
1833 __ movl(out, Address(obj,
1834 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
1835 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001836 __ movl(out, Address(obj, index.As<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001837 }
1838 break;
1839 }
1840
1841 case Primitive::kPrimLong: {
1842 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001843 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001844 if (index.IsConstant()) {
1845 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001846 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
1847 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001848 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001849 __ movl(out.AsRegisterPairLow<Register>(),
1850 Address(obj, index.As<Register>(), TIMES_8, data_offset));
1851 __ movl(out.AsRegisterPairHigh<Register>(),
1852 Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001853 }
1854 break;
1855 }
1856
1857 case Primitive::kPrimFloat:
1858 case Primitive::kPrimDouble:
1859 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001860 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001861 case Primitive::kPrimVoid:
1862 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001863 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001864 }
1865}
1866
1867void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001868 Primitive::Type value_type = instruction->GetComponentType();
1869 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1870 instruction,
1871 value_type == Primitive::kPrimNot ? LocationSummary::kCall : LocationSummary::kNoCall);
1872
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001873 if (value_type == Primitive::kPrimNot) {
1874 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001875 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1876 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1877 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001878 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001879 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
1880 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001881 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001882 // In case of a byte operation, the register allocator does not support multiple
1883 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001884 locations->SetInAt(0, Location::RequiresRegister());
1885 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001886 if (is_byte_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001887 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001888 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001889 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001890 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001891 }
1892 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001893}
1894
1895void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
1896 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001897 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001898 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001899 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001900 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001901
1902 switch (value_type) {
1903 case Primitive::kPrimBoolean:
1904 case Primitive::kPrimByte: {
1905 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001906 if (index.IsConstant()) {
1907 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001908 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001909 __ movb(Address(obj, offset), value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001910 } else {
1911 __ movb(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 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
1917 value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001918 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001919 __ movb(Address(obj, index.As<Register>(), TIMES_1, 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::kPrimShort:
1927 case Primitive::kPrimChar: {
1928 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001929 if (index.IsConstant()) {
1930 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001931 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001932 __ movw(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001933 } else {
1934 __ movw(Address(obj, offset),
1935 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1936 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001937 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001938 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001939 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
1940 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001941 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001942 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001943 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1944 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001945 }
1946 break;
1947 }
1948
1949 case Primitive::kPrimInt: {
1950 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001951 if (index.IsConstant()) {
1952 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001953 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001954 __ movl(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001955 } else {
1956 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1957 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001958 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001959 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001960 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
1961 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001962 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001963 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001964 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1965 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001966 }
1967 break;
1968 }
1969
1970 case Primitive::kPrimNot: {
1971 DCHECK(!codegen_->IsLeafMethod());
1972 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001973 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001974 break;
1975 }
1976
1977 case Primitive::kPrimLong: {
1978 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001979 if (index.IsConstant()) {
1980 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001981 if (value.IsRegisterPair()) {
1982 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
1983 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001984 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001985 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001986 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
1987 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
1988 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
1989 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001990 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001991 if (value.IsRegisterPair()) {
1992 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
1993 value.AsRegisterPairLow<Register>());
1994 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
1995 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001996 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001997 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001998 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001999 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002000 Immediate(Low32Bits(val)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002001 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002002 Immediate(High32Bits(val)));
2003 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002004 }
2005 break;
2006 }
2007
2008 case Primitive::kPrimFloat:
2009 case Primitive::kPrimDouble:
2010 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002011 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002012 case Primitive::kPrimVoid:
2013 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002014 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002015 }
2016}
2017
2018void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
2019 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002020 locations->SetInAt(0, Location::RequiresRegister());
2021 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002022 instruction->SetLocations(locations);
2023}
2024
2025void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
2026 LocationSummary* locations = instruction->GetLocations();
2027 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002028 Register obj = locations->InAt(0).As<Register>();
2029 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002030 __ movl(out, Address(obj, offset));
2031}
2032
2033void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002034 LocationSummary* locations =
2035 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002036 locations->SetInAt(0, Location::RequiresRegister());
2037 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002038 if (instruction->HasUses()) {
2039 locations->SetOut(Location::SameAsFirstInput());
2040 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002041}
2042
2043void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
2044 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002045 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01002046 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002047 codegen_->AddSlowPath(slow_path);
2048
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002049 Register index = locations->InAt(0).As<Register>();
2050 Register length = locations->InAt(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002051
2052 __ cmpl(index, length);
2053 __ j(kAboveEqual, slow_path->GetEntryLabel());
2054}
2055
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002056void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
2057 temp->SetLocations(nullptr);
2058}
2059
2060void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
2061 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002062 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002063}
2064
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002065void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002066 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002067 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002068}
2069
2070void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002071 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2072}
2073
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002074void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
2075 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2076}
2077
2078void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002079 HBasicBlock* block = instruction->GetBlock();
2080 if (block->GetLoopInformation() != nullptr) {
2081 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2082 // The back edge will generate the suspend check.
2083 return;
2084 }
2085 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2086 // The goto will generate the suspend check.
2087 return;
2088 }
2089 GenerateSuspendCheck(instruction, nullptr);
2090}
2091
2092void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
2093 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002094 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002095 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002096 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002097 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002098 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002099 if (successor == nullptr) {
2100 __ j(kNotEqual, slow_path->GetEntryLabel());
2101 __ Bind(slow_path->GetReturnLabel());
2102 } else {
2103 __ j(kEqual, codegen_->GetLabelOf(successor));
2104 __ jmp(slow_path->GetEntryLabel());
2105 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002106}
2107
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002108X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
2109 return codegen_->GetAssembler();
2110}
2111
2112void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
2113 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002114 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002115 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
2116 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
2117 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
2118}
2119
2120void ParallelMoveResolverX86::EmitMove(size_t index) {
2121 MoveOperands* move = moves_.Get(index);
2122 Location source = move->GetSource();
2123 Location destination = move->GetDestination();
2124
2125 if (source.IsRegister()) {
2126 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002127 __ movl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002128 } else {
2129 DCHECK(destination.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002130 __ movl(Address(ESP, destination.GetStackIndex()), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002131 }
2132 } else if (source.IsStackSlot()) {
2133 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002134 __ movl(destination.As<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002135 } else {
2136 DCHECK(destination.IsStackSlot());
2137 MoveMemoryToMemory(destination.GetStackIndex(),
2138 source.GetStackIndex());
2139 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002140 } else if (source.IsConstant()) {
2141 HIntConstant* instruction = source.GetConstant()->AsIntConstant();
2142 Immediate imm(instruction->AsIntConstant()->GetValue());
2143 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002144 __ movl(destination.As<Register>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002145 } else {
2146 __ movl(Address(ESP, destination.GetStackIndex()), imm);
2147 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002148 } else {
2149 LOG(FATAL) << "Unimplemented";
2150 }
2151}
2152
2153void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002154 Register suggested_scratch = reg == EAX ? EBX : EAX;
2155 ScratchRegisterScope ensure_scratch(
2156 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
2157
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002158 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
2159 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
2160 __ movl(Address(ESP, mem + stack_offset), reg);
2161 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
2162}
2163
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002164void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
2165 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002166 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
2167
2168 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002169 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002170 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
2171
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002172 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
2173 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
2174 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
2175 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
2176 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
2177 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
2178}
2179
2180void ParallelMoveResolverX86::EmitSwap(size_t index) {
2181 MoveOperands* move = moves_.Get(index);
2182 Location source = move->GetSource();
2183 Location destination = move->GetDestination();
2184
2185 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002186 __ xchgl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002187 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002188 Exchange(source.As<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002189 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002190 Exchange(destination.As<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002191 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
2192 Exchange(destination.GetStackIndex(), source.GetStackIndex());
2193 } else {
2194 LOG(FATAL) << "Unimplemented";
2195 }
2196}
2197
2198void ParallelMoveResolverX86::SpillScratch(int reg) {
2199 __ pushl(static_cast<Register>(reg));
2200}
2201
2202void ParallelMoveResolverX86::RestoreScratch(int reg) {
2203 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002204}
2205
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002206void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002207 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
2208 ? LocationSummary::kCallOnSlowPath
2209 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002210 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002211 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002212 locations->SetOut(Location::RequiresRegister());
2213}
2214
2215void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
2216 Register out = cls->GetLocations()->Out().As<Register>();
2217 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002218 DCHECK(!cls->CanCallRuntime());
2219 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002220 codegen_->LoadCurrentMethod(out);
2221 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
2222 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002223 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002224 codegen_->LoadCurrentMethod(out);
2225 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
2226 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002227
2228 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
2229 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2230 codegen_->AddSlowPath(slow_path);
2231 __ testl(out, out);
2232 __ j(kEqual, slow_path->GetEntryLabel());
2233 if (cls->MustGenerateClinitCheck()) {
2234 GenerateClassInitializationCheck(slow_path, out);
2235 } else {
2236 __ Bind(slow_path->GetExitLabel());
2237 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002238 }
2239}
2240
2241void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
2242 LocationSummary* locations =
2243 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2244 locations->SetInAt(0, Location::RequiresRegister());
2245 if (check->HasUses()) {
2246 locations->SetOut(Location::SameAsFirstInput());
2247 }
2248}
2249
2250void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002251 // We assume the class to not be null.
2252 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
2253 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002254 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002255 GenerateClassInitializationCheck(slow_path, check->GetLocations()->InAt(0).As<Register>());
2256}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002257
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002258void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
2259 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002260 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
2261 Immediate(mirror::Class::kStatusInitialized));
2262 __ j(kLess, slow_path->GetEntryLabel());
2263 __ Bind(slow_path->GetExitLabel());
2264 // No need for memory fence, thanks to the X86 memory model.
2265}
2266
2267void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2268 LocationSummary* locations =
2269 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2270 locations->SetInAt(0, Location::RequiresRegister());
2271 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2272}
2273
2274void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2275 LocationSummary* locations = instruction->GetLocations();
2276 Register cls = locations->InAt(0).As<Register>();
2277 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2278
2279 switch (instruction->GetType()) {
2280 case Primitive::kPrimBoolean: {
2281 Register out = locations->Out().As<Register>();
2282 __ movzxb(out, Address(cls, offset));
2283 break;
2284 }
2285
2286 case Primitive::kPrimByte: {
2287 Register out = locations->Out().As<Register>();
2288 __ movsxb(out, Address(cls, offset));
2289 break;
2290 }
2291
2292 case Primitive::kPrimShort: {
2293 Register out = locations->Out().As<Register>();
2294 __ movsxw(out, Address(cls, offset));
2295 break;
2296 }
2297
2298 case Primitive::kPrimChar: {
2299 Register out = locations->Out().As<Register>();
2300 __ movzxw(out, Address(cls, offset));
2301 break;
2302 }
2303
2304 case Primitive::kPrimInt:
2305 case Primitive::kPrimNot: {
2306 Register out = locations->Out().As<Register>();
2307 __ movl(out, Address(cls, offset));
2308 break;
2309 }
2310
2311 case Primitive::kPrimLong: {
2312 // TODO: support volatile.
2313 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(cls, offset));
2314 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(cls, kX86WordSize + offset));
2315 break;
2316 }
2317
2318 case Primitive::kPrimFloat:
2319 case Primitive::kPrimDouble:
2320 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
2321 UNREACHABLE();
2322 case Primitive::kPrimVoid:
2323 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2324 UNREACHABLE();
2325 }
2326}
2327
2328void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2329 LocationSummary* locations =
2330 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2331 locations->SetInAt(0, Location::RequiresRegister());
2332 Primitive::Type field_type = instruction->GetFieldType();
2333 bool is_object_type = field_type == Primitive::kPrimNot;
2334 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
2335 || (field_type == Primitive::kPrimByte);
2336 // The register allocator does not support multiple
2337 // inputs that die at entry with one in a specific register.
2338 if (is_byte_type) {
2339 // Ensure the value is in a byte register.
2340 locations->SetInAt(1, Location::RegisterLocation(EAX));
2341 } else {
2342 locations->SetInAt(1, Location::RequiresRegister());
2343 }
2344 // Temporary registers for the write barrier.
2345 if (is_object_type) {
2346 locations->AddTemp(Location::RequiresRegister());
2347 // Ensure the card is in a byte register.
2348 locations->AddTemp(Location::RegisterLocation(ECX));
2349 }
2350}
2351
2352void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2353 LocationSummary* locations = instruction->GetLocations();
2354 Register cls = locations->InAt(0).As<Register>();
2355 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2356 Primitive::Type field_type = instruction->GetFieldType();
2357
2358 switch (field_type) {
2359 case Primitive::kPrimBoolean:
2360 case Primitive::kPrimByte: {
2361 ByteRegister value = locations->InAt(1).As<ByteRegister>();
2362 __ movb(Address(cls, offset), value);
2363 break;
2364 }
2365
2366 case Primitive::kPrimShort:
2367 case Primitive::kPrimChar: {
2368 Register value = locations->InAt(1).As<Register>();
2369 __ movw(Address(cls, offset), value);
2370 break;
2371 }
2372
2373 case Primitive::kPrimInt:
2374 case Primitive::kPrimNot: {
2375 Register value = locations->InAt(1).As<Register>();
2376 __ movl(Address(cls, offset), value);
2377
2378 if (field_type == Primitive::kPrimNot) {
2379 Register temp = locations->GetTemp(0).As<Register>();
2380 Register card = locations->GetTemp(1).As<Register>();
2381 codegen_->MarkGCCard(temp, card, cls, value);
2382 }
2383 break;
2384 }
2385
2386 case Primitive::kPrimLong: {
2387 Location value = locations->InAt(1);
2388 __ movl(Address(cls, offset), value.AsRegisterPairLow<Register>());
2389 __ movl(Address(cls, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
2390 break;
2391 }
2392
2393 case Primitive::kPrimFloat:
2394 case Primitive::kPrimDouble:
2395 LOG(FATAL) << "Unimplemented register type " << field_type;
2396 UNREACHABLE();
2397 case Primitive::kPrimVoid:
2398 LOG(FATAL) << "Unreachable type " << field_type;
2399 UNREACHABLE();
2400 }
2401}
2402
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002403void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
2404 LocationSummary* locations =
2405 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
2406 locations->SetOut(Location::RequiresRegister());
2407}
2408
2409void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
2410 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
2411 codegen_->AddSlowPath(slow_path);
2412
2413 Register out = load->GetLocations()->Out().As<Register>();
2414 codegen_->LoadCurrentMethod(out);
2415 __ movl(out, Address(out, mirror::ArtMethod::DexCacheStringsOffset().Int32Value()));
2416 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
2417 __ testl(out, out);
2418 __ j(kEqual, slow_path->GetEntryLabel());
2419 __ Bind(slow_path->GetExitLabel());
2420}
2421
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002422} // namespace x86
2423} // namespace art