PPCInstPrinter.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  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, unsigned RegNo) const {
  44. const char *RegName = getRegisterName(RegNo);
  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. if (MI->getOpcode() == PPC::DCBT || MI->getOpcode() == PPC::DCBTST) {
  146. unsigned char TH = MI->getOperand(0).getImm();
  147. O << "\tdcbt";
  148. if (MI->getOpcode() == PPC::DCBTST)
  149. O << "st";
  150. if (TH == 16)
  151. O << "t";
  152. O << " ";
  153. bool IsBookE = STI.getFeatureBits()[PPC::FeatureBookE];
  154. if (IsBookE && TH != 0 && TH != 16)
  155. O << (unsigned int) TH << ", ";
  156. printOperand(MI, 1, STI, O);
  157. O << ", ";
  158. printOperand(MI, 2, STI, O);
  159. if (!IsBookE && TH != 0 && TH != 16)
  160. O << ", " << (unsigned int) TH;
  161. printAnnotation(O, Annot);
  162. return;
  163. }
  164. if (MI->getOpcode() == PPC::DCBF) {
  165. unsigned char L = MI->getOperand(0).getImm();
  166. if (!L || L == 1 || L == 3 || L == 4 || L == 6) {
  167. O << "\tdcb";
  168. if (L != 6)
  169. O << "f";
  170. if (L == 1)
  171. O << "l";
  172. if (L == 3)
  173. O << "lp";
  174. if (L == 4)
  175. O << "ps";
  176. if (L == 6)
  177. O << "stps";
  178. O << " ";
  179. printOperand(MI, 1, STI, O);
  180. O << ", ";
  181. printOperand(MI, 2, STI, O);
  182. printAnnotation(O, Annot);
  183. return;
  184. }
  185. }
  186. if (!printAliasInstr(MI, Address, STI, O))
  187. printInstruction(MI, Address, STI, O);
  188. printAnnotation(O, Annot);
  189. }
  190. void PPCInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
  191. const MCSubtargetInfo &STI,
  192. raw_ostream &O,
  193. const char *Modifier) {
  194. unsigned Code = MI->getOperand(OpNo).getImm();
  195. if (StringRef(Modifier) == "cc") {
  196. switch ((PPC::Predicate)Code) {
  197. case PPC::PRED_LT_MINUS:
  198. case PPC::PRED_LT_PLUS:
  199. case PPC::PRED_LT:
  200. O << "lt";
  201. return;
  202. case PPC::PRED_LE_MINUS:
  203. case PPC::PRED_LE_PLUS:
  204. case PPC::PRED_LE:
  205. O << "le";
  206. return;
  207. case PPC::PRED_EQ_MINUS:
  208. case PPC::PRED_EQ_PLUS:
  209. case PPC::PRED_EQ:
  210. O << "eq";
  211. return;
  212. case PPC::PRED_GE_MINUS:
  213. case PPC::PRED_GE_PLUS:
  214. case PPC::PRED_GE:
  215. O << "ge";
  216. return;
  217. case PPC::PRED_GT_MINUS:
  218. case PPC::PRED_GT_PLUS:
  219. case PPC::PRED_GT:
  220. O << "gt";
  221. return;
  222. case PPC::PRED_NE_MINUS:
  223. case PPC::PRED_NE_PLUS:
  224. case PPC::PRED_NE:
  225. O << "ne";
  226. return;
  227. case PPC::PRED_UN_MINUS:
  228. case PPC::PRED_UN_PLUS:
  229. case PPC::PRED_UN:
  230. O << "un";
  231. return;
  232. case PPC::PRED_NU_MINUS:
  233. case PPC::PRED_NU_PLUS:
  234. case PPC::PRED_NU:
  235. O << "nu";
  236. return;
  237. case PPC::PRED_BIT_SET:
  238. case PPC::PRED_BIT_UNSET:
  239. llvm_unreachable("Invalid use of bit predicate code");
  240. }
  241. llvm_unreachable("Invalid predicate code");
  242. }
  243. if (StringRef(Modifier) == "pm") {
  244. switch ((PPC::Predicate)Code) {
  245. case PPC::PRED_LT:
  246. case PPC::PRED_LE:
  247. case PPC::PRED_EQ:
  248. case PPC::PRED_GE:
  249. case PPC::PRED_GT:
  250. case PPC::PRED_NE:
  251. case PPC::PRED_UN:
  252. case PPC::PRED_NU:
  253. return;
  254. case PPC::PRED_LT_MINUS:
  255. case PPC::PRED_LE_MINUS:
  256. case PPC::PRED_EQ_MINUS:
  257. case PPC::PRED_GE_MINUS:
  258. case PPC::PRED_GT_MINUS:
  259. case PPC::PRED_NE_MINUS:
  260. case PPC::PRED_UN_MINUS:
  261. case PPC::PRED_NU_MINUS:
  262. O << "-";
  263. return;
  264. case PPC::PRED_LT_PLUS:
  265. case PPC::PRED_LE_PLUS:
  266. case PPC::PRED_EQ_PLUS:
  267. case PPC::PRED_GE_PLUS:
  268. case PPC::PRED_GT_PLUS:
  269. case PPC::PRED_NE_PLUS:
  270. case PPC::PRED_UN_PLUS:
  271. case PPC::PRED_NU_PLUS:
  272. O << "+";
  273. return;
  274. case PPC::PRED_BIT_SET:
  275. case PPC::PRED_BIT_UNSET:
  276. llvm_unreachable("Invalid use of bit predicate code");
  277. }
  278. llvm_unreachable("Invalid predicate code");
  279. }
  280. assert(StringRef(Modifier) == "reg" &&
  281. "Need to specify 'cc', 'pm' or 'reg' as predicate op modifier!");
  282. printOperand(MI, OpNo + 1, STI, O);
  283. }
  284. void PPCInstPrinter::printATBitsAsHint(const MCInst *MI, unsigned OpNo,
  285. const MCSubtargetInfo &STI,
  286. raw_ostream &O) {
  287. unsigned Code = MI->getOperand(OpNo).getImm();
  288. if (Code == 2)
  289. O << "-";
  290. else if (Code == 3)
  291. O << "+";
  292. }
  293. void PPCInstPrinter::printU1ImmOperand(const MCInst *MI, unsigned OpNo,
  294. const MCSubtargetInfo &STI,
  295. raw_ostream &O) {
  296. unsigned int Value = MI->getOperand(OpNo).getImm();
  297. assert(Value <= 1 && "Invalid u1imm argument!");
  298. O << (unsigned int)Value;
  299. }
  300. void PPCInstPrinter::printU2ImmOperand(const MCInst *MI, unsigned OpNo,
  301. const MCSubtargetInfo &STI,
  302. raw_ostream &O) {
  303. unsigned int Value = MI->getOperand(OpNo).getImm();
  304. assert(Value <= 3 && "Invalid u2imm argument!");
  305. O << (unsigned int)Value;
  306. }
  307. void PPCInstPrinter::printU3ImmOperand(const MCInst *MI, unsigned OpNo,
  308. const MCSubtargetInfo &STI,
  309. raw_ostream &O) {
  310. unsigned int Value = MI->getOperand(OpNo).getImm();
  311. assert(Value <= 8 && "Invalid u3imm argument!");
  312. O << (unsigned int)Value;
  313. }
  314. void PPCInstPrinter::printU4ImmOperand(const MCInst *MI, unsigned OpNo,
  315. const MCSubtargetInfo &STI,
  316. raw_ostream &O) {
  317. unsigned int Value = MI->getOperand(OpNo).getImm();
  318. assert(Value <= 15 && "Invalid u4imm argument!");
  319. O << (unsigned int)Value;
  320. }
  321. void PPCInstPrinter::printS5ImmOperand(const MCInst *MI, unsigned OpNo,
  322. const MCSubtargetInfo &STI,
  323. raw_ostream &O) {
  324. int Value = MI->getOperand(OpNo).getImm();
  325. Value = SignExtend32<5>(Value);
  326. O << (int)Value;
  327. }
  328. void PPCInstPrinter::printImmZeroOperand(const MCInst *MI, unsigned OpNo,
  329. const MCSubtargetInfo &STI,
  330. raw_ostream &O) {
  331. unsigned int Value = MI->getOperand(OpNo).getImm();
  332. assert(Value == 0 && "Operand must be zero");
  333. O << (unsigned int)Value;
  334. }
  335. void PPCInstPrinter::printU5ImmOperand(const MCInst *MI, unsigned OpNo,
  336. const MCSubtargetInfo &STI,
  337. raw_ostream &O) {
  338. unsigned int Value = MI->getOperand(OpNo).getImm();
  339. assert(Value <= 31 && "Invalid u5imm argument!");
  340. O << (unsigned int)Value;
  341. }
  342. void PPCInstPrinter::printU6ImmOperand(const MCInst *MI, unsigned OpNo,
  343. const MCSubtargetInfo &STI,
  344. raw_ostream &O) {
  345. unsigned int Value = MI->getOperand(OpNo).getImm();
  346. assert(Value <= 63 && "Invalid u6imm argument!");
  347. O << (unsigned int)Value;
  348. }
  349. void PPCInstPrinter::printU7ImmOperand(const MCInst *MI, unsigned OpNo,
  350. const MCSubtargetInfo &STI,
  351. raw_ostream &O) {
  352. unsigned int Value = MI->getOperand(OpNo).getImm();
  353. assert(Value <= 127 && "Invalid u7imm argument!");
  354. O << (unsigned int)Value;
  355. }
  356. // Operands of BUILD_VECTOR are signed and we use this to print operands
  357. // of XXSPLTIB which are unsigned. So we simply truncate to 8 bits and
  358. // print as unsigned.
  359. void PPCInstPrinter::printU8ImmOperand(const MCInst *MI, unsigned OpNo,
  360. const MCSubtargetInfo &STI,
  361. raw_ostream &O) {
  362. unsigned char Value = MI->getOperand(OpNo).getImm();
  363. O << (unsigned int)Value;
  364. }
  365. void PPCInstPrinter::printU10ImmOperand(const MCInst *MI, unsigned OpNo,
  366. const MCSubtargetInfo &STI,
  367. raw_ostream &O) {
  368. unsigned short Value = MI->getOperand(OpNo).getImm();
  369. assert(Value <= 1023 && "Invalid u10imm argument!");
  370. O << (unsigned short)Value;
  371. }
  372. void PPCInstPrinter::printU12ImmOperand(const MCInst *MI, unsigned OpNo,
  373. const MCSubtargetInfo &STI,
  374. raw_ostream &O) {
  375. unsigned short Value = MI->getOperand(OpNo).getImm();
  376. assert(Value <= 4095 && "Invalid u12imm argument!");
  377. O << (unsigned short)Value;
  378. }
  379. void PPCInstPrinter::printS16ImmOperand(const MCInst *MI, unsigned OpNo,
  380. const MCSubtargetInfo &STI,
  381. raw_ostream &O) {
  382. if (MI->getOperand(OpNo).isImm())
  383. O << (short)MI->getOperand(OpNo).getImm();
  384. else
  385. printOperand(MI, OpNo, STI, O);
  386. }
  387. void PPCInstPrinter::printS34ImmOperand(const MCInst *MI, unsigned OpNo,
  388. const MCSubtargetInfo &STI,
  389. raw_ostream &O) {
  390. if (MI->getOperand(OpNo).isImm()) {
  391. long long Value = MI->getOperand(OpNo).getImm();
  392. assert(isInt<34>(Value) && "Invalid s34imm argument!");
  393. O << (long long)Value;
  394. }
  395. else
  396. printOperand(MI, OpNo, STI, O);
  397. }
  398. void PPCInstPrinter::printU16ImmOperand(const MCInst *MI, unsigned OpNo,
  399. const MCSubtargetInfo &STI,
  400. raw_ostream &O) {
  401. if (MI->getOperand(OpNo).isImm())
  402. O << (unsigned short)MI->getOperand(OpNo).getImm();
  403. else
  404. printOperand(MI, OpNo, STI, O);
  405. }
  406. void PPCInstPrinter::printBranchOperand(const MCInst *MI, uint64_t Address,
  407. unsigned OpNo,
  408. const MCSubtargetInfo &STI,
  409. raw_ostream &O) {
  410. if (!MI->getOperand(OpNo).isImm())
  411. return printOperand(MI, OpNo, STI, O);
  412. int32_t Imm = SignExtend32<32>((unsigned)MI->getOperand(OpNo).getImm() << 2);
  413. if (PrintBranchImmAsAddress) {
  414. uint64_t Target = Address + Imm;
  415. if (!TT.isPPC64())
  416. Target &= 0xffffffff;
  417. O << formatHex(Target);
  418. } else {
  419. // Branches can take an immediate operand. This is used by the branch
  420. // selection pass to print, for example `.+8` (for ELF) or `$+8` (for AIX)
  421. // to express an eight byte displacement from the program counter.
  422. if (!TT.isOSAIX())
  423. O << ".";
  424. else
  425. O << "$";
  426. if (Imm >= 0)
  427. O << "+";
  428. O << Imm;
  429. }
  430. }
  431. void PPCInstPrinter::printAbsBranchOperand(const MCInst *MI, unsigned OpNo,
  432. const MCSubtargetInfo &STI,
  433. raw_ostream &O) {
  434. if (!MI->getOperand(OpNo).isImm())
  435. return printOperand(MI, OpNo, STI, O);
  436. O << SignExtend32<32>((unsigned)MI->getOperand(OpNo).getImm() << 2);
  437. }
  438. void PPCInstPrinter::printcrbitm(const MCInst *MI, unsigned OpNo,
  439. const MCSubtargetInfo &STI, raw_ostream &O) {
  440. unsigned CCReg = MI->getOperand(OpNo).getReg();
  441. unsigned RegNo;
  442. switch (CCReg) {
  443. default: llvm_unreachable("Unknown CR register");
  444. case PPC::CR0: RegNo = 0; break;
  445. case PPC::CR1: RegNo = 1; break;
  446. case PPC::CR2: RegNo = 2; break;
  447. case PPC::CR3: RegNo = 3; break;
  448. case PPC::CR4: RegNo = 4; break;
  449. case PPC::CR5: RegNo = 5; break;
  450. case PPC::CR6: RegNo = 6; break;
  451. case PPC::CR7: RegNo = 7; break;
  452. }
  453. O << (0x80 >> RegNo);
  454. }
  455. void PPCInstPrinter::printMemRegImm(const MCInst *MI, unsigned OpNo,
  456. const MCSubtargetInfo &STI,
  457. raw_ostream &O) {
  458. printS16ImmOperand(MI, OpNo, STI, O);
  459. O << '(';
  460. if (MI->getOperand(OpNo+1).getReg() == PPC::R0)
  461. O << "0";
  462. else
  463. printOperand(MI, OpNo + 1, STI, O);
  464. O << ')';
  465. }
  466. void PPCInstPrinter::printMemRegImm34PCRel(const MCInst *MI, unsigned OpNo,
  467. const MCSubtargetInfo &STI,
  468. raw_ostream &O) {
  469. printS34ImmOperand(MI, OpNo, STI, O);
  470. O << '(';
  471. printImmZeroOperand(MI, OpNo + 1, STI, O);
  472. O << ')';
  473. }
  474. void PPCInstPrinter::printMemRegImm34(const MCInst *MI, unsigned OpNo,
  475. const MCSubtargetInfo &STI,
  476. raw_ostream &O) {
  477. printS34ImmOperand(MI, OpNo, STI, O);
  478. O << '(';
  479. printOperand(MI, OpNo + 1, STI, O);
  480. O << ')';
  481. }
  482. void PPCInstPrinter::printMemRegReg(const MCInst *MI, unsigned OpNo,
  483. const MCSubtargetInfo &STI,
  484. raw_ostream &O) {
  485. // When used as the base register, r0 reads constant zero rather than
  486. // the value contained in the register. For this reason, the darwin
  487. // assembler requires that we print r0 as 0 (no r) when used as the base.
  488. if (MI->getOperand(OpNo).getReg() == PPC::R0)
  489. O << "0";
  490. else
  491. printOperand(MI, OpNo, STI, O);
  492. O << ", ";
  493. printOperand(MI, OpNo + 1, STI, O);
  494. }
  495. void PPCInstPrinter::printTLSCall(const MCInst *MI, unsigned OpNo,
  496. const MCSubtargetInfo &STI, raw_ostream &O) {
  497. // On PPC64, VariantKind is VK_None, but on PPC32, it's VK_PLT, and it must
  498. // come at the _end_ of the expression.
  499. const MCOperand &Op = MI->getOperand(OpNo);
  500. const MCSymbolRefExpr *RefExp = nullptr;
  501. const MCConstantExpr *ConstExp = nullptr;
  502. if (const MCBinaryExpr *BinExpr = dyn_cast<MCBinaryExpr>(Op.getExpr())) {
  503. RefExp = cast<MCSymbolRefExpr>(BinExpr->getLHS());
  504. ConstExp = cast<MCConstantExpr>(BinExpr->getRHS());
  505. } else
  506. RefExp = cast<MCSymbolRefExpr>(Op.getExpr());
  507. O << RefExp->getSymbol().getName();
  508. // The variant kind VK_PPC_NOTOC needs to be handled as a special case
  509. // because we do not want the assembly to print out the @notoc at the
  510. // end like __tls_get_addr(x@tlsgd)@notoc. Instead we want it to look
  511. // like __tls_get_addr@notoc(x@tlsgd).
  512. if (RefExp->getKind() == MCSymbolRefExpr::VK_PPC_NOTOC)
  513. O << '@' << MCSymbolRefExpr::getVariantKindName(RefExp->getKind());
  514. O << '(';
  515. printOperand(MI, OpNo + 1, STI, O);
  516. O << ')';
  517. if (RefExp->getKind() != MCSymbolRefExpr::VK_None &&
  518. RefExp->getKind() != MCSymbolRefExpr::VK_PPC_NOTOC)
  519. O << '@' << MCSymbolRefExpr::getVariantKindName(RefExp->getKind());
  520. if (ConstExp != nullptr)
  521. O << '+' << ConstExp->getValue();
  522. }
  523. /// showRegistersWithPercentPrefix - Check if this register name should be
  524. /// printed with a percentage symbol as prefix.
  525. bool PPCInstPrinter::showRegistersWithPercentPrefix(const char *RegName) const {
  526. if (!FullRegNamesWithPercent || TT.getOS() == Triple::AIX)
  527. return false;
  528. switch (RegName[0]) {
  529. default:
  530. return false;
  531. case 'r':
  532. case 'f':
  533. case 'q':
  534. case 'v':
  535. case 'c':
  536. return true;
  537. }
  538. }
  539. /// getVerboseConditionalRegName - This method expands the condition register
  540. /// when requested explicitly or targetting Darwin.
  541. const char *PPCInstPrinter::getVerboseConditionRegName(unsigned RegNum,
  542. unsigned RegEncoding)
  543. const {
  544. if (!FullRegNames)
  545. return nullptr;
  546. if (RegNum < PPC::CR0EQ || RegNum > PPC::CR7UN)
  547. return nullptr;
  548. const char *CRBits[] = {
  549. "lt", "gt", "eq", "un",
  550. "4*cr1+lt", "4*cr1+gt", "4*cr1+eq", "4*cr1+un",
  551. "4*cr2+lt", "4*cr2+gt", "4*cr2+eq", "4*cr2+un",
  552. "4*cr3+lt", "4*cr3+gt", "4*cr3+eq", "4*cr3+un",
  553. "4*cr4+lt", "4*cr4+gt", "4*cr4+eq", "4*cr4+un",
  554. "4*cr5+lt", "4*cr5+gt", "4*cr5+eq", "4*cr5+un",
  555. "4*cr6+lt", "4*cr6+gt", "4*cr6+eq", "4*cr6+un",
  556. "4*cr7+lt", "4*cr7+gt", "4*cr7+eq", "4*cr7+un"
  557. };
  558. return CRBits[RegEncoding];
  559. }
  560. // showRegistersWithPrefix - This method determines whether registers
  561. // should be number-only or include the prefix.
  562. bool PPCInstPrinter::showRegistersWithPrefix() const {
  563. if (TT.getOS() == Triple::AIX)
  564. return false;
  565. return FullRegNamesWithPercent || FullRegNames;
  566. }
  567. void PPCInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
  568. const MCSubtargetInfo &STI, raw_ostream &O) {
  569. const MCOperand &Op = MI->getOperand(OpNo);
  570. if (Op.isReg()) {
  571. unsigned Reg = Op.getReg();
  572. if (!ShowVSRNumsAsVR)
  573. Reg = PPCInstrInfo::getRegNumForOperand(MII.get(MI->getOpcode()),
  574. Reg, OpNo);
  575. const char *RegName;
  576. RegName = getVerboseConditionRegName(Reg, MRI.getEncodingValue(Reg));
  577. if (RegName == nullptr)
  578. RegName = getRegisterName(Reg);
  579. if (showRegistersWithPercentPrefix(RegName))
  580. O << "%";
  581. if (!showRegistersWithPrefix())
  582. RegName = PPCRegisterInfo::stripRegisterPrefix(RegName);
  583. O << RegName;
  584. return;
  585. }
  586. if (Op.isImm()) {
  587. O << Op.getImm();
  588. return;
  589. }
  590. assert(Op.isExpr() && "unknown operand kind in printOperand");
  591. Op.getExpr()->print(O, &MAI);
  592. }