PPCMCCodeEmitter.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. //===-- PPCMCCodeEmitter.cpp - Convert PPC code to machine code -----------===//
  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. //
  9. // This file implements the PPCMCCodeEmitter class.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "MCTargetDesc/PPCFixupKinds.h"
  13. #include "PPCInstrInfo.h"
  14. #include "PPCMCCodeEmitter.h"
  15. #include "llvm/ADT/SmallVector.h"
  16. #include "llvm/ADT/Statistic.h"
  17. #include "llvm/ADT/Triple.h"
  18. #include "llvm/MC/MCFixup.h"
  19. #include "llvm/MC/MCInstrDesc.h"
  20. #include "llvm/MC/MCRegisterInfo.h"
  21. #include "llvm/Support/Endian.h"
  22. #include "llvm/Support/EndianStream.h"
  23. #include "llvm/Support/ErrorHandling.h"
  24. #include "llvm/Support/MathExtras.h"
  25. #include "llvm/Support/raw_ostream.h"
  26. #include <cassert>
  27. #include <cstdint>
  28. using namespace llvm;
  29. #define DEBUG_TYPE "mccodeemitter"
  30. STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
  31. MCCodeEmitter *llvm::createPPCMCCodeEmitter(const MCInstrInfo &MCII,
  32. const MCRegisterInfo &MRI,
  33. MCContext &Ctx) {
  34. return new PPCMCCodeEmitter(MCII, Ctx);
  35. }
  36. unsigned PPCMCCodeEmitter::
  37. getDirectBrEncoding(const MCInst &MI, unsigned OpNo,
  38. SmallVectorImpl<MCFixup> &Fixups,
  39. const MCSubtargetInfo &STI) const {
  40. const MCOperand &MO = MI.getOperand(OpNo);
  41. if (MO.isReg() || MO.isImm())
  42. return getMachineOpValue(MI, MO, Fixups, STI);
  43. // Add a fixup for the branch target.
  44. Fixups.push_back(MCFixup::create(0, MO.getExpr(),
  45. ((MI.getOpcode() == PPC::BL8_NOTOC ||
  46. MI.getOpcode() == PPC::BL8_NOTOC_TLS)
  47. ? (MCFixupKind)PPC::fixup_ppc_br24_notoc
  48. : (MCFixupKind)PPC::fixup_ppc_br24)));
  49. return 0;
  50. }
  51. unsigned PPCMCCodeEmitter::getCondBrEncoding(const MCInst &MI, unsigned OpNo,
  52. SmallVectorImpl<MCFixup> &Fixups,
  53. const MCSubtargetInfo &STI) const {
  54. const MCOperand &MO = MI.getOperand(OpNo);
  55. if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI);
  56. // Add a fixup for the branch target.
  57. Fixups.push_back(MCFixup::create(0, MO.getExpr(),
  58. (MCFixupKind)PPC::fixup_ppc_brcond14));
  59. return 0;
  60. }
  61. unsigned PPCMCCodeEmitter::
  62. getAbsDirectBrEncoding(const MCInst &MI, unsigned OpNo,
  63. SmallVectorImpl<MCFixup> &Fixups,
  64. const MCSubtargetInfo &STI) const {
  65. const MCOperand &MO = MI.getOperand(OpNo);
  66. if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI);
  67. // Add a fixup for the branch target.
  68. Fixups.push_back(MCFixup::create(0, MO.getExpr(),
  69. (MCFixupKind)PPC::fixup_ppc_br24abs));
  70. return 0;
  71. }
  72. unsigned PPCMCCodeEmitter::
  73. getAbsCondBrEncoding(const MCInst &MI, unsigned OpNo,
  74. SmallVectorImpl<MCFixup> &Fixups,
  75. const MCSubtargetInfo &STI) const {
  76. const MCOperand &MO = MI.getOperand(OpNo);
  77. if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI);
  78. // Add a fixup for the branch target.
  79. Fixups.push_back(MCFixup::create(0, MO.getExpr(),
  80. (MCFixupKind)PPC::fixup_ppc_brcond14abs));
  81. return 0;
  82. }
  83. unsigned
  84. PPCMCCodeEmitter::getVSRpEvenEncoding(const MCInst &MI, unsigned OpNo,
  85. SmallVectorImpl<MCFixup> &Fixups,
  86. const MCSubtargetInfo &STI) const {
  87. assert(MI.getOperand(OpNo).isReg() && "Operand should be a register");
  88. unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI)
  89. << 1;
  90. return RegBits;
  91. }
  92. unsigned PPCMCCodeEmitter::getImm16Encoding(const MCInst &MI, unsigned OpNo,
  93. SmallVectorImpl<MCFixup> &Fixups,
  94. const MCSubtargetInfo &STI) const {
  95. const MCOperand &MO = MI.getOperand(OpNo);
  96. if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI);
  97. // Add a fixup for the immediate field.
  98. Fixups.push_back(MCFixup::create(IsLittleEndian? 0 : 2, MO.getExpr(),
  99. (MCFixupKind)PPC::fixup_ppc_half16));
  100. return 0;
  101. }
  102. uint64_t PPCMCCodeEmitter::getImm34Encoding(const MCInst &MI, unsigned OpNo,
  103. SmallVectorImpl<MCFixup> &Fixups,
  104. const MCSubtargetInfo &STI,
  105. MCFixupKind Fixup) const {
  106. const MCOperand &MO = MI.getOperand(OpNo);
  107. assert(!MO.isReg() && "Not expecting a register for this operand.");
  108. if (MO.isImm())
  109. return getMachineOpValue(MI, MO, Fixups, STI);
  110. // Add a fixup for the immediate field.
  111. Fixups.push_back(MCFixup::create(0, MO.getExpr(), Fixup));
  112. return 0;
  113. }
  114. uint64_t
  115. PPCMCCodeEmitter::getImm34EncodingNoPCRel(const MCInst &MI, unsigned OpNo,
  116. SmallVectorImpl<MCFixup> &Fixups,
  117. const MCSubtargetInfo &STI) const {
  118. return getImm34Encoding(MI, OpNo, Fixups, STI,
  119. (MCFixupKind)PPC::fixup_ppc_imm34);
  120. }
  121. uint64_t
  122. PPCMCCodeEmitter::getImm34EncodingPCRel(const MCInst &MI, unsigned OpNo,
  123. SmallVectorImpl<MCFixup> &Fixups,
  124. const MCSubtargetInfo &STI) const {
  125. return getImm34Encoding(MI, OpNo, Fixups, STI,
  126. (MCFixupKind)PPC::fixup_ppc_pcrel34);
  127. }
  128. unsigned PPCMCCodeEmitter::getMemRIEncoding(const MCInst &MI, unsigned OpNo,
  129. SmallVectorImpl<MCFixup> &Fixups,
  130. const MCSubtargetInfo &STI) const {
  131. // Encode (imm, reg) as a memri, which has the low 16-bits as the
  132. // displacement and the next 5 bits as the register #.
  133. assert(MI.getOperand(OpNo+1).isReg());
  134. unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 16;
  135. const MCOperand &MO = MI.getOperand(OpNo);
  136. if (MO.isImm())
  137. return (getMachineOpValue(MI, MO, Fixups, STI) & 0xFFFF) | RegBits;
  138. // Add a fixup for the displacement field.
  139. Fixups.push_back(MCFixup::create(IsLittleEndian? 0 : 2, MO.getExpr(),
  140. (MCFixupKind)PPC::fixup_ppc_half16));
  141. return RegBits;
  142. }
  143. unsigned PPCMCCodeEmitter::getMemRIXEncoding(const MCInst &MI, unsigned OpNo,
  144. SmallVectorImpl<MCFixup> &Fixups,
  145. const MCSubtargetInfo &STI) const {
  146. // Encode (imm, reg) as a memrix, which has the low 14-bits as the
  147. // displacement and the next 5 bits as the register #.
  148. assert(MI.getOperand(OpNo+1).isReg());
  149. unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 14;
  150. const MCOperand &MO = MI.getOperand(OpNo);
  151. if (MO.isImm())
  152. return ((getMachineOpValue(MI, MO, Fixups, STI) >> 2) & 0x3FFF) | RegBits;
  153. // Add a fixup for the displacement field.
  154. Fixups.push_back(MCFixup::create(IsLittleEndian? 0 : 2, MO.getExpr(),
  155. (MCFixupKind)PPC::fixup_ppc_half16ds));
  156. return RegBits;
  157. }
  158. unsigned PPCMCCodeEmitter::getMemRIX16Encoding(const MCInst &MI, unsigned OpNo,
  159. SmallVectorImpl<MCFixup> &Fixups,
  160. const MCSubtargetInfo &STI) const {
  161. // Encode (imm, reg) as a memrix16, which has the low 12-bits as the
  162. // displacement and the next 5 bits as the register #.
  163. assert(MI.getOperand(OpNo+1).isReg());
  164. unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 12;
  165. const MCOperand &MO = MI.getOperand(OpNo);
  166. if (MO.isImm()) {
  167. assert(!(MO.getImm() % 16) &&
  168. "Expecting an immediate that is a multiple of 16");
  169. return ((getMachineOpValue(MI, MO, Fixups, STI) >> 4) & 0xFFF) | RegBits;
  170. }
  171. // Otherwise add a fixup for the displacement field.
  172. Fixups.push_back(MCFixup::create(IsLittleEndian ? 0 : 2, MO.getExpr(),
  173. (MCFixupKind)PPC::fixup_ppc_half16dq));
  174. return RegBits;
  175. }
  176. unsigned
  177. PPCMCCodeEmitter::getMemRIHashEncoding(const MCInst &MI, unsigned OpNo,
  178. SmallVectorImpl<MCFixup> &Fixups,
  179. const MCSubtargetInfo &STI) const {
  180. // Encode (imm, reg) for the hash load/store to stack for the ROP Protection
  181. // instructions.
  182. const MCOperand &RegMO = MI.getOperand(OpNo + 1);
  183. const MCOperand &MO = MI.getOperand(OpNo);
  184. assert(RegMO.isReg() && "Base address must be a register.");
  185. assert(MO.isImm() && "Expecting an immediate operand.");
  186. assert(!(MO.getImm() % 8) && "Expecting offset to be 8 byte aligned.");
  187. unsigned RegBits = getMachineOpValue(MI, RegMO, Fixups, STI) << 6;
  188. unsigned DX = (MO.getImm() >> 3) & 0x3F;
  189. return RegBits | DX;
  190. }
  191. uint64_t
  192. PPCMCCodeEmitter::getMemRI34PCRelEncoding(const MCInst &MI, unsigned OpNo,
  193. SmallVectorImpl<MCFixup> &Fixups,
  194. const MCSubtargetInfo &STI) const {
  195. // Encode the PCRelative version of memri34: imm34(r0).
  196. // In the PC relative version the register for the address must be zero.
  197. // The 34 bit immediate can fall into one of three cases:
  198. // 1) It is a relocation to be filled in by the linker represented as:
  199. // (MCExpr::SymbolRef)
  200. // 2) It is a relocation + SignedOffset represented as:
  201. // (MCExpr::Binary(MCExpr::SymbolRef + MCExpr::Constant))
  202. // 3) It is a known value at compile time.
  203. // Make sure that the register is a zero as expected.
  204. assert(MI.getOperand(OpNo + 1).isImm() && "Expecting an immediate.");
  205. uint64_t RegBits =
  206. getMachineOpValue(MI, MI.getOperand(OpNo + 1), Fixups, STI) << 34;
  207. assert(RegBits == 0 && "Operand must be 0.");
  208. // If this is not a MCExpr then we are in case 3) and we are dealing with
  209. // a value known at compile time, not a relocation.
  210. const MCOperand &MO = MI.getOperand(OpNo);
  211. if (!MO.isExpr())
  212. return ((getMachineOpValue(MI, MO, Fixups, STI)) & 0x3FFFFFFFFUL) | RegBits;
  213. // At this point in the function it is known that MO is of type MCExpr.
  214. // Therefore we are dealing with either case 1) a symbol ref or
  215. // case 2) a symbol ref plus a constant.
  216. const MCExpr *Expr = MO.getExpr();
  217. switch (Expr->getKind()) {
  218. default:
  219. llvm_unreachable("Unsupported MCExpr for getMemRI34PCRelEncoding.");
  220. case MCExpr::SymbolRef: {
  221. // Relocation alone.
  222. const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(Expr);
  223. (void)SRE;
  224. // Currently these are the only valid PCRelative Relocations.
  225. assert((SRE->getKind() == MCSymbolRefExpr::VK_PCREL ||
  226. SRE->getKind() == MCSymbolRefExpr::VK_PPC_GOT_PCREL ||
  227. SRE->getKind() == MCSymbolRefExpr::VK_PPC_GOT_TLSGD_PCREL ||
  228. SRE->getKind() == MCSymbolRefExpr::VK_PPC_GOT_TLSLD_PCREL ||
  229. SRE->getKind() == MCSymbolRefExpr::VK_PPC_GOT_TPREL_PCREL) &&
  230. "VariantKind must be VK_PCREL or VK_PPC_GOT_PCREL or "
  231. "VK_PPC_GOT_TLSGD_PCREL or VK_PPC_GOT_TLSLD_PCREL or "
  232. "VK_PPC_GOT_TPREL_PCREL.");
  233. // Generate the fixup for the relocation.
  234. Fixups.push_back(
  235. MCFixup::create(0, Expr,
  236. static_cast<MCFixupKind>(PPC::fixup_ppc_pcrel34)));
  237. // Put zero in the location of the immediate. The linker will fill in the
  238. // correct value based on the relocation.
  239. return 0;
  240. }
  241. case MCExpr::Binary: {
  242. // Relocation plus some offset.
  243. const MCBinaryExpr *BE = cast<MCBinaryExpr>(Expr);
  244. assert(BE->getOpcode() == MCBinaryExpr::Add &&
  245. "Binary expression opcode must be an add.");
  246. const MCExpr *LHS = BE->getLHS();
  247. const MCExpr *RHS = BE->getRHS();
  248. // Need to check in both directions. Reloc+Offset and Offset+Reloc.
  249. if (LHS->getKind() != MCExpr::SymbolRef)
  250. std::swap(LHS, RHS);
  251. if (LHS->getKind() != MCExpr::SymbolRef ||
  252. RHS->getKind() != MCExpr::Constant)
  253. llvm_unreachable("Expecting to have one constant and one relocation.");
  254. const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(LHS);
  255. (void)SRE;
  256. assert(isInt<34>(cast<MCConstantExpr>(RHS)->getValue()) &&
  257. "Value must fit in 34 bits.");
  258. // Currently these are the only valid PCRelative Relocations.
  259. assert((SRE->getKind() == MCSymbolRefExpr::VK_PCREL ||
  260. SRE->getKind() == MCSymbolRefExpr::VK_PPC_GOT_PCREL) &&
  261. "VariantKind must be VK_PCREL or VK_PPC_GOT_PCREL");
  262. // Generate the fixup for the relocation.
  263. Fixups.push_back(
  264. MCFixup::create(0, Expr,
  265. static_cast<MCFixupKind>(PPC::fixup_ppc_pcrel34)));
  266. // Put zero in the location of the immediate. The linker will fill in the
  267. // correct value based on the relocation.
  268. return 0;
  269. }
  270. }
  271. }
  272. uint64_t
  273. PPCMCCodeEmitter::getMemRI34Encoding(const MCInst &MI, unsigned OpNo,
  274. SmallVectorImpl<MCFixup> &Fixups,
  275. const MCSubtargetInfo &STI) const {
  276. // Encode (imm, reg) as a memri34, which has the low 34-bits as the
  277. // displacement and the next 5 bits as the register #.
  278. assert(MI.getOperand(OpNo + 1).isReg() && "Expecting a register.");
  279. uint64_t RegBits = getMachineOpValue(MI, MI.getOperand(OpNo + 1), Fixups, STI)
  280. << 34;
  281. const MCOperand &MO = MI.getOperand(OpNo);
  282. return ((getMachineOpValue(MI, MO, Fixups, STI)) & 0x3FFFFFFFFUL) | RegBits;
  283. }
  284. unsigned PPCMCCodeEmitter::getSPE8DisEncoding(const MCInst &MI, unsigned OpNo,
  285. SmallVectorImpl<MCFixup> &Fixups,
  286. const MCSubtargetInfo &STI)
  287. const {
  288. // Encode (imm, reg) as a spe8dis, which has the low 5-bits of (imm / 8)
  289. // as the displacement and the next 5 bits as the register #.
  290. assert(MI.getOperand(OpNo+1).isReg());
  291. uint32_t RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 5;
  292. const MCOperand &MO = MI.getOperand(OpNo);
  293. assert(MO.isImm());
  294. uint32_t Imm = getMachineOpValue(MI, MO, Fixups, STI) >> 3;
  295. return reverseBits(Imm | RegBits) >> 22;
  296. }
  297. unsigned PPCMCCodeEmitter::getSPE4DisEncoding(const MCInst &MI, unsigned OpNo,
  298. SmallVectorImpl<MCFixup> &Fixups,
  299. const MCSubtargetInfo &STI)
  300. const {
  301. // Encode (imm, reg) as a spe4dis, which has the low 5-bits of (imm / 4)
  302. // as the displacement and the next 5 bits as the register #.
  303. assert(MI.getOperand(OpNo+1).isReg());
  304. uint32_t RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 5;
  305. const MCOperand &MO = MI.getOperand(OpNo);
  306. assert(MO.isImm());
  307. uint32_t Imm = getMachineOpValue(MI, MO, Fixups, STI) >> 2;
  308. return reverseBits(Imm | RegBits) >> 22;
  309. }
  310. unsigned PPCMCCodeEmitter::getSPE2DisEncoding(const MCInst &MI, unsigned OpNo,
  311. SmallVectorImpl<MCFixup> &Fixups,
  312. const MCSubtargetInfo &STI)
  313. const {
  314. // Encode (imm, reg) as a spe2dis, which has the low 5-bits of (imm / 2)
  315. // as the displacement and the next 5 bits as the register #.
  316. assert(MI.getOperand(OpNo+1).isReg());
  317. uint32_t RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 5;
  318. const MCOperand &MO = MI.getOperand(OpNo);
  319. assert(MO.isImm());
  320. uint32_t Imm = getMachineOpValue(MI, MO, Fixups, STI) >> 1;
  321. return reverseBits(Imm | RegBits) >> 22;
  322. }
  323. unsigned PPCMCCodeEmitter::getTLSRegEncoding(const MCInst &MI, unsigned OpNo,
  324. SmallVectorImpl<MCFixup> &Fixups,
  325. const MCSubtargetInfo &STI) const {
  326. const MCOperand &MO = MI.getOperand(OpNo);
  327. if (MO.isReg()) return getMachineOpValue(MI, MO, Fixups, STI);
  328. // Add a fixup for the TLS register, which simply provides a relocation
  329. // hint to the linker that this statement is part of a relocation sequence.
  330. // Return the thread-pointer register's encoding. Add a one byte displacement
  331. // if using PC relative memops.
  332. const MCExpr *Expr = MO.getExpr();
  333. const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(Expr);
  334. bool IsPCRel = SRE->getKind() == MCSymbolRefExpr::VK_PPC_TLS_PCREL;
  335. Fixups.push_back(MCFixup::create(IsPCRel ? 1 : 0, Expr,
  336. (MCFixupKind)PPC::fixup_ppc_nofixup));
  337. const Triple &TT = STI.getTargetTriple();
  338. bool isPPC64 = TT.isPPC64();
  339. return CTX.getRegisterInfo()->getEncodingValue(isPPC64 ? PPC::X13 : PPC::R2);
  340. }
  341. unsigned PPCMCCodeEmitter::getTLSCallEncoding(const MCInst &MI, unsigned OpNo,
  342. SmallVectorImpl<MCFixup> &Fixups,
  343. const MCSubtargetInfo &STI) const {
  344. // For special TLS calls, we need two fixups; one for the branch target
  345. // (__tls_get_addr), which we create via getDirectBrEncoding as usual,
  346. // and one for the TLSGD or TLSLD symbol, which is emitted here.
  347. const MCOperand &MO = MI.getOperand(OpNo+1);
  348. Fixups.push_back(MCFixup::create(0, MO.getExpr(),
  349. (MCFixupKind)PPC::fixup_ppc_nofixup));
  350. return getDirectBrEncoding(MI, OpNo, Fixups, STI);
  351. }
  352. unsigned PPCMCCodeEmitter::
  353. get_crbitm_encoding(const MCInst &MI, unsigned OpNo,
  354. SmallVectorImpl<MCFixup> &Fixups,
  355. const MCSubtargetInfo &STI) const {
  356. const MCOperand &MO = MI.getOperand(OpNo);
  357. assert((MI.getOpcode() == PPC::MTOCRF || MI.getOpcode() == PPC::MTOCRF8 ||
  358. MI.getOpcode() == PPC::MFOCRF || MI.getOpcode() == PPC::MFOCRF8) &&
  359. (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7));
  360. return 0x80 >> CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
  361. }
  362. // Get the index for this operand in this instruction. This is needed for
  363. // computing the register number in PPCInstrInfo::getRegNumForOperand() for
  364. // any instructions that use a different numbering scheme for registers in
  365. // different operands.
  366. static unsigned getOpIdxForMO(const MCInst &MI, const MCOperand &MO) {
  367. for (unsigned i = 0; i < MI.getNumOperands(); i++) {
  368. const MCOperand &Op = MI.getOperand(i);
  369. if (&Op == &MO)
  370. return i;
  371. }
  372. llvm_unreachable("This operand is not part of this instruction");
  373. return ~0U; // Silence any warnings about no return.
  374. }
  375. uint64_t PPCMCCodeEmitter::
  376. getMachineOpValue(const MCInst &MI, const MCOperand &MO,
  377. SmallVectorImpl<MCFixup> &Fixups,
  378. const MCSubtargetInfo &STI) const {
  379. if (MO.isReg()) {
  380. // MTOCRF/MFOCRF should go through get_crbitm_encoding for the CR operand.
  381. // The GPR operand should come through here though.
  382. assert((MI.getOpcode() != PPC::MTOCRF && MI.getOpcode() != PPC::MTOCRF8 &&
  383. MI.getOpcode() != PPC::MFOCRF && MI.getOpcode() != PPC::MFOCRF8) ||
  384. MO.getReg() < PPC::CR0 || MO.getReg() > PPC::CR7);
  385. unsigned OpNo = getOpIdxForMO(MI, MO);
  386. unsigned Reg =
  387. PPCInstrInfo::getRegNumForOperand(MCII.get(MI.getOpcode()),
  388. MO.getReg(), OpNo);
  389. return CTX.getRegisterInfo()->getEncodingValue(Reg);
  390. }
  391. assert(MO.isImm() &&
  392. "Relocation required in an instruction that we cannot encode!");
  393. return MO.getImm();
  394. }
  395. void PPCMCCodeEmitter::encodeInstruction(
  396. const MCInst &MI, raw_ostream &OS, SmallVectorImpl<MCFixup> &Fixups,
  397. const MCSubtargetInfo &STI) const {
  398. verifyInstructionPredicates(MI,
  399. computeAvailableFeatures(STI.getFeatureBits()));
  400. uint64_t Bits = getBinaryCodeForInstr(MI, Fixups, STI);
  401. // Output the constant in big/little endian byte order.
  402. unsigned Size = getInstSizeInBytes(MI);
  403. support::endianness E = IsLittleEndian ? support::little : support::big;
  404. switch (Size) {
  405. case 0:
  406. break;
  407. case 4:
  408. support::endian::write<uint32_t>(OS, Bits, E);
  409. break;
  410. case 8:
  411. // If we emit a pair of instructions, the first one is
  412. // always in the top 32 bits, even on little-endian.
  413. support::endian::write<uint32_t>(OS, Bits >> 32, E);
  414. support::endian::write<uint32_t>(OS, Bits, E);
  415. break;
  416. default:
  417. llvm_unreachable("Invalid instruction size");
  418. }
  419. ++MCNumEmitted; // Keep track of the # of mi's emitted.
  420. }
  421. // Get the number of bytes used to encode the given MCInst.
  422. unsigned PPCMCCodeEmitter::getInstSizeInBytes(const MCInst &MI) const {
  423. unsigned Opcode = MI.getOpcode();
  424. const MCInstrDesc &Desc = MCII.get(Opcode);
  425. return Desc.getSize();
  426. }
  427. bool PPCMCCodeEmitter::isPrefixedInstruction(const MCInst &MI) const {
  428. unsigned Opcode = MI.getOpcode();
  429. const PPCInstrInfo *InstrInfo = static_cast<const PPCInstrInfo*>(&MCII);
  430. return InstrInfo->isPrefixed(Opcode);
  431. }
  432. #define ENABLE_INSTR_PREDICATE_VERIFIER
  433. #include "PPCGenMCCodeEmitter.inc"