LLVM  14.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 
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 MCInstrAnalysis;
22 class MCRegisterInfo;
23 class MCSubtargetInfo;
24 class raw_ostream;
25 class StringRef;
26 
27 /// Convert `Bytes' to a hex string and output to `OS'
28 void dumpBytes(ArrayRef<uint8_t> Bytes, raw_ostream &OS);
29 
30 namespace HexStyle {
31 
32 enum Style {
33  C, ///< 0xff
34  Asm ///< 0ffh
35 };
36 
37 } // end namespace HexStyle
38 
39 struct AliasMatchingData;
40 
41 /// This is an instance of a target assembly language printer that
42 /// converts an MCInst to valid target assembly syntax.
44 protected:
45  /// A stream that comments can be emitted to if desired. Each comment
46  /// must end with a newline. This will be null if verbose assembly emission
47  /// is disabled.
49  const MCAsmInfo &MAI;
50  const MCInstrInfo &MII;
52  const MCInstrAnalysis *MIA = nullptr;
53 
54  /// True if we are printing marked up assembly.
55  bool UseMarkup = false;
56 
57  /// True if we prefer aliases (e.g. nop) to raw mnemonics.
58  bool PrintAliases = true;
59 
60  /// True if we are printing immediates as hex.
61  bool PrintImmHex = false;
62 
63  /// Which style to use for printing hexadecimal values.
65 
66  /// If true, a branch immediate (e.g. bl 4) will be printed as a hexadecimal
67  /// address (e.g. bl 0x20004). This is useful for a stream disassembler
68  /// (llvm-objdump -d).
70 
71  /// If true, symbolize branch target and memory reference operands.
72  bool SymbolizeOperands = false;
73 
74  /// Utility function for printing annotations.
75  void printAnnotation(raw_ostream &OS, StringRef Annot);
76 
77  /// Helper for matching MCInsts to alias patterns when printing instructions.
78  const char *matchAliasPatterns(const MCInst *MI, const MCSubtargetInfo *STI,
79  const AliasMatchingData &M);
80 
81 public:
82  MCInstPrinter(const MCAsmInfo &mai, const MCInstrInfo &mii,
83  const MCRegisterInfo &mri) : MAI(mai), MII(mii), MRI(mri) {}
84 
85  virtual ~MCInstPrinter();
86 
87  /// Customize the printer according to a command line option.
88  /// @return true if the option is recognized and applied.
89  virtual bool applyTargetSpecificCLOption(StringRef Opt) { return false; }
90 
91  /// Specify a stream to emit comments to.
93 
94  /// Returns a pair containing the mnemonic for \p MI and the number of bits
95  /// left for further processing by printInstruction (generated by tablegen).
96  virtual std::pair<const char *, uint64_t> getMnemonic(const MCInst *MI) = 0;
97 
98  /// Print the specified MCInst to the specified raw_ostream.
99  ///
100  /// \p Address the address of current instruction on most targets, used to
101  /// print a PC relative immediate as the target address. On targets where a PC
102  /// relative immediate is relative to the next instruction and the length of a
103  /// MCInst is difficult to measure (e.g. x86), this is the address of the next
104  /// instruction. If Address is 0, the immediate will be printed.
105  virtual void printInst(const MCInst *MI, uint64_t Address, StringRef Annot,
106  const MCSubtargetInfo &STI, raw_ostream &OS) = 0;
107 
108  /// Return the name of the specified opcode enum (e.g. "MOV32ri") or
109  /// empty if we can't resolve it.
110  StringRef getOpcodeName(unsigned Opcode) const;
111 
112  /// Print the assembler register name.
113  virtual void printRegName(raw_ostream &OS, unsigned RegNo) const;
114 
115  bool getUseMarkup() const { return UseMarkup; }
116  void setUseMarkup(bool Value) { UseMarkup = Value; }
117 
118  /// Utility functions to make adding mark ups simpler.
119  StringRef markup(StringRef s) const;
120 
121  bool getPrintImmHex() const { return PrintImmHex; }
123 
125 
128  }
129 
132 
133  /// Utility function to print immediates in decimal or hex.
136  }
137 
138  /// Utility functions to print decimal/hexadecimal values.
139  format_object<int64_t> formatDec(int64_t Value) const;
140  format_object<int64_t> formatHex(int64_t Value) const;
142 };
143 
144 /// Map from opcode to pattern list by binary search.
149 };
150 
151 /// Data for each alias pattern. Includes feature bits, string, number of
152 /// operands, and a variadic list of conditions to check.
153 struct AliasPattern {
156  uint8_t NumOperands;
157  uint8_t NumConds;
158 };
159 
161  enum CondKind : uint8_t {
162  K_Feature, // Match only if a feature is enabled.
163  K_NegFeature, // Match only if a feature is disabled.
164  K_OrFeature, // Match only if one of a set of features is enabled.
165  K_OrNegFeature, // Match only if one of a set of features is disabled.
166  K_EndOrFeatures, // Note end of list of K_Or(Neg)?Features.
167  K_Ignore, // Match any operand.
168  K_Reg, // Match a specific register.
169  K_TiedReg, // Match another already matched register.
170  K_Imm, // Match a specific immediate.
171  K_RegClass, // Match registers in a class.
172  K_Custom, // Call custom matcher by index.
173  };
174 
177 };
178 
179 /// Tablegenerated data structures needed to match alias patterns.
185  bool (*ValidateMCOperand)(const MCOperand &MCOp, const MCSubtargetInfo &STI,
186  unsigned PredicateIndex);
187 };
188 
189 } // end namespace llvm
190 
191 #endif // LLVM_MC_MCINSTPRINTER_H
llvm::MCInstPrinter::formatDec
format_object< int64_t > formatDec(int64_t Value) const
Utility functions to print decimal/hexadecimal values.
Definition: MCInstPrinter.cpp:193
llvm::MCInstPrinter::MII
const MCInstrInfo & MII
Definition: MCInstPrinter.h:50
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:105
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
llvm::MCInstPrinter::PrintAliases
bool PrintAliases
True if we prefer aliases (e.g. nop) to raw mnemonics.
Definition: MCInstPrinter.h:58
llvm::AliasMatchingData::ValidateMCOperand
bool(* ValidateMCOperand)(const MCOperand &MCOp, const MCSubtargetInfo &STI, unsigned PredicateIndex)
Definition: MCInstPrinter.h:185
llvm::MCInstPrinter::setSymbolizeOperands
void setSymbolizeOperands(bool Value)
Definition: MCInstPrinter.h:130
llvm::MCAsmInfo
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
llvm::AliasPatternCond::K_Imm
@ K_Imm
Definition: MCInstPrinter.h:170
llvm::MCInstPrinter::getUseMarkup
bool getUseMarkup() const
Definition: MCInstPrinter.h:115
llvm::MCInstPrinter::applyTargetSpecificCLOption
virtual bool applyTargetSpecificCLOption(StringRef Opt)
Customize the printer according to a command line option.
Definition: MCInstPrinter.h:89
llvm::AliasPatternCond::K_Feature
@ K_Feature
Definition: MCInstPrinter.h:162
llvm::MCInst
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
llvm::MCInstPrinter::MIA
const MCInstrAnalysis * MIA
Definition: MCInstPrinter.h:52
Format.h
llvm::AliasMatchingData
Tablegenerated data structures needed to match alias patterns.
Definition: MCInstPrinter.h:180
llvm::MCInstPrinter::PrintBranchImmAsAddress
bool PrintBranchImmAsAddress
If true, a branch immediate (e.g.
Definition: MCInstPrinter.h:69
llvm::MCInstPrinter::printInst
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.
llvm::MCInstPrinter::MRI
const MCRegisterInfo & MRI
Definition: MCInstPrinter.h:51
llvm::MCInstPrinter::SymbolizeOperands
bool SymbolizeOperands
If true, symbolize branch target and memory reference operands.
Definition: MCInstPrinter.h:72
llvm::PatternsForOpcode::Opcode
uint32_t Opcode
Definition: MCInstPrinter.h:146
llvm::AliasPatternCond::CondKind
CondKind
Definition: MCInstPrinter.h:161
llvm::AliasPatternCond::K_OrFeature
@ K_OrFeature
Definition: MCInstPrinter.h:164
llvm::MCInstrAnalysis
Definition: MCInstrAnalysis.h:27
llvm::AliasMatchingData::PatternConds
ArrayRef< AliasPatternCond > PatternConds
Definition: MCInstPrinter.h:183
llvm::PatternsForOpcode
Map from opcode to pattern list by binary search.
Definition: MCInstPrinter.h:145
llvm::MCInstPrinter::matchAliasPatterns
const char * matchAliasPatterns(const MCInst *MI, const MCSubtargetInfo *STI, const AliasMatchingData &M)
Helper for matching MCInsts to alias patterns when printing instructions.
Definition: MCInstPrinter.cpp:123
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::MCInstPrinter::UseMarkup
bool UseMarkup
True if we are printing marked up assembly.
Definition: MCInstPrinter.h:55
llvm::MCInstPrinter::CommentStream
raw_ostream * CommentStream
A stream that comments can be emitted to if desired.
Definition: MCInstPrinter.h:48
llvm::MCInstPrinter::PrintImmHex
bool PrintImmHex
True if we are printing immediates as hex.
Definition: MCInstPrinter.h:61
llvm::AliasPatternCond::K_OrNegFeature
@ K_OrNegFeature
Definition: MCInstPrinter.h:165
llvm::AliasPatternCond
Definition: MCInstPrinter.h:160
llvm::MCInstPrinter::printAnnotation
void printAnnotation(raw_ostream &OS, StringRef Annot)
Utility function for printing annotations.
Definition: MCInstPrinter.cpp:49
llvm::AliasPatternCond::K_EndOrFeatures
@ K_EndOrFeatures
Definition: MCInstPrinter.h:166
llvm::MCInstPrinter::printRegName
virtual void printRegName(raw_ostream &OS, unsigned RegNo) const
Print the assembler register name.
Definition: MCInstPrinter.cpp:45
uint64_t
llvm::MCInstPrinter
This is an instance of a target assembly language printer that converts an MCInst to valid target ass...
Definition: MCInstPrinter.h:43
llvm::AliasMatchingData::Patterns
ArrayRef< AliasPattern > Patterns
Definition: MCInstPrinter.h:182
llvm::dumpBytes
void dumpBytes(ArrayRef< uint8_t > Bytes, raw_ostream &OS)
Convert ‘Bytes’ to a hex string and output to ‘OS’.
Definition: MCInstPrinter.cpp:24
s
multiplies can be turned into SHL s
Definition: README.txt:370
llvm::PatternsForOpcode::PatternStart
uint16_t PatternStart
Definition: MCInstPrinter.h:147
llvm::MCInstPrinter::getPrintImmHex
bool getPrintImmHex() const
Definition: MCInstPrinter.h:121
llvm::MCInstPrinter::formatHex
format_object< int64_t > formatHex(int64_t Value) const
Definition: MCInstPrinter.cpp:197
llvm::AliasPatternCond::Kind
CondKind Kind
Definition: MCInstPrinter.h:175
llvm::HexStyle::Style
Style
Definition: MCInstPrinter.h:32
llvm::MCInstPrinter::PrintHexStyle
HexStyle::Style PrintHexStyle
Which style to use for printing hexadecimal values.
Definition: MCInstPrinter.h:64
llvm::MCInstPrinter::getMnemonic
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...
llvm::MCInstPrinter::MCInstPrinter
MCInstPrinter(const MCAsmInfo &mai, const MCInstrInfo &mii, const MCRegisterInfo &mri)
Definition: MCInstPrinter.h:82
llvm::MCInstPrinter::setMCInstrAnalysis
void setMCInstrAnalysis(const MCInstrAnalysis *Value)
Definition: MCInstPrinter.h:131
llvm::MCInstPrinter::setPrintImmHex
void setPrintImmHex(bool Value)
Definition: MCInstPrinter.h:122
llvm::MCInstPrinter::formatImm
format_object< int64_t > formatImm(int64_t Value) const
Utility function to print immediates in decimal or hex.
Definition: MCInstPrinter.h:134
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
uint32_t
llvm::MCRegisterInfo
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
Definition: MCRegisterInfo.h:135
llvm::MCInstPrinter::setPrintBranchImmAsAddress
void setPrintBranchImmAsAddress(bool Value)
Definition: MCInstPrinter.h:126
llvm::AliasMatchingData::OpToPatterns
ArrayRef< PatternsForOpcode > OpToPatterns
Definition: MCInstPrinter.h:181
llvm::AliasPatternCond::K_Ignore
@ K_Ignore
Definition: MCInstPrinter.h:167
llvm::MCInstPrinter::setUseMarkup
void setUseMarkup(bool Value)
Definition: MCInstPrinter.h:116
llvm::HexStyle::C
@ C
0xff
Definition: MCInstPrinter.h:33
llvm::MCInstrInfo
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:25
uint16_t
llvm::AliasMatchingData::AsmStrings
StringRef AsmStrings
Definition: MCInstPrinter.h:184
llvm::TargetStackID::Value
Value
Definition: TargetFrameLowering.h:27
llvm::MCInstPrinter::getOpcodeName
StringRef getOpcodeName(unsigned Opcode) const
Return the name of the specified opcode enum (e.g.
Definition: MCInstPrinter.cpp:41
llvm::AliasPatternCond::Value
uint32_t Value
Definition: MCInstPrinter.h:176
llvm::AliasPattern::NumOperands
uint8_t NumOperands
Definition: MCInstPrinter.h:156
llvm::MCInstPrinter::markup
StringRef markup(StringRef s) const
Utility functions to make adding mark ups simpler.
Definition: MCInstPrinter.cpp:173
llvm::AliasPattern
Data for each alias pattern.
Definition: MCInstPrinter.h:153
llvm::MCInstPrinter::MAI
const MCAsmInfo & MAI
Definition: MCInstPrinter.h:49
llvm::AliasPatternCond::K_TiedReg
@ K_TiedReg
Definition: MCInstPrinter.h:169
llvm::AliasPatternCond::K_RegClass
@ K_RegClass
Definition: MCInstPrinter.h:171
llvm::AliasPattern::AsmStrOffset
uint32_t AsmStrOffset
Definition: MCInstPrinter.h:154
llvm::HexStyle::Asm
@ Asm
0ffh
Definition: MCInstPrinter.h:34
llvm::MCOperand
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:36
llvm::format_object
Definition: Format.h:90
llvm::PatternsForOpcode::NumPatterns
uint16_t NumPatterns
Definition: MCInstPrinter.h:148
llvm::AliasPatternCond::K_Reg
@ K_Reg
Definition: MCInstPrinter.h:168
llvm::AliasPatternCond::K_NegFeature
@ K_NegFeature
Definition: MCInstPrinter.h:163
llvm::AliasPattern::NumConds
uint8_t NumConds
Definition: MCInstPrinter.h:157
llvm::MCInstPrinter::~MCInstPrinter
virtual ~MCInstPrinter()
llvm::MCInstPrinter::setPrintHexStyle
void setPrintHexStyle(HexStyle::Style Value)
Definition: MCInstPrinter.h:124
llvm::MCInstPrinter::setCommentStream
void setCommentStream(raw_ostream &OS)
Specify a stream to emit comments to.
Definition: MCInstPrinter.h:92
llvm::MCSubtargetInfo
Generic base class for all target subtargets.
Definition: MCSubtargetInfo.h:75
llvm::AliasPattern::AliasCondStart
uint32_t AliasCondStart
Definition: MCInstPrinter.h:155
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::AliasPatternCond::K_Custom
@ K_Custom
Definition: MCInstPrinter.h:172