Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 1 | /* |
| 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" |
| 18 | #include "utils/assembler.h" |
| 19 | #include "utils/x86/assembler_x86.h" |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 20 | #include "utils/x86/managed_register_x86.h" |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 21 | |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 22 | #include "entrypoints/quick/quick_entrypoints.h" |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 23 | #include "mirror/array.h" |
| 24 | #include "mirror/art_method.h" |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 25 | #include "thread.h" |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 26 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 27 | namespace art { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 28 | |
| 29 | x86::X86ManagedRegister Location::AsX86() const { |
| 30 | return reg().AsX86(); |
| 31 | } |
| 32 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 33 | namespace x86 { |
| 34 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 35 | #define __ reinterpret_cast<X86Assembler*>(codegen->GetAssembler())-> |
| 36 | |
| 37 | class NullCheckSlowPathX86 : public SlowPathCode { |
| 38 | public: |
| 39 | explicit NullCheckSlowPathX86(uint32_t dex_pc) : dex_pc_(dex_pc) {} |
| 40 | |
| 41 | virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE { |
| 42 | __ Bind(GetEntryLabel()); |
| 43 | __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowNullPointer))); |
| 44 | codegen->RecordPcInfo(dex_pc_); |
| 45 | } |
| 46 | |
| 47 | private: |
| 48 | const uint32_t dex_pc_; |
| 49 | DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86); |
| 50 | }; |
| 51 | |
| 52 | #undef __ |
| 53 | #define __ reinterpret_cast<X86Assembler*>(GetAssembler())-> |
| 54 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 55 | inline Condition X86Condition(IfCondition cond) { |
| 56 | switch (cond) { |
| 57 | case kCondEQ: return kEqual; |
| 58 | case kCondNE: return kNotEqual; |
| 59 | case kCondLT: return kLess; |
| 60 | case kCondLE: return kLessEqual; |
| 61 | case kCondGT: return kGreater; |
| 62 | case kCondGE: return kGreaterEqual; |
| 63 | default: |
| 64 | LOG(FATAL) << "Unknown if condition"; |
| 65 | } |
| 66 | return kEqual; |
| 67 | } |
| 68 | |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 69 | static constexpr int kNumberOfPushedRegistersAtEntry = 1; |
| 70 | static constexpr int kCurrentMethodStackOffset = 0; |
| 71 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 72 | void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const { |
| 73 | stream << X86ManagedRegister::FromCpuRegister(Register(reg)); |
| 74 | } |
| 75 | |
| 76 | void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const { |
| 77 | stream << X86ManagedRegister::FromXmmRegister(XmmRegister(reg)); |
| 78 | } |
| 79 | |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 80 | CodeGeneratorX86::CodeGeneratorX86(HGraph* graph) |
| 81 | : CodeGenerator(graph, kNumberOfRegIds), |
| 82 | location_builder_(graph, this), |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 83 | instruction_visitor_(graph, this), |
| 84 | move_resolver_(graph->GetArena(), this) {} |
| 85 | |
Nicolas Geoffray | ab032bc | 2014-07-15 12:55:21 +0100 | [diff] [blame^] | 86 | size_t CodeGeneratorX86::FrameEntrySpillSize() const { |
| 87 | return kNumberOfPushedRegistersAtEntry * kX86WordSize; |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 88 | } |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 89 | |
| 90 | static bool* GetBlockedRegisterPairs(bool* blocked_registers) { |
| 91 | return blocked_registers + kNumberOfAllocIds; |
| 92 | } |
| 93 | |
| 94 | ManagedRegister CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type, |
| 95 | bool* blocked_registers) const { |
| 96 | switch (type) { |
| 97 | case Primitive::kPrimLong: { |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 98 | bool* blocked_register_pairs = GetBlockedRegisterPairs(blocked_registers); |
| 99 | size_t reg = AllocateFreeRegisterInternal(blocked_register_pairs, kNumberOfRegisterPairs); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 100 | X86ManagedRegister pair = |
| 101 | X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg)); |
| 102 | blocked_registers[pair.AsRegisterPairLow()] = true; |
| 103 | blocked_registers[pair.AsRegisterPairHigh()] = true; |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 104 | // Block all other register pairs that share a register with `pair`. |
| 105 | for (int i = 0; i < kNumberOfRegisterPairs; i++) { |
| 106 | X86ManagedRegister current = |
| 107 | X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i)); |
| 108 | if (current.AsRegisterPairLow() == pair.AsRegisterPairLow() |
| 109 | || current.AsRegisterPairLow() == pair.AsRegisterPairHigh() |
| 110 | || current.AsRegisterPairHigh() == pair.AsRegisterPairLow() |
| 111 | || current.AsRegisterPairHigh() == pair.AsRegisterPairHigh()) { |
| 112 | blocked_register_pairs[i] = true; |
| 113 | } |
| 114 | } |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 115 | return pair; |
| 116 | } |
| 117 | |
| 118 | case Primitive::kPrimByte: |
| 119 | case Primitive::kPrimBoolean: |
| 120 | case Primitive::kPrimChar: |
| 121 | case Primitive::kPrimShort: |
| 122 | case Primitive::kPrimInt: |
| 123 | case Primitive::kPrimNot: { |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 124 | Register reg = static_cast<Register>( |
| 125 | AllocateFreeRegisterInternal(blocked_registers, kNumberOfCpuRegisters)); |
| 126 | // Block all register pairs that contain `reg`. |
| 127 | bool* blocked_register_pairs = GetBlockedRegisterPairs(blocked_registers); |
| 128 | for (int i = 0; i < kNumberOfRegisterPairs; i++) { |
| 129 | X86ManagedRegister current = |
| 130 | X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i)); |
| 131 | if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) { |
| 132 | blocked_register_pairs[i] = true; |
| 133 | } |
| 134 | } |
| 135 | return X86ManagedRegister::FromCpuRegister(reg); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | case Primitive::kPrimFloat: |
| 139 | case Primitive::kPrimDouble: |
| 140 | LOG(FATAL) << "Unimplemented register type " << type; |
| 141 | |
| 142 | case Primitive::kPrimVoid: |
| 143 | LOG(FATAL) << "Unreachable type " << type; |
| 144 | } |
| 145 | |
| 146 | return ManagedRegister::NoRegister(); |
| 147 | } |
| 148 | |
| 149 | void CodeGeneratorX86::SetupBlockedRegisters(bool* blocked_registers) const { |
| 150 | bool* blocked_register_pairs = GetBlockedRegisterPairs(blocked_registers); |
| 151 | |
| 152 | // Don't allocate the dalvik style register pair passing. |
| 153 | blocked_register_pairs[ECX_EDX] = true; |
| 154 | |
| 155 | // Stack register is always reserved. |
| 156 | blocked_registers[ESP] = true; |
| 157 | |
| 158 | // TODO: We currently don't use Quick's callee saved registers. |
| 159 | blocked_registers[EBP] = true; |
| 160 | blocked_registers[ESI] = true; |
| 161 | blocked_registers[EDI] = true; |
| 162 | blocked_register_pairs[EAX_EDI] = true; |
| 163 | blocked_register_pairs[EDX_EDI] = true; |
| 164 | blocked_register_pairs[ECX_EDI] = true; |
| 165 | blocked_register_pairs[EBX_EDI] = true; |
| 166 | } |
| 167 | |
| 168 | size_t CodeGeneratorX86::GetNumberOfRegisters() const { |
| 169 | return kNumberOfRegIds; |
| 170 | } |
| 171 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 172 | static Location X86CpuLocation(Register reg) { |
| 173 | return Location::RegisterLocation(X86ManagedRegister::FromCpuRegister(reg)); |
| 174 | } |
| 175 | |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 176 | InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen) |
| 177 | : HGraphVisitor(graph), |
| 178 | assembler_(codegen->GetAssembler()), |
| 179 | codegen_(codegen) {} |
| 180 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 181 | void CodeGeneratorX86::GenerateFrameEntry() { |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 182 | // Create a fake register to mimic Quick. |
| 183 | static const int kFakeReturnRegister = 8; |
| 184 | core_spill_mask_ |= (1 << kFakeReturnRegister); |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 185 | |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 186 | // The return PC has already been pushed on the stack. |
Nicolas Geoffray | 707c809 | 2014-04-04 10:50:14 +0100 | [diff] [blame] | 187 | __ subl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize)); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 188 | __ movl(Address(ESP, kCurrentMethodStackOffset), EAX); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | void CodeGeneratorX86::GenerateFrameExit() { |
Nicolas Geoffray | 707c809 | 2014-04-04 10:50:14 +0100 | [diff] [blame] | 192 | __ addl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize)); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | void CodeGeneratorX86::Bind(Label* label) { |
| 196 | __ Bind(label); |
| 197 | } |
| 198 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 199 | void InstructionCodeGeneratorX86::LoadCurrentMethod(Register reg) { |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 200 | __ movl(reg, Address(ESP, kCurrentMethodStackOffset)); |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 203 | Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const { |
| 204 | switch (load->GetType()) { |
| 205 | case Primitive::kPrimLong: |
| 206 | return Location::DoubleStackSlot(GetStackSlot(load->GetLocal())); |
| 207 | break; |
| 208 | |
| 209 | case Primitive::kPrimInt: |
| 210 | case Primitive::kPrimNot: |
| 211 | return Location::StackSlot(GetStackSlot(load->GetLocal())); |
| 212 | |
| 213 | case Primitive::kPrimFloat: |
| 214 | case Primitive::kPrimDouble: |
| 215 | LOG(FATAL) << "Unimplemented type " << load->GetType(); |
| 216 | |
| 217 | case Primitive::kPrimBoolean: |
| 218 | case Primitive::kPrimByte: |
| 219 | case Primitive::kPrimChar: |
| 220 | case Primitive::kPrimShort: |
| 221 | case Primitive::kPrimVoid: |
| 222 | LOG(FATAL) << "Unexpected type " << load->GetType(); |
| 223 | } |
| 224 | |
| 225 | LOG(FATAL) << "Unreachable"; |
| 226 | return Location(); |
| 227 | } |
| 228 | |
Nicolas Geoffray | db928fc | 2014-04-16 17:38:32 +0100 | [diff] [blame] | 229 | static constexpr Register kRuntimeParameterCoreRegisters[] = { EAX, ECX, EDX }; |
| 230 | static constexpr size_t kRuntimeParameterCoreRegistersLength = |
| 231 | arraysize(kRuntimeParameterCoreRegisters); |
| 232 | |
| 233 | class InvokeRuntimeCallingConvention : public CallingConvention<Register> { |
| 234 | public: |
| 235 | InvokeRuntimeCallingConvention() |
| 236 | : CallingConvention(kRuntimeParameterCoreRegisters, |
| 237 | kRuntimeParameterCoreRegistersLength) {} |
| 238 | |
| 239 | private: |
| 240 | DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention); |
| 241 | }; |
| 242 | |
Nicolas Geoffray | a747a39 | 2014-04-17 14:56:23 +0100 | [diff] [blame] | 243 | Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) { |
| 244 | switch (type) { |
| 245 | case Primitive::kPrimBoolean: |
| 246 | case Primitive::kPrimByte: |
| 247 | case Primitive::kPrimChar: |
| 248 | case Primitive::kPrimShort: |
| 249 | case Primitive::kPrimInt: |
| 250 | case Primitive::kPrimNot: { |
| 251 | uint32_t index = gp_index_++; |
| 252 | if (index < calling_convention.GetNumberOfRegisters()) { |
| 253 | return X86CpuLocation(calling_convention.GetRegisterAt(index)); |
| 254 | } else { |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 255 | return Location::StackSlot(calling_convention.GetStackOffsetOf(index)); |
Nicolas Geoffray | db928fc | 2014-04-16 17:38:32 +0100 | [diff] [blame] | 256 | } |
Nicolas Geoffray | db928fc | 2014-04-16 17:38:32 +0100 | [diff] [blame] | 257 | } |
Nicolas Geoffray | a747a39 | 2014-04-17 14:56:23 +0100 | [diff] [blame] | 258 | |
| 259 | case Primitive::kPrimLong: { |
| 260 | uint32_t index = gp_index_; |
| 261 | gp_index_ += 2; |
| 262 | if (index + 1 < calling_convention.GetNumberOfRegisters()) { |
| 263 | return Location::RegisterLocation(X86ManagedRegister::FromRegisterPair( |
| 264 | calling_convention.GetRegisterPairAt(index))); |
| 265 | } else if (index + 1 == calling_convention.GetNumberOfRegisters()) { |
| 266 | return Location::QuickParameter(index); |
| 267 | } else { |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 268 | return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(index)); |
Nicolas Geoffray | a747a39 | 2014-04-17 14:56:23 +0100 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | |
| 272 | case Primitive::kPrimDouble: |
| 273 | case Primitive::kPrimFloat: |
| 274 | LOG(FATAL) << "Unimplemented parameter type " << type; |
| 275 | break; |
| 276 | |
| 277 | case Primitive::kPrimVoid: |
| 278 | LOG(FATAL) << "Unexpected parameter type " << type; |
| 279 | break; |
Nicolas Geoffray | db928fc | 2014-04-16 17:38:32 +0100 | [diff] [blame] | 280 | } |
Nicolas Geoffray | a747a39 | 2014-04-17 14:56:23 +0100 | [diff] [blame] | 281 | return Location(); |
| 282 | } |
Nicolas Geoffray | db928fc | 2014-04-16 17:38:32 +0100 | [diff] [blame] | 283 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 284 | void CodeGeneratorX86::Move32(Location destination, Location source) { |
| 285 | if (source.Equals(destination)) { |
| 286 | return; |
| 287 | } |
| 288 | if (destination.IsRegister()) { |
| 289 | if (source.IsRegister()) { |
| 290 | __ movl(destination.AsX86().AsCpuRegister(), source.AsX86().AsCpuRegister()); |
| 291 | } else { |
| 292 | DCHECK(source.IsStackSlot()); |
| 293 | __ movl(destination.AsX86().AsCpuRegister(), Address(ESP, source.GetStackIndex())); |
| 294 | } |
| 295 | } else { |
| 296 | if (source.IsRegister()) { |
| 297 | __ movl(Address(ESP, destination.GetStackIndex()), source.AsX86().AsCpuRegister()); |
| 298 | } else { |
| 299 | DCHECK(source.IsStackSlot()); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 300 | __ pushl(Address(ESP, source.GetStackIndex())); |
| 301 | __ popl(Address(ESP, destination.GetStackIndex())); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | void CodeGeneratorX86::Move64(Location destination, Location source) { |
| 307 | if (source.Equals(destination)) { |
| 308 | return; |
| 309 | } |
| 310 | if (destination.IsRegister()) { |
| 311 | if (source.IsRegister()) { |
| 312 | __ movl(destination.AsX86().AsRegisterPairLow(), source.AsX86().AsRegisterPairLow()); |
| 313 | __ movl(destination.AsX86().AsRegisterPairHigh(), source.AsX86().AsRegisterPairHigh()); |
| 314 | } else if (source.IsQuickParameter()) { |
| 315 | uint32_t argument_index = source.GetQuickParameterIndex(); |
| 316 | InvokeDexCallingConvention calling_convention; |
| 317 | __ movl(destination.AsX86().AsRegisterPairLow(), |
| 318 | calling_convention.GetRegisterAt(argument_index)); |
Nicolas Geoffray | a747a39 | 2014-04-17 14:56:23 +0100 | [diff] [blame] | 319 | __ movl(destination.AsX86().AsRegisterPairHigh(), Address(ESP, |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 320 | calling_convention.GetStackOffsetOf(argument_index + 1) + GetFrameSize())); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 321 | } else { |
| 322 | DCHECK(source.IsDoubleStackSlot()); |
| 323 | __ movl(destination.AsX86().AsRegisterPairLow(), Address(ESP, source.GetStackIndex())); |
| 324 | __ movl(destination.AsX86().AsRegisterPairHigh(), |
| 325 | Address(ESP, source.GetHighStackIndex(kX86WordSize))); |
| 326 | } |
| 327 | } else if (destination.IsQuickParameter()) { |
| 328 | InvokeDexCallingConvention calling_convention; |
| 329 | uint32_t argument_index = destination.GetQuickParameterIndex(); |
| 330 | if (source.IsRegister()) { |
| 331 | __ movl(calling_convention.GetRegisterAt(argument_index), source.AsX86().AsRegisterPairLow()); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 332 | __ movl(Address(ESP, calling_convention.GetStackOffsetOf(argument_index + 1)), |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 333 | source.AsX86().AsRegisterPairHigh()); |
| 334 | } else { |
| 335 | DCHECK(source.IsDoubleStackSlot()); |
| 336 | __ movl(calling_convention.GetRegisterAt(argument_index), |
| 337 | Address(ESP, source.GetStackIndex())); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 338 | __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize))); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 339 | __ popl(Address(ESP, calling_convention.GetStackOffsetOf(argument_index + 1))); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 340 | } |
| 341 | } else { |
| 342 | if (source.IsRegister()) { |
| 343 | __ movl(Address(ESP, destination.GetStackIndex()), source.AsX86().AsRegisterPairLow()); |
| 344 | __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), |
| 345 | source.AsX86().AsRegisterPairHigh()); |
| 346 | } else if (source.IsQuickParameter()) { |
| 347 | InvokeDexCallingConvention calling_convention; |
| 348 | uint32_t argument_index = source.GetQuickParameterIndex(); |
| 349 | __ movl(Address(ESP, destination.GetStackIndex()), |
| 350 | calling_convention.GetRegisterAt(argument_index)); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 351 | __ pushl(Address(ESP, |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 352 | calling_convention.GetStackOffsetOf(argument_index + 1) + GetFrameSize())); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 353 | __ popl(Address(ESP, destination.GetHighStackIndex(kX86WordSize))); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 354 | } else { |
| 355 | DCHECK(source.IsDoubleStackSlot()); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 356 | __ pushl(Address(ESP, source.GetStackIndex())); |
| 357 | __ popl(Address(ESP, destination.GetStackIndex())); |
| 358 | __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize))); |
| 359 | __ popl(Address(ESP, destination.GetHighStackIndex(kX86WordSize))); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | } |
| 363 | |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 364 | void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) { |
| 365 | if (instruction->AsIntConstant() != nullptr) { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 366 | Immediate imm(instruction->AsIntConstant()->GetValue()); |
| 367 | if (location.IsRegister()) { |
| 368 | __ movl(location.AsX86().AsCpuRegister(), imm); |
| 369 | } else { |
| 370 | __ movl(Address(ESP, location.GetStackIndex()), imm); |
| 371 | } |
| 372 | } else if (instruction->AsLongConstant() != nullptr) { |
| 373 | int64_t value = instruction->AsLongConstant()->GetValue(); |
| 374 | if (location.IsRegister()) { |
| 375 | __ movl(location.AsX86().AsRegisterPairLow(), Immediate(Low32Bits(value))); |
| 376 | __ movl(location.AsX86().AsRegisterPairHigh(), Immediate(High32Bits(value))); |
| 377 | } else { |
| 378 | __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value))); |
| 379 | __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value))); |
| 380 | } |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 381 | } else if (instruction->AsLoadLocal() != nullptr) { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 382 | switch (instruction->GetType()) { |
| 383 | case Primitive::kPrimBoolean: |
| 384 | case Primitive::kPrimByte: |
| 385 | case Primitive::kPrimChar: |
| 386 | case Primitive::kPrimShort: |
| 387 | case Primitive::kPrimInt: |
| 388 | case Primitive::kPrimNot: |
| 389 | Move32(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal()))); |
| 390 | break; |
| 391 | |
| 392 | case Primitive::kPrimLong: |
| 393 | Move64(location, Location::DoubleStackSlot( |
| 394 | GetStackSlot(instruction->AsLoadLocal()->GetLocal()))); |
| 395 | break; |
| 396 | |
| 397 | default: |
| 398 | LOG(FATAL) << "Unimplemented local type " << instruction->GetType(); |
| 399 | } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 400 | } else { |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 401 | DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary()); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 402 | switch (instruction->GetType()) { |
| 403 | case Primitive::kPrimBoolean: |
| 404 | case Primitive::kPrimByte: |
| 405 | case Primitive::kPrimChar: |
| 406 | case Primitive::kPrimShort: |
| 407 | case Primitive::kPrimInt: |
| 408 | case Primitive::kPrimNot: |
| 409 | Move32(location, instruction->GetLocations()->Out()); |
| 410 | break; |
| 411 | |
| 412 | case Primitive::kPrimLong: |
| 413 | Move64(location, instruction->GetLocations()->Out()); |
| 414 | break; |
| 415 | |
| 416 | default: |
| 417 | LOG(FATAL) << "Unimplemented type " << instruction->GetType(); |
| 418 | } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | |
| 422 | void LocationsBuilderX86::VisitGoto(HGoto* got) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 423 | got->SetLocations(nullptr); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 424 | } |
| 425 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 426 | void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) { |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 427 | HBasicBlock* successor = got->GetSuccessor(); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 428 | if (GetGraph()->GetExitBlock() == successor) { |
| 429 | codegen_->GenerateFrameExit(); |
| 430 | } else if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) { |
| 431 | __ jmp(codegen_->GetLabelOf(successor)); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 432 | } |
| 433 | } |
| 434 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 435 | void LocationsBuilderX86::VisitExit(HExit* exit) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 436 | exit->SetLocations(nullptr); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 439 | void InstructionCodeGeneratorX86::VisitExit(HExit* exit) { |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 440 | if (kIsDebugBuild) { |
| 441 | __ Comment("Unreachable"); |
| 442 | __ int3(); |
| 443 | } |
| 444 | } |
| 445 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 446 | void LocationsBuilderX86::VisitIf(HIf* if_instr) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 447 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr); |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 448 | HInstruction* cond = if_instr->InputAt(0); |
| 449 | DCHECK(cond->IsCondition()); |
| 450 | HCondition* condition = cond->AsCondition(); |
| 451 | if (condition->NeedsMaterialization()) { |
| 452 | locations->SetInAt(0, Location::Any()); |
| 453 | } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 454 | if_instr->SetLocations(locations); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 457 | void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) { |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 458 | HInstruction* cond = if_instr->InputAt(0); |
| 459 | DCHECK(cond->IsCondition()); |
| 460 | HCondition* condition = cond->AsCondition(); |
| 461 | if (condition->NeedsMaterialization()) { |
| 462 | // Materialized condition, compare against 0 |
| 463 | Location lhs = if_instr->GetLocations()->InAt(0); |
| 464 | if (lhs.IsRegister()) { |
| 465 | __ cmpl(lhs.AsX86().AsCpuRegister(), Immediate(0)); |
| 466 | } else { |
| 467 | __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0)); |
| 468 | } |
| 469 | __ j(kEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor())); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 470 | } else { |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 471 | Location lhs = condition->GetLocations()->InAt(0); |
| 472 | Location rhs = condition->GetLocations()->InAt(1); |
| 473 | // LHS is guaranteed to be in a register (see LocationsBuilderX86::VisitCondition). |
| 474 | if (rhs.IsRegister()) { |
| 475 | __ cmpl(lhs.AsX86().AsCpuRegister(), rhs.AsX86().AsCpuRegister()); |
| 476 | } else { |
| 477 | __ cmpl(lhs.AsX86().AsCpuRegister(), Address(ESP, rhs.GetStackIndex())); |
| 478 | } |
| 479 | __ j(X86Condition(condition->GetCondition()), |
| 480 | codegen_->GetLabelOf(if_instr->IfTrueSuccessor())); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 481 | } |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 482 | if (!codegen_->GoesToNextBlock(if_instr->GetBlock(), if_instr->IfFalseSuccessor())) { |
| 483 | __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor())); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | |
| 487 | void LocationsBuilderX86::VisitLocal(HLocal* local) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 488 | local->SetLocations(nullptr); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 491 | void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) { |
| 492 | DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock()); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 495 | void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) { |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 496 | local->SetLocations(nullptr); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 499 | void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) { |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 500 | // Nothing to do, this is driven by the code generator. |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 503 | void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) { |
| 504 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store); |
| 505 | switch (store->InputAt(1)->GetType()) { |
| 506 | case Primitive::kPrimBoolean: |
| 507 | case Primitive::kPrimByte: |
| 508 | case Primitive::kPrimChar: |
| 509 | case Primitive::kPrimShort: |
| 510 | case Primitive::kPrimInt: |
| 511 | case Primitive::kPrimNot: |
| 512 | locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal()))); |
| 513 | break; |
| 514 | |
| 515 | case Primitive::kPrimLong: |
| 516 | locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal()))); |
| 517 | break; |
| 518 | |
| 519 | default: |
| 520 | LOG(FATAL) << "Unimplemented local type " << store->InputAt(1)->GetType(); |
| 521 | } |
| 522 | store->SetLocations(locations); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 523 | } |
| 524 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 525 | void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 528 | void LocationsBuilderX86::VisitCondition(HCondition* comp) { |
| 529 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(comp); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 530 | locations->SetInAt(0, Location::RequiresRegister()); |
| 531 | locations->SetInAt(1, Location::Any()); |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 532 | if (comp->NeedsMaterialization()) { |
| 533 | locations->SetOut(Location::SameAsFirstInput()); |
| 534 | } |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 535 | comp->SetLocations(locations); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 536 | } |
| 537 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 538 | void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) { |
| 539 | if (comp->NeedsMaterialization()) { |
| 540 | LocationSummary* locations = comp->GetLocations(); |
| 541 | if (locations->InAt(1).IsRegister()) { |
| 542 | __ cmpl(locations->InAt(0).AsX86().AsCpuRegister(), |
| 543 | locations->InAt(1).AsX86().AsCpuRegister()); |
| 544 | } else { |
| 545 | __ cmpl(locations->InAt(0).AsX86().AsCpuRegister(), |
| 546 | Address(ESP, locations->InAt(1).GetStackIndex())); |
| 547 | } |
| 548 | __ setb(X86Condition(comp->GetCondition()), locations->Out().AsX86().AsCpuRegister()); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 549 | } |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | void LocationsBuilderX86::VisitEqual(HEqual* comp) { |
| 553 | VisitCondition(comp); |
| 554 | } |
| 555 | |
| 556 | void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) { |
| 557 | VisitCondition(comp); |
| 558 | } |
| 559 | |
| 560 | void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) { |
| 561 | VisitCondition(comp); |
| 562 | } |
| 563 | |
| 564 | void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) { |
| 565 | VisitCondition(comp); |
| 566 | } |
| 567 | |
| 568 | void LocationsBuilderX86::VisitLessThan(HLessThan* comp) { |
| 569 | VisitCondition(comp); |
| 570 | } |
| 571 | |
| 572 | void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) { |
| 573 | VisitCondition(comp); |
| 574 | } |
| 575 | |
| 576 | void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) { |
| 577 | VisitCondition(comp); |
| 578 | } |
| 579 | |
| 580 | void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) { |
| 581 | VisitCondition(comp); |
| 582 | } |
| 583 | |
| 584 | void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) { |
| 585 | VisitCondition(comp); |
| 586 | } |
| 587 | |
| 588 | void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) { |
| 589 | VisitCondition(comp); |
| 590 | } |
| 591 | |
| 592 | void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) { |
| 593 | VisitCondition(comp); |
| 594 | } |
| 595 | |
| 596 | void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) { |
| 597 | VisitCondition(comp); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) { |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 601 | // TODO: Support constant locations. |
| 602 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant); |
| 603 | locations->SetOut(Location::RequiresRegister()); |
| 604 | constant->SetLocations(locations); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 607 | void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) { |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 608 | codegen_->Move(constant, constant->GetLocations()->Out(), nullptr); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 609 | } |
| 610 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 611 | void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) { |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 612 | // TODO: Support constant locations. |
| 613 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant); |
| 614 | locations->SetOut(Location::RequiresRegister()); |
| 615 | constant->SetLocations(locations); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) { |
| 619 | // Will be generated at use site. |
| 620 | } |
| 621 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 622 | void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 623 | ret->SetLocations(nullptr); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 624 | } |
| 625 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 626 | void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) { |
| 627 | codegen_->GenerateFrameExit(); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 628 | __ ret(); |
| 629 | } |
| 630 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 631 | void LocationsBuilderX86::VisitReturn(HReturn* ret) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 632 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 633 | switch (ret->InputAt(0)->GetType()) { |
| 634 | case Primitive::kPrimBoolean: |
| 635 | case Primitive::kPrimByte: |
| 636 | case Primitive::kPrimChar: |
| 637 | case Primitive::kPrimShort: |
| 638 | case Primitive::kPrimInt: |
| 639 | case Primitive::kPrimNot: |
| 640 | locations->SetInAt(0, X86CpuLocation(EAX)); |
| 641 | break; |
| 642 | |
| 643 | case Primitive::kPrimLong: |
| 644 | locations->SetInAt( |
| 645 | 0, Location::RegisterLocation(X86ManagedRegister::FromRegisterPair(EAX_EDX))); |
| 646 | break; |
| 647 | |
| 648 | default: |
| 649 | LOG(FATAL) << "Unimplemented return type " << ret->InputAt(0)->GetType(); |
| 650 | } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 651 | ret->SetLocations(locations); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 652 | } |
| 653 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 654 | void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 655 | if (kIsDebugBuild) { |
| 656 | switch (ret->InputAt(0)->GetType()) { |
| 657 | case Primitive::kPrimBoolean: |
| 658 | case Primitive::kPrimByte: |
| 659 | case Primitive::kPrimChar: |
| 660 | case Primitive::kPrimShort: |
| 661 | case Primitive::kPrimInt: |
| 662 | case Primitive::kPrimNot: |
| 663 | DCHECK_EQ(ret->GetLocations()->InAt(0).AsX86().AsCpuRegister(), EAX); |
| 664 | break; |
| 665 | |
| 666 | case Primitive::kPrimLong: |
| 667 | DCHECK_EQ(ret->GetLocations()->InAt(0).AsX86().AsRegisterPair(), EAX_EDX); |
| 668 | break; |
| 669 | |
| 670 | default: |
| 671 | LOG(FATAL) << "Unimplemented return type " << ret->InputAt(0)->GetType(); |
| 672 | } |
| 673 | } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 674 | codegen_->GenerateFrameExit(); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 675 | __ ret(); |
| 676 | } |
| 677 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 678 | void LocationsBuilderX86::VisitInvokeStatic(HInvokeStatic* invoke) { |
| 679 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(invoke); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 680 | locations->AddTemp(X86CpuLocation(EAX)); |
Nicolas Geoffray | db928fc | 2014-04-16 17:38:32 +0100 | [diff] [blame] | 681 | |
| 682 | InvokeDexCallingConventionVisitor calling_convention_visitor; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 683 | for (size_t i = 0; i < invoke->InputCount(); i++) { |
Nicolas Geoffray | db928fc | 2014-04-16 17:38:32 +0100 | [diff] [blame] | 684 | HInstruction* input = invoke->InputAt(i); |
| 685 | locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType())); |
| 686 | } |
| 687 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 688 | switch (invoke->GetType()) { |
| 689 | case Primitive::kPrimBoolean: |
| 690 | case Primitive::kPrimByte: |
| 691 | case Primitive::kPrimChar: |
| 692 | case Primitive::kPrimShort: |
| 693 | case Primitive::kPrimInt: |
| 694 | case Primitive::kPrimNot: |
| 695 | locations->SetOut(X86CpuLocation(EAX)); |
| 696 | break; |
| 697 | |
| 698 | case Primitive::kPrimLong: |
| 699 | locations->SetOut(Location::RegisterLocation(X86ManagedRegister::FromRegisterPair(EAX_EDX))); |
| 700 | break; |
| 701 | |
| 702 | case Primitive::kPrimVoid: |
| 703 | break; |
| 704 | |
| 705 | case Primitive::kPrimDouble: |
| 706 | case Primitive::kPrimFloat: |
| 707 | LOG(FATAL) << "Unimplemented return type " << invoke->GetType(); |
| 708 | break; |
| 709 | } |
| 710 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 711 | invoke->SetLocations(locations); |
| 712 | } |
| 713 | |
| 714 | void InstructionCodeGeneratorX86::VisitInvokeStatic(HInvokeStatic* invoke) { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 715 | Register temp = invoke->GetLocations()->GetTemp(0).AsX86().AsCpuRegister(); |
Nicolas Geoffray | f61b537 | 2014-06-25 14:35:34 +0100 | [diff] [blame] | 716 | uint32_t heap_reference_size = sizeof(mirror::HeapReference<mirror::Object>); |
| 717 | size_t index_in_cache = mirror::Array::DataOffset(heap_reference_size).Int32Value() + |
Nicolas Geoffray | 707c809 | 2014-04-04 10:50:14 +0100 | [diff] [blame] | 718 | invoke->GetIndexInDexCache() * kX86WordSize; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 719 | |
| 720 | // TODO: Implement all kinds of calls: |
| 721 | // 1) boot -> boot |
| 722 | // 2) app -> boot |
| 723 | // 3) app -> app |
| 724 | // |
| 725 | // Currently we implement the app -> app logic, which looks up in the resolve cache. |
| 726 | |
| 727 | // temp = method; |
| 728 | LoadCurrentMethod(temp); |
| 729 | // temp = temp->dex_cache_resolved_methods_; |
| 730 | __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value())); |
| 731 | // temp = temp[index_in_cache] |
| 732 | __ movl(temp, Address(temp, index_in_cache)); |
| 733 | // (temp + offset_of_quick_compiled_code)() |
| 734 | __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value())); |
| 735 | |
| 736 | codegen_->RecordPcInfo(invoke->GetDexPc()); |
| 737 | } |
| 738 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 739 | void LocationsBuilderX86::VisitAdd(HAdd* add) { |
| 740 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(add); |
| 741 | switch (add->GetResultType()) { |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 742 | case Primitive::kPrimInt: |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 743 | case Primitive::kPrimLong: { |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 744 | locations->SetInAt(0, Location::RequiresRegister()); |
| 745 | locations->SetInAt(1, Location::Any()); |
| 746 | locations->SetOut(Location::SameAsFirstInput()); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 747 | break; |
| 748 | } |
| 749 | |
| 750 | case Primitive::kPrimBoolean: |
| 751 | case Primitive::kPrimByte: |
| 752 | case Primitive::kPrimChar: |
| 753 | case Primitive::kPrimShort: |
| 754 | LOG(FATAL) << "Unexpected add type " << add->GetResultType(); |
| 755 | break; |
| 756 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 757 | default: |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 758 | LOG(FATAL) << "Unimplemented add type " << add->GetResultType(); |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 759 | } |
| 760 | add->SetLocations(locations); |
| 761 | } |
| 762 | |
| 763 | void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) { |
| 764 | LocationSummary* locations = add->GetLocations(); |
| 765 | switch (add->GetResultType()) { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 766 | case Primitive::kPrimInt: { |
| 767 | DCHECK_EQ(locations->InAt(0).AsX86().AsCpuRegister(), |
| 768 | locations->Out().AsX86().AsCpuRegister()); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 769 | if (locations->InAt(1).IsRegister()) { |
| 770 | __ addl(locations->InAt(0).AsX86().AsCpuRegister(), |
| 771 | locations->InAt(1).AsX86().AsCpuRegister()); |
| 772 | } else { |
| 773 | __ addl(locations->InAt(0).AsX86().AsCpuRegister(), |
| 774 | Address(ESP, locations->InAt(1).GetStackIndex())); |
| 775 | } |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 776 | break; |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | case Primitive::kPrimLong: { |
| 780 | DCHECK_EQ(locations->InAt(0).AsX86().AsRegisterPair(), |
| 781 | locations->Out().AsX86().AsRegisterPair()); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 782 | if (locations->InAt(1).IsRegister()) { |
| 783 | __ addl(locations->InAt(0).AsX86().AsRegisterPairLow(), |
| 784 | locations->InAt(1).AsX86().AsRegisterPairLow()); |
| 785 | __ adcl(locations->InAt(0).AsX86().AsRegisterPairHigh(), |
| 786 | locations->InAt(1).AsX86().AsRegisterPairHigh()); |
| 787 | } else { |
| 788 | __ addl(locations->InAt(0).AsX86().AsRegisterPairLow(), |
| 789 | Address(ESP, locations->InAt(1).GetStackIndex())); |
| 790 | __ adcl(locations->InAt(0).AsX86().AsRegisterPairHigh(), |
| 791 | Address(ESP, locations->InAt(1).GetHighStackIndex(kX86WordSize))); |
| 792 | } |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 793 | break; |
| 794 | } |
| 795 | |
| 796 | case Primitive::kPrimBoolean: |
| 797 | case Primitive::kPrimByte: |
| 798 | case Primitive::kPrimChar: |
| 799 | case Primitive::kPrimShort: |
| 800 | LOG(FATAL) << "Unexpected add type " << add->GetResultType(); |
| 801 | break; |
| 802 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 803 | default: |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 804 | LOG(FATAL) << "Unimplemented add type " << add->GetResultType(); |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 805 | } |
| 806 | } |
| 807 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 808 | void LocationsBuilderX86::VisitSub(HSub* sub) { |
| 809 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(sub); |
| 810 | switch (sub->GetResultType()) { |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 811 | case Primitive::kPrimInt: |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 812 | case Primitive::kPrimLong: { |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 813 | locations->SetInAt(0, Location::RequiresRegister()); |
| 814 | locations->SetInAt(1, Location::Any()); |
| 815 | locations->SetOut(Location::SameAsFirstInput()); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 816 | break; |
| 817 | } |
| 818 | |
| 819 | case Primitive::kPrimBoolean: |
| 820 | case Primitive::kPrimByte: |
| 821 | case Primitive::kPrimChar: |
| 822 | case Primitive::kPrimShort: |
| 823 | LOG(FATAL) << "Unexpected sub type " << sub->GetResultType(); |
| 824 | break; |
| 825 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 826 | default: |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 827 | LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType(); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 828 | } |
| 829 | sub->SetLocations(locations); |
| 830 | } |
| 831 | |
| 832 | void InstructionCodeGeneratorX86::VisitSub(HSub* sub) { |
| 833 | LocationSummary* locations = sub->GetLocations(); |
| 834 | switch (sub->GetResultType()) { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 835 | case Primitive::kPrimInt: { |
| 836 | DCHECK_EQ(locations->InAt(0).AsX86().AsCpuRegister(), |
| 837 | locations->Out().AsX86().AsCpuRegister()); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 838 | if (locations->InAt(1).IsRegister()) { |
| 839 | __ subl(locations->InAt(0).AsX86().AsCpuRegister(), |
| 840 | locations->InAt(1).AsX86().AsCpuRegister()); |
| 841 | } else { |
| 842 | __ subl(locations->InAt(0).AsX86().AsCpuRegister(), |
| 843 | Address(ESP, locations->InAt(1).GetStackIndex())); |
| 844 | } |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 845 | break; |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | case Primitive::kPrimLong: { |
| 849 | DCHECK_EQ(locations->InAt(0).AsX86().AsRegisterPair(), |
| 850 | locations->Out().AsX86().AsRegisterPair()); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 851 | if (locations->InAt(1).IsRegister()) { |
| 852 | __ subl(locations->InAt(0).AsX86().AsRegisterPairLow(), |
| 853 | locations->InAt(1).AsX86().AsRegisterPairLow()); |
| 854 | __ sbbl(locations->InAt(0).AsX86().AsRegisterPairHigh(), |
| 855 | locations->InAt(1).AsX86().AsRegisterPairHigh()); |
| 856 | } else { |
| 857 | __ subl(locations->InAt(0).AsX86().AsRegisterPairLow(), |
| 858 | Address(ESP, locations->InAt(1).GetStackIndex())); |
| 859 | __ sbbl(locations->InAt(0).AsX86().AsRegisterPairHigh(), |
| 860 | Address(ESP, locations->InAt(1).GetHighStackIndex(kX86WordSize))); |
| 861 | } |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 862 | break; |
| 863 | } |
| 864 | |
| 865 | case Primitive::kPrimBoolean: |
| 866 | case Primitive::kPrimByte: |
| 867 | case Primitive::kPrimChar: |
| 868 | case Primitive::kPrimShort: |
| 869 | LOG(FATAL) << "Unexpected sub type " << sub->GetResultType(); |
| 870 | break; |
| 871 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 872 | default: |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 873 | LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType(); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 874 | } |
| 875 | } |
| 876 | |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 877 | void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) { |
| 878 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 879 | locations->SetOut(X86CpuLocation(EAX)); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 880 | InvokeRuntimeCallingConvention calling_convention; |
| 881 | locations->AddTemp(X86CpuLocation(calling_convention.GetRegisterAt(0))); |
| 882 | locations->AddTemp(X86CpuLocation(calling_convention.GetRegisterAt(1))); |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 883 | instruction->SetLocations(locations); |
| 884 | } |
| 885 | |
| 886 | void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) { |
| 887 | InvokeRuntimeCallingConvention calling_convention; |
| 888 | LoadCurrentMethod(calling_convention.GetRegisterAt(1)); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 889 | __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex())); |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 890 | |
Nicolas Geoffray | 707c809 | 2014-04-04 10:50:14 +0100 | [diff] [blame] | 891 | __ fs()->call( |
| 892 | Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck))); |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 893 | |
| 894 | codegen_->RecordPcInfo(instruction->GetDexPc()); |
| 895 | } |
| 896 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 897 | void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) { |
| 898 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction); |
Nicolas Geoffray | a747a39 | 2014-04-17 14:56:23 +0100 | [diff] [blame] | 899 | Location location = parameter_visitor_.GetNextLocation(instruction->GetType()); |
| 900 | if (location.IsStackSlot()) { |
| 901 | location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize()); |
| 902 | } else if (location.IsDoubleStackSlot()) { |
| 903 | location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize()); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 904 | } |
Nicolas Geoffray | a747a39 | 2014-04-17 14:56:23 +0100 | [diff] [blame] | 905 | locations->SetOut(location); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 906 | instruction->SetLocations(locations); |
| 907 | } |
| 908 | |
| 909 | void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) { |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 910 | } |
| 911 | |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 912 | void LocationsBuilderX86::VisitNot(HNot* instruction) { |
| 913 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 914 | locations->SetInAt(0, Location::RequiresRegister()); |
| 915 | locations->SetOut(Location::SameAsFirstInput()); |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 916 | instruction->SetLocations(locations); |
| 917 | } |
| 918 | |
| 919 | void InstructionCodeGeneratorX86::VisitNot(HNot* instruction) { |
| 920 | LocationSummary* locations = instruction->GetLocations(); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 921 | Location out = locations->Out(); |
| 922 | DCHECK_EQ(locations->InAt(0).AsX86().AsCpuRegister(), out.AsX86().AsCpuRegister()); |
| 923 | __ xorl(out.AsX86().AsCpuRegister(), Immediate(1)); |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 924 | } |
| 925 | |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 926 | void LocationsBuilderX86::VisitCompare(HCompare* compare) { |
| 927 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare); |
| 928 | locations->SetInAt(0, Location::RequiresRegister()); |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 929 | locations->SetInAt(1, Location::Any()); |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 930 | locations->SetOut(Location::RequiresRegister()); |
| 931 | compare->SetLocations(locations); |
| 932 | } |
| 933 | |
| 934 | void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) { |
| 935 | Label greater, done; |
| 936 | LocationSummary* locations = compare->GetLocations(); |
| 937 | switch (compare->InputAt(0)->GetType()) { |
| 938 | case Primitive::kPrimLong: { |
| 939 | Label less, greater, done; |
| 940 | Register output = locations->Out().AsX86().AsCpuRegister(); |
| 941 | X86ManagedRegister left = locations->InAt(0).AsX86(); |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 942 | Location right = locations->InAt(1); |
| 943 | if (right.IsRegister()) { |
| 944 | __ cmpl(left.AsRegisterPairHigh(), right.AsX86().AsRegisterPairHigh()); |
| 945 | } else { |
| 946 | DCHECK(right.IsDoubleStackSlot()); |
| 947 | __ cmpl(left.AsRegisterPairHigh(), Address(ESP, right.GetHighStackIndex(kX86WordSize))); |
| 948 | } |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 949 | __ j(kLess, &less); // Signed compare. |
| 950 | __ j(kGreater, &greater); // Signed compare. |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 951 | if (right.IsRegister()) { |
| 952 | __ cmpl(left.AsRegisterPairLow(), right.AsX86().AsRegisterPairLow()); |
| 953 | } else { |
| 954 | DCHECK(right.IsDoubleStackSlot()); |
| 955 | __ cmpl(left.AsRegisterPairLow(), Address(ESP, right.GetStackIndex())); |
| 956 | } |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 957 | __ movl(output, Immediate(0)); |
| 958 | __ j(kEqual, &done); |
| 959 | __ j(kBelow, &less); // Unsigned compare. |
| 960 | |
| 961 | __ Bind(&greater); |
| 962 | __ movl(output, Immediate(1)); |
| 963 | __ jmp(&done); |
| 964 | |
| 965 | __ Bind(&less); |
| 966 | __ movl(output, Immediate(-1)); |
| 967 | |
| 968 | __ Bind(&done); |
| 969 | break; |
| 970 | } |
| 971 | default: |
| 972 | LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType(); |
| 973 | } |
| 974 | } |
| 975 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 976 | void LocationsBuilderX86::VisitPhi(HPhi* instruction) { |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 977 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction); |
| 978 | for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) { |
| 979 | locations->SetInAt(i, Location::Any()); |
| 980 | } |
| 981 | locations->SetOut(Location::Any()); |
| 982 | instruction->SetLocations(locations); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) { |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 986 | LOG(FATAL) << "Unreachable"; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 987 | } |
| 988 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 989 | void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) { |
| 990 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction); |
| 991 | locations->SetInAt(0, Location::RequiresRegister()); |
| 992 | Primitive::Type field_type = instruction->InputAt(1)->GetType(); |
| 993 | if (field_type == Primitive::kPrimBoolean || field_type == Primitive::kPrimByte) { |
| 994 | // Ensure the value is in a byte register. |
| 995 | locations->SetInAt(1, X86CpuLocation(EAX)); |
| 996 | } else { |
| 997 | locations->SetInAt(1, Location::RequiresRegister()); |
| 998 | } |
| 999 | instruction->SetLocations(locations); |
| 1000 | } |
| 1001 | |
| 1002 | void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) { |
| 1003 | LocationSummary* locations = instruction->GetLocations(); |
| 1004 | Register obj = locations->InAt(0).AsX86().AsCpuRegister(); |
| 1005 | uint32_t offset = instruction->GetFieldOffset().Uint32Value(); |
| 1006 | Primitive::Type field_type = instruction->InputAt(1)->GetType(); |
| 1007 | |
| 1008 | switch (field_type) { |
| 1009 | case Primitive::kPrimBoolean: |
| 1010 | case Primitive::kPrimByte: { |
| 1011 | ByteRegister value = locations->InAt(1).AsX86().AsByteRegister(); |
| 1012 | __ movb(Address(obj, offset), value); |
| 1013 | break; |
| 1014 | } |
| 1015 | |
| 1016 | case Primitive::kPrimShort: |
| 1017 | case Primitive::kPrimChar: { |
| 1018 | Register value = locations->InAt(1).AsX86().AsCpuRegister(); |
| 1019 | __ movw(Address(obj, offset), value); |
| 1020 | break; |
| 1021 | } |
| 1022 | |
| 1023 | case Primitive::kPrimInt: |
| 1024 | case Primitive::kPrimNot: { |
| 1025 | Register value = locations->InAt(1).AsX86().AsCpuRegister(); |
| 1026 | __ movl(Address(obj, offset), value); |
| 1027 | break; |
| 1028 | } |
| 1029 | |
| 1030 | case Primitive::kPrimLong: { |
| 1031 | X86ManagedRegister value = locations->InAt(1).AsX86(); |
| 1032 | __ movl(Address(obj, offset), value.AsRegisterPairLow()); |
| 1033 | __ movl(Address(obj, kX86WordSize + offset), value.AsRegisterPairHigh()); |
| 1034 | break; |
| 1035 | } |
| 1036 | |
| 1037 | case Primitive::kPrimFloat: |
| 1038 | case Primitive::kPrimDouble: |
| 1039 | LOG(FATAL) << "Unimplemented register type " << field_type; |
| 1040 | |
| 1041 | case Primitive::kPrimVoid: |
| 1042 | LOG(FATAL) << "Unreachable type " << field_type; |
| 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) { |
| 1047 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction); |
| 1048 | locations->SetInAt(0, Location::RequiresRegister()); |
| 1049 | locations->SetOut(Location::RequiresRegister()); |
| 1050 | instruction->SetLocations(locations); |
| 1051 | } |
| 1052 | |
| 1053 | void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) { |
| 1054 | LocationSummary* locations = instruction->GetLocations(); |
| 1055 | Register obj = locations->InAt(0).AsX86().AsCpuRegister(); |
| 1056 | uint32_t offset = instruction->GetFieldOffset().Uint32Value(); |
| 1057 | |
| 1058 | switch (instruction->GetType()) { |
| 1059 | case Primitive::kPrimBoolean: { |
| 1060 | Register out = locations->Out().AsX86().AsCpuRegister(); |
| 1061 | __ movzxb(out, Address(obj, offset)); |
| 1062 | break; |
| 1063 | } |
| 1064 | |
| 1065 | case Primitive::kPrimByte: { |
| 1066 | Register out = locations->Out().AsX86().AsCpuRegister(); |
| 1067 | __ movsxb(out, Address(obj, offset)); |
| 1068 | break; |
| 1069 | } |
| 1070 | |
| 1071 | case Primitive::kPrimShort: { |
| 1072 | Register out = locations->Out().AsX86().AsCpuRegister(); |
| 1073 | __ movsxw(out, Address(obj, offset)); |
| 1074 | break; |
| 1075 | } |
| 1076 | |
| 1077 | case Primitive::kPrimChar: { |
| 1078 | Register out = locations->Out().AsX86().AsCpuRegister(); |
| 1079 | __ movzxw(out, Address(obj, offset)); |
| 1080 | break; |
| 1081 | } |
| 1082 | |
| 1083 | case Primitive::kPrimInt: |
| 1084 | case Primitive::kPrimNot: { |
| 1085 | Register out = locations->Out().AsX86().AsCpuRegister(); |
| 1086 | __ movl(out, Address(obj, offset)); |
| 1087 | break; |
| 1088 | } |
| 1089 | |
| 1090 | case Primitive::kPrimLong: { |
| 1091 | // TODO: support volatile. |
| 1092 | X86ManagedRegister out = locations->Out().AsX86(); |
| 1093 | __ movl(out.AsRegisterPairLow(), Address(obj, offset)); |
| 1094 | __ movl(out.AsRegisterPairHigh(), Address(obj, kX86WordSize + offset)); |
| 1095 | break; |
| 1096 | } |
| 1097 | |
| 1098 | case Primitive::kPrimFloat: |
| 1099 | case Primitive::kPrimDouble: |
| 1100 | LOG(FATAL) << "Unimplemented register type " << instruction->GetType(); |
| 1101 | |
| 1102 | case Primitive::kPrimVoid: |
| 1103 | LOG(FATAL) << "Unreachable type " << instruction->GetType(); |
| 1104 | } |
| 1105 | } |
| 1106 | |
| 1107 | void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) { |
| 1108 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction); |
| 1109 | locations->SetInAt(0, Location::Any()); |
| 1110 | // TODO: Have a normalization phase that makes this instruction never used. |
| 1111 | locations->SetOut(Location::SameAsFirstInput()); |
| 1112 | instruction->SetLocations(locations); |
| 1113 | } |
| 1114 | |
| 1115 | void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) { |
| 1116 | SlowPathCode* slow_path = |
| 1117 | new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction->GetDexPc()); |
| 1118 | codegen_->AddSlowPath(slow_path); |
| 1119 | |
| 1120 | LocationSummary* locations = instruction->GetLocations(); |
| 1121 | Location obj = locations->InAt(0); |
| 1122 | DCHECK(obj.Equals(locations->Out())); |
| 1123 | |
| 1124 | if (obj.IsRegister()) { |
| 1125 | __ cmpl(obj.AsX86().AsCpuRegister(), Immediate(0)); |
| 1126 | } else { |
| 1127 | DCHECK(locations->InAt(0).IsStackSlot()); |
| 1128 | __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0)); |
| 1129 | } |
| 1130 | __ j(kEqual, slow_path->GetEntryLabel()); |
| 1131 | } |
| 1132 | |
| 1133 | void LocationsBuilderX86::VisitTemporary(HTemporary* temp) { |
| 1134 | temp->SetLocations(nullptr); |
| 1135 | } |
| 1136 | |
| 1137 | void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) { |
| 1138 | // Nothing to do, this is driven by the code generator. |
| 1139 | } |
| 1140 | |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 1141 | void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) { |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1142 | LOG(FATAL) << "Unreachable"; |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) { |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1146 | codegen_->GetMoveResolver()->EmitNativeCode(instruction); |
| 1147 | } |
| 1148 | |
| 1149 | X86Assembler* ParallelMoveResolverX86::GetAssembler() const { |
| 1150 | return codegen_->GetAssembler(); |
| 1151 | } |
| 1152 | |
| 1153 | void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) { |
| 1154 | ScratchRegisterScope ensure_scratch( |
Nicolas Geoffray | e27f31a | 2014-06-12 17:53:14 +0100 | [diff] [blame] | 1155 | this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters()); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1156 | int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0; |
| 1157 | __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset)); |
| 1158 | __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister())); |
| 1159 | } |
| 1160 | |
| 1161 | void ParallelMoveResolverX86::EmitMove(size_t index) { |
| 1162 | MoveOperands* move = moves_.Get(index); |
| 1163 | Location source = move->GetSource(); |
| 1164 | Location destination = move->GetDestination(); |
| 1165 | |
| 1166 | if (source.IsRegister()) { |
| 1167 | if (destination.IsRegister()) { |
| 1168 | __ movl(destination.AsX86().AsCpuRegister(), source.AsX86().AsCpuRegister()); |
| 1169 | } else { |
| 1170 | DCHECK(destination.IsStackSlot()); |
| 1171 | __ movl(Address(ESP, destination.GetStackIndex()), source.AsX86().AsCpuRegister()); |
| 1172 | } |
| 1173 | } else if (source.IsStackSlot()) { |
| 1174 | if (destination.IsRegister()) { |
| 1175 | __ movl(destination.AsX86().AsCpuRegister(), Address(ESP, source.GetStackIndex())); |
| 1176 | } else { |
| 1177 | DCHECK(destination.IsStackSlot()); |
| 1178 | MoveMemoryToMemory(destination.GetStackIndex(), |
| 1179 | source.GetStackIndex()); |
| 1180 | } |
| 1181 | } else { |
| 1182 | LOG(FATAL) << "Unimplemented"; |
| 1183 | } |
| 1184 | } |
| 1185 | |
| 1186 | void ParallelMoveResolverX86::Exchange(Register reg, int mem) { |
Nicolas Geoffray | e27f31a | 2014-06-12 17:53:14 +0100 | [diff] [blame] | 1187 | Register suggested_scratch = reg == EAX ? EBX : EAX; |
| 1188 | ScratchRegisterScope ensure_scratch( |
| 1189 | this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters()); |
| 1190 | |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1191 | int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0; |
| 1192 | __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset)); |
| 1193 | __ movl(Address(ESP, mem + stack_offset), reg); |
| 1194 | __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister())); |
| 1195 | } |
| 1196 | |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1197 | void ParallelMoveResolverX86::Exchange(int mem1, int mem2) { |
| 1198 | ScratchRegisterScope ensure_scratch1( |
Nicolas Geoffray | e27f31a | 2014-06-12 17:53:14 +0100 | [diff] [blame] | 1199 | this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters()); |
| 1200 | |
| 1201 | Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX; |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1202 | ScratchRegisterScope ensure_scratch2( |
Nicolas Geoffray | e27f31a | 2014-06-12 17:53:14 +0100 | [diff] [blame] | 1203 | this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters()); |
| 1204 | |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1205 | int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0; |
| 1206 | stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0; |
| 1207 | __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset)); |
| 1208 | __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset)); |
| 1209 | __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister())); |
| 1210 | __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister())); |
| 1211 | } |
| 1212 | |
| 1213 | void ParallelMoveResolverX86::EmitSwap(size_t index) { |
| 1214 | MoveOperands* move = moves_.Get(index); |
| 1215 | Location source = move->GetSource(); |
| 1216 | Location destination = move->GetDestination(); |
| 1217 | |
| 1218 | if (source.IsRegister() && destination.IsRegister()) { |
| 1219 | __ xchgl(destination.AsX86().AsCpuRegister(), source.AsX86().AsCpuRegister()); |
| 1220 | } else if (source.IsRegister() && destination.IsStackSlot()) { |
| 1221 | Exchange(source.AsX86().AsCpuRegister(), destination.GetStackIndex()); |
| 1222 | } else if (source.IsStackSlot() && destination.IsRegister()) { |
| 1223 | Exchange(destination.AsX86().AsCpuRegister(), source.GetStackIndex()); |
| 1224 | } else if (source.IsStackSlot() && destination.IsStackSlot()) { |
| 1225 | Exchange(destination.GetStackIndex(), source.GetStackIndex()); |
| 1226 | } else { |
| 1227 | LOG(FATAL) << "Unimplemented"; |
| 1228 | } |
| 1229 | } |
| 1230 | |
| 1231 | void ParallelMoveResolverX86::SpillScratch(int reg) { |
| 1232 | __ pushl(static_cast<Register>(reg)); |
| 1233 | } |
| 1234 | |
| 1235 | void ParallelMoveResolverX86::RestoreScratch(int reg) { |
| 1236 | __ popl(static_cast<Register>(reg)); |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 1237 | } |
| 1238 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 1239 | } // namespace x86 |
| 1240 | } // namespace art |