MCInstPrinter.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //===- MCInstPrinter.h - MCInst to target assembly syntax -------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef LLVM_MC_MCINSTPRINTER_H
  10. #define LLVM_MC_MCINSTPRINTER_H
  11. #include "llvm/ADT/ArrayRef.h"
  12. #include "llvm/Support/DataTypes.h"
  13. #include "llvm/Support/Format.h"
  14. namespace llvm {
  15. class MCInst;
  16. class raw_ostream;
  17. class MCAsmInfo;
  18. class MCInstrInfo;
  19. class MCRegisterInfo;
  20. class MCSubtargetInfo;
  21. class StringRef;
  22. /// Convert `Bytes' to a hex string and output to `OS'
  23. void dumpBytes(ArrayRef<uint8_t> Bytes, raw_ostream &OS);
  24. namespace HexStyle {
  25. enum Style {
  26. C, ///< 0xff
  27. Asm ///< 0ffh
  28. };
  29. }
  30. /// \brief This is an instance of a target assembly language printer that
  31. /// converts an MCInst to valid target assembly syntax.
  32. class MCInstPrinter {
  33. protected:
  34. /// \brief A stream that comments can be emitted to if desired. Each comment
  35. /// must end with a newline. This will be null if verbose assembly emission
  36. /// is disable.
  37. raw_ostream *CommentStream;
  38. const MCAsmInfo &MAI;
  39. const MCInstrInfo &MII;
  40. const MCRegisterInfo &MRI;
  41. /// True if we are printing marked up assembly.
  42. bool UseMarkup;
  43. /// True if we are printing immediates as hex.
  44. bool PrintImmHex;
  45. /// Which style to use for printing hexadecimal values.
  46. HexStyle::Style PrintHexStyle;
  47. /// Utility function for printing annotations.
  48. void printAnnotation(raw_ostream &OS, StringRef Annot);
  49. public:
  50. MCInstPrinter(const MCAsmInfo &mai, const MCInstrInfo &mii,
  51. const MCRegisterInfo &mri)
  52. : CommentStream(nullptr), MAI(mai), MII(mii), MRI(mri), UseMarkup(0),
  53. PrintImmHex(0), PrintHexStyle(HexStyle::C) {}
  54. virtual ~MCInstPrinter();
  55. /// \brief Specify a stream to emit comments to.
  56. void setCommentStream(raw_ostream &OS) { CommentStream = &OS; }
  57. /// \brief Print the specified MCInst to the specified raw_ostream.
  58. virtual void printInst(const MCInst *MI, raw_ostream &OS, StringRef Annot,
  59. const MCSubtargetInfo &STI) = 0;
  60. /// \brief Return the name of the specified opcode enum (e.g. "MOV32ri") or
  61. /// empty if we can't resolve it.
  62. StringRef getOpcodeName(unsigned Opcode) const;
  63. /// \brief Print the assembler register name.
  64. virtual void printRegName(raw_ostream &OS, unsigned RegNo) const;
  65. bool getUseMarkup() const { return UseMarkup; }
  66. void setUseMarkup(bool Value) { UseMarkup = Value; }
  67. /// Utility functions to make adding mark ups simpler.
  68. StringRef markup(StringRef s) const;
  69. StringRef markup(StringRef a, StringRef b) const;
  70. bool getPrintImmHex() const { return PrintImmHex; }
  71. void setPrintImmHex(bool Value) { PrintImmHex = Value; }
  72. HexStyle::Style getPrintHexStyle() const { return PrintHexStyle; }
  73. void setPrintHexStyle(HexStyle::Style Value) { PrintHexStyle = Value; }
  74. /// Utility function to print immediates in decimal or hex.
  75. format_object<int64_t> formatImm(int64_t Value) const {
  76. return PrintImmHex ? formatHex(Value) : formatDec(Value);
  77. }
  78. /// Utility functions to print decimal/hexadecimal values.
  79. format_object<int64_t> formatDec(int64_t Value) const;
  80. format_object<int64_t> formatHex(int64_t Value) const;
  81. format_object<uint64_t> formatHex(uint64_t Value) const;
  82. };
  83. } // namespace llvm
  84. #endif