blob: acf410367b7599cf3c8d2abc68b2f2e7728fe348 [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 Geoffraya3d05a42014-10-20 17:41:32 +0100494 } else if (location.IsStackSlot()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100495 __ movl(Address(ESP, location.GetStackIndex()), imm);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100496 } else {
497 DCHECK(location.IsConstant());
498 DCHECK_EQ(location.GetConstant(), instruction);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100499 }
Roland Levillain476df552014-10-09 17:51:36 +0100500 } else if (instruction->IsLongConstant()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100501 int64_t value = instruction->AsLongConstant()->GetValue();
502 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100503 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
504 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100505 } else if (location.IsDoubleStackSlot()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100506 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
507 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100508 } else {
509 DCHECK(location.IsConstant());
510 DCHECK_EQ(location.GetConstant(), instruction);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100511 }
Roland Levillain476df552014-10-09 17:51:36 +0100512 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100513 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100514 switch (instruction->GetType()) {
515 case Primitive::kPrimBoolean:
516 case Primitive::kPrimByte:
517 case Primitive::kPrimChar:
518 case Primitive::kPrimShort:
519 case Primitive::kPrimInt:
520 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100521 case Primitive::kPrimFloat:
522 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100523 break;
524
525 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100526 case Primitive::kPrimDouble:
527 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100528 break;
529
530 default:
531 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
532 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000533 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100534 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100535 switch (instruction->GetType()) {
536 case Primitive::kPrimBoolean:
537 case Primitive::kPrimByte:
538 case Primitive::kPrimChar:
539 case Primitive::kPrimShort:
540 case Primitive::kPrimInt:
541 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100542 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100543 Move32(location, instruction->GetLocations()->Out());
544 break;
545
546 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100547 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100548 Move64(location, instruction->GetLocations()->Out());
549 break;
550
551 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100552 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100553 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000554 }
555}
556
557void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000558 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000559}
560
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000561void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000562 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100563 DCHECK(!successor->IsExitBlock());
564
565 HBasicBlock* block = got->GetBlock();
566 HInstruction* previous = got->GetPrevious();
567
568 HLoopInformation* info = block->GetLoopInformation();
569 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
570 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
571 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
572 return;
573 }
574
575 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
576 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
577 }
578 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000579 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000580 }
581}
582
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000583void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000584 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000585}
586
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000587void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000588 if (kIsDebugBuild) {
589 __ Comment("Unreachable");
590 __ int3();
591 }
592}
593
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000594void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100595 LocationSummary* locations =
596 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100597 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100598 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100599 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100600 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000601}
602
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000603void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700604 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100605 if (cond->IsIntConstant()) {
606 // Constant condition, statically compared against 1.
607 int32_t cond_value = cond->AsIntConstant()->GetValue();
608 if (cond_value == 1) {
609 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
610 if_instr->IfTrueSuccessor())) {
611 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100612 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100613 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100614 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100615 DCHECK_EQ(cond_value, 0);
616 }
617 } else {
618 bool materialized =
619 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
620 // Moves do not affect the eflags register, so if the condition is
621 // evaluated just before the if, we don't need to evaluate it
622 // again.
623 bool eflags_set = cond->IsCondition()
624 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
625 if (materialized) {
626 if (!eflags_set) {
627 // Materialized condition, compare against 0.
628 Location lhs = if_instr->GetLocations()->InAt(0);
629 if (lhs.IsRegister()) {
630 __ cmpl(lhs.As<Register>(), Immediate(0));
631 } else {
632 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
633 }
634 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
635 } else {
636 __ j(X86Condition(cond->AsCondition()->GetCondition()),
637 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
638 }
639 } else {
640 Location lhs = cond->GetLocations()->InAt(0);
641 Location rhs = cond->GetLocations()->InAt(1);
642 // LHS is guaranteed to be in a register (see
643 // LocationsBuilderX86::VisitCondition).
644 if (rhs.IsRegister()) {
645 __ cmpl(lhs.As<Register>(), rhs.As<Register>());
646 } else if (rhs.IsConstant()) {
647 HIntConstant* instruction = rhs.GetConstant()->AsIntConstant();
648 Immediate imm(instruction->AsIntConstant()->GetValue());
649 __ cmpl(lhs.As<Register>(), imm);
650 } else {
651 __ cmpl(lhs.As<Register>(), Address(ESP, rhs.GetStackIndex()));
652 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100653 __ j(X86Condition(cond->AsCondition()->GetCondition()),
654 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700655 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100656 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100657 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
658 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700659 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000660 }
661}
662
663void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000664 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000665}
666
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000667void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
668 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000669}
670
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000671void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100672 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000673}
674
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000675void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100676 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000677}
678
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100679void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100680 LocationSummary* locations =
681 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100682 switch (store->InputAt(1)->GetType()) {
683 case Primitive::kPrimBoolean:
684 case Primitive::kPrimByte:
685 case Primitive::kPrimChar:
686 case Primitive::kPrimShort:
687 case Primitive::kPrimInt:
688 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100689 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100690 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
691 break;
692
693 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100694 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100695 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
696 break;
697
698 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100699 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100700 }
701 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000702}
703
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000704void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000705}
706
Dave Allison20dfc792014-06-16 20:44:29 -0700707void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100708 LocationSummary* locations =
709 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100710 locations->SetInAt(0, Location::RequiresRegister());
711 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100712 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100713 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100714 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000715}
716
Dave Allison20dfc792014-06-16 20:44:29 -0700717void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
718 if (comp->NeedsMaterialization()) {
719 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100720 Register reg = locations->Out().As<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100721 // Clear register: setcc only sets the low byte.
722 __ xorl(reg, reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700723 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100724 __ cmpl(locations->InAt(0).As<Register>(),
725 locations->InAt(1).As<Register>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100726 } else if (locations->InAt(1).IsConstant()) {
727 HConstant* instruction = locations->InAt(1).GetConstant();
728 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100729 __ cmpl(locations->InAt(0).As<Register>(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700730 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100731 __ cmpl(locations->InAt(0).As<Register>(),
Dave Allison20dfc792014-06-16 20:44:29 -0700732 Address(ESP, locations->InAt(1).GetStackIndex()));
733 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100734 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100735 }
Dave Allison20dfc792014-06-16 20:44:29 -0700736}
737
738void LocationsBuilderX86::VisitEqual(HEqual* comp) {
739 VisitCondition(comp);
740}
741
742void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
743 VisitCondition(comp);
744}
745
746void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
747 VisitCondition(comp);
748}
749
750void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
751 VisitCondition(comp);
752}
753
754void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
755 VisitCondition(comp);
756}
757
758void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
759 VisitCondition(comp);
760}
761
762void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
763 VisitCondition(comp);
764}
765
766void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
767 VisitCondition(comp);
768}
769
770void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
771 VisitCondition(comp);
772}
773
774void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
775 VisitCondition(comp);
776}
777
778void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
779 VisitCondition(comp);
780}
781
782void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
783 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000784}
785
786void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100787 LocationSummary* locations =
788 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100789 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000790}
791
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000792void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100793 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000794}
795
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100796void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100797 LocationSummary* locations =
798 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100799 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100800}
801
802void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
803 // Will be generated at use site.
804}
805
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100806void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
807 LocationSummary* locations =
808 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
809 locations->SetOut(Location::ConstantLocation(constant));
810}
811
812void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
813 // Will be generated at use site.
814}
815
816void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
817 LocationSummary* locations =
818 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
819 locations->SetOut(Location::ConstantLocation(constant));
820}
821
822void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
823 // Will be generated at use site.
824}
825
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000826void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000827 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000828}
829
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000830void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
831 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000832 __ ret();
833}
834
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000835void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100836 LocationSummary* locations =
837 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100838 switch (ret->InputAt(0)->GetType()) {
839 case Primitive::kPrimBoolean:
840 case Primitive::kPrimByte:
841 case Primitive::kPrimChar:
842 case Primitive::kPrimShort:
843 case Primitive::kPrimInt:
844 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100845 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100846 break;
847
848 case Primitive::kPrimLong:
849 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100850 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100851 break;
852
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100853 case Primitive::kPrimFloat:
854 case Primitive::kPrimDouble:
855 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100856 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100857 break;
858
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100859 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100860 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100861 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000862}
863
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000864void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100865 if (kIsDebugBuild) {
866 switch (ret->InputAt(0)->GetType()) {
867 case Primitive::kPrimBoolean:
868 case Primitive::kPrimByte:
869 case Primitive::kPrimChar:
870 case Primitive::kPrimShort:
871 case Primitive::kPrimInt:
872 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100873 DCHECK_EQ(ret->GetLocations()->InAt(0).As<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100874 break;
875
876 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100877 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
878 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100879 break;
880
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100881 case Primitive::kPrimFloat:
882 case Primitive::kPrimDouble:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100883 DCHECK_EQ(ret->GetLocations()->InAt(0).As<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100884 break;
885
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100886 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100887 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100888 }
889 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000890 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000891 __ ret();
892}
893
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000894void LocationsBuilderX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100895 HandleInvoke(invoke);
896}
897
898void InstructionCodeGeneratorX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100899 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100900 uint32_t heap_reference_size = sizeof(mirror::HeapReference<mirror::Object>);
901 size_t index_in_cache = mirror::Array::DataOffset(heap_reference_size).Int32Value() +
902 invoke->GetIndexInDexCache() * kX86WordSize;
903
904 // TODO: Implement all kinds of calls:
905 // 1) boot -> boot
906 // 2) app -> boot
907 // 3) app -> app
908 //
909 // Currently we implement the app -> app logic, which looks up in the resolve cache.
910
911 // temp = method;
912 LoadCurrentMethod(temp);
913 // temp = temp->dex_cache_resolved_methods_;
914 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
915 // temp = temp[index_in_cache]
916 __ movl(temp, Address(temp, index_in_cache));
917 // (temp + offset_of_quick_compiled_code)()
918 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
919
920 DCHECK(!codegen_->IsLeafMethod());
921 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
922}
923
924void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
925 HandleInvoke(invoke);
926}
927
928void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100929 LocationSummary* locations =
930 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100931 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100932
933 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100934 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100935 HInstruction* input = invoke->InputAt(i);
936 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
937 }
938
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100939 switch (invoke->GetType()) {
940 case Primitive::kPrimBoolean:
941 case Primitive::kPrimByte:
942 case Primitive::kPrimChar:
943 case Primitive::kPrimShort:
944 case Primitive::kPrimInt:
945 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100946 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100947 break;
948
949 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100950 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100951 break;
952
953 case Primitive::kPrimVoid:
954 break;
955
956 case Primitive::kPrimDouble:
957 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100958 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100959 break;
960 }
961
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000962 invoke->SetLocations(locations);
963}
964
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100965void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100966 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100967 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
968 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
969 LocationSummary* locations = invoke->GetLocations();
970 Location receiver = locations->InAt(0);
971 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
972 // temp = object->GetClass();
973 if (receiver.IsStackSlot()) {
974 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
975 __ movl(temp, Address(temp, class_offset));
976 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100977 __ movl(temp, Address(receiver.As<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100978 }
979 // temp = temp->GetMethodAt(method_offset);
980 __ movl(temp, Address(temp, method_offset));
981 // call temp->GetEntryPoint();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000982 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
983
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100984 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +0100985 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000986}
987
Roland Levillain88cb1752014-10-20 16:36:47 +0100988void LocationsBuilderX86::VisitNeg(HNeg* neg) {
989 LocationSummary* locations =
990 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
991 switch (neg->GetResultType()) {
992 case Primitive::kPrimInt:
993 locations->SetInAt(0, Location::RequiresRegister());
994 locations->SetOut(Location::SameAsFirstInput());
995 break;
996
997 case Primitive::kPrimLong:
998 case Primitive::kPrimFloat:
999 case Primitive::kPrimDouble:
1000 LOG(FATAL) << "Not yet implemented neg type " << neg->GetResultType();
1001 break;
1002
1003 default:
1004 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1005 }
1006}
1007
1008void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1009 LocationSummary* locations = neg->GetLocations();
1010 Location out = locations->Out();
1011 Location in = locations->InAt(0);
1012 switch (neg->GetResultType()) {
1013 case Primitive::kPrimInt:
1014 DCHECK(in.IsRegister());
1015 __ negl(out.As<Register>());
1016 break;
1017
1018 case Primitive::kPrimLong:
1019 case Primitive::kPrimFloat:
1020 case Primitive::kPrimDouble:
1021 LOG(FATAL) << "Not yet implemented neg type " << neg->GetResultType();
1022 break;
1023
1024 default:
1025 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1026 }
1027}
1028
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001029void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001030 LocationSummary* locations =
1031 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001032 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001033 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001034 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001035 locations->SetInAt(0, Location::RequiresRegister());
1036 locations->SetInAt(1, Location::Any());
1037 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001038 break;
1039 }
1040
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001041 case Primitive::kPrimFloat:
1042 case Primitive::kPrimDouble: {
1043 locations->SetInAt(0, Location::RequiresFpuRegister());
1044 locations->SetInAt(1, Location::Any());
1045 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001046 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001047 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001048
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001049 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001050 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1051 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001052 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001053}
1054
1055void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1056 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001057 Location first = locations->InAt(0);
1058 Location second = locations->InAt(1);
1059
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001060 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001061 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001062 DCHECK_EQ(first.As<Register>(), locations->Out().As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001063 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001064 __ addl(first.As<Register>(), second.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001065 } else if (second.IsConstant()) {
1066 HConstant* instruction = second.GetConstant();
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001067 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001068 __ addl(first.As<Register>(), imm);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001069 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001070 __ addl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001071 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001072 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001073 }
1074
1075 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001076 DCHECK_EQ(first.AsRegisterPairLow<Register>(),
1077 locations->Out().AsRegisterPairLow<Register>());
1078 DCHECK_EQ(first.AsRegisterPairHigh<Register>(),
1079 locations->Out().AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001080 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001081 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1082 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001083 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001084 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1085 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001086 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001087 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001088 break;
1089 }
1090
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001091 case Primitive::kPrimFloat: {
1092 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001093 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001094 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001095 __ addss(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001096 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001097 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001098 }
1099
1100 case Primitive::kPrimDouble: {
1101 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001102 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001103 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001104 __ addsd(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001105 }
1106 break;
1107 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001108
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001109 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001110 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001111 }
1112}
1113
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001114void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001115 LocationSummary* locations =
1116 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001117 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001118 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001119 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001120 locations->SetInAt(0, Location::RequiresRegister());
1121 locations->SetInAt(1, Location::Any());
1122 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001123 break;
1124 }
1125
1126 case Primitive::kPrimBoolean:
1127 case Primitive::kPrimByte:
1128 case Primitive::kPrimChar:
1129 case Primitive::kPrimShort:
1130 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
1131 break;
1132
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001133 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001134 LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001135 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001136}
1137
1138void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
1139 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001140 Location first = locations->InAt(0);
1141 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001142 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001143 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001144 DCHECK_EQ(first.As<Register>(),
1145 locations->Out().As<Register>());
1146 if (second.IsRegister()) {
1147 __ subl(first.As<Register>(),
1148 second.As<Register>());
1149 } else if (second.IsConstant()) {
1150 HConstant* instruction = second.GetConstant();
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001151 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001152 __ subl(first.As<Register>(), imm);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001153 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001154 __ subl(first.As<Register>(),
1155 Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001156 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001157 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001158 }
1159
1160 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001161 DCHECK_EQ(first.AsRegisterPairLow<Register>(),
1162 locations->Out().AsRegisterPairLow<Register>());
1163 DCHECK_EQ(first.AsRegisterPairHigh<Register>(),
1164 locations->Out().AsRegisterPairHigh<Register>());
1165 if (second.IsRegister()) {
1166 __ subl(first.AsRegisterPairLow<Register>(),
1167 second.AsRegisterPairLow<Register>());
1168 __ sbbl(first.AsRegisterPairHigh<Register>(),
1169 second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001170 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001171 __ subl(first.AsRegisterPairLow<Register>(),
1172 Address(ESP, second.GetStackIndex()));
1173 __ sbbl(first.AsRegisterPairHigh<Register>(),
1174 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001175 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001176 break;
1177 }
1178
1179 case Primitive::kPrimBoolean:
1180 case Primitive::kPrimByte:
1181 case Primitive::kPrimChar:
1182 case Primitive::kPrimShort:
1183 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
1184 break;
1185
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001186 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001187 LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001188 }
1189}
1190
Calin Juravle34bacdf2014-10-07 20:23:36 +01001191void LocationsBuilderX86::VisitMul(HMul* mul) {
1192 LocationSummary* locations =
1193 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1194 switch (mul->GetResultType()) {
1195 case Primitive::kPrimInt:
1196 locations->SetInAt(0, Location::RequiresRegister());
1197 locations->SetInAt(1, Location::Any());
1198 locations->SetOut(Location::SameAsFirstInput());
1199 break;
1200 case Primitive::kPrimLong: {
1201 locations->SetInAt(0, Location::RequiresRegister());
1202 // TODO: Currently this handles only stack operands:
1203 // - we don't have enough registers because we currently use Quick ABI.
1204 // - by the time we have a working register allocator we will probably change the ABI
1205 // and fix the above.
1206 // - we don't have a way yet to request operands on stack but the base line compiler
1207 // will leave the operands on the stack with Any().
1208 locations->SetInAt(1, Location::Any());
1209 locations->SetOut(Location::SameAsFirstInput());
1210 // Needed for imul on 32bits with 64bits output.
1211 locations->AddTemp(Location::RegisterLocation(EAX));
1212 locations->AddTemp(Location::RegisterLocation(EDX));
1213 break;
1214 }
1215
1216 case Primitive::kPrimBoolean:
1217 case Primitive::kPrimByte:
1218 case Primitive::kPrimChar:
1219 case Primitive::kPrimShort:
1220 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
1221 break;
1222
1223 default:
1224 LOG(FATAL) << "Unimplemented mul type " << mul->GetResultType();
1225 }
1226}
1227
1228void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
1229 LocationSummary* locations = mul->GetLocations();
1230 Location first = locations->InAt(0);
1231 Location second = locations->InAt(1);
1232 DCHECK(first.Equals(locations->Out()));
1233
1234 switch (mul->GetResultType()) {
1235 case Primitive::kPrimInt: {
1236 if (second.IsRegister()) {
1237 __ imull(first.As<Register>(), second.As<Register>());
1238 } else if (second.IsConstant()) {
1239 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1240 __ imull(first.As<Register>(), imm);
1241 } else {
1242 DCHECK(second.IsStackSlot());
1243 __ imull(first.As<Register>(), Address(ESP, second.GetStackIndex()));
1244 }
1245 break;
1246 }
1247
1248 case Primitive::kPrimLong: {
1249 DCHECK(second.IsDoubleStackSlot());
1250
1251 Register in1_hi = first.AsRegisterPairHigh<Register>();
1252 Register in1_lo = first.AsRegisterPairLow<Register>();
1253 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
1254 Address in2_lo(ESP, second.GetStackIndex());
1255 Register eax = locations->GetTemp(0).As<Register>();
1256 Register edx = locations->GetTemp(1).As<Register>();
1257
1258 DCHECK_EQ(EAX, eax);
1259 DCHECK_EQ(EDX, edx);
1260
1261 // input: in1 - 64 bits, in2 - 64 bits
1262 // output: in1
1263 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
1264 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
1265 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
1266
1267 __ movl(eax, in2_hi);
1268 // eax <- in1.lo * in2.hi
1269 __ imull(eax, in1_lo);
1270 // in1.hi <- in1.hi * in2.lo
1271 __ imull(in1_hi, in2_lo);
1272 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
1273 __ addl(in1_hi, eax);
1274 // move in1_lo to eax to prepare for double precision
1275 __ movl(eax, in1_lo);
1276 // edx:eax <- in1.lo * in2.lo
1277 __ mull(in2_lo);
1278 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
1279 __ addl(in1_hi, edx);
1280 // in1.lo <- (in1.lo * in2.lo)[31:0];
1281 __ movl(in1_lo, eax);
1282
1283 break;
1284 }
1285
1286 case Primitive::kPrimBoolean:
1287 case Primitive::kPrimByte:
1288 case Primitive::kPrimChar:
1289 case Primitive::kPrimShort:
1290 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
1291 break;
1292
1293 default:
1294 LOG(FATAL) << "Unimplemented mul type " << mul->GetResultType();
1295 }
1296}
1297
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001298void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001299 LocationSummary* locations =
1300 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001301 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001302 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001303 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1304 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001305}
1306
1307void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
1308 InvokeRuntimeCallingConvention calling_convention;
1309 LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001310 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001311
Nicolas Geoffray707c8092014-04-04 10:50:14 +01001312 __ fs()->call(
1313 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001314
Nicolas Geoffray39468442014-09-02 15:17:15 +01001315 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001316 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001317}
1318
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001319void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
1320 LocationSummary* locations =
1321 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
1322 locations->SetOut(Location::RegisterLocation(EAX));
1323 InvokeRuntimeCallingConvention calling_convention;
1324 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1325 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1326 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1327}
1328
1329void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
1330 InvokeRuntimeCallingConvention calling_convention;
1331 LoadCurrentMethod(calling_convention.GetRegisterAt(1));
1332 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
1333
1334 __ fs()->call(
1335 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocArrayWithAccessCheck)));
1336
1337 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
1338 DCHECK(!codegen_->IsLeafMethod());
1339}
1340
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001341void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001342 LocationSummary* locations =
1343 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001344 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1345 if (location.IsStackSlot()) {
1346 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1347 } else if (location.IsDoubleStackSlot()) {
1348 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001349 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001350 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001351}
1352
1353void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001354}
1355
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001356void LocationsBuilderX86::VisitNot(HNot* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001357 LocationSummary* locations =
1358 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001359 locations->SetInAt(0, Location::RequiresRegister());
1360 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001361}
1362
1363void InstructionCodeGeneratorX86::VisitNot(HNot* instruction) {
1364 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001365 Location out = locations->Out();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001366 DCHECK_EQ(locations->InAt(0).As<Register>(), out.As<Register>());
1367 __ xorl(out.As<Register>(), Immediate(1));
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001368}
1369
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001370void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001371 LocationSummary* locations =
1372 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001373 locations->SetInAt(0, Location::RequiresRegister());
1374 locations->SetInAt(1, Location::Any());
1375 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001376}
1377
1378void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
1379 Label greater, done;
1380 LocationSummary* locations = compare->GetLocations();
1381 switch (compare->InputAt(0)->GetType()) {
1382 case Primitive::kPrimLong: {
1383 Label less, greater, done;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001384 Register output = locations->Out().As<Register>();
1385 Location left = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001386 Location right = locations->InAt(1);
1387 if (right.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001388 __ cmpl(left.AsRegisterPairHigh<Register>(), right.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001389 } else {
1390 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001391 __ cmpl(left.AsRegisterPairHigh<Register>(),
1392 Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001393 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001394 __ j(kLess, &less); // Signed compare.
1395 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001396 if (right.IsRegisterPair()) {
1397 __ cmpl(left.AsRegisterPairLow<Register>(), right.AsRegisterPairLow<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001398 } else {
1399 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001400 __ cmpl(left.AsRegisterPairLow<Register>(), Address(ESP, right.GetStackIndex()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001401 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001402 __ movl(output, Immediate(0));
1403 __ j(kEqual, &done);
1404 __ j(kBelow, &less); // Unsigned compare.
1405
1406 __ Bind(&greater);
1407 __ movl(output, Immediate(1));
1408 __ jmp(&done);
1409
1410 __ Bind(&less);
1411 __ movl(output, Immediate(-1));
1412
1413 __ Bind(&done);
1414 break;
1415 }
1416 default:
1417 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
1418 }
1419}
1420
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001421void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001422 LocationSummary* locations =
1423 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001424 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1425 locations->SetInAt(i, Location::Any());
1426 }
1427 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001428}
1429
1430void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001431 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001432}
1433
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001434void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001435 LocationSummary* locations =
1436 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001437 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001438 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001439 bool is_object_type = field_type == Primitive::kPrimNot;
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001440 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
1441 || (field_type == Primitive::kPrimByte);
1442 // The register allocator does not support multiple
1443 // inputs that die at entry with one in a specific register.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001444 if (is_byte_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001445 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001446 locations->SetInAt(1, Location::RegisterLocation(EAX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001447 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001448 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001449 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001450 // Temporary registers for the write barrier.
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001451 if (is_object_type) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001452 locations->AddTemp(Location::RequiresRegister());
1453 // Ensure the card is in a byte register.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001454 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001455 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001456}
1457
1458void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1459 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001460 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001461 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001462 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001463
1464 switch (field_type) {
1465 case Primitive::kPrimBoolean:
1466 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001467 ByteRegister value = locations->InAt(1).As<ByteRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001468 __ movb(Address(obj, offset), value);
1469 break;
1470 }
1471
1472 case Primitive::kPrimShort:
1473 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001474 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001475 __ movw(Address(obj, offset), value);
1476 break;
1477 }
1478
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001479 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001480 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001481 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001482 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001483
1484 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001485 Register temp = locations->GetTemp(0).As<Register>();
1486 Register card = locations->GetTemp(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001487 codegen_->MarkGCCard(temp, card, obj, value);
1488 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001489 break;
1490 }
1491
1492 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001493 Location value = locations->InAt(1);
1494 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
1495 __ movl(Address(obj, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001496 break;
1497 }
1498
1499 case Primitive::kPrimFloat:
1500 case Primitive::kPrimDouble:
1501 LOG(FATAL) << "Unimplemented register type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001502 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001503 case Primitive::kPrimVoid:
1504 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001505 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001506 }
1507}
1508
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001509void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
1510 Label is_null;
1511 __ testl(value, value);
1512 __ j(kEqual, &is_null);
1513 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
1514 __ movl(temp, object);
1515 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
1516 __ movb(Address(temp, card, TIMES_1, 0),
1517 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
1518 __ Bind(&is_null);
1519}
1520
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001521void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001522 LocationSummary* locations =
1523 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001524 locations->SetInAt(0, Location::RequiresRegister());
1525 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001526}
1527
1528void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1529 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001530 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001531 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
1532
1533 switch (instruction->GetType()) {
1534 case Primitive::kPrimBoolean: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001535 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001536 __ movzxb(out, Address(obj, offset));
1537 break;
1538 }
1539
1540 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001541 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001542 __ movsxb(out, Address(obj, offset));
1543 break;
1544 }
1545
1546 case Primitive::kPrimShort: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001547 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001548 __ movsxw(out, Address(obj, offset));
1549 break;
1550 }
1551
1552 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001553 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001554 __ movzxw(out, Address(obj, offset));
1555 break;
1556 }
1557
1558 case Primitive::kPrimInt:
1559 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001560 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001561 __ movl(out, Address(obj, offset));
1562 break;
1563 }
1564
1565 case Primitive::kPrimLong: {
1566 // TODO: support volatile.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001567 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(obj, offset));
1568 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(obj, kX86WordSize + offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001569 break;
1570 }
1571
1572 case Primitive::kPrimFloat:
1573 case Primitive::kPrimDouble:
1574 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001575 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001576 case Primitive::kPrimVoid:
1577 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001578 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001579 }
1580}
1581
1582void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001583 LocationSummary* locations =
1584 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001585 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001586 if (instruction->HasUses()) {
1587 locations->SetOut(Location::SameAsFirstInput());
1588 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001589}
1590
1591void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001592 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001593 codegen_->AddSlowPath(slow_path);
1594
1595 LocationSummary* locations = instruction->GetLocations();
1596 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001597
1598 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001599 __ cmpl(obj.As<Register>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001600 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001601 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001602 } else {
1603 DCHECK(obj.IsConstant()) << obj;
1604 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
1605 __ jmp(slow_path->GetEntryLabel());
1606 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001607 }
1608 __ j(kEqual, slow_path->GetEntryLabel());
1609}
1610
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001611void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001612 LocationSummary* locations =
1613 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001614 locations->SetInAt(0, Location::RequiresRegister());
1615 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1616 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001617}
1618
1619void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
1620 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001621 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001622 Location index = locations->InAt(1);
1623
1624 switch (instruction->GetType()) {
1625 case Primitive::kPrimBoolean: {
1626 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001627 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001628 if (index.IsConstant()) {
1629 __ movzxb(out, Address(obj,
1630 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1631 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001632 __ movzxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001633 }
1634 break;
1635 }
1636
1637 case Primitive::kPrimByte: {
1638 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001639 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001640 if (index.IsConstant()) {
1641 __ movsxb(out, Address(obj,
1642 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1643 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001644 __ movsxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001645 }
1646 break;
1647 }
1648
1649 case Primitive::kPrimShort: {
1650 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001651 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001652 if (index.IsConstant()) {
1653 __ movsxw(out, Address(obj,
1654 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1655 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001656 __ movsxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001657 }
1658 break;
1659 }
1660
1661 case Primitive::kPrimChar: {
1662 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001663 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001664 if (index.IsConstant()) {
1665 __ movzxw(out, Address(obj,
1666 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1667 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001668 __ movzxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001669 }
1670 break;
1671 }
1672
1673 case Primitive::kPrimInt:
1674 case Primitive::kPrimNot: {
1675 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001676 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001677 if (index.IsConstant()) {
1678 __ movl(out, Address(obj,
1679 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
1680 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001681 __ movl(out, Address(obj, index.As<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001682 }
1683 break;
1684 }
1685
1686 case Primitive::kPrimLong: {
1687 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001688 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001689 if (index.IsConstant()) {
1690 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001691 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
1692 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001693 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001694 __ movl(out.AsRegisterPairLow<Register>(),
1695 Address(obj, index.As<Register>(), TIMES_8, data_offset));
1696 __ movl(out.AsRegisterPairHigh<Register>(),
1697 Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001698 }
1699 break;
1700 }
1701
1702 case Primitive::kPrimFloat:
1703 case Primitive::kPrimDouble:
1704 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001705 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001706 case Primitive::kPrimVoid:
1707 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001708 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001709 }
1710}
1711
1712void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001713 Primitive::Type value_type = instruction->GetComponentType();
1714 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1715 instruction,
1716 value_type == Primitive::kPrimNot ? LocationSummary::kCall : LocationSummary::kNoCall);
1717
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001718 if (value_type == Primitive::kPrimNot) {
1719 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001720 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1721 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1722 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001723 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001724 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
1725 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001726 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001727 // In case of a byte operation, the register allocator does not support multiple
1728 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001729 locations->SetInAt(0, Location::RequiresRegister());
1730 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001731 if (is_byte_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001732 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001733 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001734 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001735 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001736 }
1737 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001738}
1739
1740void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
1741 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001742 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001743 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001744 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001745 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001746
1747 switch (value_type) {
1748 case Primitive::kPrimBoolean:
1749 case Primitive::kPrimByte: {
1750 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001751 if (index.IsConstant()) {
1752 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001753 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001754 __ movb(Address(obj, offset), value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001755 } else {
1756 __ movb(Address(obj, offset),
1757 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1758 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001759 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001760 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001761 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
1762 value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001763 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001764 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001765 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1766 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001767 }
1768 break;
1769 }
1770
1771 case Primitive::kPrimShort:
1772 case Primitive::kPrimChar: {
1773 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001774 if (index.IsConstant()) {
1775 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001776 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001777 __ movw(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001778 } else {
1779 __ movw(Address(obj, offset),
1780 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1781 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001782 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001783 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001784 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
1785 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001786 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001787 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001788 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1789 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001790 }
1791 break;
1792 }
1793
1794 case Primitive::kPrimInt: {
1795 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001796 if (index.IsConstant()) {
1797 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001798 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001799 __ movl(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001800 } else {
1801 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1802 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001803 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001804 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001805 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
1806 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001807 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001808 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001809 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1810 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001811 }
1812 break;
1813 }
1814
1815 case Primitive::kPrimNot: {
1816 DCHECK(!codegen_->IsLeafMethod());
1817 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001818 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001819 break;
1820 }
1821
1822 case Primitive::kPrimLong: {
1823 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001824 if (index.IsConstant()) {
1825 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001826 if (value.IsRegisterPair()) {
1827 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
1828 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001829 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001830 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001831 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
1832 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
1833 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
1834 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001835 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001836 if (value.IsRegisterPair()) {
1837 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
1838 value.AsRegisterPairLow<Register>());
1839 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
1840 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001841 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001842 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001843 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001844 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001845 Immediate(Low32Bits(val)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001846 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001847 Immediate(High32Bits(val)));
1848 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001849 }
1850 break;
1851 }
1852
1853 case Primitive::kPrimFloat:
1854 case Primitive::kPrimDouble:
1855 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001856 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001857 case Primitive::kPrimVoid:
1858 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001859 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001860 }
1861}
1862
1863void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
1864 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001865 locations->SetInAt(0, Location::RequiresRegister());
1866 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001867 instruction->SetLocations(locations);
1868}
1869
1870void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
1871 LocationSummary* locations = instruction->GetLocations();
1872 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001873 Register obj = locations->InAt(0).As<Register>();
1874 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001875 __ movl(out, Address(obj, offset));
1876}
1877
1878void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001879 LocationSummary* locations =
1880 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001881 locations->SetInAt(0, Location::RequiresRegister());
1882 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001883 if (instruction->HasUses()) {
1884 locations->SetOut(Location::SameAsFirstInput());
1885 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001886}
1887
1888void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
1889 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001890 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01001891 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001892 codegen_->AddSlowPath(slow_path);
1893
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001894 Register index = locations->InAt(0).As<Register>();
1895 Register length = locations->InAt(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001896
1897 __ cmpl(index, length);
1898 __ j(kAboveEqual, slow_path->GetEntryLabel());
1899}
1900
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001901void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
1902 temp->SetLocations(nullptr);
1903}
1904
1905void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
1906 // Nothing to do, this is driven by the code generator.
1907}
1908
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001909void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001910 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001911}
1912
1913void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001914 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
1915}
1916
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001917void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
1918 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
1919}
1920
1921void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001922 HBasicBlock* block = instruction->GetBlock();
1923 if (block->GetLoopInformation() != nullptr) {
1924 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
1925 // The back edge will generate the suspend check.
1926 return;
1927 }
1928 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
1929 // The goto will generate the suspend check.
1930 return;
1931 }
1932 GenerateSuspendCheck(instruction, nullptr);
1933}
1934
1935void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
1936 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001937 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001938 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001939 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001940 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001941 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001942 if (successor == nullptr) {
1943 __ j(kNotEqual, slow_path->GetEntryLabel());
1944 __ Bind(slow_path->GetReturnLabel());
1945 } else {
1946 __ j(kEqual, codegen_->GetLabelOf(successor));
1947 __ jmp(slow_path->GetEntryLabel());
1948 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001949}
1950
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001951X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
1952 return codegen_->GetAssembler();
1953}
1954
1955void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
1956 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001957 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001958 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
1959 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
1960 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
1961}
1962
1963void ParallelMoveResolverX86::EmitMove(size_t index) {
1964 MoveOperands* move = moves_.Get(index);
1965 Location source = move->GetSource();
1966 Location destination = move->GetDestination();
1967
1968 if (source.IsRegister()) {
1969 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001970 __ movl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001971 } else {
1972 DCHECK(destination.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001973 __ movl(Address(ESP, destination.GetStackIndex()), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001974 }
1975 } else if (source.IsStackSlot()) {
1976 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001977 __ movl(destination.As<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001978 } else {
1979 DCHECK(destination.IsStackSlot());
1980 MoveMemoryToMemory(destination.GetStackIndex(),
1981 source.GetStackIndex());
1982 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001983 } else if (source.IsConstant()) {
1984 HIntConstant* instruction = source.GetConstant()->AsIntConstant();
1985 Immediate imm(instruction->AsIntConstant()->GetValue());
1986 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001987 __ movl(destination.As<Register>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001988 } else {
1989 __ movl(Address(ESP, destination.GetStackIndex()), imm);
1990 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001991 } else {
1992 LOG(FATAL) << "Unimplemented";
1993 }
1994}
1995
1996void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001997 Register suggested_scratch = reg == EAX ? EBX : EAX;
1998 ScratchRegisterScope ensure_scratch(
1999 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
2000
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002001 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
2002 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
2003 __ movl(Address(ESP, mem + stack_offset), reg);
2004 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
2005}
2006
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002007void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
2008 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002009 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
2010
2011 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002012 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002013 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
2014
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002015 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
2016 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
2017 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
2018 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
2019 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
2020 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
2021}
2022
2023void ParallelMoveResolverX86::EmitSwap(size_t index) {
2024 MoveOperands* move = moves_.Get(index);
2025 Location source = move->GetSource();
2026 Location destination = move->GetDestination();
2027
2028 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002029 __ xchgl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002030 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002031 Exchange(source.As<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002032 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002033 Exchange(destination.As<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002034 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
2035 Exchange(destination.GetStackIndex(), source.GetStackIndex());
2036 } else {
2037 LOG(FATAL) << "Unimplemented";
2038 }
2039}
2040
2041void ParallelMoveResolverX86::SpillScratch(int reg) {
2042 __ pushl(static_cast<Register>(reg));
2043}
2044
2045void ParallelMoveResolverX86::RestoreScratch(int reg) {
2046 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002047}
2048
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002049} // namespace x86
2050} // namespace art