blob: 2550518db1886af36538b538bb6cb56716acc3db [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 Geoffraye5038322014-07-04 09:41:32 +0100160#undef __
161#define __ reinterpret_cast<X86Assembler*>(GetAssembler())->
162
Dave Allison20dfc792014-06-16 20:44:29 -0700163inline Condition X86Condition(IfCondition cond) {
164 switch (cond) {
165 case kCondEQ: return kEqual;
166 case kCondNE: return kNotEqual;
167 case kCondLT: return kLess;
168 case kCondLE: return kLessEqual;
169 case kCondGT: return kGreater;
170 case kCondGE: return kGreaterEqual;
171 default:
172 LOG(FATAL) << "Unknown if condition";
173 }
174 return kEqual;
175}
176
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100177void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
178 stream << X86ManagedRegister::FromCpuRegister(Register(reg));
179}
180
181void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
182 stream << X86ManagedRegister::FromXmmRegister(XmmRegister(reg));
183}
184
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100185size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
186 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
187 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100188}
189
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100190size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
191 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
192 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100193}
194
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100195CodeGeneratorX86::CodeGeneratorX86(HGraph* graph)
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100196 : CodeGenerator(graph, kNumberOfCpuRegisters, kNumberOfXmmRegisters, kNumberOfRegisterPairs),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100197 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100198 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100199 instruction_visitor_(graph, this),
200 move_resolver_(graph->GetArena(), this) {}
201
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100202size_t CodeGeneratorX86::FrameEntrySpillSize() const {
203 return kNumberOfPushedRegistersAtEntry * kX86WordSize;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100204}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100205
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100206Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100207 switch (type) {
208 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100209 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100210 X86ManagedRegister pair =
211 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100212 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
213 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100214 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
215 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100216 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100217 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100218 }
219
220 case Primitive::kPrimByte:
221 case Primitive::kPrimBoolean:
222 case Primitive::kPrimChar:
223 case Primitive::kPrimShort:
224 case Primitive::kPrimInt:
225 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100226 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100227 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100228 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100229 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
230 X86ManagedRegister current =
231 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
232 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100233 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100234 }
235 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100236 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100237 }
238
239 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100240 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100241 return Location::FpuRegisterLocation(
242 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100243 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100244
245 case Primitive::kPrimVoid:
246 LOG(FATAL) << "Unreachable type " << type;
247 }
248
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100249 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100250}
251
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100252void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100253 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100254 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100255
256 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100257 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100258
259 // TODO: We currently don't use Quick's callee saved registers.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100260 blocked_core_registers_[EBP] = true;
261 blocked_core_registers_[ESI] = true;
262 blocked_core_registers_[EDI] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100263
264 UpdateBlockedPairRegisters();
265}
266
267void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
268 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
269 X86ManagedRegister current =
270 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
271 if (blocked_core_registers_[current.AsRegisterPairLow()]
272 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
273 blocked_register_pairs_[i] = true;
274 }
275 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100276}
277
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100278InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
279 : HGraphVisitor(graph),
280 assembler_(codegen->GetAssembler()),
281 codegen_(codegen) {}
282
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000283void CodeGeneratorX86::GenerateFrameEntry() {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000284 // Create a fake register to mimic Quick.
285 static const int kFakeReturnRegister = 8;
286 core_spill_mask_ |= (1 << kFakeReturnRegister);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000287
Dave Allison648d7112014-07-25 16:15:27 -0700288 bool skip_overflow_check = IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100289 if (!skip_overflow_check && !kExplicitStackOverflowCheck) {
290 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100291 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100292 }
293
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100294 // The return PC has already been pushed on the stack.
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100295 __ subl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100296
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100297 if (!skip_overflow_check && kExplicitStackOverflowCheck) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100298 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathX86();
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100299 AddSlowPath(slow_path);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100300
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100301 __ fs()->cmpl(ESP, Address::Absolute(Thread::StackEndOffset<kX86WordSize>()));
302 __ j(kLess, slow_path->GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100303 }
304
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100305 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000306}
307
308void CodeGeneratorX86::GenerateFrameExit() {
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100309 __ addl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000310}
311
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100312void CodeGeneratorX86::Bind(HBasicBlock* block) {
313 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000314}
315
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000316void InstructionCodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100317 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000318}
319
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100320Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
321 switch (load->GetType()) {
322 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100323 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100324 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
325 break;
326
327 case Primitive::kPrimInt:
328 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100329 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100330 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100331
332 case Primitive::kPrimBoolean:
333 case Primitive::kPrimByte:
334 case Primitive::kPrimChar:
335 case Primitive::kPrimShort:
336 case Primitive::kPrimVoid:
337 LOG(FATAL) << "Unexpected type " << load->GetType();
338 }
339
340 LOG(FATAL) << "Unreachable";
341 return Location();
342}
343
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100344Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
345 switch (type) {
346 case Primitive::kPrimBoolean:
347 case Primitive::kPrimByte:
348 case Primitive::kPrimChar:
349 case Primitive::kPrimShort:
350 case Primitive::kPrimInt:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100351 case Primitive::kPrimFloat:
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100352 case Primitive::kPrimNot: {
353 uint32_t index = gp_index_++;
354 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100355 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100356 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100357 return Location::StackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100358 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100359 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100360
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100361 case Primitive::kPrimLong:
362 case Primitive::kPrimDouble: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100363 uint32_t index = gp_index_;
364 gp_index_ += 2;
365 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100366 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
367 calling_convention.GetRegisterPairAt(index));
368 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100369 } else if (index + 1 == calling_convention.GetNumberOfRegisters()) {
370 return Location::QuickParameter(index);
371 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100372 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100373 }
374 }
375
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100376 case Primitive::kPrimVoid:
377 LOG(FATAL) << "Unexpected parameter type " << type;
378 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100379 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100380 return Location();
381}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100382
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100383void CodeGeneratorX86::Move32(Location destination, Location source) {
384 if (source.Equals(destination)) {
385 return;
386 }
387 if (destination.IsRegister()) {
388 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100389 __ movl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100390 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100391 __ movd(destination.As<Register>(), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100392 } else {
393 DCHECK(source.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100394 __ movl(destination.As<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100395 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100396 } else if (destination.IsFpuRegister()) {
397 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100398 __ movd(destination.As<XmmRegister>(), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100399 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100400 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100401 } else {
402 DCHECK(source.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100403 __ movss(destination.As<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100404 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100405 } else {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100406 DCHECK(destination.IsStackSlot());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100407 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100408 __ movl(Address(ESP, destination.GetStackIndex()), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100409 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100410 __ movss(Address(ESP, destination.GetStackIndex()), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100411 } else {
412 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100413 __ pushl(Address(ESP, source.GetStackIndex()));
414 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100415 }
416 }
417}
418
419void CodeGeneratorX86::Move64(Location destination, Location source) {
420 if (source.Equals(destination)) {
421 return;
422 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100423 if (destination.IsRegisterPair()) {
424 if (source.IsRegisterPair()) {
425 __ movl(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
426 __ movl(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100427 } else if (source.IsFpuRegister()) {
428 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100429 } else if (source.IsQuickParameter()) {
430 uint32_t argument_index = source.GetQuickParameterIndex();
431 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100432 __ movl(destination.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100433 calling_convention.GetRegisterAt(argument_index));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100434 __ movl(destination.AsRegisterPairHigh<Register>(), Address(ESP,
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100435 calling_convention.GetStackOffsetOf(argument_index + 1) + GetFrameSize()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100436 } else {
437 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100438 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
439 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100440 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
441 }
442 } else if (destination.IsQuickParameter()) {
443 InvokeDexCallingConvention calling_convention;
444 uint32_t argument_index = destination.GetQuickParameterIndex();
445 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100446 __ movl(calling_convention.GetRegisterAt(argument_index), source.AsRegisterPairLow<Register>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100447 __ movl(Address(ESP, calling_convention.GetStackOffsetOf(argument_index + 1)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100448 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100449 } else if (source.IsFpuRegister()) {
450 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100451 } else {
452 DCHECK(source.IsDoubleStackSlot());
453 __ movl(calling_convention.GetRegisterAt(argument_index),
454 Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100455 __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100456 __ popl(Address(ESP, calling_convention.GetStackOffsetOf(argument_index + 1)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100457 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100458 } else if (destination.IsFpuRegister()) {
459 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100460 __ movsd(destination.As<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100461 } else {
462 LOG(FATAL) << "Unimplemented";
463 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100464 } else {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100465 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100466 if (source.IsRegisterPair()) {
467 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100468 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100469 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100470 } else if (source.IsQuickParameter()) {
471 InvokeDexCallingConvention calling_convention;
472 uint32_t argument_index = source.GetQuickParameterIndex();
473 __ movl(Address(ESP, destination.GetStackIndex()),
474 calling_convention.GetRegisterAt(argument_index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100475 DCHECK_EQ(calling_convention.GetStackOffsetOf(argument_index + 1) + GetFrameSize(),
476 static_cast<size_t>(destination.GetHighStackIndex(kX86WordSize)));
477 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100478 __ movsd(Address(ESP, destination.GetStackIndex()), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100479 } else {
480 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100481 __ pushl(Address(ESP, source.GetStackIndex()));
482 __ popl(Address(ESP, destination.GetStackIndex()));
483 __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize)));
484 __ popl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100485 }
486 }
487}
488
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100489void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Roland Levillain476df552014-10-09 17:51:36 +0100490 if (instruction->IsIntConstant()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100491 Immediate imm(instruction->AsIntConstant()->GetValue());
492 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100493 __ movl(location.As<Register>(), imm);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100494 } else {
495 __ movl(Address(ESP, location.GetStackIndex()), imm);
496 }
Roland Levillain476df552014-10-09 17:51:36 +0100497 } else if (instruction->IsLongConstant()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100498 int64_t value = instruction->AsLongConstant()->GetValue();
499 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100500 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
501 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100502 } else {
503 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
504 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
505 }
Roland Levillain476df552014-10-09 17:51:36 +0100506 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100507 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100508 switch (instruction->GetType()) {
509 case Primitive::kPrimBoolean:
510 case Primitive::kPrimByte:
511 case Primitive::kPrimChar:
512 case Primitive::kPrimShort:
513 case Primitive::kPrimInt:
514 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100515 case Primitive::kPrimFloat:
516 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100517 break;
518
519 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100520 case Primitive::kPrimDouble:
521 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100522 break;
523
524 default:
525 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
526 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000527 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100528 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100529 switch (instruction->GetType()) {
530 case Primitive::kPrimBoolean:
531 case Primitive::kPrimByte:
532 case Primitive::kPrimChar:
533 case Primitive::kPrimShort:
534 case Primitive::kPrimInt:
535 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100536 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100537 Move32(location, instruction->GetLocations()->Out());
538 break;
539
540 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100541 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100542 Move64(location, instruction->GetLocations()->Out());
543 break;
544
545 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100546 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100547 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000548 }
549}
550
551void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000552 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000553}
554
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000555void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000556 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100557 DCHECK(!successor->IsExitBlock());
558
559 HBasicBlock* block = got->GetBlock();
560 HInstruction* previous = got->GetPrevious();
561
562 HLoopInformation* info = block->GetLoopInformation();
563 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
564 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
565 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
566 return;
567 }
568
569 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
570 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
571 }
572 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000573 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000574 }
575}
576
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000577void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000578 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000579}
580
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000581void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000582 if (kIsDebugBuild) {
583 __ Comment("Unreachable");
584 __ int3();
585 }
586}
587
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000588void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100589 LocationSummary* locations =
590 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100591 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100592 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100593 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100594 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000595}
596
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000597void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700598 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100599 if (cond->IsIntConstant()) {
600 // Constant condition, statically compared against 1.
601 int32_t cond_value = cond->AsIntConstant()->GetValue();
602 if (cond_value == 1) {
603 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
604 if_instr->IfTrueSuccessor())) {
605 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100606 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100607 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100608 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100609 DCHECK_EQ(cond_value, 0);
610 }
611 } else {
612 bool materialized =
613 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
614 // Moves do not affect the eflags register, so if the condition is
615 // evaluated just before the if, we don't need to evaluate it
616 // again.
617 bool eflags_set = cond->IsCondition()
618 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
619 if (materialized) {
620 if (!eflags_set) {
621 // Materialized condition, compare against 0.
622 Location lhs = if_instr->GetLocations()->InAt(0);
623 if (lhs.IsRegister()) {
624 __ cmpl(lhs.As<Register>(), Immediate(0));
625 } else {
626 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
627 }
628 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
629 } else {
630 __ j(X86Condition(cond->AsCondition()->GetCondition()),
631 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
632 }
633 } else {
634 Location lhs = cond->GetLocations()->InAt(0);
635 Location rhs = cond->GetLocations()->InAt(1);
636 // LHS is guaranteed to be in a register (see
637 // LocationsBuilderX86::VisitCondition).
638 if (rhs.IsRegister()) {
639 __ cmpl(lhs.As<Register>(), rhs.As<Register>());
640 } else if (rhs.IsConstant()) {
641 HIntConstant* instruction = rhs.GetConstant()->AsIntConstant();
642 Immediate imm(instruction->AsIntConstant()->GetValue());
643 __ cmpl(lhs.As<Register>(), imm);
644 } else {
645 __ cmpl(lhs.As<Register>(), Address(ESP, rhs.GetStackIndex()));
646 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100647 __ j(X86Condition(cond->AsCondition()->GetCondition()),
648 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700649 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100650 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100651 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
652 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700653 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000654 }
655}
656
657void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000658 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000659}
660
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000661void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
662 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000663}
664
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000665void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100666 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000667}
668
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000669void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100670 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000671}
672
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100673void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100674 LocationSummary* locations =
675 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100676 switch (store->InputAt(1)->GetType()) {
677 case Primitive::kPrimBoolean:
678 case Primitive::kPrimByte:
679 case Primitive::kPrimChar:
680 case Primitive::kPrimShort:
681 case Primitive::kPrimInt:
682 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100683 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100684 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
685 break;
686
687 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100688 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100689 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
690 break;
691
692 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100693 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100694 }
695 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000696}
697
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000698void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000699}
700
Dave Allison20dfc792014-06-16 20:44:29 -0700701void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100702 LocationSummary* locations =
703 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100704 locations->SetInAt(0, Location::RequiresRegister());
705 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100706 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100707 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100708 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000709}
710
Dave Allison20dfc792014-06-16 20:44:29 -0700711void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
712 if (comp->NeedsMaterialization()) {
713 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100714 Register reg = locations->Out().As<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100715 // Clear register: setcc only sets the low byte.
716 __ xorl(reg, reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700717 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100718 __ cmpl(locations->InAt(0).As<Register>(),
719 locations->InAt(1).As<Register>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100720 } else if (locations->InAt(1).IsConstant()) {
721 HConstant* instruction = locations->InAt(1).GetConstant();
722 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100723 __ cmpl(locations->InAt(0).As<Register>(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700724 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100725 __ cmpl(locations->InAt(0).As<Register>(),
Dave Allison20dfc792014-06-16 20:44:29 -0700726 Address(ESP, locations->InAt(1).GetStackIndex()));
727 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100728 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100729 }
Dave Allison20dfc792014-06-16 20:44:29 -0700730}
731
732void LocationsBuilderX86::VisitEqual(HEqual* comp) {
733 VisitCondition(comp);
734}
735
736void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
737 VisitCondition(comp);
738}
739
740void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
741 VisitCondition(comp);
742}
743
744void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
745 VisitCondition(comp);
746}
747
748void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
749 VisitCondition(comp);
750}
751
752void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
753 VisitCondition(comp);
754}
755
756void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
757 VisitCondition(comp);
758}
759
760void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
761 VisitCondition(comp);
762}
763
764void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
765 VisitCondition(comp);
766}
767
768void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
769 VisitCondition(comp);
770}
771
772void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
773 VisitCondition(comp);
774}
775
776void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
777 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000778}
779
780void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100781 LocationSummary* locations =
782 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100783 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000784}
785
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000786void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100787 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000788}
789
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100790void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100791 LocationSummary* locations =
792 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100793 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100794}
795
796void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
797 // Will be generated at use site.
798}
799
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100800void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
801 LocationSummary* locations =
802 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
803 locations->SetOut(Location::ConstantLocation(constant));
804}
805
806void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
807 // Will be generated at use site.
808}
809
810void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
811 LocationSummary* locations =
812 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
813 locations->SetOut(Location::ConstantLocation(constant));
814}
815
816void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
817 // Will be generated at use site.
818}
819
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000820void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000821 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000822}
823
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000824void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
825 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000826 __ ret();
827}
828
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000829void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100830 LocationSummary* locations =
831 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100832 switch (ret->InputAt(0)->GetType()) {
833 case Primitive::kPrimBoolean:
834 case Primitive::kPrimByte:
835 case Primitive::kPrimChar:
836 case Primitive::kPrimShort:
837 case Primitive::kPrimInt:
838 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100839 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100840 break;
841
842 case Primitive::kPrimLong:
843 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100844 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100845 break;
846
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100847 case Primitive::kPrimFloat:
848 case Primitive::kPrimDouble:
849 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100850 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100851 break;
852
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100853 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100854 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100855 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000856}
857
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000858void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100859 if (kIsDebugBuild) {
860 switch (ret->InputAt(0)->GetType()) {
861 case Primitive::kPrimBoolean:
862 case Primitive::kPrimByte:
863 case Primitive::kPrimChar:
864 case Primitive::kPrimShort:
865 case Primitive::kPrimInt:
866 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100867 DCHECK_EQ(ret->GetLocations()->InAt(0).As<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100868 break;
869
870 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100871 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
872 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100873 break;
874
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100875 case Primitive::kPrimFloat:
876 case Primitive::kPrimDouble:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100877 DCHECK_EQ(ret->GetLocations()->InAt(0).As<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100878 break;
879
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100880 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100881 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100882 }
883 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000884 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000885 __ ret();
886}
887
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000888void LocationsBuilderX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100889 HandleInvoke(invoke);
890}
891
892void InstructionCodeGeneratorX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100893 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100894 uint32_t heap_reference_size = sizeof(mirror::HeapReference<mirror::Object>);
895 size_t index_in_cache = mirror::Array::DataOffset(heap_reference_size).Int32Value() +
896 invoke->GetIndexInDexCache() * kX86WordSize;
897
898 // TODO: Implement all kinds of calls:
899 // 1) boot -> boot
900 // 2) app -> boot
901 // 3) app -> app
902 //
903 // Currently we implement the app -> app logic, which looks up in the resolve cache.
904
905 // temp = method;
906 LoadCurrentMethod(temp);
907 // temp = temp->dex_cache_resolved_methods_;
908 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
909 // temp = temp[index_in_cache]
910 __ movl(temp, Address(temp, index_in_cache));
911 // (temp + offset_of_quick_compiled_code)()
912 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
913
914 DCHECK(!codegen_->IsLeafMethod());
915 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
916}
917
918void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
919 HandleInvoke(invoke);
920}
921
922void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100923 LocationSummary* locations =
924 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100925 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100926
927 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100928 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100929 HInstruction* input = invoke->InputAt(i);
930 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
931 }
932
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100933 switch (invoke->GetType()) {
934 case Primitive::kPrimBoolean:
935 case Primitive::kPrimByte:
936 case Primitive::kPrimChar:
937 case Primitive::kPrimShort:
938 case Primitive::kPrimInt:
939 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100940 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100941 break;
942
943 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100944 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100945 break;
946
947 case Primitive::kPrimVoid:
948 break;
949
950 case Primitive::kPrimDouble:
951 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100952 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100953 break;
954 }
955
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000956 invoke->SetLocations(locations);
957}
958
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100959void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100960 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100961 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
962 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
963 LocationSummary* locations = invoke->GetLocations();
964 Location receiver = locations->InAt(0);
965 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
966 // temp = object->GetClass();
967 if (receiver.IsStackSlot()) {
968 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
969 __ movl(temp, Address(temp, class_offset));
970 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100971 __ movl(temp, Address(receiver.As<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100972 }
973 // temp = temp->GetMethodAt(method_offset);
974 __ movl(temp, Address(temp, method_offset));
975 // call temp->GetEntryPoint();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000976 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
977
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100978 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +0100979 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000980}
981
Roland Levillain88cb1752014-10-20 16:36:47 +0100982void LocationsBuilderX86::VisitNeg(HNeg* neg) {
983 LocationSummary* locations =
984 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
985 switch (neg->GetResultType()) {
986 case Primitive::kPrimInt:
987 locations->SetInAt(0, Location::RequiresRegister());
988 locations->SetOut(Location::SameAsFirstInput());
989 break;
990
991 case Primitive::kPrimLong:
992 case Primitive::kPrimFloat:
993 case Primitive::kPrimDouble:
994 LOG(FATAL) << "Not yet implemented neg type " << neg->GetResultType();
995 break;
996
997 default:
998 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
999 }
1000}
1001
1002void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1003 LocationSummary* locations = neg->GetLocations();
1004 Location out = locations->Out();
1005 Location in = locations->InAt(0);
1006 switch (neg->GetResultType()) {
1007 case Primitive::kPrimInt:
1008 DCHECK(in.IsRegister());
1009 __ negl(out.As<Register>());
1010 break;
1011
1012 case Primitive::kPrimLong:
1013 case Primitive::kPrimFloat:
1014 case Primitive::kPrimDouble:
1015 LOG(FATAL) << "Not yet implemented neg type " << neg->GetResultType();
1016 break;
1017
1018 default:
1019 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1020 }
1021}
1022
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001023void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001024 LocationSummary* locations =
1025 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001026 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001027 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001028 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001029 locations->SetInAt(0, Location::RequiresRegister());
1030 locations->SetInAt(1, Location::Any());
1031 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001032 break;
1033 }
1034
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001035 case Primitive::kPrimFloat:
1036 case Primitive::kPrimDouble: {
1037 locations->SetInAt(0, Location::RequiresFpuRegister());
1038 locations->SetInAt(1, Location::Any());
1039 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001040 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001041 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001042
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001043 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001044 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1045 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001046 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001047}
1048
1049void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1050 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001051 Location first = locations->InAt(0);
1052 Location second = locations->InAt(1);
1053
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001054 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001055 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001056 DCHECK_EQ(first.As<Register>(), locations->Out().As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001057 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001058 __ addl(first.As<Register>(), second.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001059 } else if (second.IsConstant()) {
1060 HConstant* instruction = second.GetConstant();
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001061 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001062 __ addl(first.As<Register>(), imm);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001063 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001064 __ addl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001065 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001066 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001067 }
1068
1069 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001070 DCHECK_EQ(first.AsRegisterPairLow<Register>(),
1071 locations->Out().AsRegisterPairLow<Register>());
1072 DCHECK_EQ(first.AsRegisterPairHigh<Register>(),
1073 locations->Out().AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001074 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001075 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1076 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001077 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001078 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1079 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001080 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001081 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001082 break;
1083 }
1084
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001085 case Primitive::kPrimFloat: {
1086 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001087 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001088 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001089 __ addss(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001090 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001091 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001092 }
1093
1094 case Primitive::kPrimDouble: {
1095 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001096 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001097 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001098 __ addsd(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001099 }
1100 break;
1101 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001102
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001103 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001104 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001105 }
1106}
1107
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001108void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001109 LocationSummary* locations =
1110 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001111 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001112 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001113 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001114 locations->SetInAt(0, Location::RequiresRegister());
1115 locations->SetInAt(1, Location::Any());
1116 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001117 break;
1118 }
1119
1120 case Primitive::kPrimBoolean:
1121 case Primitive::kPrimByte:
1122 case Primitive::kPrimChar:
1123 case Primitive::kPrimShort:
1124 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
1125 break;
1126
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001127 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001128 LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001129 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001130}
1131
1132void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
1133 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001134 Location first = locations->InAt(0);
1135 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001136 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001137 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001138 DCHECK_EQ(first.As<Register>(),
1139 locations->Out().As<Register>());
1140 if (second.IsRegister()) {
1141 __ subl(first.As<Register>(),
1142 second.As<Register>());
1143 } else if (second.IsConstant()) {
1144 HConstant* instruction = second.GetConstant();
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001145 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001146 __ subl(first.As<Register>(), imm);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001147 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001148 __ subl(first.As<Register>(),
1149 Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001150 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001151 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001152 }
1153
1154 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001155 DCHECK_EQ(first.AsRegisterPairLow<Register>(),
1156 locations->Out().AsRegisterPairLow<Register>());
1157 DCHECK_EQ(first.AsRegisterPairHigh<Register>(),
1158 locations->Out().AsRegisterPairHigh<Register>());
1159 if (second.IsRegister()) {
1160 __ subl(first.AsRegisterPairLow<Register>(),
1161 second.AsRegisterPairLow<Register>());
1162 __ sbbl(first.AsRegisterPairHigh<Register>(),
1163 second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001164 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001165 __ subl(first.AsRegisterPairLow<Register>(),
1166 Address(ESP, second.GetStackIndex()));
1167 __ sbbl(first.AsRegisterPairHigh<Register>(),
1168 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001169 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001170 break;
1171 }
1172
1173 case Primitive::kPrimBoolean:
1174 case Primitive::kPrimByte:
1175 case Primitive::kPrimChar:
1176 case Primitive::kPrimShort:
1177 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
1178 break;
1179
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001180 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001181 LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001182 }
1183}
1184
Calin Juravle34bacdf2014-10-07 20:23:36 +01001185void LocationsBuilderX86::VisitMul(HMul* mul) {
1186 LocationSummary* locations =
1187 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1188 switch (mul->GetResultType()) {
1189 case Primitive::kPrimInt:
1190 locations->SetInAt(0, Location::RequiresRegister());
1191 locations->SetInAt(1, Location::Any());
1192 locations->SetOut(Location::SameAsFirstInput());
1193 break;
1194 case Primitive::kPrimLong: {
1195 locations->SetInAt(0, Location::RequiresRegister());
1196 // TODO: Currently this handles only stack operands:
1197 // - we don't have enough registers because we currently use Quick ABI.
1198 // - by the time we have a working register allocator we will probably change the ABI
1199 // and fix the above.
1200 // - we don't have a way yet to request operands on stack but the base line compiler
1201 // will leave the operands on the stack with Any().
1202 locations->SetInAt(1, Location::Any());
1203 locations->SetOut(Location::SameAsFirstInput());
1204 // Needed for imul on 32bits with 64bits output.
1205 locations->AddTemp(Location::RegisterLocation(EAX));
1206 locations->AddTemp(Location::RegisterLocation(EDX));
1207 break;
1208 }
1209
1210 case Primitive::kPrimBoolean:
1211 case Primitive::kPrimByte:
1212 case Primitive::kPrimChar:
1213 case Primitive::kPrimShort:
1214 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
1215 break;
1216
1217 default:
1218 LOG(FATAL) << "Unimplemented mul type " << mul->GetResultType();
1219 }
1220}
1221
1222void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
1223 LocationSummary* locations = mul->GetLocations();
1224 Location first = locations->InAt(0);
1225 Location second = locations->InAt(1);
1226 DCHECK(first.Equals(locations->Out()));
1227
1228 switch (mul->GetResultType()) {
1229 case Primitive::kPrimInt: {
1230 if (second.IsRegister()) {
1231 __ imull(first.As<Register>(), second.As<Register>());
1232 } else if (second.IsConstant()) {
1233 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1234 __ imull(first.As<Register>(), imm);
1235 } else {
1236 DCHECK(second.IsStackSlot());
1237 __ imull(first.As<Register>(), Address(ESP, second.GetStackIndex()));
1238 }
1239 break;
1240 }
1241
1242 case Primitive::kPrimLong: {
1243 DCHECK(second.IsDoubleStackSlot());
1244
1245 Register in1_hi = first.AsRegisterPairHigh<Register>();
1246 Register in1_lo = first.AsRegisterPairLow<Register>();
1247 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
1248 Address in2_lo(ESP, second.GetStackIndex());
1249 Register eax = locations->GetTemp(0).As<Register>();
1250 Register edx = locations->GetTemp(1).As<Register>();
1251
1252 DCHECK_EQ(EAX, eax);
1253 DCHECK_EQ(EDX, edx);
1254
1255 // input: in1 - 64 bits, in2 - 64 bits
1256 // output: in1
1257 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
1258 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
1259 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
1260
1261 __ movl(eax, in2_hi);
1262 // eax <- in1.lo * in2.hi
1263 __ imull(eax, in1_lo);
1264 // in1.hi <- in1.hi * in2.lo
1265 __ imull(in1_hi, in2_lo);
1266 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
1267 __ addl(in1_hi, eax);
1268 // move in1_lo to eax to prepare for double precision
1269 __ movl(eax, in1_lo);
1270 // edx:eax <- in1.lo * in2.lo
1271 __ mull(in2_lo);
1272 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
1273 __ addl(in1_hi, edx);
1274 // in1.lo <- (in1.lo * in2.lo)[31:0];
1275 __ movl(in1_lo, eax);
1276
1277 break;
1278 }
1279
1280 case Primitive::kPrimBoolean:
1281 case Primitive::kPrimByte:
1282 case Primitive::kPrimChar:
1283 case Primitive::kPrimShort:
1284 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
1285 break;
1286
1287 default:
1288 LOG(FATAL) << "Unimplemented mul type " << mul->GetResultType();
1289 }
1290}
1291
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001292void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001293 LocationSummary* locations =
1294 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001295 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001296 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001297 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1298 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001299}
1300
1301void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
1302 InvokeRuntimeCallingConvention calling_convention;
1303 LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001304 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001305
Nicolas Geoffray707c8092014-04-04 10:50:14 +01001306 __ fs()->call(
1307 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001308
Nicolas Geoffray39468442014-09-02 15:17:15 +01001309 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001310 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001311}
1312
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001313void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001314 LocationSummary* locations =
1315 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001316 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1317 if (location.IsStackSlot()) {
1318 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1319 } else if (location.IsDoubleStackSlot()) {
1320 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001321 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001322 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001323}
1324
1325void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001326}
1327
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001328void LocationsBuilderX86::VisitNot(HNot* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001329 LocationSummary* locations =
1330 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001331 locations->SetInAt(0, Location::RequiresRegister());
1332 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001333}
1334
1335void InstructionCodeGeneratorX86::VisitNot(HNot* instruction) {
1336 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001337 Location out = locations->Out();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001338 DCHECK_EQ(locations->InAt(0).As<Register>(), out.As<Register>());
1339 __ xorl(out.As<Register>(), Immediate(1));
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001340}
1341
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001342void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001343 LocationSummary* locations =
1344 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001345 locations->SetInAt(0, Location::RequiresRegister());
1346 locations->SetInAt(1, Location::Any());
1347 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001348}
1349
1350void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
1351 Label greater, done;
1352 LocationSummary* locations = compare->GetLocations();
1353 switch (compare->InputAt(0)->GetType()) {
1354 case Primitive::kPrimLong: {
1355 Label less, greater, done;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001356 Register output = locations->Out().As<Register>();
1357 Location left = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001358 Location right = locations->InAt(1);
1359 if (right.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001360 __ cmpl(left.AsRegisterPairHigh<Register>(), right.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001361 } else {
1362 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001363 __ cmpl(left.AsRegisterPairHigh<Register>(),
1364 Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001365 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001366 __ j(kLess, &less); // Signed compare.
1367 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001368 if (right.IsRegisterPair()) {
1369 __ cmpl(left.AsRegisterPairLow<Register>(), right.AsRegisterPairLow<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001370 } else {
1371 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001372 __ cmpl(left.AsRegisterPairLow<Register>(), Address(ESP, right.GetStackIndex()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001373 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001374 __ movl(output, Immediate(0));
1375 __ j(kEqual, &done);
1376 __ j(kBelow, &less); // Unsigned compare.
1377
1378 __ Bind(&greater);
1379 __ movl(output, Immediate(1));
1380 __ jmp(&done);
1381
1382 __ Bind(&less);
1383 __ movl(output, Immediate(-1));
1384
1385 __ Bind(&done);
1386 break;
1387 }
1388 default:
1389 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
1390 }
1391}
1392
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001393void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001394 LocationSummary* locations =
1395 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001396 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1397 locations->SetInAt(i, Location::Any());
1398 }
1399 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001400}
1401
1402void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001403 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001404}
1405
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001406void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001407 LocationSummary* locations =
1408 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001409 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001410 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001411 bool is_object_type = field_type == Primitive::kPrimNot;
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001412 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
1413 || (field_type == Primitive::kPrimByte);
1414 // The register allocator does not support multiple
1415 // inputs that die at entry with one in a specific register.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001416 if (is_byte_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001417 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001418 locations->SetInAt(1, Location::RegisterLocation(EAX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001419 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001420 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001421 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001422 // Temporary registers for the write barrier.
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001423 if (is_object_type) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001424 locations->AddTemp(Location::RequiresRegister());
1425 // Ensure the card is in a byte register.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001426 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001427 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001428}
1429
1430void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1431 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001432 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001433 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001434 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001435
1436 switch (field_type) {
1437 case Primitive::kPrimBoolean:
1438 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001439 ByteRegister value = locations->InAt(1).As<ByteRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001440 __ movb(Address(obj, offset), value);
1441 break;
1442 }
1443
1444 case Primitive::kPrimShort:
1445 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001446 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001447 __ movw(Address(obj, offset), value);
1448 break;
1449 }
1450
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001451 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001452 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001453 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001454 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001455
1456 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001457 Register temp = locations->GetTemp(0).As<Register>();
1458 Register card = locations->GetTemp(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001459 codegen_->MarkGCCard(temp, card, obj, value);
1460 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001461 break;
1462 }
1463
1464 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001465 Location value = locations->InAt(1);
1466 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
1467 __ movl(Address(obj, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001468 break;
1469 }
1470
1471 case Primitive::kPrimFloat:
1472 case Primitive::kPrimDouble:
1473 LOG(FATAL) << "Unimplemented register type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001474 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001475 case Primitive::kPrimVoid:
1476 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001477 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001478 }
1479}
1480
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001481void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
1482 Label is_null;
1483 __ testl(value, value);
1484 __ j(kEqual, &is_null);
1485 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
1486 __ movl(temp, object);
1487 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
1488 __ movb(Address(temp, card, TIMES_1, 0),
1489 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
1490 __ Bind(&is_null);
1491}
1492
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001493void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001494 LocationSummary* locations =
1495 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001496 locations->SetInAt(0, Location::RequiresRegister());
1497 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001498}
1499
1500void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1501 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001502 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001503 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
1504
1505 switch (instruction->GetType()) {
1506 case Primitive::kPrimBoolean: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001507 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001508 __ movzxb(out, Address(obj, offset));
1509 break;
1510 }
1511
1512 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001513 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001514 __ movsxb(out, Address(obj, offset));
1515 break;
1516 }
1517
1518 case Primitive::kPrimShort: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001519 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001520 __ movsxw(out, Address(obj, offset));
1521 break;
1522 }
1523
1524 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001525 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001526 __ movzxw(out, Address(obj, offset));
1527 break;
1528 }
1529
1530 case Primitive::kPrimInt:
1531 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001532 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001533 __ movl(out, Address(obj, offset));
1534 break;
1535 }
1536
1537 case Primitive::kPrimLong: {
1538 // TODO: support volatile.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001539 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(obj, offset));
1540 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(obj, kX86WordSize + offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001541 break;
1542 }
1543
1544 case Primitive::kPrimFloat:
1545 case Primitive::kPrimDouble:
1546 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001547 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001548 case Primitive::kPrimVoid:
1549 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001550 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001551 }
1552}
1553
1554void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001555 LocationSummary* locations =
1556 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001557 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001558 if (instruction->HasUses()) {
1559 locations->SetOut(Location::SameAsFirstInput());
1560 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001561}
1562
1563void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001564 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001565 codegen_->AddSlowPath(slow_path);
1566
1567 LocationSummary* locations = instruction->GetLocations();
1568 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001569
1570 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001571 __ cmpl(obj.As<Register>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001572 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001573 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001574 } else {
1575 DCHECK(obj.IsConstant()) << obj;
1576 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
1577 __ jmp(slow_path->GetEntryLabel());
1578 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001579 }
1580 __ j(kEqual, slow_path->GetEntryLabel());
1581}
1582
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001583void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001584 LocationSummary* locations =
1585 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001586 locations->SetInAt(0, Location::RequiresRegister());
1587 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1588 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001589}
1590
1591void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
1592 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001593 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001594 Location index = locations->InAt(1);
1595
1596 switch (instruction->GetType()) {
1597 case Primitive::kPrimBoolean: {
1598 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001599 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001600 if (index.IsConstant()) {
1601 __ movzxb(out, Address(obj,
1602 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1603 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001604 __ movzxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001605 }
1606 break;
1607 }
1608
1609 case Primitive::kPrimByte: {
1610 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001611 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001612 if (index.IsConstant()) {
1613 __ movsxb(out, Address(obj,
1614 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1615 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001616 __ movsxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001617 }
1618 break;
1619 }
1620
1621 case Primitive::kPrimShort: {
1622 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001623 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001624 if (index.IsConstant()) {
1625 __ movsxw(out, Address(obj,
1626 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1627 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001628 __ movsxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001629 }
1630 break;
1631 }
1632
1633 case Primitive::kPrimChar: {
1634 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001635 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001636 if (index.IsConstant()) {
1637 __ movzxw(out, Address(obj,
1638 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1639 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001640 __ movzxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001641 }
1642 break;
1643 }
1644
1645 case Primitive::kPrimInt:
1646 case Primitive::kPrimNot: {
1647 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001648 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001649 if (index.IsConstant()) {
1650 __ movl(out, Address(obj,
1651 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
1652 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001653 __ movl(out, Address(obj, index.As<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001654 }
1655 break;
1656 }
1657
1658 case Primitive::kPrimLong: {
1659 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001660 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001661 if (index.IsConstant()) {
1662 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001663 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
1664 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001665 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001666 __ movl(out.AsRegisterPairLow<Register>(),
1667 Address(obj, index.As<Register>(), TIMES_8, data_offset));
1668 __ movl(out.AsRegisterPairHigh<Register>(),
1669 Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001670 }
1671 break;
1672 }
1673
1674 case Primitive::kPrimFloat:
1675 case Primitive::kPrimDouble:
1676 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001677 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001678 case Primitive::kPrimVoid:
1679 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001680 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001681 }
1682}
1683
1684void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001685 Primitive::Type value_type = instruction->GetComponentType();
1686 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1687 instruction,
1688 value_type == Primitive::kPrimNot ? LocationSummary::kCall : LocationSummary::kNoCall);
1689
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001690 if (value_type == Primitive::kPrimNot) {
1691 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001692 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1693 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1694 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001695 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001696 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
1697 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001698 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001699 // In case of a byte operation, the register allocator does not support multiple
1700 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001701 locations->SetInAt(0, Location::RequiresRegister());
1702 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001703 if (is_byte_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001704 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001705 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001706 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001707 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001708 }
1709 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001710}
1711
1712void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
1713 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001714 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001715 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001716 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001717 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001718
1719 switch (value_type) {
1720 case Primitive::kPrimBoolean:
1721 case Primitive::kPrimByte: {
1722 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001723 if (index.IsConstant()) {
1724 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001725 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001726 __ movb(Address(obj, offset), value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001727 } else {
1728 __ movb(Address(obj, offset),
1729 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1730 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001731 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001732 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001733 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
1734 value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001735 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001736 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001737 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1738 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001739 }
1740 break;
1741 }
1742
1743 case Primitive::kPrimShort:
1744 case Primitive::kPrimChar: {
1745 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001746 if (index.IsConstant()) {
1747 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001748 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001749 __ movw(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001750 } else {
1751 __ movw(Address(obj, offset),
1752 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1753 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001754 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001755 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001756 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
1757 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001758 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001759 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001760 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1761 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001762 }
1763 break;
1764 }
1765
1766 case Primitive::kPrimInt: {
1767 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001768 if (index.IsConstant()) {
1769 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001770 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001771 __ movl(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001772 } else {
1773 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1774 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001775 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001776 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001777 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
1778 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001779 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001780 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001781 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1782 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001783 }
1784 break;
1785 }
1786
1787 case Primitive::kPrimNot: {
1788 DCHECK(!codegen_->IsLeafMethod());
1789 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001790 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001791 break;
1792 }
1793
1794 case Primitive::kPrimLong: {
1795 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001796 if (index.IsConstant()) {
1797 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001798 if (value.IsRegisterPair()) {
1799 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
1800 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001801 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001802 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001803 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
1804 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
1805 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
1806 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001807 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001808 if (value.IsRegisterPair()) {
1809 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
1810 value.AsRegisterPairLow<Register>());
1811 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
1812 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001813 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001814 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001815 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001816 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001817 Immediate(Low32Bits(val)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001818 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001819 Immediate(High32Bits(val)));
1820 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001821 }
1822 break;
1823 }
1824
1825 case Primitive::kPrimFloat:
1826 case Primitive::kPrimDouble:
1827 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001828 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001829 case Primitive::kPrimVoid:
1830 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001831 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001832 }
1833}
1834
1835void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
1836 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001837 locations->SetInAt(0, Location::RequiresRegister());
1838 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001839 instruction->SetLocations(locations);
1840}
1841
1842void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
1843 LocationSummary* locations = instruction->GetLocations();
1844 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001845 Register obj = locations->InAt(0).As<Register>();
1846 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001847 __ movl(out, Address(obj, offset));
1848}
1849
1850void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001851 LocationSummary* locations =
1852 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001853 locations->SetInAt(0, Location::RequiresRegister());
1854 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001855 if (instruction->HasUses()) {
1856 locations->SetOut(Location::SameAsFirstInput());
1857 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001858}
1859
1860void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
1861 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001862 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01001863 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001864 codegen_->AddSlowPath(slow_path);
1865
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001866 Register index = locations->InAt(0).As<Register>();
1867 Register length = locations->InAt(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001868
1869 __ cmpl(index, length);
1870 __ j(kAboveEqual, slow_path->GetEntryLabel());
1871}
1872
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001873void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
1874 temp->SetLocations(nullptr);
1875}
1876
1877void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
1878 // Nothing to do, this is driven by the code generator.
1879}
1880
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001881void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001882 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001883}
1884
1885void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001886 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
1887}
1888
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001889void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
1890 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
1891}
1892
1893void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001894 HBasicBlock* block = instruction->GetBlock();
1895 if (block->GetLoopInformation() != nullptr) {
1896 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
1897 // The back edge will generate the suspend check.
1898 return;
1899 }
1900 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
1901 // The goto will generate the suspend check.
1902 return;
1903 }
1904 GenerateSuspendCheck(instruction, nullptr);
1905}
1906
1907void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
1908 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001909 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001910 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001911 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001912 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001913 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001914 if (successor == nullptr) {
1915 __ j(kNotEqual, slow_path->GetEntryLabel());
1916 __ Bind(slow_path->GetReturnLabel());
1917 } else {
1918 __ j(kEqual, codegen_->GetLabelOf(successor));
1919 __ jmp(slow_path->GetEntryLabel());
1920 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001921}
1922
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001923X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
1924 return codegen_->GetAssembler();
1925}
1926
1927void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
1928 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001929 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001930 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
1931 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
1932 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
1933}
1934
1935void ParallelMoveResolverX86::EmitMove(size_t index) {
1936 MoveOperands* move = moves_.Get(index);
1937 Location source = move->GetSource();
1938 Location destination = move->GetDestination();
1939
1940 if (source.IsRegister()) {
1941 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001942 __ movl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001943 } else {
1944 DCHECK(destination.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001945 __ movl(Address(ESP, destination.GetStackIndex()), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001946 }
1947 } else if (source.IsStackSlot()) {
1948 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001949 __ movl(destination.As<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001950 } else {
1951 DCHECK(destination.IsStackSlot());
1952 MoveMemoryToMemory(destination.GetStackIndex(),
1953 source.GetStackIndex());
1954 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001955 } else if (source.IsConstant()) {
1956 HIntConstant* instruction = source.GetConstant()->AsIntConstant();
1957 Immediate imm(instruction->AsIntConstant()->GetValue());
1958 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001959 __ movl(destination.As<Register>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001960 } else {
1961 __ movl(Address(ESP, destination.GetStackIndex()), imm);
1962 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001963 } else {
1964 LOG(FATAL) << "Unimplemented";
1965 }
1966}
1967
1968void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001969 Register suggested_scratch = reg == EAX ? EBX : EAX;
1970 ScratchRegisterScope ensure_scratch(
1971 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
1972
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001973 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
1974 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
1975 __ movl(Address(ESP, mem + stack_offset), reg);
1976 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
1977}
1978
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001979void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
1980 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001981 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
1982
1983 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001984 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001985 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
1986
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001987 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
1988 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
1989 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
1990 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
1991 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
1992 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
1993}
1994
1995void ParallelMoveResolverX86::EmitSwap(size_t index) {
1996 MoveOperands* move = moves_.Get(index);
1997 Location source = move->GetSource();
1998 Location destination = move->GetDestination();
1999
2000 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002001 __ xchgl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002002 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002003 Exchange(source.As<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002004 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002005 Exchange(destination.As<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002006 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
2007 Exchange(destination.GetStackIndex(), source.GetStackIndex());
2008 } else {
2009 LOG(FATAL) << "Unimplemented";
2010 }
2011}
2012
2013void ParallelMoveResolverX86::SpillScratch(int reg) {
2014 __ pushl(static_cast<Register>(reg));
2015}
2016
2017void ParallelMoveResolverX86::RestoreScratch(int reg) {
2018 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002019}
2020
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002021} // namespace x86
2022} // namespace art