LLVM 19.0.0git
MCInstPrinter.h
Go to the documentation of this file.
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
13#include "llvm/Support/Format.h"
14#include <cstdint>
15
16namespace llvm {
17
18class MCAsmInfo;
19class MCInst;
20class MCInstrAnalysis;
21class MCInstrInfo;
22class MCOperand;
23class MCRegister;
24class MCRegisterInfo;
25class MCSubtargetInfo;
26class StringRef;
27class raw_ostream;
28
29/// Convert `Bytes' to a hex string and output to `OS'
30void dumpBytes(ArrayRef<uint8_t> Bytes, raw_ostream &OS);
31
32namespace HexStyle {
33
34enum Style {
35 C, ///< 0xff
36 Asm ///< 0ffh
37};
38
39} // end namespace HexStyle
40
41struct AliasMatchingData;
42
43/// This is an instance of a target assembly language printer that
44/// converts an MCInst to valid target assembly syntax.
46protected:
47 /// A stream that comments can be emitted to if desired. Each comment
48 /// must end with a newline. This will be null if verbose assembly emission
49 /// is disabled.
51 const MCAsmInfo &MAI;
54 const MCInstrAnalysis *MIA = nullptr;
55
56 /// True if we are printing marked up assembly.
57 bool UseMarkup = false;
58
59 /// True if we are printing colored assembly.
60 bool UseColor = false;
61
62 /// True if we prefer aliases (e.g. nop) to raw mnemonics.
63 bool PrintAliases = true;
64
65 /// True if we are printing immediates as hex.
66 bool PrintImmHex = false;
67
68 /// Which style to use for printing hexadecimal values.
70
71 /// If true, a branch immediate (e.g. bl 4) will be printed as a hexadecimal
72 /// address (e.g. bl 0x20004). This is useful for a stream disassembler
73 /// (llvm-objdump -d).
75
76 /// If true, symbolize branch target and memory reference operands.
77 bool SymbolizeOperands = false;
78
79 /// Utility function for printing annotations.
81
82 /// Helper for matching MCInsts to alias patterns when printing instructions.
83 const char *matchAliasPatterns(const MCInst *MI, const MCSubtargetInfo *STI,
84 const AliasMatchingData &M);
85
86public:
87 MCInstPrinter(const MCAsmInfo &mai, const MCInstrInfo &mii,
88 const MCRegisterInfo &mri) : MAI(mai), MII(mii), MRI(mri) {}
89
90 virtual ~MCInstPrinter();
91
92 enum class Markup {
95 Target,
96 Memory,
97 };
98
99 class WithMarkup {
100 public:
101 LLVM_CTOR_NODISCARD WithMarkup(raw_ostream &OS, Markup M, bool EnableMarkup,
102 bool EnableColor);
103 ~WithMarkup();
104
105 template <typename T> WithMarkup &operator<<(T &O) {
106 OS << O;
107 return *this;
108 }
109
110 template <typename T> WithMarkup &operator<<(const T &O) {
111 OS << O;
112 return *this;
113 }
114
115 private:
117 bool EnableMarkup;
118 bool EnableColor;
119 };
120
121 /// Customize the printer according to a command line option.
122 /// @return true if the option is recognized and applied.
123 virtual bool applyTargetSpecificCLOption(StringRef Opt) { return false; }
124
125 /// Specify a stream to emit comments to.
127
128 /// Returns a pair containing the mnemonic for \p MI and the number of bits
129 /// left for further processing by printInstruction (generated by tablegen).
130 virtual std::pair<const char *, uint64_t> getMnemonic(const MCInst *MI) = 0;
131
132 /// Print the specified MCInst to the specified raw_ostream.
133 ///
134 /// \p Address the address of current instruction on most targets, used to
135 /// print a PC relative immediate as the target address. On targets where a PC
136 /// relative immediate is relative to the next instruction and the length of a
137 /// MCInst is difficult to measure (e.g. x86), this is the address of the next
138 /// instruction. If Address is 0, the immediate will be printed.
139 virtual void printInst(const MCInst *MI, uint64_t Address, StringRef Annot,
140 const MCSubtargetInfo &STI, raw_ostream &OS) = 0;
141
142 /// Return the name of the specified opcode enum (e.g. "MOV32ri") or
143 /// empty if we can't resolve it.
144 StringRef getOpcodeName(unsigned Opcode) const;
145
146 /// Print the assembler register name.
147 virtual void printRegName(raw_ostream &OS, MCRegister Reg) const;
148
149 bool getUseMarkup() const { return UseMarkup; }
151
152 bool getUseColor() const { return UseColor; }
153 void setUseColor(bool Value) { UseColor = Value; }
154
155 WithMarkup markup(raw_ostream &OS, Markup M) const;
156
157 bool getPrintImmHex() const { return PrintImmHex; }
159
161
164 }
165
168
169 /// Utility function to print immediates in decimal or hex.
172 }
173
174 /// Utility functions to print decimal/hexadecimal values.
178};
179
180/// Map from opcode to pattern list by binary search.
185};
186
187/// Data for each alias pattern. Includes feature bits, string, number of
188/// operands, and a variadic list of conditions to check.
192 uint8_t NumOperands;
193 uint8_t NumConds;
194};
195
197 enum CondKind : uint8_t {
198 K_Feature, // Match only if a feature is enabled.
199 K_NegFeature, // Match only if a feature is disabled.
200 K_OrFeature, // Match only if one of a set of features is enabled.
201 K_OrNegFeature, // Match only if one of a set of features is disabled.
202 K_EndOrFeatures, // Note end of list of K_Or(Neg)?Features.
203 K_Ignore, // Match any operand.
204 K_Reg, // Match a specific register.
205 K_TiedReg, // Match another already matched register.
206 K_Imm, // Match a specific immediate.
207 K_RegClass, // Match registers in a class.
208 K_Custom, // Call custom matcher by index.
209 };
210
213};
214
215/// Tablegenerated data structures needed to match alias patterns.
222 unsigned PredicateIndex);
223};
224
225} // end namespace llvm
226
227#endif // LLVM_MC_MCINSTPRINTER_H
#define LLVM_CTOR_NODISCARD
Definition: Compiler.h:332
IRTranslator LLVM IR MI
unsigned Reg
raw_pwrite_stream & OS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
WithMarkup & operator<<(const T &O)
This is an instance of a target assembly language printer that converts an MCInst to valid target ass...
Definition: MCInstPrinter.h:45
void setPrintBranchImmAsAddress(bool Value)
bool UseMarkup
True if we are printing marked up assembly.
Definition: MCInstPrinter.h:57
void setMCInstrAnalysis(const MCInstrAnalysis *Value)
format_object< int64_t > formatHex(int64_t Value) const
bool PrintImmHex
True if we are printing immediates as hex.
Definition: MCInstPrinter.h:66
bool getUseColor() const
void setCommentStream(raw_ostream &OS)
Specify a stream to emit comments to.
virtual bool applyTargetSpecificCLOption(StringRef Opt)
Customize the printer according to a command line option.
void setUseColor(bool Value)
const MCInstrInfo & MII
Definition: MCInstPrinter.h:52
raw_ostream * CommentStream
A stream that comments can be emitted to if desired.
Definition: MCInstPrinter.h:50
bool getPrintImmHex() const
void setSymbolizeOperands(bool Value)
bool SymbolizeOperands
If true, symbolize branch target and memory reference operands.
Definition: MCInstPrinter.h:77
void setPrintImmHex(bool Value)
virtual ~MCInstPrinter()
virtual void printRegName(raw_ostream &OS, MCRegister Reg) const
Print the assembler register name.
WithMarkup markup(raw_ostream &OS, Markup M) const
StringRef getOpcodeName(unsigned Opcode) const
Return the name of the specified opcode enum (e.g.
virtual std::pair< const char *, uint64_t > getMnemonic(const MCInst *MI)=0
Returns a pair containing the mnemonic for MI and the number of bits left for further processing by p...
void setUseMarkup(bool Value)
format_object< int64_t > formatDec(int64_t Value) const
Utility functions to print decimal/hexadecimal values.
const MCRegisterInfo & MRI
Definition: MCInstPrinter.h:53
void printAnnotation(raw_ostream &OS, StringRef Annot)
Utility function for printing annotations.
const MCAsmInfo & MAI
Definition: MCInstPrinter.h:51
format_object< int64_t > formatImm(int64_t Value) const
Utility function to print immediates in decimal or hex.
bool UseColor
True if we are printing colored assembly.
Definition: MCInstPrinter.h:60
bool getUseMarkup() const
virtual void printInst(const MCInst *MI, uint64_t Address, StringRef Annot, const MCSubtargetInfo &STI, raw_ostream &OS)=0
Print the specified MCInst to the specified raw_ostream.
bool PrintBranchImmAsAddress
If true, a branch immediate (e.g.
Definition: MCInstPrinter.h:74
void setPrintHexStyle(HexStyle::Style Value)
const char * matchAliasPatterns(const MCInst *MI, const MCSubtargetInfo *STI, const AliasMatchingData &M)
Helper for matching MCInsts to alias patterns when printing instructions.
const MCInstrAnalysis * MIA
Definition: MCInstPrinter.h:54
MCInstPrinter(const MCAsmInfo &mai, const MCInstrInfo &mii, const MCRegisterInfo &mri)
Definition: MCInstPrinter.h:87
HexStyle::Style PrintHexStyle
Which style to use for printing hexadecimal values.
Definition: MCInstPrinter.h:69
bool PrintAliases
True if we prefer aliases (e.g. nop) to raw mnemonics.
Definition: MCInstPrinter.h:63
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:26
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:36
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
Wrapper class representing physical registers. Should be passed by value.
Definition: MCRegister.h:33
Generic base class for all target subtargets.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
LLVM Value Representation.
Definition: Value.h:74
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void dumpBytes(ArrayRef< uint8_t > Bytes, raw_ostream &OS)
Convert ‘Bytes’ to a hex string and output to ‘OS’.
Tablegenerated data structures needed to match alias patterns.
ArrayRef< AliasPatternCond > PatternConds
bool(* ValidateMCOperand)(const MCOperand &MCOp, const MCSubtargetInfo &STI, unsigned PredicateIndex)
ArrayRef< PatternsForOpcode > OpToPatterns
ArrayRef< AliasPattern > Patterns
Data for each alias pattern.
Map from opcode to pattern list by binary search.