blob: 495ff8b4a4612a177f76e3933eed5f3e7f985c28 [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:
Roland Levillain2e07b4f2014-10-23 18:12:09 +0100993 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +0100994 locations->SetInAt(0, Location::RequiresRegister());
995 locations->SetOut(Location::SameAsFirstInput());
996 break;
997
Roland Levillain88cb1752014-10-20 16:36:47 +0100998 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:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001019 DCHECK(in.IsRegisterPair());
1020 __ negl(out.AsRegisterPairLow<Register>());
1021 // Negation is similar to subtraction from zero. The least
1022 // significant byte triggers a borrow when it is different from
1023 // zero; to take it into account, add 1 to the most significant
1024 // byte if the carry flag (CF) is set to 1 after the first NEGL
1025 // operation.
1026 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1027 __ negl(out.AsRegisterPairHigh<Register>());
1028 break;
1029
Roland Levillain88cb1752014-10-20 16:36:47 +01001030 case Primitive::kPrimFloat:
1031 case Primitive::kPrimDouble:
1032 LOG(FATAL) << "Not yet implemented neg type " << neg->GetResultType();
1033 break;
1034
1035 default:
1036 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1037 }
1038}
1039
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001040void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001041 LocationSummary* locations =
1042 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001043 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001044 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001045 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001046 locations->SetInAt(0, Location::RequiresRegister());
1047 locations->SetInAt(1, Location::Any());
1048 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001049 break;
1050 }
1051
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001052 case Primitive::kPrimFloat:
1053 case Primitive::kPrimDouble: {
1054 locations->SetInAt(0, Location::RequiresFpuRegister());
1055 locations->SetInAt(1, Location::Any());
1056 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001057 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001058 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001059
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001060 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001061 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1062 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001063 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001064}
1065
1066void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1067 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001068 Location first = locations->InAt(0);
1069 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001070 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001071 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001072 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001073 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001074 __ addl(first.As<Register>(), second.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001075 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001076 __ addl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001077 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001078 __ addl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001079 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001080 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001081 }
1082
1083 case Primitive::kPrimLong: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001084 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001085 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1086 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001087 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001088 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1089 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001090 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001091 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001092 break;
1093 }
1094
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001095 case Primitive::kPrimFloat: {
1096 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001097 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001098 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001099 __ addss(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001100 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001101 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001102 }
1103
1104 case Primitive::kPrimDouble: {
1105 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001106 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001107 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001108 __ addsd(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001109 }
1110 break;
1111 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001112
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001113 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001114 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001115 }
1116}
1117
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001118void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001119 LocationSummary* locations =
1120 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001121 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001122 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001123 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001124 locations->SetInAt(0, Location::RequiresRegister());
1125 locations->SetInAt(1, Location::Any());
1126 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001127 break;
1128 }
Calin Juravle11351682014-10-23 15:38:15 +01001129 case Primitive::kPrimFloat:
1130 case Primitive::kPrimDouble: {
1131 locations->SetInAt(0, Location::RequiresFpuRegister());
1132 locations->SetInAt(1, Location::RequiresFpuRegister());
1133 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001134 break;
Calin Juravle11351682014-10-23 15:38:15 +01001135 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001136
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001137 default:
Calin Juravle11351682014-10-23 15:38:15 +01001138 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001139 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001140}
1141
1142void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
1143 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001144 Location first = locations->InAt(0);
1145 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001146 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001147 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001148 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001149 if (second.IsRegister()) {
Calin Juravle11351682014-10-23 15:38:15 +01001150 __ subl(first.As<Register>(), second.As<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001151 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001152 __ subl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001153 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001154 __ subl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001155 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001156 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001157 }
1158
1159 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001160 if (second.IsRegister()) {
Calin Juravle11351682014-10-23 15:38:15 +01001161 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1162 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001163 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001164 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001165 __ sbbl(first.AsRegisterPairHigh<Register>(),
1166 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001167 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001168 break;
1169 }
1170
Calin Juravle11351682014-10-23 15:38:15 +01001171 case Primitive::kPrimFloat: {
1172 __ subss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001173 break;
Calin Juravle11351682014-10-23 15:38:15 +01001174 }
1175
1176 case Primitive::kPrimDouble: {
1177 __ subsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1178 break;
1179 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001180
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001181 default:
Calin Juravle11351682014-10-23 15:38:15 +01001182 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001183 }
1184}
1185
Calin Juravle34bacdf2014-10-07 20:23:36 +01001186void LocationsBuilderX86::VisitMul(HMul* mul) {
1187 LocationSummary* locations =
1188 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1189 switch (mul->GetResultType()) {
1190 case Primitive::kPrimInt:
1191 locations->SetInAt(0, Location::RequiresRegister());
1192 locations->SetInAt(1, Location::Any());
1193 locations->SetOut(Location::SameAsFirstInput());
1194 break;
1195 case Primitive::kPrimLong: {
1196 locations->SetInAt(0, Location::RequiresRegister());
1197 // TODO: Currently this handles only stack operands:
1198 // - we don't have enough registers because we currently use Quick ABI.
1199 // - by the time we have a working register allocator we will probably change the ABI
1200 // and fix the above.
1201 // - we don't have a way yet to request operands on stack but the base line compiler
1202 // will leave the operands on the stack with Any().
1203 locations->SetInAt(1, Location::Any());
1204 locations->SetOut(Location::SameAsFirstInput());
1205 // Needed for imul on 32bits with 64bits output.
1206 locations->AddTemp(Location::RegisterLocation(EAX));
1207 locations->AddTemp(Location::RegisterLocation(EDX));
1208 break;
1209 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001210 case Primitive::kPrimFloat:
1211 case Primitive::kPrimDouble: {
1212 locations->SetInAt(0, Location::RequiresFpuRegister());
1213 locations->SetInAt(1, Location::RequiresFpuRegister());
1214 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001215 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001216 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001217
1218 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001219 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001220 }
1221}
1222
1223void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
1224 LocationSummary* locations = mul->GetLocations();
1225 Location first = locations->InAt(0);
1226 Location second = locations->InAt(1);
1227 DCHECK(first.Equals(locations->Out()));
1228
1229 switch (mul->GetResultType()) {
1230 case Primitive::kPrimInt: {
1231 if (second.IsRegister()) {
1232 __ imull(first.As<Register>(), second.As<Register>());
1233 } else if (second.IsConstant()) {
1234 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1235 __ imull(first.As<Register>(), imm);
1236 } else {
1237 DCHECK(second.IsStackSlot());
1238 __ imull(first.As<Register>(), Address(ESP, second.GetStackIndex()));
1239 }
1240 break;
1241 }
1242
1243 case Primitive::kPrimLong: {
1244 DCHECK(second.IsDoubleStackSlot());
1245
1246 Register in1_hi = first.AsRegisterPairHigh<Register>();
1247 Register in1_lo = first.AsRegisterPairLow<Register>();
1248 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
1249 Address in2_lo(ESP, second.GetStackIndex());
1250 Register eax = locations->GetTemp(0).As<Register>();
1251 Register edx = locations->GetTemp(1).As<Register>();
1252
1253 DCHECK_EQ(EAX, eax);
1254 DCHECK_EQ(EDX, edx);
1255
1256 // input: in1 - 64 bits, in2 - 64 bits
1257 // output: in1
1258 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
1259 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
1260 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
1261
1262 __ movl(eax, in2_hi);
1263 // eax <- in1.lo * in2.hi
1264 __ imull(eax, in1_lo);
1265 // in1.hi <- in1.hi * in2.lo
1266 __ imull(in1_hi, in2_lo);
1267 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
1268 __ addl(in1_hi, eax);
1269 // move in1_lo to eax to prepare for double precision
1270 __ movl(eax, in1_lo);
1271 // edx:eax <- in1.lo * in2.lo
1272 __ mull(in2_lo);
1273 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
1274 __ addl(in1_hi, edx);
1275 // in1.lo <- (in1.lo * in2.lo)[31:0];
1276 __ movl(in1_lo, eax);
1277
1278 break;
1279 }
1280
Calin Juravleb5bfa962014-10-21 18:02:24 +01001281 case Primitive::kPrimFloat: {
1282 __ mulss(first.As<XmmRegister>(), second.As<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001283 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001284 }
1285
1286 case Primitive::kPrimDouble: {
1287 __ mulsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1288 break;
1289 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001290
1291 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001292 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001293 }
1294}
1295
Calin Juravle7c4954d2014-10-28 16:57:40 +00001296void LocationsBuilderX86::VisitDiv(HDiv* div) {
1297 LocationSummary* locations =
1298 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1299 switch (div->GetResultType()) {
1300 case Primitive::kPrimInt:
1301 case Primitive::kPrimLong: {
1302 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1303 break;
1304 }
1305 case Primitive::kPrimFloat:
1306 case Primitive::kPrimDouble: {
1307 locations->SetInAt(0, Location::RequiresFpuRegister());
1308 locations->SetInAt(1, Location::RequiresFpuRegister());
1309 locations->SetOut(Location::SameAsFirstInput());
1310 break;
1311 }
1312
1313 default:
1314 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1315 }
1316}
1317
1318void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
1319 LocationSummary* locations = div->GetLocations();
1320 Location first = locations->InAt(0);
1321 Location second = locations->InAt(1);
1322 DCHECK(first.Equals(locations->Out()));
1323
1324 switch (div->GetResultType()) {
1325 case Primitive::kPrimInt:
1326 case Primitive::kPrimLong: {
1327 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1328 break;
1329 }
1330
1331 case Primitive::kPrimFloat: {
1332 __ divss(first.As<XmmRegister>(), second.As<XmmRegister>());
1333 break;
1334 }
1335
1336 case Primitive::kPrimDouble: {
1337 __ divsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1338 break;
1339 }
1340
1341 default:
1342 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1343 }
1344}
1345
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001346void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001347 LocationSummary* locations =
1348 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001349 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001350 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001351 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1352 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001353}
1354
1355void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
1356 InvokeRuntimeCallingConvention calling_convention;
1357 LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001358 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001359
Nicolas Geoffray707c8092014-04-04 10:50:14 +01001360 __ fs()->call(
1361 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001362
Nicolas Geoffray39468442014-09-02 15:17:15 +01001363 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001364 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001365}
1366
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001367void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
1368 LocationSummary* locations =
1369 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
1370 locations->SetOut(Location::RegisterLocation(EAX));
1371 InvokeRuntimeCallingConvention calling_convention;
1372 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1373 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1374 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1375}
1376
1377void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
1378 InvokeRuntimeCallingConvention calling_convention;
1379 LoadCurrentMethod(calling_convention.GetRegisterAt(1));
1380 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
1381
1382 __ fs()->call(
1383 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocArrayWithAccessCheck)));
1384
1385 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
1386 DCHECK(!codegen_->IsLeafMethod());
1387}
1388
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001389void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001390 LocationSummary* locations =
1391 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001392 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1393 if (location.IsStackSlot()) {
1394 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1395 } else if (location.IsDoubleStackSlot()) {
1396 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001397 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001398 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001399}
1400
1401void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001402}
1403
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001404void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001405 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001406 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001407 locations->SetInAt(0, Location::RequiresRegister());
1408 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001409}
1410
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001411void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
1412 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01001413 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001414 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01001415 DCHECK(in.Equals(out));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001416 switch (not_->InputAt(0)->GetType()) {
1417 case Primitive::kPrimBoolean:
1418 __ xorl(out.As<Register>(), Immediate(1));
1419 break;
1420
1421 case Primitive::kPrimInt:
1422 __ notl(out.As<Register>());
1423 break;
1424
1425 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01001426 __ notl(out.AsRegisterPairLow<Register>());
1427 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001428 break;
1429
1430 default:
1431 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
1432 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001433}
1434
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001435void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001436 LocationSummary* locations =
1437 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001438 locations->SetInAt(0, Location::RequiresRegister());
1439 locations->SetInAt(1, Location::Any());
1440 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001441}
1442
1443void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
1444 Label greater, done;
1445 LocationSummary* locations = compare->GetLocations();
1446 switch (compare->InputAt(0)->GetType()) {
1447 case Primitive::kPrimLong: {
1448 Label less, greater, done;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001449 Register output = locations->Out().As<Register>();
1450 Location left = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001451 Location right = locations->InAt(1);
1452 if (right.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001453 __ cmpl(left.AsRegisterPairHigh<Register>(), right.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001454 } else {
1455 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001456 __ cmpl(left.AsRegisterPairHigh<Register>(),
1457 Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001458 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001459 __ j(kLess, &less); // Signed compare.
1460 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001461 if (right.IsRegisterPair()) {
1462 __ cmpl(left.AsRegisterPairLow<Register>(), right.AsRegisterPairLow<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001463 } else {
1464 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001465 __ cmpl(left.AsRegisterPairLow<Register>(), Address(ESP, right.GetStackIndex()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001466 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001467 __ movl(output, Immediate(0));
1468 __ j(kEqual, &done);
1469 __ j(kBelow, &less); // Unsigned compare.
1470
1471 __ Bind(&greater);
1472 __ movl(output, Immediate(1));
1473 __ jmp(&done);
1474
1475 __ Bind(&less);
1476 __ movl(output, Immediate(-1));
1477
1478 __ Bind(&done);
1479 break;
1480 }
1481 default:
1482 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
1483 }
1484}
1485
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001486void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001487 LocationSummary* locations =
1488 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001489 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1490 locations->SetInAt(i, Location::Any());
1491 }
1492 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001493}
1494
1495void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001496 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001497}
1498
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001499void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001500 LocationSummary* locations =
1501 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001502 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001503 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001504 bool is_object_type = field_type == Primitive::kPrimNot;
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001505 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
1506 || (field_type == Primitive::kPrimByte);
1507 // The register allocator does not support multiple
1508 // inputs that die at entry with one in a specific register.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001509 if (is_byte_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001510 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001511 locations->SetInAt(1, Location::RegisterLocation(EAX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001512 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001513 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001514 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001515 // Temporary registers for the write barrier.
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001516 if (is_object_type) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001517 locations->AddTemp(Location::RequiresRegister());
1518 // Ensure the card is in a byte register.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001519 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001520 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001521}
1522
1523void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1524 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001525 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001526 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001527 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001528
1529 switch (field_type) {
1530 case Primitive::kPrimBoolean:
1531 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001532 ByteRegister value = locations->InAt(1).As<ByteRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001533 __ movb(Address(obj, offset), value);
1534 break;
1535 }
1536
1537 case Primitive::kPrimShort:
1538 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001539 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001540 __ movw(Address(obj, offset), value);
1541 break;
1542 }
1543
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001544 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001545 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001546 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001547 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001548
1549 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001550 Register temp = locations->GetTemp(0).As<Register>();
1551 Register card = locations->GetTemp(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001552 codegen_->MarkGCCard(temp, card, obj, value);
1553 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001554 break;
1555 }
1556
1557 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001558 Location value = locations->InAt(1);
1559 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
1560 __ movl(Address(obj, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001561 break;
1562 }
1563
1564 case Primitive::kPrimFloat:
1565 case Primitive::kPrimDouble:
1566 LOG(FATAL) << "Unimplemented register type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001567 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001568 case Primitive::kPrimVoid:
1569 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001570 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001571 }
1572}
1573
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001574void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
1575 Label is_null;
1576 __ testl(value, value);
1577 __ j(kEqual, &is_null);
1578 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
1579 __ movl(temp, object);
1580 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
1581 __ movb(Address(temp, card, TIMES_1, 0),
1582 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
1583 __ Bind(&is_null);
1584}
1585
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001586void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001587 LocationSummary* locations =
1588 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001589 locations->SetInAt(0, Location::RequiresRegister());
1590 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001591}
1592
1593void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1594 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001595 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001596 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
1597
1598 switch (instruction->GetType()) {
1599 case Primitive::kPrimBoolean: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001600 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001601 __ movzxb(out, Address(obj, offset));
1602 break;
1603 }
1604
1605 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001606 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001607 __ movsxb(out, Address(obj, offset));
1608 break;
1609 }
1610
1611 case Primitive::kPrimShort: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001612 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001613 __ movsxw(out, Address(obj, offset));
1614 break;
1615 }
1616
1617 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001618 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001619 __ movzxw(out, Address(obj, offset));
1620 break;
1621 }
1622
1623 case Primitive::kPrimInt:
1624 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001625 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001626 __ movl(out, Address(obj, offset));
1627 break;
1628 }
1629
1630 case Primitive::kPrimLong: {
1631 // TODO: support volatile.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001632 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(obj, offset));
1633 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(obj, kX86WordSize + offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001634 break;
1635 }
1636
1637 case Primitive::kPrimFloat:
1638 case Primitive::kPrimDouble:
1639 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001640 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001641 case Primitive::kPrimVoid:
1642 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001643 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001644 }
1645}
1646
1647void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001648 LocationSummary* locations =
1649 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001650 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001651 if (instruction->HasUses()) {
1652 locations->SetOut(Location::SameAsFirstInput());
1653 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001654}
1655
1656void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001657 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001658 codegen_->AddSlowPath(slow_path);
1659
1660 LocationSummary* locations = instruction->GetLocations();
1661 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001662
1663 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001664 __ cmpl(obj.As<Register>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001665 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001666 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001667 } else {
1668 DCHECK(obj.IsConstant()) << obj;
1669 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
1670 __ jmp(slow_path->GetEntryLabel());
1671 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001672 }
1673 __ j(kEqual, slow_path->GetEntryLabel());
1674}
1675
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001676void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001677 LocationSummary* locations =
1678 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001679 locations->SetInAt(0, Location::RequiresRegister());
1680 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1681 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001682}
1683
1684void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
1685 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001686 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001687 Location index = locations->InAt(1);
1688
1689 switch (instruction->GetType()) {
1690 case Primitive::kPrimBoolean: {
1691 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001692 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001693 if (index.IsConstant()) {
1694 __ movzxb(out, Address(obj,
1695 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1696 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001697 __ movzxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001698 }
1699 break;
1700 }
1701
1702 case Primitive::kPrimByte: {
1703 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001704 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001705 if (index.IsConstant()) {
1706 __ movsxb(out, Address(obj,
1707 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1708 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001709 __ movsxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001710 }
1711 break;
1712 }
1713
1714 case Primitive::kPrimShort: {
1715 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001716 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001717 if (index.IsConstant()) {
1718 __ movsxw(out, Address(obj,
1719 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1720 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001721 __ movsxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001722 }
1723 break;
1724 }
1725
1726 case Primitive::kPrimChar: {
1727 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001728 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001729 if (index.IsConstant()) {
1730 __ movzxw(out, Address(obj,
1731 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1732 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001733 __ movzxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001734 }
1735 break;
1736 }
1737
1738 case Primitive::kPrimInt:
1739 case Primitive::kPrimNot: {
1740 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001741 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001742 if (index.IsConstant()) {
1743 __ movl(out, Address(obj,
1744 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
1745 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001746 __ movl(out, Address(obj, index.As<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001747 }
1748 break;
1749 }
1750
1751 case Primitive::kPrimLong: {
1752 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001753 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001754 if (index.IsConstant()) {
1755 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001756 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
1757 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001758 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001759 __ movl(out.AsRegisterPairLow<Register>(),
1760 Address(obj, index.As<Register>(), TIMES_8, data_offset));
1761 __ movl(out.AsRegisterPairHigh<Register>(),
1762 Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001763 }
1764 break;
1765 }
1766
1767 case Primitive::kPrimFloat:
1768 case Primitive::kPrimDouble:
1769 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001770 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001771 case Primitive::kPrimVoid:
1772 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001773 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001774 }
1775}
1776
1777void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001778 Primitive::Type value_type = instruction->GetComponentType();
1779 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1780 instruction,
1781 value_type == Primitive::kPrimNot ? LocationSummary::kCall : LocationSummary::kNoCall);
1782
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001783 if (value_type == Primitive::kPrimNot) {
1784 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001785 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1786 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1787 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001788 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001789 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
1790 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001791 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001792 // In case of a byte operation, the register allocator does not support multiple
1793 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001794 locations->SetInAt(0, Location::RequiresRegister());
1795 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001796 if (is_byte_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001797 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001798 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001799 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001800 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001801 }
1802 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001803}
1804
1805void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
1806 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001807 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001808 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001809 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001810 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001811
1812 switch (value_type) {
1813 case Primitive::kPrimBoolean:
1814 case Primitive::kPrimByte: {
1815 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001816 if (index.IsConstant()) {
1817 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001818 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001819 __ movb(Address(obj, offset), value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001820 } else {
1821 __ movb(Address(obj, offset),
1822 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1823 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001824 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001825 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001826 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
1827 value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001828 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001829 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001830 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1831 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001832 }
1833 break;
1834 }
1835
1836 case Primitive::kPrimShort:
1837 case Primitive::kPrimChar: {
1838 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001839 if (index.IsConstant()) {
1840 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001841 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001842 __ movw(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001843 } else {
1844 __ movw(Address(obj, offset),
1845 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1846 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001847 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001848 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001849 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
1850 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001851 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001852 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001853 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1854 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001855 }
1856 break;
1857 }
1858
1859 case Primitive::kPrimInt: {
1860 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001861 if (index.IsConstant()) {
1862 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001863 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001864 __ movl(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001865 } else {
1866 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1867 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001868 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001869 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001870 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
1871 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001872 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001873 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001874 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1875 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001876 }
1877 break;
1878 }
1879
1880 case Primitive::kPrimNot: {
1881 DCHECK(!codegen_->IsLeafMethod());
1882 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001883 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001884 break;
1885 }
1886
1887 case Primitive::kPrimLong: {
1888 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001889 if (index.IsConstant()) {
1890 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001891 if (value.IsRegisterPair()) {
1892 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
1893 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001894 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001895 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001896 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
1897 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
1898 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
1899 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001900 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001901 if (value.IsRegisterPair()) {
1902 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
1903 value.AsRegisterPairLow<Register>());
1904 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
1905 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001906 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001907 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001908 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001909 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001910 Immediate(Low32Bits(val)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001911 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001912 Immediate(High32Bits(val)));
1913 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001914 }
1915 break;
1916 }
1917
1918 case Primitive::kPrimFloat:
1919 case Primitive::kPrimDouble:
1920 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001921 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001922 case Primitive::kPrimVoid:
1923 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001924 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001925 }
1926}
1927
1928void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
1929 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001930 locations->SetInAt(0, Location::RequiresRegister());
1931 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001932 instruction->SetLocations(locations);
1933}
1934
1935void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
1936 LocationSummary* locations = instruction->GetLocations();
1937 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001938 Register obj = locations->InAt(0).As<Register>();
1939 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001940 __ movl(out, Address(obj, offset));
1941}
1942
1943void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001944 LocationSummary* locations =
1945 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001946 locations->SetInAt(0, Location::RequiresRegister());
1947 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001948 if (instruction->HasUses()) {
1949 locations->SetOut(Location::SameAsFirstInput());
1950 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001951}
1952
1953void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
1954 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001955 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01001956 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001957 codegen_->AddSlowPath(slow_path);
1958
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001959 Register index = locations->InAt(0).As<Register>();
1960 Register length = locations->InAt(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001961
1962 __ cmpl(index, length);
1963 __ j(kAboveEqual, slow_path->GetEntryLabel());
1964}
1965
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001966void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
1967 temp->SetLocations(nullptr);
1968}
1969
1970void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
1971 // Nothing to do, this is driven by the code generator.
1972}
1973
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001974void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001975 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001976}
1977
1978void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001979 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
1980}
1981
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001982void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
1983 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
1984}
1985
1986void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001987 HBasicBlock* block = instruction->GetBlock();
1988 if (block->GetLoopInformation() != nullptr) {
1989 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
1990 // The back edge will generate the suspend check.
1991 return;
1992 }
1993 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
1994 // The goto will generate the suspend check.
1995 return;
1996 }
1997 GenerateSuspendCheck(instruction, nullptr);
1998}
1999
2000void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
2001 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002002 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002003 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002004 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002005 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002006 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002007 if (successor == nullptr) {
2008 __ j(kNotEqual, slow_path->GetEntryLabel());
2009 __ Bind(slow_path->GetReturnLabel());
2010 } else {
2011 __ j(kEqual, codegen_->GetLabelOf(successor));
2012 __ jmp(slow_path->GetEntryLabel());
2013 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002014}
2015
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002016X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
2017 return codegen_->GetAssembler();
2018}
2019
2020void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
2021 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002022 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002023 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
2024 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
2025 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
2026}
2027
2028void ParallelMoveResolverX86::EmitMove(size_t index) {
2029 MoveOperands* move = moves_.Get(index);
2030 Location source = move->GetSource();
2031 Location destination = move->GetDestination();
2032
2033 if (source.IsRegister()) {
2034 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002035 __ movl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002036 } else {
2037 DCHECK(destination.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002038 __ movl(Address(ESP, destination.GetStackIndex()), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002039 }
2040 } else if (source.IsStackSlot()) {
2041 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002042 __ movl(destination.As<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002043 } else {
2044 DCHECK(destination.IsStackSlot());
2045 MoveMemoryToMemory(destination.GetStackIndex(),
2046 source.GetStackIndex());
2047 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002048 } else if (source.IsConstant()) {
2049 HIntConstant* instruction = source.GetConstant()->AsIntConstant();
2050 Immediate imm(instruction->AsIntConstant()->GetValue());
2051 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002052 __ movl(destination.As<Register>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002053 } else {
2054 __ movl(Address(ESP, destination.GetStackIndex()), imm);
2055 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002056 } else {
2057 LOG(FATAL) << "Unimplemented";
2058 }
2059}
2060
2061void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002062 Register suggested_scratch = reg == EAX ? EBX : EAX;
2063 ScratchRegisterScope ensure_scratch(
2064 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
2065
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002066 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
2067 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
2068 __ movl(Address(ESP, mem + stack_offset), reg);
2069 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
2070}
2071
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002072void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
2073 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002074 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
2075
2076 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002077 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002078 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
2079
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002080 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
2081 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
2082 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
2083 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
2084 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
2085 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
2086}
2087
2088void ParallelMoveResolverX86::EmitSwap(size_t index) {
2089 MoveOperands* move = moves_.Get(index);
2090 Location source = move->GetSource();
2091 Location destination = move->GetDestination();
2092
2093 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002094 __ xchgl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002095 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002096 Exchange(source.As<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002097 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002098 Exchange(destination.As<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002099 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
2100 Exchange(destination.GetStackIndex(), source.GetStackIndex());
2101 } else {
2102 LOG(FATAL) << "Unimplemented";
2103 }
2104}
2105
2106void ParallelMoveResolverX86::SpillScratch(int reg) {
2107 __ pushl(static_cast<Register>(reg));
2108}
2109
2110void ParallelMoveResolverX86::RestoreScratch(int reg) {
2111 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002112}
2113
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002114} // namespace x86
2115} // namespace art