1 //===- MCInstPrinter.h - MCInst to target assembly syntax -------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_MC_MCINSTPRINTER_H
10 #define LLVM_MC_MCINSTPRINTER_H
12 #include "llvm/Support/Format.h"
22 class MCSubtargetInfo;
26 /// Convert `Bytes' to a hex string and output to `OS'
27 void dumpBytes(ArrayRef<uint8_t> Bytes, raw_ostream &OS);
36 } // end namespace HexStyle
38 struct AliasMatchingData;
40 /// This is an instance of a target assembly language printer that
41 /// converts an MCInst to valid target assembly syntax.
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
47 raw_ostream *CommentStream = nullptr;
49 const MCInstrInfo &MII;
50 const MCRegisterInfo &MRI;
52 /// True if we are printing marked up assembly.
53 bool UseMarkup = false;
55 /// True if we are printing immediates as hex.
56 bool PrintImmHex = false;
58 /// Which style to use for printing hexadecimal values.
59 HexStyle::Style PrintHexStyle = HexStyle::C;
61 /// Utility function for printing annotations.
62 void printAnnotation(raw_ostream &OS, StringRef Annot);
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);
69 MCInstPrinter(const MCAsmInfo &mai, const MCInstrInfo &mii,
70 const MCRegisterInfo &mri) : MAI(mai), MII(mii), MRI(mri) {}
72 virtual ~MCInstPrinter();
74 /// Customize the printer according to a command line option.
75 /// @return true if the option is recognized and applied.
76 virtual bool applyTargetSpecificCLOption(StringRef Opt) { return false; }
78 /// Specify a stream to emit comments to.
79 void setCommentStream(raw_ostream &OS) { CommentStream = &OS; }
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;
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;
89 /// Print the assembler register name.
90 virtual void printRegName(raw_ostream &OS, unsigned RegNo) const;
92 bool getUseMarkup() const { return UseMarkup; }
93 void setUseMarkup(bool Value) { UseMarkup = Value; }
95 /// Utility functions to make adding mark ups simpler.
96 StringRef markup(StringRef s) const;
98 bool getPrintImmHex() const { return PrintImmHex; }
99 void setPrintImmHex(bool Value) { PrintImmHex = Value; }
101 void setPrintHexStyle(HexStyle::Style Value) { PrintHexStyle = Value; }
103 /// Utility function to print immediates in decimal or hex.
104 format_object<int64_t> formatImm(int64_t Value) const {
105 return PrintImmHex ? formatHex(Value) : formatDec(Value);
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;
114 /// Map from opcode to pattern list by binary search.
115 struct PatternsForOpcode {
117 uint16_t PatternStart;
118 uint16_t NumPatterns;
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;
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.
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);
156 } // end namespace llvm
158 #endif // LLVM_MC_MCINSTPRINTER_H