1 //===------ PPCDisassembler.cpp - Disassembler for PowerPC ------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "PPC.h" 11 #include "llvm/MC/MCDisassembler.h" 12 #include "llvm/MC/MCFixedLenDisassembler.h" 13 #include "llvm/MC/MCInst.h" 14 #include "llvm/MC/MCSubtargetInfo.h" 15 #include "llvm/Support/TargetRegistry.h" 16 17 using namespace llvm; 18 19 #define DEBUG_TYPE "ppc-disassembler" 20 21 typedef MCDisassembler::DecodeStatus DecodeStatus; 22 23 namespace { 24 class PPCDisassembler : public MCDisassembler { 25 public: 26 PPCDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx) 27 : MCDisassembler(STI, Ctx) {} 28 ~PPCDisassembler() override {} 29 30 DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size, 31 ArrayRef<uint8_t> Bytes, uint64_t Address, 32 raw_ostream &VStream, 33 raw_ostream &CStream) const override; 34 }; 35 } // end anonymous namespace 36 37 static MCDisassembler *createPPCDisassembler(const Target &T, 38 const MCSubtargetInfo &STI, 39 MCContext &Ctx) { 40 return new PPCDisassembler(STI, Ctx); 41 } 42 43 extern "C" void LLVMInitializePowerPCDisassembler() { 44 // Register the disassembler for each target. 45 TargetRegistry::RegisterMCDisassembler(ThePPC32Target, 46 createPPCDisassembler); 47 TargetRegistry::RegisterMCDisassembler(ThePPC64Target, 48 createPPCDisassembler); 49 TargetRegistry::RegisterMCDisassembler(ThePPC64LETarget, 50 createPPCDisassembler); 51 } 52 53 // FIXME: These can be generated by TableGen from the existing register 54 // encoding values! 55 56 static const unsigned CRRegs[] = { 57 PPC::CR0, PPC::CR1, PPC::CR2, PPC::CR3, 58 PPC::CR4, PPC::CR5, PPC::CR6, PPC::CR7 59 }; 60 61 static const unsigned CRBITRegs[] = { 62 PPC::CR0LT, PPC::CR0GT, PPC::CR0EQ, PPC::CR0UN, 63 PPC::CR1LT, PPC::CR1GT, PPC::CR1EQ, PPC::CR1UN, 64 PPC::CR2LT, PPC::CR2GT, PPC::CR2EQ, PPC::CR2UN, 65 PPC::CR3LT, PPC::CR3GT, PPC::CR3EQ, PPC::CR3UN, 66 PPC::CR4LT, PPC::CR4GT, PPC::CR4EQ, PPC::CR4UN, 67 PPC::CR5LT, PPC::CR5GT, PPC::CR5EQ, PPC::CR5UN, 68 PPC::CR6LT, PPC::CR6GT, PPC::CR6EQ, PPC::CR6UN, 69 PPC::CR7LT, PPC::CR7GT, PPC::CR7EQ, PPC::CR7UN 70 }; 71 72 static const unsigned FRegs[] = { 73 PPC::F0, PPC::F1, PPC::F2, PPC::F3, 74 PPC::F4, PPC::F5, PPC::F6, PPC::F7, 75 PPC::F8, PPC::F9, PPC::F10, PPC::F11, 76 PPC::F12, PPC::F13, PPC::F14, PPC::F15, 77 PPC::F16, PPC::F17, PPC::F18, PPC::F19, 78 PPC::F20, PPC::F21, PPC::F22, PPC::F23, 79 PPC::F24, PPC::F25, PPC::F26, PPC::F27, 80 PPC::F28, PPC::F29, PPC::F30, PPC::F31 81 }; 82 83 static const unsigned VRegs[] = { 84 PPC::V0, PPC::V1, PPC::V2, PPC::V3, 85 PPC::V4, PPC::V5, PPC::V6, PPC::V7, 86 PPC::V8, PPC::V9, PPC::V10, PPC::V11, 87 PPC::V12, PPC::V13, PPC::V14, PPC::V15, 88 PPC::V16, PPC::V17, PPC::V18, PPC::V19, 89 PPC::V20, PPC::V21, PPC::V22, PPC::V23, 90 PPC::V24, PPC::V25, PPC::V26, PPC::V27, 91 PPC::V28, PPC::V29, PPC::V30, PPC::V31 92 }; 93 94 static const unsigned VSRegs[] = { 95 PPC::VSL0, PPC::VSL1, PPC::VSL2, PPC::VSL3, 96 PPC::VSL4, PPC::VSL5, PPC::VSL6, PPC::VSL7, 97 PPC::VSL8, PPC::VSL9, PPC::VSL10, PPC::VSL11, 98 PPC::VSL12, PPC::VSL13, PPC::VSL14, PPC::VSL15, 99 PPC::VSL16, PPC::VSL17, PPC::VSL18, PPC::VSL19, 100 PPC::VSL20, PPC::VSL21, PPC::VSL22, PPC::VSL23, 101 PPC::VSL24, PPC::VSL25, PPC::VSL26, PPC::VSL27, 102 PPC::VSL28, PPC::VSL29, PPC::VSL30, PPC::VSL31, 103 104 PPC::VSH0, PPC::VSH1, PPC::VSH2, PPC::VSH3, 105 PPC::VSH4, PPC::VSH5, PPC::VSH6, PPC::VSH7, 106 PPC::VSH8, PPC::VSH9, PPC::VSH10, PPC::VSH11, 107 PPC::VSH12, PPC::VSH13, PPC::VSH14, PPC::VSH15, 108 PPC::VSH16, PPC::VSH17, PPC::VSH18, PPC::VSH19, 109 PPC::VSH20, PPC::VSH21, PPC::VSH22, PPC::VSH23, 110 PPC::VSH24, PPC::VSH25, PPC::VSH26, PPC::VSH27, 111 PPC::VSH28, PPC::VSH29, PPC::VSH30, PPC::VSH31 112 }; 113 114 static const unsigned VSFRegs[] = { 115 PPC::F0, PPC::F1, PPC::F2, PPC::F3, 116 PPC::F4, PPC::F5, PPC::F6, PPC::F7, 117 PPC::F8, PPC::F9, PPC::F10, PPC::F11, 118 PPC::F12, PPC::F13, PPC::F14, PPC::F15, 119 PPC::F16, PPC::F17, PPC::F18, PPC::F19, 120 PPC::F20, PPC::F21, PPC::F22, PPC::F23, 121 PPC::F24, PPC::F25, PPC::F26, PPC::F27, 122 PPC::F28, PPC::F29, PPC::F30, PPC::F31, 123 124 PPC::VF0, PPC::VF1, PPC::VF2, PPC::VF3, 125 PPC::VF4, PPC::VF5, PPC::VF6, PPC::VF7, 126 PPC::VF8, PPC::VF9, PPC::VF10, PPC::VF11, 127 PPC::VF12, PPC::VF13, PPC::VF14, PPC::VF15, 128 PPC::VF16, PPC::VF17, PPC::VF18, PPC::VF19, 129 PPC::VF20, PPC::VF21, PPC::VF22, PPC::VF23, 130 PPC::VF24, PPC::VF25, PPC::VF26, PPC::VF27, 131 PPC::VF28, PPC::VF29, PPC::VF30, PPC::VF31 132 }; 133 134 static const unsigned VSSRegs[] = { 135 PPC::F0, PPC::F1, PPC::F2, PPC::F3, 136 PPC::F4, PPC::F5, PPC::F6, PPC::F7, 137 PPC::F8, PPC::F9, PPC::F10, PPC::F11, 138 PPC::F12, PPC::F13, PPC::F14, PPC::F15, 139 PPC::F16, PPC::F17, PPC::F18, PPC::F19, 140 PPC::F20, PPC::F21, PPC::F22, PPC::F23, 141 PPC::F24, PPC::F25, PPC::F26, PPC::F27, 142 PPC::F28, PPC::F29, PPC::F30, PPC::F31, 143 144 PPC::VF0, PPC::VF1, PPC::VF2, PPC::VF3, 145 PPC::VF4, PPC::VF5, PPC::VF6, PPC::VF7, 146 PPC::VF8, PPC::VF9, PPC::VF10, PPC::VF11, 147 PPC::VF12, PPC::VF13, PPC::VF14, PPC::VF15, 148 PPC::VF16, PPC::VF17, PPC::VF18, PPC::VF19, 149 PPC::VF20, PPC::VF21, PPC::VF22, PPC::VF23, 150 PPC::VF24, PPC::VF25, PPC::VF26, PPC::VF27, 151 PPC::VF28, PPC::VF29, PPC::VF30, PPC::VF31 152 }; 153 154 static const unsigned GPRegs[] = { 155 PPC::R0, PPC::R1, PPC::R2, PPC::R3, 156 PPC::R4, PPC::R5, PPC::R6, PPC::R7, 157 PPC::R8, PPC::R9, PPC::R10, PPC::R11, 158 PPC::R12, PPC::R13, PPC::R14, PPC::R15, 159 PPC::R16, PPC::R17, PPC::R18, PPC::R19, 160 PPC::R20, PPC::R21, PPC::R22, PPC::R23, 161 PPC::R24, PPC::R25, PPC::R26, PPC::R27, 162 PPC::R28, PPC::R29, PPC::R30, PPC::R31 163 }; 164 165 static const unsigned GP0Regs[] = { 166 PPC::ZERO, PPC::R1, PPC::R2, PPC::R3, 167 PPC::R4, PPC::R5, PPC::R6, PPC::R7, 168 PPC::R8, PPC::R9, PPC::R10, PPC::R11, 169 PPC::R12, PPC::R13, PPC::R14, PPC::R15, 170 PPC::R16, PPC::R17, PPC::R18, PPC::R19, 171 PPC::R20, PPC::R21, PPC::R22, PPC::R23, 172 PPC::R24, PPC::R25, PPC::R26, PPC::R27, 173 PPC::R28, PPC::R29, PPC::R30, PPC::R31 174 }; 175 176 static const unsigned G8Regs[] = { 177 PPC::X0, PPC::X1, PPC::X2, PPC::X3, 178 PPC::X4, PPC::X5, PPC::X6, PPC::X7, 179 PPC::X8, PPC::X9, PPC::X10, PPC::X11, 180 PPC::X12, PPC::X13, PPC::X14, PPC::X15, 181 PPC::X16, PPC::X17, PPC::X18, PPC::X19, 182 PPC::X20, PPC::X21, PPC::X22, PPC::X23, 183 PPC::X24, PPC::X25, PPC::X26, PPC::X27, 184 PPC::X28, PPC::X29, PPC::X30, PPC::X31 185 }; 186 187 static const unsigned QFRegs[] = { 188 PPC::QF0, PPC::QF1, PPC::QF2, PPC::QF3, 189 PPC::QF4, PPC::QF5, PPC::QF6, PPC::QF7, 190 PPC::QF8, PPC::QF9, PPC::QF10, PPC::QF11, 191 PPC::QF12, PPC::QF13, PPC::QF14, PPC::QF15, 192 PPC::QF16, PPC::QF17, PPC::QF18, PPC::QF19, 193 PPC::QF20, PPC::QF21, PPC::QF22, PPC::QF23, 194 PPC::QF24, PPC::QF25, PPC::QF26, PPC::QF27, 195 PPC::QF28, PPC::QF29, PPC::QF30, PPC::QF31 196 }; 197 198 template <std::size_t N> 199 static DecodeStatus decodeRegisterClass(MCInst &Inst, uint64_t RegNo, 200 const unsigned (&Regs)[N]) { 201 assert(RegNo < N && "Invalid register number"); 202 Inst.addOperand(MCOperand::CreateReg(Regs[RegNo])); 203 return MCDisassembler::Success; 204 } 205 206 static DecodeStatus DecodeCRRCRegisterClass(MCInst &Inst, uint64_t RegNo, 207 uint64_t Address, 208 const void *Decoder) { 209 return decodeRegisterClass(Inst, RegNo, CRRegs); 210 } 211 212 static DecodeStatus DecodeCRRC0RegisterClass(MCInst &Inst, uint64_t RegNo, 213 uint64_t Address, 214 const void *Decoder) { 215 return decodeRegisterClass(Inst, RegNo, CRRegs); 216 } 217 218 static DecodeStatus DecodeCRBITRCRegisterClass(MCInst &Inst, uint64_t RegNo, 219 uint64_t Address, 220 const void *Decoder) { 221 return decodeRegisterClass(Inst, RegNo, CRBITRegs); 222 } 223 224 static DecodeStatus DecodeF4RCRegisterClass(MCInst &Inst, uint64_t RegNo, 225 uint64_t Address, 226 const void *Decoder) { 227 return decodeRegisterClass(Inst, RegNo, FRegs); 228 } 229 230 static DecodeStatus DecodeF8RCRegisterClass(MCInst &Inst, uint64_t RegNo, 231 uint64_t Address, 232 const void *Decoder) { 233 return decodeRegisterClass(Inst, RegNo, FRegs); 234 } 235 236 static DecodeStatus DecodeVRRCRegisterClass(MCInst &Inst, uint64_t RegNo, 237 uint64_t Address, 238 const void *Decoder) { 239 return decodeRegisterClass(Inst, RegNo, VRegs); 240 } 241 242 static DecodeStatus DecodeVSRCRegisterClass(MCInst &Inst, uint64_t RegNo, 243 uint64_t Address, 244 const void *Decoder) { 245 return decodeRegisterClass(Inst, RegNo, VSRegs); 246 } 247 248 static DecodeStatus DecodeVSFRCRegisterClass(MCInst &Inst, uint64_t RegNo, 249 uint64_t Address, 250 const void *Decoder) { 251 return decodeRegisterClass(Inst, RegNo, VSFRegs); 252 } 253 254 static DecodeStatus DecodeVSSRCRegisterClass(MCInst &Inst, uint64_t RegNo, 255 uint64_t Address, 256 const void *Decoder) { 257 return decodeRegisterClass(Inst, RegNo, VSSRegs); 258 } 259 260 static DecodeStatus DecodeGPRCRegisterClass(MCInst &Inst, uint64_t RegNo, 261 uint64_t Address, 262 const void *Decoder) { 263 return decodeRegisterClass(Inst, RegNo, GPRegs); 264 } 265 266 static DecodeStatus DecodeGPRC_NOR0RegisterClass(MCInst &Inst, uint64_t RegNo, 267 uint64_t Address, 268 const void *Decoder) { 269 return decodeRegisterClass(Inst, RegNo, GP0Regs); 270 } 271 272 static DecodeStatus DecodeG8RCRegisterClass(MCInst &Inst, uint64_t RegNo, 273 uint64_t Address, 274 const void *Decoder) { 275 return decodeRegisterClass(Inst, RegNo, G8Regs); 276 } 277 278 #define DecodePointerLikeRegClass0 DecodeGPRCRegisterClass 279 #define DecodePointerLikeRegClass1 DecodeGPRC_NOR0RegisterClass 280 281 static DecodeStatus DecodeQFRCRegisterClass(MCInst &Inst, uint64_t RegNo, 282 uint64_t Address, 283 const void *Decoder) { 284 return decodeRegisterClass(Inst, RegNo, QFRegs); 285 } 286 287 #define DecodeQSRCRegisterClass DecodeQFRCRegisterClass 288 #define DecodeQBRCRegisterClass DecodeQFRCRegisterClass 289 290 template<unsigned N> 291 static DecodeStatus decodeUImmOperand(MCInst &Inst, uint64_t Imm, 292 int64_t Address, const void *Decoder) { 293 assert(isUInt<N>(Imm) && "Invalid immediate"); 294 Inst.addOperand(MCOperand::CreateImm(Imm)); 295 return MCDisassembler::Success; 296 } 297 298 template<unsigned N> 299 static DecodeStatus decodeSImmOperand(MCInst &Inst, uint64_t Imm, 300 int64_t Address, const void *Decoder) { 301 assert(isUInt<N>(Imm) && "Invalid immediate"); 302 Inst.addOperand(MCOperand::CreateImm(SignExtend64<N>(Imm))); 303 return MCDisassembler::Success; 304 } 305 306 static DecodeStatus decodeMemRIOperands(MCInst &Inst, uint64_t Imm, 307 int64_t Address, const void *Decoder) { 308 // Decode the memri field (imm, reg), which has the low 16-bits as the 309 // displacement and the next 5 bits as the register #. 310 311 uint64_t Base = Imm >> 16; 312 uint64_t Disp = Imm & 0xFFFF; 313 314 assert(Base < 32 && "Invalid base register"); 315 316 switch (Inst.getOpcode()) { 317 default: break; 318 case PPC::LBZU: 319 case PPC::LHAU: 320 case PPC::LHZU: 321 case PPC::LWZU: 322 case PPC::LFSU: 323 case PPC::LFDU: 324 // Add the tied output operand. 325 Inst.addOperand(MCOperand::CreateReg(GP0Regs[Base])); 326 break; 327 case PPC::STBU: 328 case PPC::STHU: 329 case PPC::STWU: 330 case PPC::STFSU: 331 case PPC::STFDU: 332 Inst.insert(Inst.begin(), MCOperand::CreateReg(GP0Regs[Base])); 333 break; 334 } 335 336 Inst.addOperand(MCOperand::CreateImm(SignExtend64<16>(Disp))); 337 Inst.addOperand(MCOperand::CreateReg(GP0Regs[Base])); 338 return MCDisassembler::Success; 339 } 340 341 static DecodeStatus decodeMemRIXOperands(MCInst &Inst, uint64_t Imm, 342 int64_t Address, const void *Decoder) { 343 // Decode the memrix field (imm, reg), which has the low 14-bits as the 344 // displacement and the next 5 bits as the register #. 345 346 uint64_t Base = Imm >> 14; 347 uint64_t Disp = Imm & 0x3FFF; 348 349 assert(Base < 32 && "Invalid base register"); 350 351 if (Inst.getOpcode() == PPC::LDU) 352 // Add the tied output operand. 353 Inst.addOperand(MCOperand::CreateReg(GP0Regs[Base])); 354 else if (Inst.getOpcode() == PPC::STDU) 355 Inst.insert(Inst.begin(), MCOperand::CreateReg(GP0Regs[Base])); 356 357 Inst.addOperand(MCOperand::CreateImm(SignExtend64<16>(Disp << 2))); 358 Inst.addOperand(MCOperand::CreateReg(GP0Regs[Base])); 359 return MCDisassembler::Success; 360 } 361 362 static DecodeStatus decodeCRBitMOperand(MCInst &Inst, uint64_t Imm, 363 int64_t Address, const void *Decoder) { 364 // The cr bit encoding is 0x80 >> cr_reg_num. 365 366 unsigned Zeros = countTrailingZeros(Imm); 367 assert(Zeros < 8 && "Invalid CR bit value"); 368 369 Inst.addOperand(MCOperand::CreateReg(CRRegs[7 - Zeros])); 370 return MCDisassembler::Success; 371 } 372 373 #include "PPCGenDisassemblerTables.inc" 374 375 DecodeStatus PPCDisassembler::getInstruction(MCInst &MI, uint64_t &Size, 376 ArrayRef<uint8_t> Bytes, 377 uint64_t Address, raw_ostream &OS, 378 raw_ostream &CS) const { 379 // Get the four bytes of the instruction. 380 Size = 4; 381 if (Bytes.size() < 4) { 382 Size = 0; 383 return MCDisassembler::Fail; 384 } 385 386 // The instruction is big-endian encoded. 387 uint32_t Inst = 388 (Bytes[0] << 24) | (Bytes[1] << 16) | (Bytes[2] << 8) | (Bytes[3] << 0); 389 390 if (STI.getFeatureBits()[PPC::FeatureQPX]) { 391 DecodeStatus result = 392 decodeInstruction(DecoderTableQPX32, MI, Inst, Address, this, STI); 393 if (result != MCDisassembler::Fail) 394 return result; 395 396 MI.clear(); 397 } 398 399 return decodeInstruction(DecoderTable32, MI, Inst, Address, this, STI); 400 } 401 402