blob: 916aa8928c73ed3a730ce1362357be91cb326fbd [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- MCInstPrinter.h - MCInst to target assembly syntax -------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
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
15namespace llvm {
16
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010017class MCAsmInfo;
18class MCInst;
19class MCInstrInfo;
20class MCRegisterInfo;
21class MCSubtargetInfo;
22class raw_ostream;
23class StringRef;
24
25/// Convert `Bytes' to a hex string and output to `OS'
26void dumpBytes(ArrayRef<uint8_t> Bytes, raw_ostream &OS);
27
28namespace HexStyle {
29
30enum Style {
31 C, ///< 0xff
32 Asm ///< 0ffh
33};
34
35} // end namespace HexStyle
36
Andrew Scullcdfcccc2018-10-05 20:58:37 +010037/// This is an instance of a target assembly language printer that
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010038/// converts an MCInst to valid target assembly syntax.
39class MCInstPrinter {
40protected:
Andrew Scullcdfcccc2018-10-05 20:58:37 +010041 /// A stream that comments can be emitted to if desired. Each comment
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010042 /// must end with a newline. This will be null if verbose assembly emission
Andrew Scullcdfcccc2018-10-05 20:58:37 +010043 /// is disabled.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010044 raw_ostream *CommentStream = nullptr;
45 const MCAsmInfo &MAI;
46 const MCInstrInfo &MII;
47 const MCRegisterInfo &MRI;
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
61public:
62 MCInstPrinter(const MCAsmInfo &mai, const MCInstrInfo &mii,
63 const MCRegisterInfo &mri) : MAI(mai), MII(mii), MRI(mri) {}
64
65 virtual ~MCInstPrinter();
66
Andrew Scullcdfcccc2018-10-05 20:58:37 +010067 /// Specify a stream to emit comments to.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010068 void setCommentStream(raw_ostream &OS) { CommentStream = &OS; }
69
Andrew Scullcdfcccc2018-10-05 20:58:37 +010070 /// Print the specified MCInst to the specified raw_ostream.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010071 virtual void printInst(const MCInst *MI, raw_ostream &OS, StringRef Annot,
72 const MCSubtargetInfo &STI) = 0;
73
Andrew Scullcdfcccc2018-10-05 20:58:37 +010074 /// Return the name of the specified opcode enum (e.g. "MOV32ri") or
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010075 /// empty if we can't resolve it.
76 StringRef getOpcodeName(unsigned Opcode) const;
77
Andrew Scullcdfcccc2018-10-05 20:58:37 +010078 /// Print the assembler register name.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010079 virtual void printRegName(raw_ostream &OS, unsigned RegNo) const;
80
81 bool getUseMarkup() const { return UseMarkup; }
82 void setUseMarkup(bool Value) { UseMarkup = Value; }
83
84 /// Utility functions to make adding mark ups simpler.
85 StringRef markup(StringRef s) const;
86 StringRef markup(StringRef a, StringRef b) const;
87
88 bool getPrintImmHex() const { return PrintImmHex; }
89 void setPrintImmHex(bool Value) { PrintImmHex = Value; }
90
91 HexStyle::Style getPrintHexStyle() const { return PrintHexStyle; }
92 void setPrintHexStyle(HexStyle::Style Value) { PrintHexStyle = Value; }
93
94 /// Utility function to print immediates in decimal or hex.
95 format_object<int64_t> formatImm(int64_t Value) const {
96 return PrintImmHex ? formatHex(Value) : formatDec(Value);
97 }
98
99 /// Utility functions to print decimal/hexadecimal values.
100 format_object<int64_t> formatDec(int64_t Value) const;
101 format_object<int64_t> formatHex(int64_t Value) const;
102 format_object<uint64_t> formatHex(uint64_t Value) const;
103};
104
105} // end namespace llvm
106
107#endif // LLVM_MC_MCINSTPRINTER_H