Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:13

0001 //===- MCInstPrinter.h - MCInst to target assembly syntax -------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
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 /// Convert `Bytes' to a hex string and output to `OS'
0031 void dumpBytes(ArrayRef<uint8_t> Bytes, raw_ostream &OS);
0032 
0033 namespace HexStyle {
0034 
0035 enum Style {
0036   C,  ///< 0xff
0037   Asm ///< 0ffh
0038 };
0039 
0040 } // end namespace HexStyle
0041 
0042 struct AliasMatchingData;
0043 
0044 /// This is an instance of a target assembly language printer that
0045 /// converts an MCInst to valid target assembly syntax.
0046 class MCInstPrinter {
0047 protected:
0048   /// A stream that comments can be emitted to if desired.  Each comment
0049   /// must end with a newline.  This will be null if verbose assembly emission
0050   /// is disabled.
0051   raw_ostream *CommentStream = nullptr;
0052   const MCAsmInfo &MAI;
0053   const MCInstrInfo &MII;
0054   const MCRegisterInfo &MRI;
0055   const MCInstrAnalysis *MIA = nullptr;
0056 
0057   /// True if we are printing marked up assembly.
0058   bool UseMarkup = false;
0059 
0060   /// True if we are printing colored assembly.
0061   bool UseColor = false;
0062 
0063   /// True if we prefer aliases (e.g. nop) to raw mnemonics.
0064   bool PrintAliases = true;
0065 
0066   /// True if we are printing immediates as hex.
0067   bool PrintImmHex = false;
0068 
0069   /// Which style to use for printing hexadecimal values.
0070   HexStyle::Style PrintHexStyle = HexStyle::C;
0071 
0072   /// If true, a branch immediate (e.g. bl 4) will be printed as a hexadecimal
0073   /// address (e.g. bl 0x20004). This is useful for a stream disassembler
0074   /// (llvm-objdump -d).
0075   bool PrintBranchImmAsAddress = false;
0076 
0077   /// If true, symbolize branch target and memory reference operands.
0078   bool SymbolizeOperands = false;
0079 
0080   SmallVector<raw_ostream::Colors, 4> ColorStack{raw_ostream::Colors::RESET};
0081 
0082   /// Utility function for printing annotations.
0083   void printAnnotation(raw_ostream &OS, StringRef Annot);
0084 
0085   /// Helper for matching MCInsts to alias patterns when printing instructions.
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   /// Customize the printer according to a command line option.
0126   /// @return true if the option is recognized and applied.
0127   virtual bool applyTargetSpecificCLOption(StringRef Opt) { return false; }
0128 
0129   /// Specify a stream to emit comments to.
0130   void setCommentStream(raw_ostream &OS) { CommentStream = &OS; }
0131 
0132   /// Returns a pair containing the mnemonic for \p MI and the number of bits
0133   /// left for further processing by printInstruction (generated by tablegen).
0134   virtual std::pair<const char *, uint64_t>
0135   getMnemonic(const MCInst &MI) const = 0;
0136 
0137   /// Print the specified MCInst to the specified raw_ostream.
0138   ///
0139   /// \p Address the address of current instruction on most targets, used to
0140   /// print a PC relative immediate as the target address. On targets where a PC
0141   /// relative immediate is relative to the next instruction and the length of a
0142   /// MCInst is difficult to measure (e.g. x86), this is the address of the next
0143   /// instruction. If Address is 0, the immediate will be printed.
0144   virtual void printInst(const MCInst *MI, uint64_t Address, StringRef Annot,
0145                          const MCSubtargetInfo &STI, raw_ostream &OS) = 0;
0146 
0147   /// Return the name of the specified opcode enum (e.g. "MOV32ri") or
0148   /// empty if we can't resolve it.
0149   StringRef getOpcodeName(unsigned Opcode) const;
0150 
0151   /// Print the assembler register name.
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   /// Utility function to print immediates in decimal or hex.
0175   format_object<int64_t> formatImm(int64_t Value) const {
0176     return PrintImmHex ? formatHex(Value) : formatDec(Value);
0177   }
0178 
0179   /// Utility functions to print decimal/hexadecimal values.
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 /// Map from opcode to pattern list by binary search.
0186 struct PatternsForOpcode {
0187   uint32_t Opcode;
0188   uint16_t PatternStart;
0189   uint16_t NumPatterns;
0190 };
0191 
0192 /// Data for each alias pattern. Includes feature bits, string, number of
0193 /// operands, and a variadic list of conditions to check.
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,       // Match only if a feature is enabled.
0204     K_NegFeature,    // Match only if a feature is disabled.
0205     K_OrFeature,     // Match only if one of a set of features is enabled.
0206     K_OrNegFeature,  // Match only if one of a set of features is disabled.
0207     K_EndOrFeatures, // Note end of list of K_Or(Neg)?Features.
0208     K_Ignore,        // Match any operand.
0209     K_Reg,           // Match a specific register.
0210     K_TiedReg,       // Match another already matched register.
0211     K_Imm,           // Match a specific immediate.
0212     K_RegClass,      // Match registers in a class.
0213     K_Custom,        // Call custom matcher by index.
0214   };
0215 
0216   CondKind Kind;
0217   uint32_t Value;
0218 };
0219 
0220 /// Tablegenerated data structures needed to match alias patterns.
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 } // end namespace llvm
0231 
0232 #endif // LLVM_MC_MCINSTPRINTER_H