1 //===- MCInstPrinter.h - MCInst to target assembly syntax -------*- C++ -*-===// 2 // 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 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 17 class MCAsmInfo; 18 class MCInst; 19 class MCOperand; 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 38 struct AliasMatchingData; 39 40 /// This is an instance of a target assembly language printer that 41 /// converts an MCInst to valid target assembly syntax. 42 class MCInstPrinter { 43 protected: 44 /// A stream that comments can be emitted to if desired. Each comment 45 /// must end with a newline. This will be null if verbose assembly emission 46 /// is disabled. 47 raw_ostream *CommentStream = nullptr; 48 const MCAsmInfo &MAI; 49 const MCInstrInfo &MII; 50 const MCRegisterInfo &MRI; 51 52 /// True if we are printing marked up assembly. 53 bool UseMarkup = false; 54 55 /// True if we are printing immediates as hex. 56 bool PrintImmHex = false; 57 58 /// Which style to use for printing hexadecimal values. 59 HexStyle::Style PrintHexStyle = HexStyle::C; 60 61 /// Utility function for printing annotations. 62 void printAnnotation(raw_ostream &OS, StringRef Annot); 63 64 /// Helper for matching MCInsts to alias patterns when printing instructions. 65 const char *matchAliasPatterns(const MCInst *MI, const MCSubtargetInfo *STI, 66 const AliasMatchingData &M); 67 68 public: MCInstPrinter(const MCAsmInfo & mai,const MCInstrInfo & mii,const MCRegisterInfo & mri)69 MCInstPrinter(const MCAsmInfo &mai, const MCInstrInfo &mii, 70 const MCRegisterInfo &mri) : MAI(mai), MII(mii), MRI(mri) {} 71 72 virtual ~MCInstPrinter(); 73 74 /// Customize the printer according to a command line option. 75 /// @return true if the option is recognized and applied. applyTargetSpecificCLOption(StringRef Opt)76 virtual bool applyTargetSpecificCLOption(StringRef Opt) { return false; } 77 78 /// Specify a stream to emit comments to. setCommentStream(raw_ostream & OS)79 void setCommentStream(raw_ostream &OS) { CommentStream = &OS; } 80 81 /// Print the specified MCInst to the specified raw_ostream. 82 virtual void printInst(const MCInst *MI, uint64_t Address, StringRef Annot, 83 const MCSubtargetInfo &STI, raw_ostream &OS) = 0; 84 85 /// Return the name of the specified opcode enum (e.g. "MOV32ri") or 86 /// empty if we can't resolve it. 87 StringRef getOpcodeName(unsigned Opcode) const; 88 89 /// Print the assembler register name. 90 virtual void printRegName(raw_ostream &OS, unsigned RegNo) const; 91 getUseMarkup()92 bool getUseMarkup() const { return UseMarkup; } setUseMarkup(bool Value)93 void setUseMarkup(bool Value) { UseMarkup = Value; } 94 95 /// Utility functions to make adding mark ups simpler. 96 StringRef markup(StringRef s) const; 97 getPrintImmHex()98 bool getPrintImmHex() const { return PrintImmHex; } setPrintImmHex(bool Value)99 void setPrintImmHex(bool Value) { PrintImmHex = Value; } 100 setPrintHexStyle(HexStyle::Style Value)101 void setPrintHexStyle(HexStyle::Style Value) { PrintHexStyle = Value; } 102 103 /// Utility function to print immediates in decimal or hex. formatImm(int64_t Value)104 format_object<int64_t> formatImm(int64_t Value) const { 105 return PrintImmHex ? formatHex(Value) : formatDec(Value); 106 } 107 108 /// Utility functions to print decimal/hexadecimal values. 109 format_object<int64_t> formatDec(int64_t Value) const; 110 format_object<int64_t> formatHex(int64_t Value) const; 111 format_object<uint64_t> formatHex(uint64_t Value) const; 112 }; 113 114 /// Map from opcode to pattern list by binary search. 115 struct PatternsForOpcode { 116 uint32_t Opcode; 117 uint16_t PatternStart; 118 uint16_t NumPatterns; 119 }; 120 121 /// Data for each alias pattern. Includes feature bits, string, number of 122 /// operands, and a variadic list of conditions to check. 123 struct AliasPattern { 124 uint32_t AsmStrOffset; 125 uint32_t AliasCondStart; 126 uint8_t NumOperands; 127 uint8_t NumConds; 128 }; 129 130 struct AliasPatternCond { 131 enum CondKind : uint8_t { 132 K_Feature, // Match only if a feature is enabled. 133 K_NegFeature, // Match only if a feature is disabled. 134 K_Ignore, // Match any operand. 135 K_Reg, // Match a specific register. 136 K_TiedReg, // Match another already matched register. 137 K_Imm, // Match a specific immediate. 138 K_RegClass, // Match registers in a class. 139 K_Custom, // Call custom matcher by index. 140 }; 141 142 CondKind Kind; 143 uint32_t Value; 144 }; 145 146 /// Tablegenerated data structures needed to match alias patterns. 147 struct AliasMatchingData { 148 ArrayRef<PatternsForOpcode> OpToPatterns; 149 ArrayRef<AliasPattern> Patterns; 150 ArrayRef<AliasPatternCond> PatternConds; 151 StringRef AsmStrings; 152 bool (*ValidateMCOperand)(const MCOperand &MCOp, const MCSubtargetInfo &STI, 153 unsigned PredicateIndex); 154 }; 155 156 } // end namespace llvm 157 158 #endif // LLVM_MC_MCINSTPRINTER_H 159