LLVM 20.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
14#include "llvm/Support/Format.h"
16#include <cstdint>
17
18namespace llvm {
19
20class MCAsmInfo;
21class MCInst;
22class MCInstrAnalysis;
23class MCInstrInfo;
24class MCOperand;
25class MCRegister;
26class MCRegisterInfo;
27class MCSubtargetInfo;
28class StringRef;
29
30/// Convert `Bytes' to a hex string and output to `OS'
31void dumpBytes(ArrayRef<uint8_t> Bytes, raw_ostream &OS);
32
33namespace HexStyle {
34
35enum Style {
36 C, ///< 0xff
37 Asm ///< 0ffh
38};
39
40} // end namespace HexStyle
41
42struct AliasMatchingData;
43
44/// This is an instance of a target assembly language printer that
45/// converts an MCInst to valid target assembly syntax.
47protected:
48 /// A stream that comments can be emitted to if desired. Each comment
49 /// must end with a newline. This will be null if verbose assembly emission
50 /// is disabled.
52 const MCAsmInfo &MAI;
55 const MCInstrAnalysis *MIA = nullptr;
56
57 /// True if we are printing marked up assembly.
58 bool UseMarkup = false;
59
60 /// True if we are printing colored assembly.
61 bool UseColor = false;
62
63 /// True if we prefer aliases (e.g. nop) to raw mnemonics.
64 bool PrintAliases = true;
65
66 /// True if we are printing immediates as hex.
67 bool PrintImmHex = false;
68
69 /// Which style to use for printing hexadecimal values.
71
72 /// If true, a branch immediate (e.g. bl 4) will be printed as a hexadecimal
73 /// address (e.g. bl 0x20004). This is useful for a stream disassembler
74 /// (llvm-objdump -d).
76
77 /// If true, symbolize branch target and memory reference operands.
78 bool SymbolizeOperands = false;
79
81
82 /// Utility function for printing annotations.
84
85 /// Helper for matching MCInsts to alias patterns when printing instructions.
86 const char *matchAliasPatterns(const MCInst *MI, const MCSubtargetInfo *STI,
87 const AliasMatchingData &M);
88
89public:
90 MCInstPrinter(const MCAsmInfo &mai, const MCInstrInfo &mii,
91 const MCRegisterInfo &mri) : MAI(mai), MII(mii), MRI(mri) {}
92
93 virtual ~MCInstPrinter();
94
95 enum class Markup {
98 Target,
99 Memory,
100 };
101
103 public:
105 bool EnableMarkup, bool EnableColor);
106 ~WithMarkup();
107
108 template <typename T> WithMarkup &operator<<(T &O) {
109 OS << O;
110 return *this;
111 }
112
113 template <typename T> WithMarkup &operator<<(const T &O) {
114 OS << O;
115 return *this;
116 }
117
118 private:
119 MCInstPrinter &IP;
121 bool EnableMarkup;
122 bool EnableColor;
123 };
124
125 /// Customize the printer according to a command line option.
126 /// @return true if the option is recognized and applied.
127 virtual bool applyTargetSpecificCLOption(StringRef Opt) { return false; }
128
129 /// Specify a stream to emit comments to.
131
132 /// Returns a pair containing the mnemonic for \p MI and the number of bits
133 /// left for further processing by printInstruction (generated by tablegen).
134 virtual std::pair<const char *, uint64_t>
135 getMnemonic(const MCInst &MI) const = 0;
136
137 /// Print the specified MCInst to the specified raw_ostream.
138 ///
139 /// \p Address the address of current instruction on most targets, used to
140 /// print a PC relative immediate as the target address. On targets where a PC
141 /// relative immediate is relative to the next instruction and the length of a
142 /// MCInst is difficult to measure (e.g. x86), this is the address of the next
143 /// instruction. If Address is 0, the immediate will be printed.
144 virtual void printInst(const MCInst *MI, uint64_t Address, StringRef Annot,
145 const MCSubtargetInfo &STI, raw_ostream &OS) = 0;
146
147 /// Return the name of the specified opcode enum (e.g. "MOV32ri") or
148 /// empty if we can't resolve it.
149 StringRef getOpcodeName(unsigned Opcode) const;
150
151 /// Print the assembler register name.
152 virtual void printRegName(raw_ostream &OS, MCRegister Reg);
153
154 bool getUseMarkup() const { return UseMarkup; }
156
157 bool getUseColor() const { return UseColor; }
158 void setUseColor(bool Value) { UseColor = Value; }
159
160 WithMarkup markup(raw_ostream &OS, Markup M);
161
162 bool getPrintImmHex() const { return PrintImmHex; }
164
166
169 }
170
173
174 /// Utility function to print immediates in decimal or hex.
177 }
178
179 /// Utility functions to print decimal/hexadecimal values.
183};
184
185/// Map from opcode to pattern list by binary search.
190};
191
192/// Data for each alias pattern. Includes feature bits, string, number of
193/// operands, and a variadic list of conditions to check.
199};
200
203 K_Feature, // Match only if a feature is enabled.
204 K_NegFeature, // Match only if a feature is disabled.
205 K_OrFeature, // Match only if one of a set of features is enabled.
206 K_OrNegFeature, // Match only if one of a set of features is disabled.
207 K_EndOrFeatures, // Note end of list of K_Or(Neg)?Features.
208 K_Ignore, // Match any operand.
209 K_Reg, // Match a specific register.
210 K_TiedReg, // Match another already matched register.
211 K_Imm, // Match a specific immediate.
212 K_RegClass, // Match registers in a class.
213 K_Custom, // Call custom matcher by index.
214 };
215
218};
219
220/// Tablegenerated data structures needed to match alias patterns.
227 unsigned PredicateIndex);
228};
229
230} // end namespace llvm
231
232#endif // LLVM_MC_MCINSTPRINTER_H
#define LLVM_CTOR_NODISCARD
Definition: Compiler.h:425
IRTranslator LLVM IR MI
unsigned Reg
raw_pwrite_stream & OS
This file defines the SmallVector class.
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:46
WithMarkup markup(raw_ostream &OS, Markup M)
void setPrintBranchImmAsAddress(bool Value)
bool UseMarkup
True if we are printing marked up assembly.
Definition: MCInstPrinter.h:58
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:67
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:53
raw_ostream * CommentStream
A stream that comments can be emitted to if desired.
Definition: MCInstPrinter.h:51
bool getPrintImmHex() const
void setSymbolizeOperands(bool Value)
bool SymbolizeOperands
If true, symbolize branch target and memory reference operands.
Definition: MCInstPrinter.h:78
void setPrintImmHex(bool Value)
virtual ~MCInstPrinter()
StringRef getOpcodeName(unsigned Opcode) const
Return the name of the specified opcode enum (e.g.
SmallVector< raw_ostream::Colors, 4 > ColorStack
Definition: MCInstPrinter.h:80
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:54
void printAnnotation(raw_ostream &OS, StringRef Annot)
Utility function for printing annotations.
const MCAsmInfo & MAI
Definition: MCInstPrinter.h:52
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:61
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:75
virtual void printRegName(raw_ostream &OS, MCRegister Reg)
Print the assembler register name.
virtual std::pair< const char *, uint64_t > getMnemonic(const MCInst &MI) const =0
Returns a pair containing the mnemonic for MI and the number of bits left for further processing by p...
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:55
MCInstPrinter(const MCAsmInfo &mai, const MCInstrInfo &mii, const MCRegisterInfo &mri)
Definition: MCInstPrinter.h:90
HexStyle::Style PrintHexStyle
Which style to use for printing hexadecimal values.
Definition: MCInstPrinter.h:70
bool PrintAliases
True if we prefer aliases (e.g. nop) to raw mnemonics.
Definition: MCInstPrinter.h:64
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:185
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:26
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:37
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.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
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.