PPCDisassembler.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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/MCDecoderOps.h"
  11. #include "llvm/MC/MCDisassembler/MCDisassembler.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 MCDisassembler * /*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 MCDisassembler * /*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 MCDisassembler *Decoder) {
  78. return decodeRegisterClass(Inst, RegNo, CRRegs);
  79. }
  80. static DecodeStatus DecodeCRBITRCRegisterClass(MCInst &Inst, uint64_t RegNo,
  81. uint64_t Address,
  82. const MCDisassembler *Decoder) {
  83. return decodeRegisterClass(Inst, RegNo, CRBITRegs);
  84. }
  85. static DecodeStatus DecodeF4RCRegisterClass(MCInst &Inst, uint64_t RegNo,
  86. uint64_t Address,
  87. const MCDisassembler *Decoder) {
  88. return decodeRegisterClass(Inst, RegNo, FRegs);
  89. }
  90. static DecodeStatus DecodeF8RCRegisterClass(MCInst &Inst, uint64_t RegNo,
  91. uint64_t Address,
  92. const MCDisassembler *Decoder) {
  93. return decodeRegisterClass(Inst, RegNo, FRegs);
  94. }
  95. static DecodeStatus DecodeVFRCRegisterClass(MCInst &Inst, uint64_t RegNo,
  96. uint64_t Address,
  97. const MCDisassembler *Decoder) {
  98. return decodeRegisterClass(Inst, RegNo, VFRegs);
  99. }
  100. static DecodeStatus DecodeVRRCRegisterClass(MCInst &Inst, uint64_t RegNo,
  101. uint64_t Address,
  102. const MCDisassembler *Decoder) {
  103. return decodeRegisterClass(Inst, RegNo, VRegs);
  104. }
  105. static DecodeStatus DecodeVSRCRegisterClass(MCInst &Inst, uint64_t RegNo,
  106. uint64_t Address,
  107. const MCDisassembler *Decoder) {
  108. return decodeRegisterClass(Inst, RegNo, VSRegs);
  109. }
  110. static DecodeStatus DecodeVSFRCRegisterClass(MCInst &Inst, uint64_t RegNo,
  111. uint64_t Address,
  112. const MCDisassembler *Decoder) {
  113. return decodeRegisterClass(Inst, RegNo, VSFRegs);
  114. }
  115. static DecodeStatus DecodeVSSRCRegisterClass(MCInst &Inst, uint64_t RegNo,
  116. uint64_t Address,
  117. const MCDisassembler *Decoder) {
  118. return decodeRegisterClass(Inst, RegNo, VSSRegs);
  119. }
  120. static DecodeStatus DecodeGPRCRegisterClass(MCInst &Inst, uint64_t RegNo,
  121. uint64_t Address,
  122. const MCDisassembler *Decoder) {
  123. return decodeRegisterClass(Inst, RegNo, RRegs);
  124. }
  125. static DecodeStatus
  126. DecodeGPRC_NOR0RegisterClass(MCInst &Inst, uint64_t RegNo, uint64_t Address,
  127. const MCDisassembler *Decoder) {
  128. return decodeRegisterClass(Inst, RegNo, RRegsNoR0);
  129. }
  130. static DecodeStatus DecodeG8RCRegisterClass(MCInst &Inst, uint64_t RegNo,
  131. uint64_t Address,
  132. const MCDisassembler *Decoder) {
  133. return decodeRegisterClass(Inst, RegNo, XRegs);
  134. }
  135. static DecodeStatus DecodeG8pRCRegisterClass(MCInst &Inst, uint64_t RegNo,
  136. uint64_t Address,
  137. const MCDisassembler *Decoder) {
  138. return decodeRegisterClass(Inst, RegNo, XRegs);
  139. }
  140. static DecodeStatus
  141. DecodeG8RC_NOX0RegisterClass(MCInst &Inst, uint64_t RegNo, uint64_t Address,
  142. const MCDisassembler *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 MCDisassembler *Decoder) {
  150. return decodeRegisterClass(Inst, RegNo, SPERegs);
  151. }
  152. static DecodeStatus DecodeACCRCRegisterClass(MCInst &Inst, uint64_t RegNo,
  153. uint64_t Address,
  154. const MCDisassembler *Decoder) {
  155. return decodeRegisterClass(Inst, RegNo, ACCRegs);
  156. }
  157. static DecodeStatus DecodeWACCRCRegisterClass(MCInst &Inst, uint64_t RegNo,
  158. uint64_t Address,
  159. const void *Decoder) {
  160. return decodeRegisterClass(Inst, RegNo, WACCRegs);
  161. }
  162. static DecodeStatus DecodeWACC_HIRCRegisterClass(MCInst &Inst, uint64_t RegNo,
  163. uint64_t Address,
  164. const void *Decoder) {
  165. return decodeRegisterClass(Inst, RegNo, WACC_HIRegs);
  166. }
  167. // TODO: Make this function static when the register class is used by a new
  168. // instruction.
  169. DecodeStatus DecodeDMRROWRCRegisterClass(MCInst &Inst, uint64_t RegNo,
  170. uint64_t Address,
  171. const void *Decoder) {
  172. return decodeRegisterClass(Inst, RegNo, DMRROWRegs);
  173. }
  174. static DecodeStatus DecodeDMRROWpRCRegisterClass(MCInst &Inst, uint64_t RegNo,
  175. uint64_t Address,
  176. const void *Decoder) {
  177. return decodeRegisterClass(Inst, RegNo, DMRROWpRegs);
  178. }
  179. static DecodeStatus DecodeDMRRCRegisterClass(MCInst &Inst, uint64_t RegNo,
  180. uint64_t Address,
  181. const void *Decoder) {
  182. return decodeRegisterClass(Inst, RegNo, DMRRegs);
  183. }
  184. // TODO: Make this function static when the register class is used by a new
  185. // instruction.
  186. DecodeStatus DecodeDMRpRCRegisterClass(MCInst &Inst, uint64_t RegNo,
  187. uint64_t Address, const void *Decoder) {
  188. return decodeRegisterClass(Inst, RegNo, DMRpRegs);
  189. }
  190. static DecodeStatus DecodeVSRpRCRegisterClass(MCInst &Inst, uint64_t RegNo,
  191. uint64_t Address,
  192. const MCDisassembler *Decoder) {
  193. return decodeRegisterClass(Inst, RegNo, VSRpRegs);
  194. }
  195. #define DecodeQSRCRegisterClass DecodeQFRCRegisterClass
  196. #define DecodeQBRCRegisterClass DecodeQFRCRegisterClass
  197. template <unsigned N>
  198. static DecodeStatus decodeUImmOperand(MCInst &Inst, uint64_t Imm,
  199. int64_t Address,
  200. const MCDisassembler *Decoder) {
  201. assert(isUInt<N>(Imm) && "Invalid immediate");
  202. Inst.addOperand(MCOperand::createImm(Imm));
  203. return MCDisassembler::Success;
  204. }
  205. template <unsigned N>
  206. static DecodeStatus decodeSImmOperand(MCInst &Inst, uint64_t Imm,
  207. int64_t Address,
  208. const MCDisassembler *Decoder) {
  209. assert(isUInt<N>(Imm) && "Invalid immediate");
  210. Inst.addOperand(MCOperand::createImm(SignExtend64<N>(Imm)));
  211. return MCDisassembler::Success;
  212. }
  213. static DecodeStatus decodeImmZeroOperand(MCInst &Inst, uint64_t Imm,
  214. int64_t Address,
  215. const MCDisassembler *Decoder) {
  216. if (Imm != 0)
  217. return MCDisassembler::Fail;
  218. Inst.addOperand(MCOperand::createImm(Imm));
  219. return MCDisassembler::Success;
  220. }
  221. static DecodeStatus decodeVSRpEvenOperands(MCInst &Inst, uint64_t RegNo,
  222. uint64_t Address,
  223. const MCDisassembler *Decoder) {
  224. if (RegNo & 1)
  225. return MCDisassembler::Fail;
  226. Inst.addOperand(MCOperand::createReg(VSRpRegs[RegNo >> 1]));
  227. return MCDisassembler::Success;
  228. }
  229. static DecodeStatus decodeMemRIOperands(MCInst &Inst, uint64_t Imm,
  230. int64_t Address,
  231. const MCDisassembler *Decoder) {
  232. // Decode the memri field (imm, reg), which has the low 16-bits as the
  233. // displacement and the next 5 bits as the register #.
  234. uint64_t Base = Imm >> 16;
  235. uint64_t Disp = Imm & 0xFFFF;
  236. assert(Base < 32 && "Invalid base register");
  237. switch (Inst.getOpcode()) {
  238. default: break;
  239. case PPC::LBZU:
  240. case PPC::LHAU:
  241. case PPC::LHZU:
  242. case PPC::LWZU:
  243. case PPC::LFSU:
  244. case PPC::LFDU:
  245. // Add the tied output operand.
  246. Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base]));
  247. break;
  248. case PPC::STBU:
  249. case PPC::STHU:
  250. case PPC::STWU:
  251. case PPC::STFSU:
  252. case PPC::STFDU:
  253. Inst.insert(Inst.begin(), MCOperand::createReg(RRegsNoR0[Base]));
  254. break;
  255. }
  256. Inst.addOperand(MCOperand::createImm(SignExtend64<16>(Disp)));
  257. Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base]));
  258. return MCDisassembler::Success;
  259. }
  260. static DecodeStatus decodeMemRIXOperands(MCInst &Inst, uint64_t Imm,
  261. int64_t Address,
  262. const MCDisassembler *Decoder) {
  263. // Decode the memrix field (imm, reg), which has the low 14-bits as the
  264. // displacement and the next 5 bits as the register #.
  265. uint64_t Base = Imm >> 14;
  266. uint64_t Disp = Imm & 0x3FFF;
  267. assert(Base < 32 && "Invalid base register");
  268. if (Inst.getOpcode() == PPC::LDU)
  269. // Add the tied output operand.
  270. Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base]));
  271. else if (Inst.getOpcode() == PPC::STDU)
  272. Inst.insert(Inst.begin(), MCOperand::createReg(RRegsNoR0[Base]));
  273. Inst.addOperand(MCOperand::createImm(SignExtend64<16>(Disp << 2)));
  274. Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base]));
  275. return MCDisassembler::Success;
  276. }
  277. static DecodeStatus decodeMemRIHashOperands(MCInst &Inst, uint64_t Imm,
  278. int64_t Address,
  279. const MCDisassembler *Decoder) {
  280. // Decode the memrix field for a hash store or hash check operation.
  281. // The field is composed of a register and an immediate value that is 6 bits
  282. // and covers the range -8 to -512. The immediate is always negative and 2s
  283. // complement which is why we sign extend a 7 bit value.
  284. const uint64_t Base = Imm >> 6;
  285. const int64_t Disp = SignExtend64<7>((Imm & 0x3F) + 64) * 8;
  286. assert(Base < 32 && "Invalid base register");
  287. Inst.addOperand(MCOperand::createImm(Disp));
  288. Inst.addOperand(MCOperand::createReg(RRegs[Base]));
  289. return MCDisassembler::Success;
  290. }
  291. static DecodeStatus decodeMemRIX16Operands(MCInst &Inst, uint64_t Imm,
  292. int64_t Address,
  293. const MCDisassembler *Decoder) {
  294. // Decode the memrix16 field (imm, reg), which has the low 12-bits as the
  295. // displacement with 16-byte aligned, and the next 5 bits as the register #.
  296. uint64_t Base = Imm >> 12;
  297. uint64_t Disp = Imm & 0xFFF;
  298. assert(Base < 32 && "Invalid base register");
  299. Inst.addOperand(MCOperand::createImm(SignExtend64<16>(Disp << 4)));
  300. Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base]));
  301. return MCDisassembler::Success;
  302. }
  303. static DecodeStatus decodeMemRI34PCRelOperands(MCInst &Inst, uint64_t Imm,
  304. int64_t Address,
  305. const MCDisassembler *Decoder) {
  306. // Decode the memri34_pcrel field (imm, reg), which has the low 34-bits as the
  307. // displacement, and the next 5 bits as an immediate 0.
  308. uint64_t Base = Imm >> 34;
  309. uint64_t Disp = Imm & 0x3FFFFFFFFUL;
  310. assert(Base < 32 && "Invalid base register");
  311. Inst.addOperand(MCOperand::createImm(SignExtend64<34>(Disp)));
  312. return decodeImmZeroOperand(Inst, Base, Address, Decoder);
  313. }
  314. static DecodeStatus decodeMemRI34Operands(MCInst &Inst, uint64_t Imm,
  315. int64_t Address,
  316. const MCDisassembler *Decoder) {
  317. // Decode the memri34 field (imm, reg), which has the low 34-bits as the
  318. // displacement, and the next 5 bits as the register #.
  319. uint64_t Base = Imm >> 34;
  320. uint64_t Disp = Imm & 0x3FFFFFFFFUL;
  321. assert(Base < 32 && "Invalid base register");
  322. Inst.addOperand(MCOperand::createImm(SignExtend64<34>(Disp)));
  323. Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base]));
  324. return MCDisassembler::Success;
  325. }
  326. static DecodeStatus decodeSPE8Operands(MCInst &Inst, uint64_t Imm,
  327. int64_t Address,
  328. const MCDisassembler *Decoder) {
  329. // Decode the spe8disp field (imm, reg), which has the low 5-bits as the
  330. // displacement with 8-byte aligned, and the next 5 bits as the register #.
  331. uint64_t Base = Imm >> 5;
  332. uint64_t Disp = Imm & 0x1F;
  333. assert(Base < 32 && "Invalid base register");
  334. Inst.addOperand(MCOperand::createImm(Disp << 3));
  335. Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base]));
  336. return MCDisassembler::Success;
  337. }
  338. static DecodeStatus decodeSPE4Operands(MCInst &Inst, uint64_t Imm,
  339. int64_t Address,
  340. const MCDisassembler *Decoder) {
  341. // Decode the spe4disp field (imm, reg), which has the low 5-bits as the
  342. // displacement with 4-byte aligned, and the next 5 bits as the register #.
  343. uint64_t Base = Imm >> 5;
  344. uint64_t Disp = Imm & 0x1F;
  345. assert(Base < 32 && "Invalid base register");
  346. Inst.addOperand(MCOperand::createImm(Disp << 2));
  347. Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base]));
  348. return MCDisassembler::Success;
  349. }
  350. static DecodeStatus decodeSPE2Operands(MCInst &Inst, uint64_t Imm,
  351. int64_t Address,
  352. const MCDisassembler *Decoder) {
  353. // Decode the spe2disp field (imm, reg), which has the low 5-bits as the
  354. // displacement with 2-byte aligned, and the next 5 bits as the register #.
  355. uint64_t Base = Imm >> 5;
  356. uint64_t Disp = Imm & 0x1F;
  357. assert(Base < 32 && "Invalid base register");
  358. Inst.addOperand(MCOperand::createImm(Disp << 1));
  359. Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base]));
  360. return MCDisassembler::Success;
  361. }
  362. static DecodeStatus decodeCRBitMOperand(MCInst &Inst, uint64_t Imm,
  363. int64_t Address,
  364. const MCDisassembler *Decoder) {
  365. // The cr bit encoding is 0x80 >> cr_reg_num.
  366. unsigned Zeros = countTrailingZeros(Imm);
  367. assert(Zeros < 8 && "Invalid CR bit value");
  368. Inst.addOperand(MCOperand::createReg(CRRegs[7 - Zeros]));
  369. return MCDisassembler::Success;
  370. }
  371. #include "PPCGenDisassemblerTables.inc"
  372. DecodeStatus PPCDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
  373. ArrayRef<uint8_t> Bytes,
  374. uint64_t Address,
  375. raw_ostream &CS) const {
  376. auto *ReadFunc = IsLittleEndian ? support::endian::read32le
  377. : support::endian::read32be;
  378. // If this is an 8-byte prefixed instruction, handle it here.
  379. // Note: prefixed instructions aren't technically 8-byte entities - the prefix
  380. // appears in memory at an address 4 bytes prior to that of the base
  381. // instruction regardless of endianness. So we read the two pieces and
  382. // rebuild the 8-byte instruction.
  383. // TODO: In this function we call decodeInstruction several times with
  384. // different decoder tables. It may be possible to only call once by
  385. // looking at the top 6 bits of the instruction.
  386. if (STI.getFeatureBits()[PPC::FeaturePrefixInstrs] && Bytes.size() >= 8) {
  387. uint32_t Prefix = ReadFunc(Bytes.data());
  388. uint32_t BaseInst = ReadFunc(Bytes.data() + 4);
  389. uint64_t Inst = BaseInst | (uint64_t)Prefix << 32;
  390. DecodeStatus result = decodeInstruction(DecoderTable64, MI, Inst, Address,
  391. this, STI);
  392. if (result != MCDisassembler::Fail) {
  393. Size = 8;
  394. return result;
  395. }
  396. }
  397. // Get the four bytes of the instruction.
  398. Size = 4;
  399. if (Bytes.size() < 4) {
  400. Size = 0;
  401. return MCDisassembler::Fail;
  402. }
  403. // Read the instruction in the proper endianness.
  404. uint64_t Inst = ReadFunc(Bytes.data());
  405. if (STI.getFeatureBits()[PPC::FeatureSPE]) {
  406. DecodeStatus result =
  407. decodeInstruction(DecoderTableSPE32, MI, Inst, Address, this, STI);
  408. if (result != MCDisassembler::Fail)
  409. return result;
  410. }
  411. return decodeInstruction(DecoderTable32, MI, Inst, Address, this, STI);
  412. }