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