LLVM API Documentation

MCInstPrinter.h
Go to the documentation of this file.
00001 //===-- MCInstPrinter.h - Convert an MCInst to target assembly syntax -----===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #ifndef LLVM_MC_MCINSTPRINTER_H
00011 #define LLVM_MC_MCINSTPRINTER_H
00012 
00013 #include "llvm/Support/DataTypes.h"
00014 #include "llvm/Support/Format.h"
00015 
00016 namespace llvm {
00017 class MCInst;
00018 class raw_ostream;
00019 class MCAsmInfo;
00020 class MCInstrInfo;
00021 class MCRegisterInfo;
00022 class StringRef;
00023 
00024 namespace HexStyle {
00025     enum Style {
00026         C,          ///< 0xff
00027         Asm         ///< 0ffh
00028     };
00029 }
00030 
00031 /// MCInstPrinter - This is an instance of a target assembly language printer
00032 /// that converts an MCInst to valid target assembly syntax.
00033 class MCInstPrinter {
00034 protected:
00035   /// CommentStream - a stream that comments can be emitted to if desired.
00036   /// Each comment must end with a newline.  This will be null if verbose
00037   /// assembly emission is disable.
00038   raw_ostream *CommentStream;
00039   const MCAsmInfo &MAI;
00040   const MCInstrInfo &MII;
00041   const MCRegisterInfo &MRI;
00042 
00043   /// The current set of available features.
00044   uint64_t AvailableFeatures;
00045 
00046   /// True if we are printing marked up assembly.
00047   bool UseMarkup;
00048 
00049   /// True if we are printing immediates as hex.
00050   bool PrintImmHex;
00051 
00052   /// Which style to use for printing hexadecimal values.
00053   HexStyle::Style PrintHexStyle;
00054 
00055   /// Utility function for printing annotations.
00056   void printAnnotation(raw_ostream &OS, StringRef Annot);
00057 public:
00058   MCInstPrinter(const MCAsmInfo &mai, const MCInstrInfo &mii,
00059                 const MCRegisterInfo &mri)
00060     : CommentStream(nullptr), MAI(mai), MII(mii), MRI(mri),
00061       AvailableFeatures(0), UseMarkup(0), PrintImmHex(0),
00062       PrintHexStyle(HexStyle::C) {}
00063 
00064   virtual ~MCInstPrinter();
00065 
00066   /// setCommentStream - Specify a stream to emit comments to.
00067   void setCommentStream(raw_ostream &OS) { CommentStream = &OS; }
00068 
00069   /// printInst - Print the specified MCInst to the specified raw_ostream.
00070   ///
00071   virtual void printInst(const MCInst *MI, raw_ostream &OS,
00072                          StringRef Annot) = 0;
00073 
00074   /// getOpcodeName - Return the name of the specified opcode enum (e.g.
00075   /// "MOV32ri") or empty if we can't resolve it.
00076   StringRef getOpcodeName(unsigned Opcode) const;
00077 
00078   /// printRegName - Print the assembler register name.
00079   virtual void printRegName(raw_ostream &OS, unsigned RegNo) const;
00080 
00081   uint64_t getAvailableFeatures() const { return AvailableFeatures; }
00082   void setAvailableFeatures(uint64_t Value) { AvailableFeatures = Value; }
00083 
00084   bool getUseMarkup() const { return UseMarkup; }
00085   void setUseMarkup(bool Value) { UseMarkup = Value; }
00086 
00087   /// Utility functions to make adding mark ups simpler.
00088   StringRef markup(StringRef s) const;
00089   StringRef markup(StringRef a, StringRef b) const;
00090 
00091   bool getPrintImmHex() const { return PrintImmHex; }
00092   void setPrintImmHex(bool Value) { PrintImmHex = Value; }
00093 
00094   HexStyle::Style getPrintHexStyleHex() const { return PrintHexStyle; }
00095   void setPrintImmHex(HexStyle::Style Value) { PrintHexStyle = Value; }
00096 
00097   /// Utility function to print immediates in decimal or hex.
00098   format_object1<int64_t> formatImm(const int64_t Value) const { return PrintImmHex ? formatHex(Value) : formatDec(Value); }
00099 
00100   /// Utility functions to print decimal/hexadecimal values.
00101   format_object1<int64_t> formatDec(const int64_t Value) const;
00102   format_object1<int64_t> formatHex(const int64_t Value) const;
00103   format_object1<uint64_t> formatHex(const uint64_t Value) const;
00104 };
00105 
00106 } // namespace llvm
00107 
00108 #endif