File indexing completed on 2026-05-10 08:44:13
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef LLVM_MC_MCINSTPRINTER_H
0010 #define LLVM_MC_MCINSTPRINTER_H
0011
0012 #include "llvm/ADT/SmallVector.h"
0013 #include "llvm/Support/Compiler.h"
0014 #include "llvm/Support/Format.h"
0015 #include "llvm/Support/raw_ostream.h"
0016 #include <cstdint>
0017
0018 namespace llvm {
0019
0020 class MCAsmInfo;
0021 class MCInst;
0022 class MCInstrAnalysis;
0023 class MCInstrInfo;
0024 class MCOperand;
0025 class MCRegister;
0026 class MCRegisterInfo;
0027 class MCSubtargetInfo;
0028 class StringRef;
0029
0030
0031 void dumpBytes(ArrayRef<uint8_t> Bytes, raw_ostream &OS);
0032
0033 namespace HexStyle {
0034
0035 enum Style {
0036 C,
0037 Asm
0038 };
0039
0040 }
0041
0042 struct AliasMatchingData;
0043
0044
0045
0046 class MCInstPrinter {
0047 protected:
0048
0049
0050
0051 raw_ostream *CommentStream = nullptr;
0052 const MCAsmInfo &MAI;
0053 const MCInstrInfo &MII;
0054 const MCRegisterInfo &MRI;
0055 const MCInstrAnalysis *MIA = nullptr;
0056
0057
0058 bool UseMarkup = false;
0059
0060
0061 bool UseColor = false;
0062
0063
0064 bool PrintAliases = true;
0065
0066
0067 bool PrintImmHex = false;
0068
0069
0070 HexStyle::Style PrintHexStyle = HexStyle::C;
0071
0072
0073
0074
0075 bool PrintBranchImmAsAddress = false;
0076
0077
0078 bool SymbolizeOperands = false;
0079
0080 SmallVector<raw_ostream::Colors, 4> ColorStack{raw_ostream::Colors::RESET};
0081
0082
0083 void printAnnotation(raw_ostream &OS, StringRef Annot);
0084
0085
0086 const char *matchAliasPatterns(const MCInst *MI, const MCSubtargetInfo *STI,
0087 const AliasMatchingData &M);
0088
0089 public:
0090 MCInstPrinter(const MCAsmInfo &mai, const MCInstrInfo &mii,
0091 const MCRegisterInfo &mri) : MAI(mai), MII(mii), MRI(mri) {}
0092
0093 virtual ~MCInstPrinter();
0094
0095 enum class Markup {
0096 Immediate,
0097 Register,
0098 Target,
0099 Memory,
0100 };
0101
0102 class WithMarkup {
0103 public:
0104 LLVM_CTOR_NODISCARD WithMarkup(MCInstPrinter &IP, raw_ostream &OS, Markup M,
0105 bool EnableMarkup, bool EnableColor);
0106 ~WithMarkup();
0107
0108 template <typename T> WithMarkup &operator<<(T &O) {
0109 OS << O;
0110 return *this;
0111 }
0112
0113 template <typename T> WithMarkup &operator<<(const T &O) {
0114 OS << O;
0115 return *this;
0116 }
0117
0118 private:
0119 MCInstPrinter &IP;
0120 raw_ostream &OS;
0121 bool EnableMarkup;
0122 bool EnableColor;
0123 };
0124
0125
0126
0127 virtual bool applyTargetSpecificCLOption(StringRef Opt) { return false; }
0128
0129
0130 void setCommentStream(raw_ostream &OS) { CommentStream = &OS; }
0131
0132
0133
0134 virtual std::pair<const char *, uint64_t>
0135 getMnemonic(const MCInst &MI) const = 0;
0136
0137
0138
0139
0140
0141
0142
0143
0144 virtual void printInst(const MCInst *MI, uint64_t Address, StringRef Annot,
0145 const MCSubtargetInfo &STI, raw_ostream &OS) = 0;
0146
0147
0148
0149 StringRef getOpcodeName(unsigned Opcode) const;
0150
0151
0152 virtual void printRegName(raw_ostream &OS, MCRegister Reg);
0153
0154 bool getUseMarkup() const { return UseMarkup; }
0155 void setUseMarkup(bool Value) { UseMarkup = Value; }
0156
0157 bool getUseColor() const { return UseColor; }
0158 void setUseColor(bool Value) { UseColor = Value; }
0159
0160 WithMarkup markup(raw_ostream &OS, Markup M);
0161
0162 bool getPrintImmHex() const { return PrintImmHex; }
0163 void setPrintImmHex(bool Value) { PrintImmHex = Value; }
0164
0165 void setPrintHexStyle(HexStyle::Style Value) { PrintHexStyle = Value; }
0166
0167 void setPrintBranchImmAsAddress(bool Value) {
0168 PrintBranchImmAsAddress = Value;
0169 }
0170
0171 void setSymbolizeOperands(bool Value) { SymbolizeOperands = Value; }
0172 void setMCInstrAnalysis(const MCInstrAnalysis *Value) { MIA = Value; }
0173
0174
0175 format_object<int64_t> formatImm(int64_t Value) const {
0176 return PrintImmHex ? formatHex(Value) : formatDec(Value);
0177 }
0178
0179
0180 format_object<int64_t> formatDec(int64_t Value) const;
0181 format_object<int64_t> formatHex(int64_t Value) const;
0182 format_object<uint64_t> formatHex(uint64_t Value) const;
0183 };
0184
0185
0186 struct PatternsForOpcode {
0187 uint32_t Opcode;
0188 uint16_t PatternStart;
0189 uint16_t NumPatterns;
0190 };
0191
0192
0193
0194 struct AliasPattern {
0195 uint32_t AsmStrOffset;
0196 uint32_t AliasCondStart;
0197 uint8_t NumOperands;
0198 uint8_t NumConds;
0199 };
0200
0201 struct AliasPatternCond {
0202 enum CondKind : uint8_t {
0203 K_Feature,
0204 K_NegFeature,
0205 K_OrFeature,
0206 K_OrNegFeature,
0207 K_EndOrFeatures,
0208 K_Ignore,
0209 K_Reg,
0210 K_TiedReg,
0211 K_Imm,
0212 K_RegClass,
0213 K_Custom,
0214 };
0215
0216 CondKind Kind;
0217 uint32_t Value;
0218 };
0219
0220
0221 struct AliasMatchingData {
0222 ArrayRef<PatternsForOpcode> OpToPatterns;
0223 ArrayRef<AliasPattern> Patterns;
0224 ArrayRef<AliasPatternCond> PatternConds;
0225 StringRef AsmStrings;
0226 bool (*ValidateMCOperand)(const MCOperand &MCOp, const MCSubtargetInfo &STI,
0227 unsigned PredicateIndex);
0228 };
0229
0230 }
0231
0232 #endif