LLVM  4.0.0
MCInstPrinter.h
Go to the documentation of this file.
1 //===- MCInstPrinter.h - MCInst to target assembly syntax -------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_MC_MCINSTPRINTER_H
11 #define LLVM_MC_MCINSTPRINTER_H
12 
13 #include "llvm/Support/Format.h"
14 #include <cstdint>
15 
16 namespace llvm {
17 
18 template <typename T> class ArrayRef;
19 class MCInst;
20 class raw_ostream;
21 class MCAsmInfo;
22 class MCInstrInfo;
23 class MCRegisterInfo;
24 class MCSubtargetInfo;
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 /// \brief This is an instance of a target assembly language printer that
40 /// converts an MCInst to valid target assembly syntax.
42 protected:
43  /// \brief A stream that comments can be emitted to if desired. Each comment
44  /// must end with a newline. This will be null if verbose assembly emission
45  /// is disable.
47  const MCAsmInfo &MAI;
48  const MCInstrInfo &MII;
50 
51  /// True if we are printing marked up assembly.
52  bool UseMarkup;
53 
54  /// True if we are printing immediates as hex.
56 
57  /// Which style to use for printing hexadecimal values.
59 
60  /// Utility function for printing annotations.
61  void printAnnotation(raw_ostream &OS, StringRef Annot);
62 
63 public:
64  MCInstPrinter(const MCAsmInfo &mai, const MCInstrInfo &mii,
65  const MCRegisterInfo &mri)
66  : CommentStream(nullptr), MAI(mai), MII(mii), MRI(mri), UseMarkup(false),
67  PrintImmHex(false), PrintHexStyle(HexStyle::C) {}
68 
69  virtual ~MCInstPrinter();
70 
71  /// \brief Specify a stream to emit comments to.
73 
74  /// \brief Print the specified MCInst to the specified raw_ostream.
75  virtual void printInst(const MCInst *MI, raw_ostream &OS, StringRef Annot,
76  const MCSubtargetInfo &STI) = 0;
77 
78  /// \brief Return the name of the specified opcode enum (e.g. "MOV32ri") or
79  /// empty if we can't resolve it.
80  StringRef getOpcodeName(unsigned Opcode) const;
81 
82  /// \brief Print the assembler register name.
83  virtual void printRegName(raw_ostream &OS, unsigned RegNo) const;
84 
85  bool getUseMarkup() const { return UseMarkup; }
86  void setUseMarkup(bool Value) { UseMarkup = Value; }
87 
88  /// Utility functions to make adding mark ups simpler.
89  StringRef markup(StringRef s) const;
90  StringRef markup(StringRef a, StringRef b) const;
91 
92  bool getPrintImmHex() const { return PrintImmHex; }
93  void setPrintImmHex(bool Value) { PrintImmHex = Value; }
94 
97 
98  /// Utility function to print immediates in decimal or hex.
100  return PrintImmHex ? formatHex(Value) : formatDec(Value);
101  }
102 
103  /// Utility functions to print decimal/hexadecimal values.
104  format_object<int64_t> formatDec(int64_t Value) const;
105  format_object<int64_t> formatHex(int64_t Value) const;
106  format_object<uint64_t> formatHex(uint64_t Value) const;
107 };
108 
109 } // end namespace llvm
110 
111 #endif // LLVM_MC_MCINSTPRINTER_H
bool PrintImmHex
True if we are printing immediates as hex.
Definition: MCInstPrinter.h:55
HexStyle::Style PrintHexStyle
Which style to use for printing hexadecimal values.
Definition: MCInstPrinter.h:58
virtual void printInst(const MCInst *MI, raw_ostream &OS, StringRef Annot, const MCSubtargetInfo &STI)=0
Print the specified MCInst to the specified raw_ostream.
format_object< int64_t > formatHex(int64_t Value) const
void setPrintHexStyle(HexStyle::Style Value)
Definition: MCInstPrinter.h:96
void setPrintImmHex(bool Value)
Definition: MCInstPrinter.h:93
HexStyle::Style getPrintHexStyle() const
Definition: MCInstPrinter.h:95
Function Alias Analysis false
format_object< int64_t > formatDec(int64_t Value) const
Utility functions to print decimal/hexadecimal values.
virtual void printRegName(raw_ostream &OS, unsigned RegNo) const
Print the assembler register name.
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:150
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:57
StringRef markup(StringRef s) const
Utility functions to make adding mark ups simpler.
bool getUseMarkup() const
Definition: MCInstPrinter.h:85
void setCommentStream(raw_ostream &OS)
Specify a stream to emit comments to.
Definition: MCInstPrinter.h:72
StringRef getOpcodeName(unsigned Opcode) const
Return the name of the specified opcode enum (e.g.
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:24
format_object< int64_t > formatImm(int64_t Value) const
Utility function to print immediates in decimal or hex.
Definition: MCInstPrinter.h:99
raw_ostream * CommentStream
A stream that comments can be emitted to if desired.
Definition: MCInstPrinter.h:46
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
void setUseMarkup(bool Value)
Definition: MCInstPrinter.h:86
const MCAsmInfo & MAI
Definition: MCInstPrinter.h:47
bool UseMarkup
True if we are printing marked up assembly.
Definition: MCInstPrinter.h:52
This is an instance of a target assembly language printer that converts an MCInst to valid target ass...
Definition: MCInstPrinter.h:41
MCSubtargetInfo - Generic base class for all target subtargets.
const MCInstrInfo & MII
Definition: MCInstPrinter.h:48
void dumpBytes(ArrayRef< uint8_t > Bytes, raw_ostream &OS)
Convert `Bytes' to a hex string and output to `OS'.
LLVM Value Representation.
Definition: Value.h:71
void printAnnotation(raw_ostream &OS, StringRef Annot)
Utility function for printing annotations.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
bool getPrintImmHex() const
Definition: MCInstPrinter.h:92
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
MCInstPrinter(const MCAsmInfo &mai, const MCInstrInfo &mii, const MCRegisterInfo &mri)
Definition: MCInstPrinter.h:64
const MCRegisterInfo & MRI
Definition: MCInstPrinter.h:49