MCInstPrinter.h revision 263508
1238106Sdes//===-- MCInstPrinter.h - Convert an MCInst to target assembly syntax -----===//
2238106Sdes//
3238106Sdes//                     The LLVM Compiler Infrastructure
4238106Sdes//
5238106Sdes// This file is distributed under the University of Illinois Open Source
6238106Sdes// License. See LICENSE.TXT for details.
7238106Sdes//
8238106Sdes//===----------------------------------------------------------------------===//
9238106Sdes
10238106Sdes#ifndef LLVM_MC_MCINSTPRINTER_H
11238106Sdes#define LLVM_MC_MCINSTPRINTER_H
12238106Sdes
13238106Sdes#include "llvm/Support/DataTypes.h"
14238106Sdes#include "llvm/Support/Format.h"
15238106Sdes
16238106Sdesnamespace llvm {
17238106Sdesclass MCInst;
18238106Sdesclass raw_ostream;
19238106Sdesclass MCAsmInfo;
20238106Sdesclass MCInstrInfo;
21238106Sdesclass MCRegisterInfo;
22238106Sdesclass StringRef;
23238106Sdes
24269257Sdesnamespace HexStyle {
25269257Sdes    enum Style {
26269257Sdes        C,          ///< 0xff
27269257Sdes        Asm         ///< 0ffh
28269257Sdes    };
29269257Sdes}
30269257Sdes
31269257Sdes/// MCInstPrinter - This is an instance of a target assembly language printer
32269257Sdes/// that converts an MCInst to valid target assembly syntax.
33269257Sdesclass MCInstPrinter {
34238106Sdesprotected:
35238106Sdes  /// CommentStream - a stream that comments can be emitted to if desired.
36238106Sdes  /// Each comment must end with a newline.  This will be null if verbose
37238106Sdes  /// assembly emission is disable.
38238106Sdes  raw_ostream *CommentStream;
39238106Sdes  const MCAsmInfo &MAI;
40238106Sdes  const MCInstrInfo &MII;
41238106Sdes  const MCRegisterInfo &MRI;
42238106Sdes
43238106Sdes  /// The current set of available features.
44255585Sdes  uint64_t AvailableFeatures;
45255585Sdes
46255585Sdes  /// True if we are printing marked up assembly.
47238106Sdes  bool UseMarkup;
48238106Sdes
49238106Sdes  /// True if we are printing immediates as hex.
50238106Sdes  bool PrintImmHex;
51238106Sdes
52238106Sdes  /// Which style to use for printing hexadecimal values.
53238106Sdes  HexStyle::Style PrintHexStyle;
54238106Sdes
55238106Sdes  /// Utility function for printing annotations.
56238106Sdes  void printAnnotation(raw_ostream &OS, StringRef Annot);
57238106Sdespublic:
58238106Sdes  MCInstPrinter(const MCAsmInfo &mai, const MCInstrInfo &mii,
59238106Sdes                const MCRegisterInfo &mri)
60238106Sdes    : CommentStream(0), MAI(mai), MII(mii), MRI(mri), AvailableFeatures(0),
61238106Sdes      UseMarkup(0), PrintImmHex(0), PrintHexStyle(HexStyle::C) {}
62238106Sdes
63238106Sdes  virtual ~MCInstPrinter();
64238106Sdes
65238106Sdes  /// setCommentStream - Specify a stream to emit comments to.
66238106Sdes  void setCommentStream(raw_ostream &OS) { CommentStream = &OS; }
67238106Sdes
68238106Sdes  /// printInst - Print the specified MCInst to the specified raw_ostream.
69238106Sdes  ///
70238106Sdes  virtual void printInst(const MCInst *MI, raw_ostream &OS,
71238106Sdes                         StringRef Annot) = 0;
72238106Sdes
73238106Sdes  /// getOpcodeName - Return the name of the specified opcode enum (e.g.
74238106Sdes  /// "MOV32ri") or empty if we can't resolve it.
75238106Sdes  StringRef getOpcodeName(unsigned Opcode) const;
76238106Sdes
77238106Sdes  /// printRegName - Print the assembler register name.
78238106Sdes  virtual void printRegName(raw_ostream &OS, unsigned RegNo) const;
79238106Sdes
80238106Sdes  uint64_t getAvailableFeatures() const { return AvailableFeatures; }
81238106Sdes  void setAvailableFeatures(uint64_t Value) { AvailableFeatures = Value; }
82238106Sdes
83238106Sdes  bool getUseMarkup() const { return UseMarkup; }
84238106Sdes  void setUseMarkup(bool Value) { UseMarkup = Value; }
85238106Sdes
86238106Sdes  /// Utility functions to make adding mark ups simpler.
87238106Sdes  StringRef markup(StringRef s) const;
88238106Sdes  StringRef markup(StringRef a, StringRef b) const;
89238106Sdes
90238106Sdes  bool getPrintImmHex() const { return PrintImmHex; }
91238106Sdes  void setPrintImmHex(bool Value) { PrintImmHex = Value; }
92238106Sdes
93238106Sdes  HexStyle::Style getPrintHexStyleHex() const { return PrintHexStyle; }
94238106Sdes  void setPrintImmHex(HexStyle::Style Value) { PrintHexStyle = Value; }
95238106Sdes
96238106Sdes  /// Utility function to print immediates in decimal or hex.
97238106Sdes  format_object1<int64_t> formatImm(const int64_t Value) const { return PrintImmHex ? formatHex(Value) : formatDec(Value); }
98238106Sdes
99238106Sdes  /// Utility functions to print decimal/hexadecimal values.
100238106Sdes  format_object1<int64_t> formatDec(const int64_t Value) const;
101238106Sdes  format_object1<int64_t> formatHex(const int64_t Value) const;
102238106Sdes  format_object1<uint64_t> formatHex(const uint64_t Value) const;
103238106Sdes};
104238106Sdes
105255585Sdes} // namespace llvm
106285206Sdes
107255585Sdes#endif
108255585Sdes