blob: 447daa82d85654cc4fa6821fa1c443e5a050f505 [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) {
1534 Label greater, done;
1535 LocationSummary* locations = compare->GetLocations();
1536 switch (compare->InputAt(0)->GetType()) {
1537 case Primitive::kPrimLong: {
1538 Label less, greater, done;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001539 Register output = locations->Out().As<Register>();
1540 Location left = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001541 Location right = locations->InAt(1);
1542 if (right.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001543 __ cmpl(left.AsRegisterPairHigh<Register>(), right.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001544 } else {
1545 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001546 __ cmpl(left.AsRegisterPairHigh<Register>(),
1547 Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001548 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001549 __ j(kLess, &less); // Signed compare.
1550 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001551 if (right.IsRegisterPair()) {
1552 __ cmpl(left.AsRegisterPairLow<Register>(), right.AsRegisterPairLow<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001553 } else {
1554 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001555 __ cmpl(left.AsRegisterPairLow<Register>(), Address(ESP, right.GetStackIndex()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001556 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001557 __ movl(output, Immediate(0));
1558 __ j(kEqual, &done);
1559 __ j(kBelow, &less); // Unsigned compare.
1560
1561 __ Bind(&greater);
1562 __ movl(output, Immediate(1));
1563 __ jmp(&done);
1564
1565 __ Bind(&less);
1566 __ movl(output, Immediate(-1));
1567
1568 __ Bind(&done);
1569 break;
1570 }
1571 default:
1572 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
1573 }
1574}
1575
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001576void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001577 LocationSummary* locations =
1578 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001579 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1580 locations->SetInAt(i, Location::Any());
1581 }
1582 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001583}
1584
1585void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001586 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001587 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001588}
1589
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001590void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001591 LocationSummary* locations =
1592 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001593 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001594 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001595 bool is_object_type = field_type == Primitive::kPrimNot;
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001596 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
1597 || (field_type == Primitive::kPrimByte);
1598 // The register allocator does not support multiple
1599 // inputs that die at entry with one in a specific register.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001600 if (is_byte_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001601 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001602 locations->SetInAt(1, Location::RegisterLocation(EAX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001603 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001604 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001605 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001606 // Temporary registers for the write barrier.
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001607 if (is_object_type) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001608 locations->AddTemp(Location::RequiresRegister());
1609 // Ensure the card is in a byte register.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001610 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001611 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001612}
1613
1614void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1615 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001616 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001617 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001618 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001619
1620 switch (field_type) {
1621 case Primitive::kPrimBoolean:
1622 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001623 ByteRegister value = locations->InAt(1).As<ByteRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001624 __ movb(Address(obj, offset), value);
1625 break;
1626 }
1627
1628 case Primitive::kPrimShort:
1629 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001630 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001631 __ movw(Address(obj, offset), value);
1632 break;
1633 }
1634
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001635 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001636 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001637 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001638 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001639
1640 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001641 Register temp = locations->GetTemp(0).As<Register>();
1642 Register card = locations->GetTemp(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001643 codegen_->MarkGCCard(temp, card, obj, value);
1644 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001645 break;
1646 }
1647
1648 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001649 Location value = locations->InAt(1);
1650 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
1651 __ movl(Address(obj, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001652 break;
1653 }
1654
1655 case Primitive::kPrimFloat:
1656 case Primitive::kPrimDouble:
1657 LOG(FATAL) << "Unimplemented register type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001658 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001659 case Primitive::kPrimVoid:
1660 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001661 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001662 }
1663}
1664
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001665void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
1666 Label is_null;
1667 __ testl(value, value);
1668 __ j(kEqual, &is_null);
1669 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
1670 __ movl(temp, object);
1671 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
1672 __ movb(Address(temp, card, TIMES_1, 0),
1673 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
1674 __ Bind(&is_null);
1675}
1676
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001677void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001678 LocationSummary* locations =
1679 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001680 locations->SetInAt(0, Location::RequiresRegister());
1681 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001682}
1683
1684void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1685 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001686 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001687 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
1688
1689 switch (instruction->GetType()) {
1690 case Primitive::kPrimBoolean: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001691 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001692 __ movzxb(out, Address(obj, offset));
1693 break;
1694 }
1695
1696 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001697 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001698 __ movsxb(out, Address(obj, offset));
1699 break;
1700 }
1701
1702 case Primitive::kPrimShort: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001703 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001704 __ movsxw(out, Address(obj, offset));
1705 break;
1706 }
1707
1708 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001709 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001710 __ movzxw(out, Address(obj, offset));
1711 break;
1712 }
1713
1714 case Primitive::kPrimInt:
1715 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001716 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001717 __ movl(out, Address(obj, offset));
1718 break;
1719 }
1720
1721 case Primitive::kPrimLong: {
1722 // TODO: support volatile.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001723 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(obj, offset));
1724 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(obj, kX86WordSize + offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001725 break;
1726 }
1727
1728 case Primitive::kPrimFloat:
1729 case Primitive::kPrimDouble:
1730 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001731 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001732 case Primitive::kPrimVoid:
1733 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001734 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001735 }
1736}
1737
1738void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001739 LocationSummary* locations =
1740 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001741 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001742 if (instruction->HasUses()) {
1743 locations->SetOut(Location::SameAsFirstInput());
1744 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001745}
1746
1747void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001748 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001749 codegen_->AddSlowPath(slow_path);
1750
1751 LocationSummary* locations = instruction->GetLocations();
1752 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001753
1754 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001755 __ cmpl(obj.As<Register>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001756 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001757 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001758 } else {
1759 DCHECK(obj.IsConstant()) << obj;
1760 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
1761 __ jmp(slow_path->GetEntryLabel());
1762 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001763 }
1764 __ j(kEqual, slow_path->GetEntryLabel());
1765}
1766
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001767void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001768 LocationSummary* locations =
1769 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001770 locations->SetInAt(0, Location::RequiresRegister());
1771 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1772 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001773}
1774
1775void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
1776 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001777 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001778 Location index = locations->InAt(1);
1779
1780 switch (instruction->GetType()) {
1781 case Primitive::kPrimBoolean: {
1782 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001783 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001784 if (index.IsConstant()) {
1785 __ movzxb(out, Address(obj,
1786 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1787 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001788 __ movzxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001789 }
1790 break;
1791 }
1792
1793 case Primitive::kPrimByte: {
1794 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001795 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001796 if (index.IsConstant()) {
1797 __ movsxb(out, Address(obj,
1798 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1799 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001800 __ movsxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001801 }
1802 break;
1803 }
1804
1805 case Primitive::kPrimShort: {
1806 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001807 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001808 if (index.IsConstant()) {
1809 __ movsxw(out, Address(obj,
1810 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1811 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001812 __ movsxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001813 }
1814 break;
1815 }
1816
1817 case Primitive::kPrimChar: {
1818 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001819 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001820 if (index.IsConstant()) {
1821 __ movzxw(out, Address(obj,
1822 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1823 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001824 __ movzxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001825 }
1826 break;
1827 }
1828
1829 case Primitive::kPrimInt:
1830 case Primitive::kPrimNot: {
1831 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001832 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001833 if (index.IsConstant()) {
1834 __ movl(out, Address(obj,
1835 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
1836 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001837 __ movl(out, Address(obj, index.As<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001838 }
1839 break;
1840 }
1841
1842 case Primitive::kPrimLong: {
1843 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001844 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001845 if (index.IsConstant()) {
1846 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001847 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
1848 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001849 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001850 __ movl(out.AsRegisterPairLow<Register>(),
1851 Address(obj, index.As<Register>(), TIMES_8, data_offset));
1852 __ movl(out.AsRegisterPairHigh<Register>(),
1853 Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001854 }
1855 break;
1856 }
1857
1858 case Primitive::kPrimFloat:
1859 case Primitive::kPrimDouble:
1860 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001861 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001862 case Primitive::kPrimVoid:
1863 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001864 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001865 }
1866}
1867
1868void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001869 Primitive::Type value_type = instruction->GetComponentType();
1870 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1871 instruction,
1872 value_type == Primitive::kPrimNot ? LocationSummary::kCall : LocationSummary::kNoCall);
1873
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001874 if (value_type == Primitive::kPrimNot) {
1875 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001876 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1877 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1878 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001879 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001880 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
1881 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001882 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001883 // In case of a byte operation, the register allocator does not support multiple
1884 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001885 locations->SetInAt(0, Location::RequiresRegister());
1886 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001887 if (is_byte_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001888 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001889 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001890 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001891 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001892 }
1893 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001894}
1895
1896void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
1897 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001898 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001899 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001900 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001901 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001902
1903 switch (value_type) {
1904 case Primitive::kPrimBoolean:
1905 case Primitive::kPrimByte: {
1906 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001907 if (index.IsConstant()) {
1908 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001909 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001910 __ movb(Address(obj, offset), value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001911 } else {
1912 __ movb(Address(obj, offset),
1913 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1914 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001915 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001916 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001917 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
1918 value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001919 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001920 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001921 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1922 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001923 }
1924 break;
1925 }
1926
1927 case Primitive::kPrimShort:
1928 case Primitive::kPrimChar: {
1929 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001930 if (index.IsConstant()) {
1931 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001932 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001933 __ movw(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001934 } else {
1935 __ movw(Address(obj, offset),
1936 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1937 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001938 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001939 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001940 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
1941 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001942 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001943 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001944 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1945 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001946 }
1947 break;
1948 }
1949
1950 case Primitive::kPrimInt: {
1951 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001952 if (index.IsConstant()) {
1953 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001954 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001955 __ movl(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001956 } else {
1957 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1958 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001959 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001960 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001961 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
1962 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001963 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001964 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001965 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1966 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001967 }
1968 break;
1969 }
1970
1971 case Primitive::kPrimNot: {
1972 DCHECK(!codegen_->IsLeafMethod());
1973 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001974 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001975 break;
1976 }
1977
1978 case Primitive::kPrimLong: {
1979 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001980 if (index.IsConstant()) {
1981 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001982 if (value.IsRegisterPair()) {
1983 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
1984 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001985 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001986 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001987 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
1988 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
1989 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
1990 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001991 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001992 if (value.IsRegisterPair()) {
1993 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
1994 value.AsRegisterPairLow<Register>());
1995 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
1996 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001997 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001998 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001999 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002000 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002001 Immediate(Low32Bits(val)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002002 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002003 Immediate(High32Bits(val)));
2004 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002005 }
2006 break;
2007 }
2008
2009 case Primitive::kPrimFloat:
2010 case Primitive::kPrimDouble:
2011 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002012 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002013 case Primitive::kPrimVoid:
2014 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002015 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002016 }
2017}
2018
2019void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
2020 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002021 locations->SetInAt(0, Location::RequiresRegister());
2022 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002023 instruction->SetLocations(locations);
2024}
2025
2026void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
2027 LocationSummary* locations = instruction->GetLocations();
2028 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002029 Register obj = locations->InAt(0).As<Register>();
2030 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002031 __ movl(out, Address(obj, offset));
2032}
2033
2034void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002035 LocationSummary* locations =
2036 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002037 locations->SetInAt(0, Location::RequiresRegister());
2038 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002039 if (instruction->HasUses()) {
2040 locations->SetOut(Location::SameAsFirstInput());
2041 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002042}
2043
2044void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
2045 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002046 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01002047 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002048 codegen_->AddSlowPath(slow_path);
2049
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002050 Register index = locations->InAt(0).As<Register>();
2051 Register length = locations->InAt(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002052
2053 __ cmpl(index, length);
2054 __ j(kAboveEqual, slow_path->GetEntryLabel());
2055}
2056
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002057void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
2058 temp->SetLocations(nullptr);
2059}
2060
2061void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
2062 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002063 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002064}
2065
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002066void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002067 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002068 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002069}
2070
2071void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002072 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2073}
2074
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002075void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
2076 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2077}
2078
2079void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002080 HBasicBlock* block = instruction->GetBlock();
2081 if (block->GetLoopInformation() != nullptr) {
2082 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2083 // The back edge will generate the suspend check.
2084 return;
2085 }
2086 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2087 // The goto will generate the suspend check.
2088 return;
2089 }
2090 GenerateSuspendCheck(instruction, nullptr);
2091}
2092
2093void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
2094 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002095 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002096 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002097 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002098 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002099 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002100 if (successor == nullptr) {
2101 __ j(kNotEqual, slow_path->GetEntryLabel());
2102 __ Bind(slow_path->GetReturnLabel());
2103 } else {
2104 __ j(kEqual, codegen_->GetLabelOf(successor));
2105 __ jmp(slow_path->GetEntryLabel());
2106 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002107}
2108
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002109X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
2110 return codegen_->GetAssembler();
2111}
2112
2113void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
2114 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002115 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002116 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
2117 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
2118 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
2119}
2120
2121void ParallelMoveResolverX86::EmitMove(size_t index) {
2122 MoveOperands* move = moves_.Get(index);
2123 Location source = move->GetSource();
2124 Location destination = move->GetDestination();
2125
2126 if (source.IsRegister()) {
2127 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002128 __ movl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002129 } else {
2130 DCHECK(destination.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002131 __ movl(Address(ESP, destination.GetStackIndex()), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002132 }
2133 } else if (source.IsStackSlot()) {
2134 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002135 __ movl(destination.As<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002136 } else {
2137 DCHECK(destination.IsStackSlot());
2138 MoveMemoryToMemory(destination.GetStackIndex(),
2139 source.GetStackIndex());
2140 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002141 } else if (source.IsConstant()) {
2142 HIntConstant* instruction = source.GetConstant()->AsIntConstant();
2143 Immediate imm(instruction->AsIntConstant()->GetValue());
2144 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002145 __ movl(destination.As<Register>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002146 } else {
2147 __ movl(Address(ESP, destination.GetStackIndex()), imm);
2148 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002149 } else {
2150 LOG(FATAL) << "Unimplemented";
2151 }
2152}
2153
2154void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002155 Register suggested_scratch = reg == EAX ? EBX : EAX;
2156 ScratchRegisterScope ensure_scratch(
2157 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
2158
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002159 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
2160 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
2161 __ movl(Address(ESP, mem + stack_offset), reg);
2162 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
2163}
2164
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002165void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
2166 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002167 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
2168
2169 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002170 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002171 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
2172
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002173 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
2174 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
2175 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
2176 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
2177 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
2178 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
2179}
2180
2181void ParallelMoveResolverX86::EmitSwap(size_t index) {
2182 MoveOperands* move = moves_.Get(index);
2183 Location source = move->GetSource();
2184 Location destination = move->GetDestination();
2185
2186 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002187 __ xchgl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002188 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002189 Exchange(source.As<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002190 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002191 Exchange(destination.As<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002192 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
2193 Exchange(destination.GetStackIndex(), source.GetStackIndex());
2194 } else {
2195 LOG(FATAL) << "Unimplemented";
2196 }
2197}
2198
2199void ParallelMoveResolverX86::SpillScratch(int reg) {
2200 __ pushl(static_cast<Register>(reg));
2201}
2202
2203void ParallelMoveResolverX86::RestoreScratch(int reg) {
2204 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002205}
2206
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002207void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002208 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
2209 ? LocationSummary::kCallOnSlowPath
2210 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002211 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002212 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002213 locations->SetOut(Location::RequiresRegister());
2214}
2215
2216void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
2217 Register out = cls->GetLocations()->Out().As<Register>();
2218 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002219 DCHECK(!cls->CanCallRuntime());
2220 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002221 codegen_->LoadCurrentMethod(out);
2222 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
2223 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002224 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002225 codegen_->LoadCurrentMethod(out);
2226 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
2227 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002228
2229 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
2230 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2231 codegen_->AddSlowPath(slow_path);
2232 __ testl(out, out);
2233 __ j(kEqual, slow_path->GetEntryLabel());
2234 if (cls->MustGenerateClinitCheck()) {
2235 GenerateClassInitializationCheck(slow_path, out);
2236 } else {
2237 __ Bind(slow_path->GetExitLabel());
2238 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002239 }
2240}
2241
2242void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
2243 LocationSummary* locations =
2244 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2245 locations->SetInAt(0, Location::RequiresRegister());
2246 if (check->HasUses()) {
2247 locations->SetOut(Location::SameAsFirstInput());
2248 }
2249}
2250
2251void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002252 // We assume the class to not be null.
2253 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
2254 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002255 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002256 GenerateClassInitializationCheck(slow_path, check->GetLocations()->InAt(0).As<Register>());
2257}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002258
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002259void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
2260 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002261 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
2262 Immediate(mirror::Class::kStatusInitialized));
2263 __ j(kLess, slow_path->GetEntryLabel());
2264 __ Bind(slow_path->GetExitLabel());
2265 // No need for memory fence, thanks to the X86 memory model.
2266}
2267
2268void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2269 LocationSummary* locations =
2270 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2271 locations->SetInAt(0, Location::RequiresRegister());
2272 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2273}
2274
2275void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2276 LocationSummary* locations = instruction->GetLocations();
2277 Register cls = locations->InAt(0).As<Register>();
2278 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2279
2280 switch (instruction->GetType()) {
2281 case Primitive::kPrimBoolean: {
2282 Register out = locations->Out().As<Register>();
2283 __ movzxb(out, Address(cls, offset));
2284 break;
2285 }
2286
2287 case Primitive::kPrimByte: {
2288 Register out = locations->Out().As<Register>();
2289 __ movsxb(out, Address(cls, offset));
2290 break;
2291 }
2292
2293 case Primitive::kPrimShort: {
2294 Register out = locations->Out().As<Register>();
2295 __ movsxw(out, Address(cls, offset));
2296 break;
2297 }
2298
2299 case Primitive::kPrimChar: {
2300 Register out = locations->Out().As<Register>();
2301 __ movzxw(out, Address(cls, offset));
2302 break;
2303 }
2304
2305 case Primitive::kPrimInt:
2306 case Primitive::kPrimNot: {
2307 Register out = locations->Out().As<Register>();
2308 __ movl(out, Address(cls, offset));
2309 break;
2310 }
2311
2312 case Primitive::kPrimLong: {
2313 // TODO: support volatile.
2314 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(cls, offset));
2315 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(cls, kX86WordSize + offset));
2316 break;
2317 }
2318
2319 case Primitive::kPrimFloat:
2320 case Primitive::kPrimDouble:
2321 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
2322 UNREACHABLE();
2323 case Primitive::kPrimVoid:
2324 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2325 UNREACHABLE();
2326 }
2327}
2328
2329void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2330 LocationSummary* locations =
2331 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2332 locations->SetInAt(0, Location::RequiresRegister());
2333 Primitive::Type field_type = instruction->GetFieldType();
2334 bool is_object_type = field_type == Primitive::kPrimNot;
2335 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
2336 || (field_type == Primitive::kPrimByte);
2337 // The register allocator does not support multiple
2338 // inputs that die at entry with one in a specific register.
2339 if (is_byte_type) {
2340 // Ensure the value is in a byte register.
2341 locations->SetInAt(1, Location::RegisterLocation(EAX));
2342 } else {
2343 locations->SetInAt(1, Location::RequiresRegister());
2344 }
2345 // Temporary registers for the write barrier.
2346 if (is_object_type) {
2347 locations->AddTemp(Location::RequiresRegister());
2348 // Ensure the card is in a byte register.
2349 locations->AddTemp(Location::RegisterLocation(ECX));
2350 }
2351}
2352
2353void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2354 LocationSummary* locations = instruction->GetLocations();
2355 Register cls = locations->InAt(0).As<Register>();
2356 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2357 Primitive::Type field_type = instruction->GetFieldType();
2358
2359 switch (field_type) {
2360 case Primitive::kPrimBoolean:
2361 case Primitive::kPrimByte: {
2362 ByteRegister value = locations->InAt(1).As<ByteRegister>();
2363 __ movb(Address(cls, offset), value);
2364 break;
2365 }
2366
2367 case Primitive::kPrimShort:
2368 case Primitive::kPrimChar: {
2369 Register value = locations->InAt(1).As<Register>();
2370 __ movw(Address(cls, offset), value);
2371 break;
2372 }
2373
2374 case Primitive::kPrimInt:
2375 case Primitive::kPrimNot: {
2376 Register value = locations->InAt(1).As<Register>();
2377 __ movl(Address(cls, offset), value);
2378
2379 if (field_type == Primitive::kPrimNot) {
2380 Register temp = locations->GetTemp(0).As<Register>();
2381 Register card = locations->GetTemp(1).As<Register>();
2382 codegen_->MarkGCCard(temp, card, cls, value);
2383 }
2384 break;
2385 }
2386
2387 case Primitive::kPrimLong: {
2388 Location value = locations->InAt(1);
2389 __ movl(Address(cls, offset), value.AsRegisterPairLow<Register>());
2390 __ movl(Address(cls, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
2391 break;
2392 }
2393
2394 case Primitive::kPrimFloat:
2395 case Primitive::kPrimDouble:
2396 LOG(FATAL) << "Unimplemented register type " << field_type;
2397 UNREACHABLE();
2398 case Primitive::kPrimVoid:
2399 LOG(FATAL) << "Unreachable type " << field_type;
2400 UNREACHABLE();
2401 }
2402}
2403
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002404void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
2405 LocationSummary* locations =
2406 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
2407 locations->SetOut(Location::RequiresRegister());
2408}
2409
2410void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
2411 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
2412 codegen_->AddSlowPath(slow_path);
2413
2414 Register out = load->GetLocations()->Out().As<Register>();
2415 codegen_->LoadCurrentMethod(out);
2416 __ movl(out, Address(out, mirror::ArtMethod::DexCacheStringsOffset().Int32Value()));
2417 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
2418 __ testl(out, out);
2419 __ j(kEqual, slow_path->GetEntryLabel());
2420 __ Bind(slow_path->GetExitLabel());
2421}
2422
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002423} // namespace x86
2424} // namespace art