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