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