]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/llvm/include/llvm/MC/MCInstPrinter.h
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm-project / llvm / include / llvm / MC / MCInstPrinter.h
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   /// If true, a branch immediate (e.g. bl 4) will be printed as a hexadecimal
62   /// address (e.g. bl 0x20004). This is useful for a stream disassembler
63   /// (llvm-objdump -d).
64   bool PrintBranchImmAsAddress = false;
65
66   /// Utility function for printing annotations.
67   void printAnnotation(raw_ostream &OS, StringRef Annot);
68
69   /// Helper for matching MCInsts to alias patterns when printing instructions.
70   const char *matchAliasPatterns(const MCInst *MI, const MCSubtargetInfo *STI,
71                                  const AliasMatchingData &M);
72
73 public:
74   MCInstPrinter(const MCAsmInfo &mai, const MCInstrInfo &mii,
75                 const MCRegisterInfo &mri) : MAI(mai), MII(mii), MRI(mri) {}
76
77   virtual ~MCInstPrinter();
78
79   /// Customize the printer according to a command line option.
80   /// @return true if the option is recognized and applied.
81   virtual bool applyTargetSpecificCLOption(StringRef Opt) { return false; }
82
83   /// Specify a stream to emit comments to.
84   void setCommentStream(raw_ostream &OS) { CommentStream = &OS; }
85
86   /// Print the specified MCInst to the specified raw_ostream.
87   ///
88   /// \p Address the address of current instruction on most targets, used to
89   /// print a PC relative immediate as the target address. On targets where a PC
90   /// relative immediate is relative to the next instruction and the length of a
91   /// MCInst is difficult to measure (e.g. x86), this is the address of the next
92   /// instruction. If Address is 0, the immediate will be printed.
93   virtual void printInst(const MCInst *MI, uint64_t Address, StringRef Annot,
94                          const MCSubtargetInfo &STI, raw_ostream &OS) = 0;
95
96   /// Return the name of the specified opcode enum (e.g. "MOV32ri") or
97   /// empty if we can't resolve it.
98   StringRef getOpcodeName(unsigned Opcode) const;
99
100   /// Print the assembler register name.
101   virtual void printRegName(raw_ostream &OS, unsigned RegNo) const;
102
103   bool getUseMarkup() const { return UseMarkup; }
104   void setUseMarkup(bool Value) { UseMarkup = Value; }
105
106   /// Utility functions to make adding mark ups simpler.
107   StringRef markup(StringRef s) const;
108
109   bool getPrintImmHex() const { return PrintImmHex; }
110   void setPrintImmHex(bool Value) { PrintImmHex = Value; }
111
112   void setPrintHexStyle(HexStyle::Style Value) { PrintHexStyle = Value; }
113
114   void setPrintBranchImmAsAddress(bool Value) {
115     PrintBranchImmAsAddress = Value;
116   }
117
118   /// Utility function to print immediates in decimal or hex.
119   format_object<int64_t> formatImm(int64_t Value) const {
120     return PrintImmHex ? formatHex(Value) : formatDec(Value);
121   }
122
123   /// Utility functions to print decimal/hexadecimal values.
124   format_object<int64_t> formatDec(int64_t Value) const;
125   format_object<int64_t> formatHex(int64_t Value) const;
126   format_object<uint64_t> formatHex(uint64_t Value) const;
127 };
128
129 /// Map from opcode to pattern list by binary search.
130 struct PatternsForOpcode {
131   uint32_t Opcode;
132   uint16_t PatternStart;
133   uint16_t NumPatterns;
134 };
135
136 /// Data for each alias pattern. Includes feature bits, string, number of
137 /// operands, and a variadic list of conditions to check.
138 struct AliasPattern {
139   uint32_t AsmStrOffset;
140   uint32_t AliasCondStart;
141   uint8_t NumOperands;
142   uint8_t NumConds;
143 };
144
145 struct AliasPatternCond {
146   enum CondKind : uint8_t {
147     K_Feature,       // Match only if a feature is enabled.
148     K_NegFeature,    // Match only if a feature is disabled.
149     K_OrFeature,     // Match only if one of a set of features is enabled.
150     K_OrNegFeature,  // Match only if one of a set of features is disabled.
151     K_EndOrFeatures, // Note end of list of K_Or(Neg)?Features.
152     K_Ignore,        // Match any operand.
153     K_Reg,           // Match a specific register.
154     K_TiedReg,       // Match another already matched register.
155     K_Imm,           // Match a specific immediate.
156     K_RegClass,      // Match registers in a class.
157     K_Custom,        // Call custom matcher by index.
158   };
159
160   CondKind Kind;
161   uint32_t Value;
162 };
163
164 /// Tablegenerated data structures needed to match alias patterns.
165 struct AliasMatchingData {
166   ArrayRef<PatternsForOpcode> OpToPatterns;
167   ArrayRef<AliasPattern> Patterns;
168   ArrayRef<AliasPatternCond> PatternConds;
169   StringRef AsmStrings;
170   bool (*ValidateMCOperand)(const MCOperand &MCOp, const MCSubtargetInfo &STI,
171                             unsigned PredicateIndex);
172 };
173
174 } // end namespace llvm
175
176 #endif // LLVM_MC_MCINSTPRINTER_H