LLVM  10.0.0
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 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.
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;
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  /// Utility function for printing annotations.
62  void printAnnotation(raw_ostream &OS, StringRef Annot);
63 
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);
67 
68 public:
69  MCInstPrinter(const MCAsmInfo &mai, const MCInstrInfo &mii,
70  const MCRegisterInfo &mri) : MAI(mai), MII(mii), MRI(mri) {}
71 
72  virtual ~MCInstPrinter();
73 
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; }
77 
78  /// Specify a stream to emit comments to.
79  void setCommentStream(raw_ostream &OS) { CommentStream = &OS; }
80 
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;
84 
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;
88 
89  /// Print the assembler register name.
90  virtual void printRegName(raw_ostream &OS, unsigned RegNo) const;
91 
92  bool getUseMarkup() const { return UseMarkup; }
93  void setUseMarkup(bool Value) { UseMarkup = Value; }
94 
95  /// Utility functions to make adding mark ups simpler.
96  StringRef markup(StringRef s) const;
97 
98  bool getPrintImmHex() const { return PrintImmHex; }
99  void setPrintImmHex(bool Value) { PrintImmHex = Value; }
100 
101  void setPrintHexStyle(HexStyle::Style Value) { PrintHexStyle = Value; }
102 
103  /// Utility function to print immediates in decimal or hex.
105  return PrintImmHex ? formatHex(Value) : formatDec(Value);
106  }
107 
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;
112 };
113 
114 /// Map from opcode to pattern list by binary search.
119 };
120 
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 {
126  uint8_t NumOperands;
127  uint8_t NumConds;
128 };
129 
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.
140  };
141 
144 };
145 
146 /// Tablegenerated data structures needed to match alias patterns.
152  bool (*ValidateMCOperand)(const MCOperand &MCOp, const MCSubtargetInfo &STI,
153  unsigned PredicateIndex);
154 };
155 
156 } // end namespace llvm
157 
158 #endif // LLVM_MC_MCINSTPRINTER_H
virtual bool applyTargetSpecificCLOption(StringRef Opt)
Customize the printer according to a command line option.
Definition: MCInstPrinter.h:76
Data for each alias pattern.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
ArrayRef< AliasPattern > Patterns
void setPrintHexStyle(HexStyle::Style Value)
void setPrintImmHex(bool Value)
Definition: MCInstPrinter.h:99
Map from opcode to pattern list by binary search.
format_object< int64_t > formatImm(int64_t Value) const
Utility function to print immediates in decimal or hex.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:158
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
ArrayRef< PatternsForOpcode > OpToPatterns
bool getPrintImmHex() const
Definition: MCInstPrinter.h:98
void setCommentStream(raw_ostream &OS)
Specify a stream to emit comments to.
Definition: MCInstPrinter.h:79
Tablegenerated data structures needed to match alias patterns.
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:23
ArrayRef< AliasPatternCond > PatternConds
void setUseMarkup(bool Value)
Definition: MCInstPrinter.h:93
const MCAsmInfo & MAI
Definition: MCInstPrinter.h:48
bool getUseMarkup() const
Definition: MCInstPrinter.h:92
This is an instance of a target assembly language printer that converts an MCInst to valid target ass...
Definition: MCInstPrinter.h:42
Generic base class for all target subtargets.
const MCInstrInfo & MII
Definition: MCInstPrinter.h:49
void dumpBytes(ArrayRef< uint8_t > Bytes, raw_ostream &OS)
Convert `Bytes&#39; to a hex string and output to `OS&#39;.
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:46
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:54
MCInstPrinter(const MCAsmInfo &mai, const MCInstrInfo &mii, const MCRegisterInfo &mri)
Definition: MCInstPrinter.h:69
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:34
const MCRegisterInfo & MRI
Definition: MCInstPrinter.h:50