LLVM  9.0.0svn
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 MCInstrInfo;
20 class MCRegisterInfo;
21 class MCSubtargetInfo;
22 class raw_ostream;
23 class StringRef;
24 
25 /// Convert `Bytes' to a hex string and output to `OS'
26 void dumpBytes(ArrayRef<uint8_t> Bytes, raw_ostream &OS);
27 
28 namespace HexStyle {
29 
30 enum Style {
31  C, ///< 0xff
32  Asm ///< 0ffh
33 };
34 
35 } // end namespace HexStyle
36 
37 /// This is an instance of a target assembly language printer that
38 /// converts an MCInst to valid target assembly syntax.
40 protected:
41  /// A stream that comments can be emitted to if desired. Each comment
42  /// must end with a newline. This will be null if verbose assembly emission
43  /// is disabled.
44  raw_ostream *CommentStream = nullptr;
45  const MCAsmInfo &MAI;
46  const MCInstrInfo &MII;
48 
49  /// True if we are printing marked up assembly.
50  bool UseMarkup = false;
51 
52  /// True if we are printing immediates as hex.
53  bool PrintImmHex = false;
54 
55  /// Which style to use for printing hexadecimal values.
56  HexStyle::Style PrintHexStyle = HexStyle::C;
57 
58  /// Utility function for printing annotations.
59  void printAnnotation(raw_ostream &OS, StringRef Annot);
60 
61 public:
62  MCInstPrinter(const MCAsmInfo &mai, const MCInstrInfo &mii,
63  const MCRegisterInfo &mri) : MAI(mai), MII(mii), MRI(mri) {}
64 
65  virtual ~MCInstPrinter();
66 
67  /// Customize the printer according to a command line option.
68  /// @return true if the option is recognized and applied.
69  virtual bool applyTargetSpecificCLOption(StringRef Opt) { return false; }
70 
71  /// Specify a stream to emit comments to.
72  void setCommentStream(raw_ostream &OS) { CommentStream = &OS; }
73 
74  /// 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  /// 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  /// 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 
95  HexStyle::Style getPrintHexStyle() const { return PrintHexStyle; }
96  void setPrintHexStyle(HexStyle::Style Value) { PrintHexStyle = Value; }
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
virtual bool applyTargetSpecificCLOption(StringRef Opt)
Customize the printer according to a command line option.
Definition: MCInstPrinter.h:69
This class represents lattice values for constants.
Definition: AllocatorList.h:23
void setPrintHexStyle(HexStyle::Style Value)
Definition: MCInstPrinter.h:96
void setPrintImmHex(bool Value)
Definition: MCInstPrinter.h:93
format_object< int64_t > formatImm(int64_t Value) const
Utility function to print immediates in decimal or hex.
Definition: MCInstPrinter.h:99
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:55
bool getPrintImmHex() const
Definition: MCInstPrinter.h:92
void setCommentStream(raw_ostream &OS)
Specify a stream to emit comments to.
Definition: MCInstPrinter.h:72
HexStyle::Style getPrintHexStyle() const
Definition: MCInstPrinter.h:95
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:23
void setUseMarkup(bool Value)
Definition: MCInstPrinter.h:86
const MCAsmInfo & MAI
Definition: MCInstPrinter.h:45
bool getUseMarkup() const
Definition: MCInstPrinter.h:85
This is an instance of a target assembly language printer that converts an MCInst to valid target ass...
Definition: MCInstPrinter.h:39
Generic base class for all target subtargets.
const MCInstrInfo & MII
Definition: MCInstPrinter.h:46
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:72
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
MCInstPrinter(const MCAsmInfo &mai, const MCInstrInfo &mii, const MCRegisterInfo &mri)
Definition: MCInstPrinter.h:62
const MCRegisterInfo & MRI
Definition: MCInstPrinter.h:47