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