PPCDisassembler.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. //===------ PPCDisassembler.cpp - Disassembler for PowerPC ------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "MCTargetDesc/PPCMCTargetDesc.h"
  9. #include "TargetInfo/PowerPCTargetInfo.h"
  10. #include "llvm/MC/MCDisassembler/MCDisassembler.h"
  11. #include "llvm/MC/MCFixedLenDisassembler.h"
  12. #include "llvm/MC/MCInst.h"
  13. #include "llvm/MC/MCSubtargetInfo.h"
  14. #include "llvm/MC/TargetRegistry.h"
  15. #include "llvm/Support/Endian.h"
  16. using namespace llvm;
  17. DEFINE_PPC_REGCLASSES;
  18. #define DEBUG_TYPE "ppc-disassembler"
  19. typedef MCDisassembler::DecodeStatus DecodeStatus;
  20. namespace {
  21. class PPCDisassembler : public MCDisassembler {
  22. bool IsLittleEndian;
  23. public:
  24. PPCDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx,
  25. bool IsLittleEndian)
  26. : MCDisassembler(STI, Ctx), IsLittleEndian(IsLittleEndian) {}
  27. DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
  28. ArrayRef<uint8_t> Bytes, uint64_t Address,
  29. raw_ostream &CStream) const override;
  30. };
  31. } // end anonymous namespace
  32. static MCDisassembler *createPPCDisassembler(const Target &T,
  33. const MCSubtargetInfo &STI,
  34. MCContext &Ctx) {
  35. return new PPCDisassembler(STI, Ctx, /*IsLittleEndian=*/false);
  36. }
  37. static MCDisassembler *createPPCLEDisassembler(const Target &T,
  38. const MCSubtargetInfo &STI,
  39. MCContext &Ctx) {
  40. return new PPCDisassembler(STI, Ctx, /*IsLittleEndian=*/true);
  41. }
  42. extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializePowerPCDisassembler() {
  43. // Register the disassembler for each target.
  44. TargetRegistry::RegisterMCDisassembler(getThePPC32Target(),
  45. createPPCDisassembler);
  46. TargetRegistry::RegisterMCDisassembler(getThePPC32LETarget(),
  47. createPPCLEDisassembler);
  48. TargetRegistry::RegisterMCDisassembler(getThePPC64Target(),
  49. createPPCDisassembler);
  50. TargetRegistry::RegisterMCDisassembler(getThePPC64LETarget(),
  51. createPPCLEDisassembler);
  52. }
  53. static DecodeStatus decodeCondBrTarget(MCInst &Inst, unsigned Imm,
  54. uint64_t /*Address*/,
  55. const void * /*Decoder*/) {
  56. Inst.addOperand(MCOperand::createImm(SignExtend32<14>(Imm)));
  57. return MCDisassembler::Success;
  58. }
  59. static DecodeStatus decodeDirectBrTarget(MCInst &Inst, unsigned Imm,
  60. uint64_t /*Address*/,
  61. const void * /*Decoder*/) {
  62. int32_t Offset = SignExtend32<24>(Imm);
  63. Inst.addOperand(MCOperand::createImm(Offset));
  64. return MCDisassembler::Success;
  65. }
  66. // FIXME: These can be generated by TableGen from the existing register
  67. // encoding values!
  68. template <std::size_t N>
  69. static DecodeStatus decodeRegisterClass(MCInst &Inst, uint64_t RegNo,
  70. const MCPhysReg (&Regs)[N]) {
  71. assert(RegNo < N && "Invalid register number");
  72. Inst.addOperand(MCOperand::createReg(Regs[RegNo]));
  73. return MCDisassembler::Success;
  74. }
  75. static DecodeStatus DecodeCRRCRegisterClass(MCInst &Inst, uint64_t RegNo,
  76. uint64_t Address,
  77. const void *Decoder) {
  78. return decodeRegisterClass(Inst, RegNo, CRRegs);
  79. }
  80. static DecodeStatus DecodeCRBITRCRegisterClass(MCInst &Inst, uint64_t RegNo,
  81. uint64_t Address,
  82. const void *Decoder) {
  83. return decodeRegisterClass(Inst, RegNo, CRBITRegs);
  84. }
  85. static DecodeStatus DecodeF4RCRegisterClass(MCInst &Inst, uint64_t RegNo,
  86. uint64_t Address,
  87. const void *Decoder) {
  88. return decodeRegisterClass(Inst, RegNo, FRegs);
  89. }
  90. static DecodeStatus DecodeF8RCRegisterClass(MCInst &Inst, uint64_t RegNo,
  91. uint64_t Address,
  92. const void *Decoder) {
  93. return decodeRegisterClass(Inst, RegNo, FRegs);
  94. }
  95. static DecodeStatus DecodeVFRCRegisterClass(MCInst &Inst, uint64_t RegNo,
  96. uint64_t Address,
  97. const void *Decoder) {
  98. return decodeRegisterClass(Inst, RegNo, VFRegs);
  99. }
  100. static DecodeStatus DecodeVRRCRegisterClass(MCInst &Inst, uint64_t RegNo,
  101. uint64_t Address,
  102. const void *Decoder) {
  103. return decodeRegisterClass(Inst, RegNo, VRegs);
  104. }
  105. static DecodeStatus DecodeVSRCRegisterClass(MCInst &Inst, uint64_t RegNo,
  106. uint64_t Address,
  107. const void *Decoder) {
  108. return decodeRegisterClass(Inst, RegNo, VSRegs);
  109. }
  110. static DecodeStatus DecodeVSFRCRegisterClass(MCInst &Inst, uint64_t RegNo,
  111. uint64_t Address,
  112. const void *Decoder) {
  113. return decodeRegisterClass(Inst, RegNo, VSFRegs);
  114. }
  115. static DecodeStatus DecodeVSSRCRegisterClass(MCInst &Inst, uint64_t RegNo,
  116. uint64_t Address,
  117. const void *Decoder) {
  118. return decodeRegisterClass(Inst, RegNo, VSSRegs);
  119. }
  120. static DecodeStatus DecodeGPRCRegisterClass(MCInst &Inst, uint64_t RegNo,
  121. uint64_t Address,
  122. const void *Decoder) {
  123. return decodeRegisterClass(Inst, RegNo, RRegs);
  124. }
  125. static DecodeStatus DecodeGPRC_NOR0RegisterClass(MCInst &Inst, uint64_t RegNo,
  126. uint64_t Address,
  127. const void *Decoder) {
  128. return decodeRegisterClass(Inst, RegNo, RRegsNoR0);
  129. }
  130. static DecodeStatus DecodeG8RCRegisterClass(MCInst &Inst, uint64_t RegNo,
  131. uint64_t Address,
  132. const void *Decoder) {
  133. return decodeRegisterClass(Inst, RegNo, XRegs);
  134. }
  135. static DecodeStatus DecodeG8pRCRegisterClass(MCInst &Inst, uint64_t RegNo,
  136. uint64_t Address,
  137. const void *Decoder) {
  138. return decodeRegisterClass(Inst, RegNo, XRegs);
  139. }
  140. static DecodeStatus DecodeG8RC_NOX0RegisterClass(MCInst &Inst, uint64_t RegNo,
  141. uint64_t Address,
  142. const void *Decoder) {
  143. return decodeRegisterClass(Inst, RegNo, XRegsNoX0);
  144. }
  145. #define DecodePointerLikeRegClass0 DecodeGPRCRegisterClass
  146. #define DecodePointerLikeRegClass1 DecodeGPRC_NOR0RegisterClass
  147. static DecodeStatus DecodeSPERCRegisterClass(MCInst &Inst, uint64_t RegNo,
  148. uint64_t Address,
  149. const void *Decoder) {
  150. return decodeRegisterClass(Inst, RegNo, SPERegs);
  151. }
  152. static DecodeStatus DecodeACCRCRegisterClass(MCInst &Inst, uint64_t RegNo,
  153. uint64_t Address,
  154. const void *Decoder) {
  155. return decodeRegisterClass(Inst, RegNo, ACCRegs);
  156. }
  157. static DecodeStatus DecodeVSRpRCRegisterClass(MCInst &Inst, uint64_t RegNo,
  158. uint64_t Address,
  159. const void *Decoder) {
  160. return decodeRegisterClass(Inst, RegNo, VSRpRegs);
  161. }
  162. #define DecodeQSRCRegisterClass DecodeQFRCRegisterClass
  163. #define DecodeQBRCRegisterClass DecodeQFRCRegisterClass
  164. template<unsigned N>
  165. static DecodeStatus decodeUImmOperand(MCInst &Inst, uint64_t Imm,
  166. int64_t Address, const void *Decoder) {
  167. assert(isUInt<N>(Imm) && "Invalid immediate");
  168. Inst.addOperand(MCOperand::createImm(Imm));
  169. return MCDisassembler::Success;
  170. }
  171. template<unsigned N>
  172. static DecodeStatus decodeSImmOperand(MCInst &Inst, uint64_t Imm,
  173. int64_t Address, const void *Decoder) {
  174. assert(isUInt<N>(Imm) && "Invalid immediate");
  175. Inst.addOperand(MCOperand::createImm(SignExtend64<N>(Imm)));
  176. return MCDisassembler::Success;
  177. }
  178. static DecodeStatus decodeImmZeroOperand(MCInst &Inst, uint64_t Imm,
  179. int64_t Address, const void *Decoder) {
  180. if (Imm != 0)
  181. return MCDisassembler::Fail;
  182. Inst.addOperand(MCOperand::createImm(Imm));
  183. return MCDisassembler::Success;
  184. }
  185. static DecodeStatus decodeVSRpEvenOperands(MCInst &Inst, uint64_t RegNo,
  186. uint64_t Address,
  187. const void *Decoder) {
  188. if (RegNo & 1)
  189. return MCDisassembler::Fail;
  190. Inst.addOperand(MCOperand::createReg(VSRpRegs[RegNo >> 1]));
  191. return MCDisassembler::Success;
  192. }
  193. static DecodeStatus decodeMemRIOperands(MCInst &Inst, uint64_t Imm,
  194. int64_t Address, const void *Decoder) {
  195. // Decode the memri field (imm, reg), which has the low 16-bits as the
  196. // displacement and the next 5 bits as the register #.
  197. uint64_t Base = Imm >> 16;
  198. uint64_t Disp = Imm & 0xFFFF;
  199. assert(Base < 32 && "Invalid base register");
  200. switch (Inst.getOpcode()) {
  201. default: break;
  202. case PPC::LBZU:
  203. case PPC::LHAU:
  204. case PPC::LHZU:
  205. case PPC::LWZU:
  206. case PPC::LFSU:
  207. case PPC::LFDU:
  208. // Add the tied output operand.
  209. Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base]));
  210. break;
  211. case PPC::STBU:
  212. case PPC::STHU:
  213. case PPC::STWU:
  214. case PPC::STFSU:
  215. case PPC::STFDU:
  216. Inst.insert(Inst.begin(), MCOperand::createReg(RRegsNoR0[Base]));
  217. break;
  218. }
  219. Inst.addOperand(MCOperand::createImm(SignExtend64<16>(Disp)));
  220. Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base]));
  221. return MCDisassembler::Success;
  222. }
  223. static DecodeStatus decodeMemRIXOperands(MCInst &Inst, uint64_t Imm,
  224. int64_t Address, const void *Decoder) {
  225. // Decode the memrix field (imm, reg), which has the low 14-bits as the
  226. // displacement and the next 5 bits as the register #.
  227. uint64_t Base = Imm >> 14;
  228. uint64_t Disp = Imm & 0x3FFF;
  229. assert(Base < 32 && "Invalid base register");
  230. if (Inst.getOpcode() == PPC::LDU)
  231. // Add the tied output operand.
  232. Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base]));
  233. else if (Inst.getOpcode() == PPC::STDU)
  234. Inst.insert(Inst.begin(), MCOperand::createReg(RRegsNoR0[Base]));
  235. Inst.addOperand(MCOperand::createImm(SignExtend64<16>(Disp << 2)));
  236. Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base]));
  237. return MCDisassembler::Success;
  238. }
  239. static DecodeStatus decodeMemRIHashOperands(MCInst &Inst, uint64_t Imm,
  240. int64_t Address,
  241. const void *Decoder) {
  242. // Decode the memrix field for a hash store or hash check operation.
  243. // The field is composed of a register and an immediate value that is 6 bits
  244. // and covers the range -8 to -512. The immediate is always negative and 2s
  245. // complement which is why we sign extend a 7 bit value.
  246. const uint64_t Base = Imm >> 6;
  247. const int64_t Disp = SignExtend64<7>((Imm & 0x3F) + 64) * 8;
  248. assert(Base < 32 && "Invalid base register");
  249. Inst.addOperand(MCOperand::createImm(Disp));
  250. Inst.addOperand(MCOperand::createReg(RRegs[Base]));
  251. return MCDisassembler::Success;
  252. }
  253. static DecodeStatus decodeMemRIX16Operands(MCInst &Inst, uint64_t Imm,
  254. int64_t Address, const void *Decoder) {
  255. // Decode the memrix16 field (imm, reg), which has the low 12-bits as the
  256. // displacement with 16-byte aligned, and the next 5 bits as the register #.
  257. uint64_t Base = Imm >> 12;
  258. uint64_t Disp = Imm & 0xFFF;
  259. assert(Base < 32 && "Invalid base register");
  260. Inst.addOperand(MCOperand::createImm(SignExtend64<16>(Disp << 4)));
  261. Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base]));
  262. return MCDisassembler::Success;
  263. }
  264. static DecodeStatus decodeMemRI34PCRelOperands(MCInst &Inst, uint64_t Imm,
  265. int64_t Address,
  266. const void *Decoder) {
  267. // Decode the memri34_pcrel field (imm, reg), which has the low 34-bits as the
  268. // displacement, and the next 5 bits as an immediate 0.
  269. uint64_t Base = Imm >> 34;
  270. uint64_t Disp = Imm & 0x3FFFFFFFFUL;
  271. assert(Base < 32 && "Invalid base register");
  272. Inst.addOperand(MCOperand::createImm(SignExtend64<34>(Disp)));
  273. return decodeImmZeroOperand(Inst, Base, Address, Decoder);
  274. }
  275. static DecodeStatus decodeMemRI34Operands(MCInst &Inst, uint64_t Imm,
  276. int64_t Address,
  277. const void *Decoder) {
  278. // Decode the memri34 field (imm, reg), which has the low 34-bits as the
  279. // displacement, and the next 5 bits as the register #.
  280. uint64_t Base = Imm >> 34;
  281. uint64_t Disp = Imm & 0x3FFFFFFFFUL;
  282. assert(Base < 32 && "Invalid base register");
  283. Inst.addOperand(MCOperand::createImm(SignExtend64<34>(Disp)));
  284. Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base]));
  285. return MCDisassembler::Success;
  286. }
  287. static DecodeStatus decodeSPE8Operands(MCInst &Inst, uint64_t Imm,
  288. int64_t Address, const void *Decoder) {
  289. // Decode the spe8disp field (imm, reg), which has the low 5-bits as the
  290. // displacement with 8-byte aligned, and the next 5 bits as the register #.
  291. uint64_t Base = Imm >> 5;
  292. uint64_t Disp = Imm & 0x1F;
  293. assert(Base < 32 && "Invalid base register");
  294. Inst.addOperand(MCOperand::createImm(Disp << 3));
  295. Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base]));
  296. return MCDisassembler::Success;
  297. }
  298. static DecodeStatus decodeSPE4Operands(MCInst &Inst, uint64_t Imm,
  299. int64_t Address, const void *Decoder) {
  300. // Decode the spe4disp field (imm, reg), which has the low 5-bits as the
  301. // displacement with 4-byte aligned, and the next 5 bits as the register #.
  302. uint64_t Base = Imm >> 5;
  303. uint64_t Disp = Imm & 0x1F;
  304. assert(Base < 32 && "Invalid base register");
  305. Inst.addOperand(MCOperand::createImm(Disp << 2));
  306. Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base]));
  307. return MCDisassembler::Success;
  308. }
  309. static DecodeStatus decodeSPE2Operands(MCInst &Inst, uint64_t Imm,
  310. int64_t Address, const void *Decoder) {
  311. // Decode the spe2disp field (imm, reg), which has the low 5-bits as the
  312. // displacement with 2-byte aligned, and the next 5 bits as the register #.
  313. uint64_t Base = Imm >> 5;
  314. uint64_t Disp = Imm & 0x1F;
  315. assert(Base < 32 && "Invalid base register");
  316. Inst.addOperand(MCOperand::createImm(Disp << 1));
  317. Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base]));
  318. return MCDisassembler::Success;
  319. }
  320. static DecodeStatus decodeCRBitMOperand(MCInst &Inst, uint64_t Imm,
  321. int64_t Address, const void *Decoder) {
  322. // The cr bit encoding is 0x80 >> cr_reg_num.
  323. unsigned Zeros = countTrailingZeros(Imm);
  324. assert(Zeros < 8 && "Invalid CR bit value");
  325. Inst.addOperand(MCOperand::createReg(CRRegs[7 - Zeros]));
  326. return MCDisassembler::Success;
  327. }
  328. #include "PPCGenDisassemblerTables.inc"
  329. DecodeStatus PPCDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
  330. ArrayRef<uint8_t> Bytes,
  331. uint64_t Address,
  332. raw_ostream &CS) const {
  333. auto *ReadFunc = IsLittleEndian ? support::endian::read32le
  334. : support::endian::read32be;
  335. // If this is an 8-byte prefixed instruction, handle it here.
  336. // Note: prefixed instructions aren't technically 8-byte entities - the prefix
  337. // appears in memory at an address 4 bytes prior to that of the base
  338. // instruction regardless of endianness. So we read the two pieces and
  339. // rebuild the 8-byte instruction.
  340. // TODO: In this function we call decodeInstruction several times with
  341. // different decoder tables. It may be possible to only call once by
  342. // looking at the top 6 bits of the instruction.
  343. if (STI.getFeatureBits()[PPC::FeaturePrefixInstrs] && Bytes.size() >= 8) {
  344. uint32_t Prefix = ReadFunc(Bytes.data());
  345. uint32_t BaseInst = ReadFunc(Bytes.data() + 4);
  346. uint64_t Inst = BaseInst | (uint64_t)Prefix << 32;
  347. DecodeStatus result = decodeInstruction(DecoderTable64, MI, Inst, Address,
  348. this, STI);
  349. if (result != MCDisassembler::Fail) {
  350. Size = 8;
  351. return result;
  352. }
  353. }
  354. // Get the four bytes of the instruction.
  355. Size = 4;
  356. if (Bytes.size() < 4) {
  357. Size = 0;
  358. return MCDisassembler::Fail;
  359. }
  360. // Read the instruction in the proper endianness.
  361. uint64_t Inst = ReadFunc(Bytes.data());
  362. if (STI.getFeatureBits()[PPC::FeatureSPE]) {
  363. DecodeStatus result =
  364. decodeInstruction(DecoderTableSPE32, MI, Inst, Address, this, STI);
  365. if (result != MCDisassembler::Fail)
  366. return result;
  367. }
  368. return decodeInstruction(DecoderTable32, MI, Inst, Address, this, STI);
  369. }