BPFInstPrinter.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //===-- BPFInstPrinter.cpp - Convert BPF 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 BPF MCInst to a .s file.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "MCTargetDesc/BPFInstPrinter.h"
  13. #include "llvm/MC/MCAsmInfo.h"
  14. #include "llvm/MC/MCExpr.h"
  15. #include "llvm/MC/MCInst.h"
  16. #include "llvm/MC/MCSymbol.h"
  17. #include "llvm/Support/ErrorHandling.h"
  18. #include "llvm/Support/FormattedStream.h"
  19. using namespace llvm;
  20. #define DEBUG_TYPE "asm-printer"
  21. // Include the auto-generated portion of the assembly writer.
  22. #include "BPFGenAsmWriter.inc"
  23. void BPFInstPrinter::printInst(const MCInst *MI, uint64_t Address,
  24. StringRef Annot, const MCSubtargetInfo &STI,
  25. raw_ostream &O) {
  26. printInstruction(MI, Address, O);
  27. printAnnotation(O, Annot);
  28. }
  29. static void printExpr(const MCExpr *Expr, raw_ostream &O) {
  30. #ifndef NDEBUG
  31. const MCSymbolRefExpr *SRE;
  32. if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr))
  33. SRE = dyn_cast<MCSymbolRefExpr>(BE->getLHS());
  34. else
  35. SRE = dyn_cast<MCSymbolRefExpr>(Expr);
  36. assert(SRE && "Unexpected MCExpr type.");
  37. MCSymbolRefExpr::VariantKind Kind = SRE->getKind();
  38. assert(Kind == MCSymbolRefExpr::VK_None);
  39. #endif
  40. O << *Expr;
  41. }
  42. void BPFInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
  43. raw_ostream &O, const char *Modifier) {
  44. assert((Modifier == nullptr || Modifier[0] == 0) && "No modifiers supported");
  45. const MCOperand &Op = MI->getOperand(OpNo);
  46. if (Op.isReg()) {
  47. O << getRegisterName(Op.getReg());
  48. } else if (Op.isImm()) {
  49. O << formatImm((int32_t)Op.getImm());
  50. } else {
  51. assert(Op.isExpr() && "Expected an expression");
  52. printExpr(Op.getExpr(), O);
  53. }
  54. }
  55. void BPFInstPrinter::printMemOperand(const MCInst *MI, int OpNo, raw_ostream &O,
  56. const char *Modifier) {
  57. const MCOperand &RegOp = MI->getOperand(OpNo);
  58. const MCOperand &OffsetOp = MI->getOperand(OpNo + 1);
  59. // register
  60. assert(RegOp.isReg() && "Register operand not a register");
  61. O << getRegisterName(RegOp.getReg());
  62. // offset
  63. if (OffsetOp.isImm()) {
  64. auto Imm = OffsetOp.getImm();
  65. if (Imm >= 0)
  66. O << " + " << formatImm(Imm);
  67. else
  68. O << " - " << formatImm(-Imm);
  69. } else {
  70. assert(0 && "Expected an immediate");
  71. }
  72. }
  73. void BPFInstPrinter::printImm64Operand(const MCInst *MI, unsigned OpNo,
  74. raw_ostream &O) {
  75. const MCOperand &Op = MI->getOperand(OpNo);
  76. if (Op.isImm())
  77. O << formatImm(Op.getImm());
  78. else if (Op.isExpr())
  79. printExpr(Op.getExpr(), O);
  80. else
  81. O << Op;
  82. }
  83. void BPFInstPrinter::printBrTargetOperand(const MCInst *MI, unsigned OpNo,
  84. raw_ostream &O) {
  85. const MCOperand &Op = MI->getOperand(OpNo);
  86. if (Op.isImm()) {
  87. int16_t Imm = Op.getImm();
  88. O << ((Imm >= 0) ? "+" : "") << formatImm(Imm);
  89. } else if (Op.isExpr()) {
  90. printExpr(Op.getExpr(), O);
  91. } else {
  92. O << Op;
  93. }
  94. }