PPCInstPrinter.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. //===-- PPCInstPrinter.cpp - Convert PPC MCInst to assembly syntax --------===//
  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 class prints an PPC MCInst to a .s file.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "MCTargetDesc/PPCInstPrinter.h"
  13. #include "MCTargetDesc/PPCMCTargetDesc.h"
  14. #include "MCTargetDesc/PPCPredicates.h"
  15. #include "PPCInstrInfo.h"
  16. #include "llvm/CodeGen/TargetOpcodes.h"
  17. #include "llvm/MC/MCExpr.h"
  18. #include "llvm/MC/MCInst.h"
  19. #include "llvm/MC/MCInstrInfo.h"
  20. #include "llvm/MC/MCRegisterInfo.h"
  21. #include "llvm/MC/MCSubtargetInfo.h"
  22. #include "llvm/MC/MCSymbol.h"
  23. #include "llvm/Support/CommandLine.h"
  24. #include "llvm/Support/raw_ostream.h"
  25. using namespace llvm;
  26. #define DEBUG_TYPE "asm-printer"
  27. // FIXME: Once the integrated assembler supports full register names, tie this
  28. // to the verbose-asm setting.
  29. static cl::opt<bool>
  30. FullRegNames("ppc-asm-full-reg-names", cl::Hidden, cl::init(false),
  31. cl::desc("Use full register names when printing assembly"));
  32. // Useful for testing purposes. Prints vs{31-63} as v{0-31} respectively.
  33. static cl::opt<bool>
  34. ShowVSRNumsAsVR("ppc-vsr-nums-as-vr", cl::Hidden, cl::init(false),
  35. cl::desc("Prints full register names with vs{31-63} as v{0-31}"));
  36. // Prints full register names with percent symbol.
  37. static cl::opt<bool>
  38. FullRegNamesWithPercent("ppc-reg-with-percent-prefix", cl::Hidden,
  39. cl::init(false),
  40. cl::desc("Prints full register names with percent"));
  41. #define PRINT_ALIAS_INSTR
  42. #include "PPCGenAsmWriter.inc"
  43. void PPCInstPrinter::printRegName(raw_ostream &OS, MCRegister Reg) const {
  44. const char *RegName = getRegisterName(Reg);
  45. OS << RegName;
  46. }
  47. void PPCInstPrinter::printInst(const MCInst *MI, uint64_t Address,
  48. StringRef Annot, const MCSubtargetInfo &STI,
  49. raw_ostream &O) {
  50. // Customize printing of the addis instruction on AIX. When an operand is a
  51. // symbol reference, the instruction syntax is changed to look like a load
  52. // operation, i.e:
  53. // Transform: addis $rD, $rA, $src --> addis $rD, $src($rA).
  54. if (TT.isOSAIX() &&
  55. (MI->getOpcode() == PPC::ADDIS8 || MI->getOpcode() == PPC::ADDIS) &&
  56. MI->getOperand(2).isExpr()) {
  57. assert((MI->getOperand(0).isReg() && MI->getOperand(1).isReg()) &&
  58. "The first and the second operand of an addis instruction"
  59. " should be registers.");
  60. assert(isa<MCSymbolRefExpr>(MI->getOperand(2).getExpr()) &&
  61. "The third operand of an addis instruction should be a symbol "
  62. "reference expression if it is an expression at all.");
  63. O << "\taddis ";
  64. printOperand(MI, 0, STI, O);
  65. O << ", ";
  66. printOperand(MI, 2, STI, O);
  67. O << "(";
  68. printOperand(MI, 1, STI, O);
  69. O << ")";
  70. return;
  71. }
  72. // Check if the last operand is an expression with the variant kind
  73. // VK_PPC_PCREL_OPT. If this is the case then this is a linker optimization
  74. // relocation and the .reloc directive needs to be added.
  75. unsigned LastOp = MI->getNumOperands() - 1;
  76. if (MI->getNumOperands() > 1) {
  77. const MCOperand &Operand = MI->getOperand(LastOp);
  78. if (Operand.isExpr()) {
  79. const MCExpr *Expr = Operand.getExpr();
  80. const MCSymbolRefExpr *SymExpr =
  81. static_cast<const MCSymbolRefExpr *>(Expr);
  82. if (SymExpr && SymExpr->getKind() == MCSymbolRefExpr::VK_PPC_PCREL_OPT) {
  83. const MCSymbol &Symbol = SymExpr->getSymbol();
  84. if (MI->getOpcode() == PPC::PLDpc) {
  85. printInstruction(MI, Address, STI, O);
  86. O << "\n";
  87. Symbol.print(O, &MAI);
  88. O << ":";
  89. return;
  90. } else {
  91. O << "\t.reloc ";
  92. Symbol.print(O, &MAI);
  93. O << "-8,R_PPC64_PCREL_OPT,.-(";
  94. Symbol.print(O, &MAI);
  95. O << "-8)\n";
  96. }
  97. }
  98. }
  99. }
  100. // Check for slwi/srwi mnemonics.
  101. if (MI->getOpcode() == PPC::RLWINM) {
  102. unsigned char SH = MI->getOperand(2).getImm();
  103. unsigned char MB = MI->getOperand(3).getImm();
  104. unsigned char ME = MI->getOperand(4).getImm();
  105. bool useSubstituteMnemonic = false;
  106. if (SH <= 31 && MB == 0 && ME == (31-SH)) {
  107. O << "\tslwi "; useSubstituteMnemonic = true;
  108. }
  109. if (SH <= 31 && MB == (32-SH) && ME == 31) {
  110. O << "\tsrwi "; useSubstituteMnemonic = true;
  111. SH = 32-SH;
  112. }
  113. if (useSubstituteMnemonic) {
  114. printOperand(MI, 0, STI, O);
  115. O << ", ";
  116. printOperand(MI, 1, STI, O);
  117. O << ", " << (unsigned int)SH;
  118. printAnnotation(O, Annot);
  119. return;
  120. }
  121. }
  122. if (MI->getOpcode() == PPC::RLDICR ||
  123. MI->getOpcode() == PPC::RLDICR_32) {
  124. unsigned char SH = MI->getOperand(2).getImm();
  125. unsigned char ME = MI->getOperand(3).getImm();
  126. // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
  127. if (63-SH == ME) {
  128. O << "\tsldi ";
  129. printOperand(MI, 0, STI, O);
  130. O << ", ";
  131. printOperand(MI, 1, STI, O);
  132. O << ", " << (unsigned int)SH;
  133. printAnnotation(O, Annot);
  134. return;
  135. }
  136. }
  137. // dcbt[st] is printed manually here because:
  138. // 1. The assembly syntax is different between embedded and server targets
  139. // 2. We must print the short mnemonics for TH == 0 because the
  140. // embedded/server syntax default will not be stable across assemblers
  141. // The syntax for dcbt is:
  142. // dcbt ra, rb, th [server]
  143. // dcbt th, ra, rb [embedded]
  144. // where th can be omitted when it is 0. dcbtst is the same.
  145. // On AIX, only emit the extended mnemonics for dcbt and dcbtst if
  146. // the "modern assembler" is available.
  147. if ((MI->getOpcode() == PPC::DCBT || MI->getOpcode() == PPC::DCBTST) &&
  148. (!TT.isOSAIX() || STI.getFeatureBits()[PPC::FeatureModernAIXAs])) {
  149. unsigned char TH = MI->getOperand(0).getImm();
  150. O << "\tdcbt";
  151. if (MI->getOpcode() == PPC::DCBTST)
  152. O << "st";
  153. if (TH == 16)
  154. O << "t";
  155. O << " ";
  156. bool IsBookE = STI.getFeatureBits()[PPC::FeatureBookE];
  157. if (IsBookE && TH != 0 && TH != 16)
  158. O << (unsigned int) TH << ", ";
  159. printOperand(MI, 1, STI, O);
  160. O << ", ";
  161. printOperand(MI, 2, STI, O);
  162. if (!IsBookE && TH != 0 && TH != 16)
  163. O << ", " << (unsigned int) TH;
  164. printAnnotation(O, Annot);
  165. return;
  166. }
  167. if (MI->getOpcode() == PPC::DCBF) {
  168. unsigned char L = MI->getOperand(0).getImm();
  169. if (!L || L == 1 || L == 3 || L == 4 || L == 6) {
  170. O << "\tdcb";
  171. if (L != 6)
  172. O << "f";
  173. if (L == 1)
  174. O << "l";
  175. if (L == 3)
  176. O << "lp";
  177. if (L == 4)
  178. O << "ps";
  179. if (L == 6)
  180. O << "stps";
  181. O << " ";
  182. printOperand(MI, 1, STI, O);
  183. O << ", ";
  184. printOperand(MI, 2, STI, O);
  185. printAnnotation(O, Annot);
  186. return;
  187. }
  188. }
  189. if (!printAliasInstr(MI, Address, STI, O))
  190. printInstruction(MI, Address, STI, O);
  191. printAnnotation(O, Annot);
  192. }
  193. void PPCInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
  194. const MCSubtargetInfo &STI,
  195. raw_ostream &O,
  196. const char *Modifier) {
  197. unsigned Code = MI->getOperand(OpNo).getImm();
  198. if (StringRef(Modifier) == "cc") {
  199. switch ((PPC::Predicate)Code) {
  200. case PPC::PRED_LT_MINUS:
  201. case PPC::PRED_LT_PLUS:
  202. case PPC::PRED_LT:
  203. O << "lt";
  204. return;
  205. case PPC::PRED_LE_MINUS:
  206. case PPC::PRED_LE_PLUS:
  207. case PPC::PRED_LE:
  208. O << "le";
  209. return;
  210. case PPC::PRED_EQ_MINUS:
  211. case PPC::PRED_EQ_PLUS:
  212. case PPC::PRED_EQ:
  213. O << "eq";
  214. return;
  215. case PPC::PRED_GE_MINUS:
  216. case PPC::PRED_GE_PLUS:
  217. case PPC::PRED_GE:
  218. O << "ge";
  219. return;
  220. case PPC::PRED_GT_MINUS:
  221. case PPC::PRED_GT_PLUS:
  222. case PPC::PRED_GT:
  223. O << "gt";
  224. return;
  225. case PPC::PRED_NE_MINUS:
  226. case PPC::PRED_NE_PLUS:
  227. case PPC::PRED_NE:
  228. O << "ne";
  229. return;
  230. case PPC::PRED_UN_MINUS:
  231. case PPC::PRED_UN_PLUS:
  232. case PPC::PRED_UN:
  233. O << "un";
  234. return;
  235. case PPC::PRED_NU_MINUS:
  236. case PPC::PRED_NU_PLUS:
  237. case PPC::PRED_NU:
  238. O << "nu";
  239. return;
  240. case PPC::PRED_BIT_SET:
  241. case PPC::PRED_BIT_UNSET:
  242. llvm_unreachable("Invalid use of bit predicate code");
  243. }
  244. llvm_unreachable("Invalid predicate code");
  245. }
  246. if (StringRef(Modifier) == "pm") {
  247. switch ((PPC::Predicate)Code) {
  248. case PPC::PRED_LT:
  249. case PPC::PRED_LE:
  250. case PPC::PRED_EQ:
  251. case PPC::PRED_GE:
  252. case PPC::PRED_GT:
  253. case PPC::PRED_NE:
  254. case PPC::PRED_UN:
  255. case PPC::PRED_NU:
  256. return;
  257. case PPC::PRED_LT_MINUS:
  258. case PPC::PRED_LE_MINUS:
  259. case PPC::PRED_EQ_MINUS:
  260. case PPC::PRED_GE_MINUS:
  261. case PPC::PRED_GT_MINUS:
  262. case PPC::PRED_NE_MINUS:
  263. case PPC::PRED_UN_MINUS:
  264. case PPC::PRED_NU_MINUS:
  265. O << "-";
  266. return;
  267. case PPC::PRED_LT_PLUS:
  268. case PPC::PRED_LE_PLUS:
  269. case PPC::PRED_EQ_PLUS:
  270. case PPC::PRED_GE_PLUS:
  271. case PPC::PRED_GT_PLUS:
  272. case PPC::PRED_NE_PLUS:
  273. case PPC::PRED_UN_PLUS:
  274. case PPC::PRED_NU_PLUS:
  275. O << "+";
  276. return;
  277. case PPC::PRED_BIT_SET:
  278. case PPC::PRED_BIT_UNSET:
  279. llvm_unreachable("Invalid use of bit predicate code");
  280. }
  281. llvm_unreachable("Invalid predicate code");
  282. }
  283. assert(StringRef(Modifier) == "reg" &&
  284. "Need to specify 'cc', 'pm' or 'reg' as predicate op modifier!");
  285. printOperand(MI, OpNo + 1, STI, O);
  286. }
  287. void PPCInstPrinter::printATBitsAsHint(const MCInst *MI, unsigned OpNo,
  288. const MCSubtargetInfo &STI,
  289. raw_ostream &O) {
  290. unsigned Code = MI->getOperand(OpNo).getImm();
  291. if (Code == 2)
  292. O << "-";
  293. else if (Code == 3)
  294. O << "+";
  295. }
  296. void PPCInstPrinter::printU1ImmOperand(const MCInst *MI, unsigned OpNo,
  297. const MCSubtargetInfo &STI,
  298. raw_ostream &O) {
  299. unsigned int Value = MI->getOperand(OpNo).getImm();
  300. assert(Value <= 1 && "Invalid u1imm argument!");
  301. O << (unsigned int)Value;
  302. }
  303. void PPCInstPrinter::printU2ImmOperand(const MCInst *MI, unsigned OpNo,
  304. const MCSubtargetInfo &STI,
  305. raw_ostream &O) {
  306. unsigned int Value = MI->getOperand(OpNo).getImm();
  307. assert(Value <= 3 && "Invalid u2imm argument!");
  308. O << (unsigned int)Value;
  309. }
  310. void PPCInstPrinter::printU3ImmOperand(const MCInst *MI, unsigned OpNo,
  311. const MCSubtargetInfo &STI,
  312. raw_ostream &O) {
  313. unsigned int Value = MI->getOperand(OpNo).getImm();
  314. assert(Value <= 8 && "Invalid u3imm argument!");
  315. O << (unsigned int)Value;
  316. }
  317. void PPCInstPrinter::printU4ImmOperand(const MCInst *MI, unsigned OpNo,
  318. const MCSubtargetInfo &STI,
  319. raw_ostream &O) {
  320. unsigned int Value = MI->getOperand(OpNo).getImm();
  321. assert(Value <= 15 && "Invalid u4imm argument!");
  322. O << (unsigned int)Value;
  323. }
  324. void PPCInstPrinter::printS5ImmOperand(const MCInst *MI, unsigned OpNo,
  325. const MCSubtargetInfo &STI,
  326. raw_ostream &O) {
  327. int Value = MI->getOperand(OpNo).getImm();
  328. Value = SignExtend32<5>(Value);
  329. O << (int)Value;
  330. }
  331. void PPCInstPrinter::printImmZeroOperand(const MCInst *MI, unsigned OpNo,
  332. const MCSubtargetInfo &STI,
  333. raw_ostream &O) {
  334. unsigned int Value = MI->getOperand(OpNo).getImm();
  335. assert(Value == 0 && "Operand must be zero");
  336. O << (unsigned int)Value;
  337. }
  338. void PPCInstPrinter::printU5ImmOperand(const MCInst *MI, unsigned OpNo,
  339. const MCSubtargetInfo &STI,
  340. raw_ostream &O) {
  341. unsigned int Value = MI->getOperand(OpNo).getImm();
  342. assert(Value <= 31 && "Invalid u5imm argument!");
  343. O << (unsigned int)Value;
  344. }
  345. void PPCInstPrinter::printU6ImmOperand(const MCInst *MI, unsigned OpNo,
  346. const MCSubtargetInfo &STI,
  347. raw_ostream &O) {
  348. unsigned int Value = MI->getOperand(OpNo).getImm();
  349. assert(Value <= 63 && "Invalid u6imm argument!");
  350. O << (unsigned int)Value;
  351. }
  352. void PPCInstPrinter::printU7ImmOperand(const MCInst *MI, unsigned OpNo,
  353. const MCSubtargetInfo &STI,
  354. raw_ostream &O) {
  355. unsigned int Value = MI->getOperand(OpNo).getImm();
  356. assert(Value <= 127 && "Invalid u7imm argument!");
  357. O << (unsigned int)Value;
  358. }
  359. // Operands of BUILD_VECTOR are signed and we use this to print operands
  360. // of XXSPLTIB which are unsigned. So we simply truncate to 8 bits and
  361. // print as unsigned.
  362. void PPCInstPrinter::printU8ImmOperand(const MCInst *MI, unsigned OpNo,
  363. const MCSubtargetInfo &STI,
  364. raw_ostream &O) {
  365. unsigned char Value = MI->getOperand(OpNo).getImm();
  366. O << (unsigned int)Value;
  367. }
  368. void PPCInstPrinter::printU10ImmOperand(const MCInst *MI, unsigned OpNo,
  369. const MCSubtargetInfo &STI,
  370. raw_ostream &O) {
  371. unsigned short Value = MI->getOperand(OpNo).getImm();
  372. assert(Value <= 1023 && "Invalid u10imm argument!");
  373. O << (unsigned short)Value;
  374. }
  375. void PPCInstPrinter::printU12ImmOperand(const MCInst *MI, unsigned OpNo,
  376. const MCSubtargetInfo &STI,
  377. raw_ostream &O) {
  378. unsigned short Value = MI->getOperand(OpNo).getImm();
  379. assert(Value <= 4095 && "Invalid u12imm argument!");
  380. O << (unsigned short)Value;
  381. }
  382. void PPCInstPrinter::printS16ImmOperand(const MCInst *MI, unsigned OpNo,
  383. const MCSubtargetInfo &STI,
  384. raw_ostream &O) {
  385. if (MI->getOperand(OpNo).isImm())
  386. O << (short)MI->getOperand(OpNo).getImm();
  387. else
  388. printOperand(MI, OpNo, STI, O);
  389. }
  390. void PPCInstPrinter::printS34ImmOperand(const MCInst *MI, unsigned OpNo,
  391. const MCSubtargetInfo &STI,
  392. raw_ostream &O) {
  393. if (MI->getOperand(OpNo).isImm()) {
  394. long long Value = MI->getOperand(OpNo).getImm();
  395. assert(isInt<34>(Value) && "Invalid s34imm argument!");
  396. O << (long long)Value;
  397. }
  398. else
  399. printOperand(MI, OpNo, STI, O);
  400. }
  401. void PPCInstPrinter::printU16ImmOperand(const MCInst *MI, unsigned OpNo,
  402. const MCSubtargetInfo &STI,
  403. raw_ostream &O) {
  404. if (MI->getOperand(OpNo).isImm())
  405. O << (unsigned short)MI->getOperand(OpNo).getImm();
  406. else
  407. printOperand(MI, OpNo, STI, O);
  408. }
  409. void PPCInstPrinter::printBranchOperand(const MCInst *MI, uint64_t Address,
  410. unsigned OpNo,
  411. const MCSubtargetInfo &STI,
  412. raw_ostream &O) {
  413. if (!MI->getOperand(OpNo).isImm())
  414. return printOperand(MI, OpNo, STI, O);
  415. int32_t Imm = SignExtend32<32>((unsigned)MI->getOperand(OpNo).getImm() << 2);
  416. if (PrintBranchImmAsAddress) {
  417. uint64_t Target = Address + Imm;
  418. if (!TT.isPPC64())
  419. Target &= 0xffffffff;
  420. O << formatHex(Target);
  421. } else {
  422. // Branches can take an immediate operand. This is used by the branch
  423. // selection pass to print, for example `.+8` (for ELF) or `$+8` (for AIX)
  424. // to express an eight byte displacement from the program counter.
  425. if (!TT.isOSAIX())
  426. O << ".";
  427. else
  428. O << "$";
  429. if (Imm >= 0)
  430. O << "+";
  431. O << Imm;
  432. }
  433. }
  434. void PPCInstPrinter::printAbsBranchOperand(const MCInst *MI, unsigned OpNo,
  435. const MCSubtargetInfo &STI,
  436. raw_ostream &O) {
  437. if (!MI->getOperand(OpNo).isImm())
  438. return printOperand(MI, OpNo, STI, O);
  439. O << SignExtend32<32>((unsigned)MI->getOperand(OpNo).getImm() << 2);
  440. }
  441. void PPCInstPrinter::printcrbitm(const MCInst *MI, unsigned OpNo,
  442. const MCSubtargetInfo &STI, raw_ostream &O) {
  443. unsigned CCReg = MI->getOperand(OpNo).getReg();
  444. unsigned RegNo;
  445. switch (CCReg) {
  446. default: llvm_unreachable("Unknown CR register");
  447. case PPC::CR0: RegNo = 0; break;
  448. case PPC::CR1: RegNo = 1; break;
  449. case PPC::CR2: RegNo = 2; break;
  450. case PPC::CR3: RegNo = 3; break;
  451. case PPC::CR4: RegNo = 4; break;
  452. case PPC::CR5: RegNo = 5; break;
  453. case PPC::CR6: RegNo = 6; break;
  454. case PPC::CR7: RegNo = 7; break;
  455. }
  456. O << (0x80 >> RegNo);
  457. }
  458. void PPCInstPrinter::printMemRegImm(const MCInst *MI, unsigned OpNo,
  459. const MCSubtargetInfo &STI,
  460. raw_ostream &O) {
  461. printS16ImmOperand(MI, OpNo, STI, O);
  462. O << '(';
  463. if (MI->getOperand(OpNo+1).getReg() == PPC::R0)
  464. O << "0";
  465. else
  466. printOperand(MI, OpNo + 1, STI, O);
  467. O << ')';
  468. }
  469. void PPCInstPrinter::printMemRegImmHash(const MCInst *MI, unsigned OpNo,
  470. const MCSubtargetInfo &STI,
  471. raw_ostream &O) {
  472. O << MI->getOperand(OpNo).getImm();
  473. O << '(';
  474. printOperand(MI, OpNo + 1, STI, O);
  475. O << ')';
  476. }
  477. void PPCInstPrinter::printMemRegImm34PCRel(const MCInst *MI, unsigned OpNo,
  478. const MCSubtargetInfo &STI,
  479. raw_ostream &O) {
  480. printS34ImmOperand(MI, OpNo, STI, O);
  481. O << '(';
  482. printImmZeroOperand(MI, OpNo + 1, STI, O);
  483. O << ')';
  484. }
  485. void PPCInstPrinter::printMemRegImm34(const MCInst *MI, unsigned OpNo,
  486. const MCSubtargetInfo &STI,
  487. raw_ostream &O) {
  488. printS34ImmOperand(MI, OpNo, STI, O);
  489. O << '(';
  490. printOperand(MI, OpNo + 1, STI, O);
  491. O << ')';
  492. }
  493. void PPCInstPrinter::printMemRegReg(const MCInst *MI, unsigned OpNo,
  494. const MCSubtargetInfo &STI,
  495. raw_ostream &O) {
  496. // When used as the base register, r0 reads constant zero rather than
  497. // the value contained in the register. For this reason, the darwin
  498. // assembler requires that we print r0 as 0 (no r) when used as the base.
  499. if (MI->getOperand(OpNo).getReg() == PPC::R0)
  500. O << "0";
  501. else
  502. printOperand(MI, OpNo, STI, O);
  503. O << ", ";
  504. printOperand(MI, OpNo + 1, STI, O);
  505. }
  506. void PPCInstPrinter::printTLSCall(const MCInst *MI, unsigned OpNo,
  507. const MCSubtargetInfo &STI, raw_ostream &O) {
  508. // On PPC64, VariantKind is VK_None, but on PPC32, it's VK_PLT, and it must
  509. // come at the _end_ of the expression.
  510. const MCOperand &Op = MI->getOperand(OpNo);
  511. const MCSymbolRefExpr *RefExp = nullptr;
  512. const MCConstantExpr *ConstExp = nullptr;
  513. if (const MCBinaryExpr *BinExpr = dyn_cast<MCBinaryExpr>(Op.getExpr())) {
  514. RefExp = cast<MCSymbolRefExpr>(BinExpr->getLHS());
  515. ConstExp = cast<MCConstantExpr>(BinExpr->getRHS());
  516. } else
  517. RefExp = cast<MCSymbolRefExpr>(Op.getExpr());
  518. O << RefExp->getSymbol().getName();
  519. // The variant kind VK_PPC_NOTOC needs to be handled as a special case
  520. // because we do not want the assembly to print out the @notoc at the
  521. // end like __tls_get_addr(x@tlsgd)@notoc. Instead we want it to look
  522. // like __tls_get_addr@notoc(x@tlsgd).
  523. if (RefExp->getKind() == MCSymbolRefExpr::VK_PPC_NOTOC)
  524. O << '@' << MCSymbolRefExpr::getVariantKindName(RefExp->getKind());
  525. O << '(';
  526. printOperand(MI, OpNo + 1, STI, O);
  527. O << ')';
  528. if (RefExp->getKind() != MCSymbolRefExpr::VK_None &&
  529. RefExp->getKind() != MCSymbolRefExpr::VK_PPC_NOTOC)
  530. O << '@' << MCSymbolRefExpr::getVariantKindName(RefExp->getKind());
  531. if (ConstExp != nullptr)
  532. O << '+' << ConstExp->getValue();
  533. }
  534. /// showRegistersWithPercentPrefix - Check if this register name should be
  535. /// printed with a percentage symbol as prefix.
  536. bool PPCInstPrinter::showRegistersWithPercentPrefix(const char *RegName) const {
  537. if (!FullRegNamesWithPercent || TT.getOS() == Triple::AIX)
  538. return false;
  539. switch (RegName[0]) {
  540. default:
  541. return false;
  542. case 'r':
  543. case 'f':
  544. case 'q':
  545. case 'v':
  546. case 'c':
  547. return true;
  548. }
  549. }
  550. /// getVerboseConditionalRegName - This method expands the condition register
  551. /// when requested explicitly or targetting Darwin.
  552. const char *PPCInstPrinter::getVerboseConditionRegName(unsigned RegNum,
  553. unsigned RegEncoding)
  554. const {
  555. if (!FullRegNames)
  556. return nullptr;
  557. if (RegNum < PPC::CR0EQ || RegNum > PPC::CR7UN)
  558. return nullptr;
  559. const char *CRBits[] = {
  560. "lt", "gt", "eq", "un",
  561. "4*cr1+lt", "4*cr1+gt", "4*cr1+eq", "4*cr1+un",
  562. "4*cr2+lt", "4*cr2+gt", "4*cr2+eq", "4*cr2+un",
  563. "4*cr3+lt", "4*cr3+gt", "4*cr3+eq", "4*cr3+un",
  564. "4*cr4+lt", "4*cr4+gt", "4*cr4+eq", "4*cr4+un",
  565. "4*cr5+lt", "4*cr5+gt", "4*cr5+eq", "4*cr5+un",
  566. "4*cr6+lt", "4*cr6+gt", "4*cr6+eq", "4*cr6+un",
  567. "4*cr7+lt", "4*cr7+gt", "4*cr7+eq", "4*cr7+un"
  568. };
  569. return CRBits[RegEncoding];
  570. }
  571. // showRegistersWithPrefix - This method determines whether registers
  572. // should be number-only or include the prefix.
  573. bool PPCInstPrinter::showRegistersWithPrefix() const {
  574. return FullRegNamesWithPercent || FullRegNames;
  575. }
  576. void PPCInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
  577. const MCSubtargetInfo &STI, raw_ostream &O) {
  578. const MCOperand &Op = MI->getOperand(OpNo);
  579. if (Op.isReg()) {
  580. unsigned Reg = Op.getReg();
  581. if (!ShowVSRNumsAsVR)
  582. Reg = PPCInstrInfo::getRegNumForOperand(MII.get(MI->getOpcode()),
  583. Reg, OpNo);
  584. const char *RegName;
  585. RegName = getVerboseConditionRegName(Reg, MRI.getEncodingValue(Reg));
  586. if (RegName == nullptr)
  587. RegName = getRegisterName(Reg);
  588. if (showRegistersWithPercentPrefix(RegName))
  589. O << "%";
  590. if (!showRegistersWithPrefix())
  591. RegName = PPCRegisterInfo::stripRegisterPrefix(RegName);
  592. O << RegName;
  593. return;
  594. }
  595. if (Op.isImm()) {
  596. O << Op.getImm();
  597. return;
  598. }
  599. assert(Op.isExpr() && "unknown operand kind in printOperand");
  600. Op.getExpr()->print(O, &MAI);
  601. }