LLVM  3.7.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/ADT/ArrayRef.h"
14 #include "llvm/Support/DataTypes.h"
15 #include "llvm/Support/Format.h"
16 
17 namespace llvm {
18 class MCInst;
19 class raw_ostream;
20 class MCAsmInfo;
21 class MCInstrInfo;
22 class MCRegisterInfo;
23 class MCSubtargetInfo;
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 enum Style {
31  C, ///< 0xff
32  Asm ///< 0ffh
33 };
34 }
35 
36 /// \brief This is an instance of a target assembly language printer that
37 /// converts an MCInst to valid target assembly syntax.
39 protected:
40  /// \brief A stream that comments can be emitted to if desired. Each comment
41  /// must end with a newline. This will be null if verbose assembly emission
42  /// is disable.
44  const MCAsmInfo &MAI;
45  const MCInstrInfo &MII;
47 
48  /// True if we are printing marked up assembly.
49  bool UseMarkup;
50 
51  /// True if we are printing immediates as hex.
53 
54  /// Which style to use for printing hexadecimal values.
56 
57  /// Utility function for printing annotations.
58  void printAnnotation(raw_ostream &OS, StringRef Annot);
59 
60 public:
61  MCInstPrinter(const MCAsmInfo &mai, const MCInstrInfo &mii,
62  const MCRegisterInfo &mri)
63  : CommentStream(nullptr), MAI(mai), MII(mii), MRI(mri), UseMarkup(0),
64  PrintImmHex(0), PrintHexStyle(HexStyle::C) {}
65 
66  virtual ~MCInstPrinter();
67 
68  /// \brief Specify a stream to emit comments to.
70 
71  /// \brief Print the specified MCInst to the specified raw_ostream.
72  virtual void printInst(const MCInst *MI, raw_ostream &OS, StringRef Annot,
73  const MCSubtargetInfo &STI) = 0;
74 
75  /// \brief Return the name of the specified opcode enum (e.g. "MOV32ri") or
76  /// empty if we can't resolve it.
77  StringRef getOpcodeName(unsigned Opcode) const;
78 
79  /// \brief Print the assembler register name.
80  virtual void printRegName(raw_ostream &OS, unsigned RegNo) const;
81 
82  bool getUseMarkup() const { return UseMarkup; }
83  void setUseMarkup(bool Value) { UseMarkup = Value; }
84 
85  /// Utility functions to make adding mark ups simpler.
86  StringRef markup(StringRef s) const;
87  StringRef markup(StringRef a, StringRef b) const;
88 
89  bool getPrintImmHex() const { return PrintImmHex; }
90  void setPrintImmHex(bool Value) { PrintImmHex = Value; }
91 
94 
95  /// Utility function to print immediates in decimal or hex.
97  return PrintImmHex ? formatHex(Value) : formatDec(Value);
98  }
99 
100  /// Utility functions to print decimal/hexadecimal values.
101  format_object<int64_t> formatDec(int64_t Value) const;
102  format_object<int64_t> formatHex(int64_t Value) const;
103  format_object<uint64_t> formatHex(uint64_t Value) const;
104 };
105 
106 } // namespace llvm
107 
108 #endif
bool PrintImmHex
True if we are printing immediates as hex.
Definition: MCInstPrinter.h:52
HexStyle::Style PrintHexStyle
Which style to use for printing hexadecimal values.
Definition: MCInstPrinter.h:55
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:93
void setPrintImmHex(bool Value)
Definition: MCInstPrinter.h:90
HexStyle::Style getPrintHexStyle() const
Definition: MCInstPrinter.h:92
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:58
StringRef markup(StringRef s) const
Utility functions to make adding mark ups simpler.
bool getUseMarkup() const
Definition: MCInstPrinter.h:82
void setCommentStream(raw_ostream &OS)
Specify a stream to emit comments to.
Definition: MCInstPrinter.h:69
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:96
raw_ostream * CommentStream
A stream that comments can be emitted to if desired.
Definition: MCInstPrinter.h:43
void setUseMarkup(bool Value)
Definition: MCInstPrinter.h:83
const MCAsmInfo & MAI
Definition: MCInstPrinter.h:44
bool UseMarkup
True if we are printing marked up assembly.
Definition: MCInstPrinter.h:49
This is an instance of a target assembly language printer that converts an MCInst to valid target ass...
Definition: MCInstPrinter.h:38
These are templated helper classes used by the format function that capture the object to be formated...
Definition: Format.h:79
MCSubtargetInfo - Generic base class for all target subtargets.
const MCInstrInfo & MII
Definition: MCInstPrinter.h:45
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:69
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:38
bool getPrintImmHex() const
Definition: MCInstPrinter.h:89
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
MCInstPrinter(const MCAsmInfo &mai, const MCInstrInfo &mii, const MCRegisterInfo &mri)
Definition: MCInstPrinter.h:61
const MCRegisterInfo & MRI
Definition: MCInstPrinter.h:46