LoongArchInstPrinter.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //===- LoongArchInstPrinter.cpp - Convert LoongArch MCInst to asm 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 LoongArch MCInst to a .s file.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "LoongArchInstPrinter.h"
  13. #include "LoongArchBaseInfo.h"
  14. #include "LoongArchMCTargetDesc.h"
  15. #include "llvm/MC/MCAsmInfo.h"
  16. #include "llvm/MC/MCInst.h"
  17. #include "llvm/MC/MCRegisterInfo.h"
  18. #include "llvm/MC/MCSubtargetInfo.h"
  19. #include "llvm/MC/MCSymbol.h"
  20. #include "llvm/Support/CommandLine.h"
  21. using namespace llvm;
  22. #define DEBUG_TYPE "loongarch-asm-printer"
  23. // Include the auto-generated portion of the assembly writer.
  24. #define PRINT_ALIAS_INSTR
  25. #include "LoongArchGenAsmWriter.inc"
  26. static cl::opt<bool>
  27. NumericReg("loongarch-numeric-reg",
  28. cl::desc("Print numeric register names rather than the ABI "
  29. "names (such as $r0 instead of $zero)"),
  30. cl::init(false), cl::Hidden);
  31. // The command-line flag above is used by llvm-mc and llc. It can be used by
  32. // `llvm-objdump`, but we override the value here to handle options passed to
  33. // `llvm-objdump` with `-M` (which matches GNU objdump). There did not seem to
  34. // be an easier way to allow these options in all these tools, without doing it
  35. // this way.
  36. bool LoongArchInstPrinter::applyTargetSpecificCLOption(StringRef Opt) {
  37. if (Opt == "numeric") {
  38. NumericReg = true;
  39. return true;
  40. }
  41. return false;
  42. }
  43. void LoongArchInstPrinter::printInst(const MCInst *MI, uint64_t Address,
  44. StringRef Annot,
  45. const MCSubtargetInfo &STI,
  46. raw_ostream &O) {
  47. if (!printAliasInstr(MI, Address, STI, O))
  48. printInstruction(MI, Address, STI, O);
  49. printAnnotation(O, Annot);
  50. }
  51. void LoongArchInstPrinter::printRegName(raw_ostream &O, MCRegister Reg) const {
  52. O << '$' << getRegisterName(Reg);
  53. }
  54. void LoongArchInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
  55. const MCSubtargetInfo &STI,
  56. raw_ostream &O) {
  57. const MCOperand &MO = MI->getOperand(OpNo);
  58. if (MO.isReg()) {
  59. printRegName(O, MO.getReg());
  60. return;
  61. }
  62. if (MO.isImm()) {
  63. O << MO.getImm();
  64. return;
  65. }
  66. assert(MO.isExpr() && "Unknown operand kind in printOperand");
  67. MO.getExpr()->print(O, &MAI);
  68. }
  69. void LoongArchInstPrinter::printAtomicMemOp(const MCInst *MI, unsigned OpNo,
  70. const MCSubtargetInfo &STI,
  71. raw_ostream &O) {
  72. const MCOperand &MO = MI->getOperand(OpNo);
  73. assert(MO.isReg() && "printAtomicMemOp can only print register operands");
  74. printRegName(O, MO.getReg());
  75. }
  76. const char *LoongArchInstPrinter::getRegisterName(MCRegister Reg) {
  77. // Default print reg alias name
  78. return getRegisterName(Reg, NumericReg ? LoongArch::NoRegAltName
  79. : LoongArch::RegAliasName);
  80. }