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