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; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 19 | class MCOperand; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 20 | class MCInstrInfo; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 21 | class MCInstrAnalysis; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 39 | struct AliasMatchingData; |
| 40 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 41 | /// This is an instance of a target assembly language printer that |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 42 | /// converts an MCInst to valid target assembly syntax. |
| 43 | class MCInstPrinter { |
| 44 | protected: |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 45 | /// A stream that comments can be emitted to if desired. Each comment |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 46 | /// 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] | 47 | /// is disabled. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 48 | raw_ostream *CommentStream = nullptr; |
| 49 | const MCAsmInfo &MAI; |
| 50 | const MCInstrInfo &MII; |
| 51 | const MCRegisterInfo &MRI; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 52 | const MCInstrAnalysis *MIA = nullptr; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 53 | |
| 54 | /// True if we are printing marked up assembly. |
| 55 | bool UseMarkup = false; |
| 56 | |
| 57 | /// True if we are printing immediates as hex. |
| 58 | bool PrintImmHex = false; |
| 59 | |
| 60 | /// Which style to use for printing hexadecimal values. |
| 61 | HexStyle::Style PrintHexStyle = HexStyle::C; |
| 62 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 63 | /// If true, a branch immediate (e.g. bl 4) will be printed as a hexadecimal |
| 64 | /// address (e.g. bl 0x20004). This is useful for a stream disassembler |
| 65 | /// (llvm-objdump -d). |
| 66 | bool PrintBranchImmAsAddress = false; |
| 67 | |
| 68 | /// If true, symbolize branch target and memory reference operands. |
| 69 | bool SymbolizeOperands = false; |
| 70 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 71 | /// Utility function for printing annotations. |
| 72 | void printAnnotation(raw_ostream &OS, StringRef Annot); |
| 73 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 74 | /// Helper for matching MCInsts to alias patterns when printing instructions. |
| 75 | const char *matchAliasPatterns(const MCInst *MI, const MCSubtargetInfo *STI, |
| 76 | const AliasMatchingData &M); |
| 77 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 78 | public: |
| 79 | MCInstPrinter(const MCAsmInfo &mai, const MCInstrInfo &mii, |
| 80 | const MCRegisterInfo &mri) : MAI(mai), MII(mii), MRI(mri) {} |
| 81 | |
| 82 | virtual ~MCInstPrinter(); |
| 83 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 84 | /// Customize the printer according to a command line option. |
| 85 | /// @return true if the option is recognized and applied. |
| 86 | virtual bool applyTargetSpecificCLOption(StringRef Opt) { return false; } |
| 87 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 88 | /// Specify a stream to emit comments to. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 89 | void setCommentStream(raw_ostream &OS) { CommentStream = &OS; } |
| 90 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 91 | /// Returns a pair containing the mnemonic for \p MI and the number of bits |
| 92 | /// left for further processing by printInstruction (generated by tablegen). |
| 93 | virtual std::pair<const char *, uint64_t> getMnemonic(const MCInst *MI) = 0; |
| 94 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 95 | /// Print the specified MCInst to the specified raw_ostream. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 96 | /// |
| 97 | /// \p Address the address of current instruction on most targets, used to |
| 98 | /// print a PC relative immediate as the target address. On targets where a PC |
| 99 | /// relative immediate is relative to the next instruction and the length of a |
| 100 | /// MCInst is difficult to measure (e.g. x86), this is the address of the next |
| 101 | /// instruction. If Address is 0, the immediate will be printed. |
| 102 | virtual void printInst(const MCInst *MI, uint64_t Address, StringRef Annot, |
| 103 | const MCSubtargetInfo &STI, raw_ostream &OS) = 0; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 104 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 105 | /// Return the name of the specified opcode enum (e.g. "MOV32ri") or |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 106 | /// empty if we can't resolve it. |
| 107 | StringRef getOpcodeName(unsigned Opcode) const; |
| 108 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 109 | /// Print the assembler register name. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 110 | virtual void printRegName(raw_ostream &OS, unsigned RegNo) const; |
| 111 | |
| 112 | bool getUseMarkup() const { return UseMarkup; } |
| 113 | void setUseMarkup(bool Value) { UseMarkup = Value; } |
| 114 | |
| 115 | /// Utility functions to make adding mark ups simpler. |
| 116 | StringRef markup(StringRef s) const; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 117 | |
| 118 | bool getPrintImmHex() const { return PrintImmHex; } |
| 119 | void setPrintImmHex(bool Value) { PrintImmHex = Value; } |
| 120 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 121 | void setPrintHexStyle(HexStyle::Style Value) { PrintHexStyle = Value; } |
| 122 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 123 | void setPrintBranchImmAsAddress(bool Value) { |
| 124 | PrintBranchImmAsAddress = Value; |
| 125 | } |
| 126 | |
| 127 | void setSymbolizeOperands(bool Value) { SymbolizeOperands = Value; } |
| 128 | void setMCInstrAnalysis(const MCInstrAnalysis *Value) { MIA = Value; } |
| 129 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 130 | /// Utility function to print immediates in decimal or hex. |
| 131 | format_object<int64_t> formatImm(int64_t Value) const { |
| 132 | return PrintImmHex ? formatHex(Value) : formatDec(Value); |
| 133 | } |
| 134 | |
| 135 | /// Utility functions to print decimal/hexadecimal values. |
| 136 | format_object<int64_t> formatDec(int64_t Value) const; |
| 137 | format_object<int64_t> formatHex(int64_t Value) const; |
| 138 | format_object<uint64_t> formatHex(uint64_t Value) const; |
| 139 | }; |
| 140 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 141 | /// Map from opcode to pattern list by binary search. |
| 142 | struct PatternsForOpcode { |
| 143 | uint32_t Opcode; |
| 144 | uint16_t PatternStart; |
| 145 | uint16_t NumPatterns; |
| 146 | }; |
| 147 | |
| 148 | /// Data for each alias pattern. Includes feature bits, string, number of |
| 149 | /// operands, and a variadic list of conditions to check. |
| 150 | struct AliasPattern { |
| 151 | uint32_t AsmStrOffset; |
| 152 | uint32_t AliasCondStart; |
| 153 | uint8_t NumOperands; |
| 154 | uint8_t NumConds; |
| 155 | }; |
| 156 | |
| 157 | struct AliasPatternCond { |
| 158 | enum CondKind : uint8_t { |
| 159 | K_Feature, // Match only if a feature is enabled. |
| 160 | K_NegFeature, // Match only if a feature is disabled. |
| 161 | K_OrFeature, // Match only if one of a set of features is enabled. |
| 162 | K_OrNegFeature, // Match only if one of a set of features is disabled. |
| 163 | K_EndOrFeatures, // Note end of list of K_Or(Neg)?Features. |
| 164 | K_Ignore, // Match any operand. |
| 165 | K_Reg, // Match a specific register. |
| 166 | K_TiedReg, // Match another already matched register. |
| 167 | K_Imm, // Match a specific immediate. |
| 168 | K_RegClass, // Match registers in a class. |
| 169 | K_Custom, // Call custom matcher by index. |
| 170 | }; |
| 171 | |
| 172 | CondKind Kind; |
| 173 | uint32_t Value; |
| 174 | }; |
| 175 | |
| 176 | /// Tablegenerated data structures needed to match alias patterns. |
| 177 | struct AliasMatchingData { |
| 178 | ArrayRef<PatternsForOpcode> OpToPatterns; |
| 179 | ArrayRef<AliasPattern> Patterns; |
| 180 | ArrayRef<AliasPatternCond> PatternConds; |
| 181 | StringRef AsmStrings; |
| 182 | bool (*ValidateMCOperand)(const MCOperand &MCOp, const MCSubtargetInfo &STI, |
| 183 | unsigned PredicateIndex); |
| 184 | }; |
| 185 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 186 | } // end namespace llvm |
| 187 | |
| 188 | #endif // LLVM_MC_MCINSTPRINTER_H |