LLVM  8.0.1
MCInstPrinter.cpp
Go to the documentation of this file.
1 //===- MCInstPrinter.cpp - 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 #include "llvm/MC/MCInstPrinter.h"
11 #include "llvm/ADT/ArrayRef.h"
12 #include "llvm/ADT/StringRef.h"
13 #include "llvm/MC/MCAsmInfo.h"
14 #include "llvm/MC/MCInstrInfo.h"
16 #include "llvm/Support/Format.h"
18 #include <cinttypes>
19 #include <cstdint>
20 
21 using namespace llvm;
22 
24  static const char hex_rep[] = "0123456789abcdef";
25  for (char i: bytes) {
26  OS << hex_rep[(i & 0xF0) >> 4];
27  OS << hex_rep[i & 0xF];
28  OS << ' ';
29  }
30 }
31 
33 
34 /// getOpcodeName - Return the name of the specified opcode enum (e.g.
35 /// "MOV32ri") or empty if we can't resolve it.
36 StringRef MCInstPrinter::getOpcodeName(unsigned Opcode) const {
37  return MII.getName(Opcode);
38 }
39 
40 void MCInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
41  llvm_unreachable("Target should implement this");
42 }
43 
45  if (!Annot.empty()) {
46  if (CommentStream) {
47  (*CommentStream) << Annot;
48  // By definition (see MCInstPrinter.h), CommentStream must end with
49  // a newline after each comment.
50  if (Annot.back() != '\n')
51  (*CommentStream) << '\n';
52  } else
53  OS << " " << MAI.getCommentString() << " " << Annot;
54  }
55 }
56 
57 /// Utility functions to make adding mark ups simpler.
59  if (getUseMarkup())
60  return s;
61  else
62  return "";
63 }
65  if (getUseMarkup())
66  return a;
67  else
68  return b;
69 }
70 
71 // For asm-style hex (e.g. 0ffh) the first digit always has to be a number.
72 static bool needsLeadingZero(uint64_t Value)
73 {
74  while (Value)
75  {
76  uint64_t digit = (Value >> 60) & 0xf;
77  if (digit != 0)
78  return (digit >= 0xa);
79  Value <<= 4;
80  }
81  return false;
82 }
83 
85  return format("%" PRId64, Value);
86 }
87 
89  switch(PrintHexStyle) {
90  case HexStyle::C:
91  if (Value < 0)
92  return format("-0x%" PRIx64, -Value);
93  else
94  return format("0x%" PRIx64, Value);
95  case HexStyle::Asm:
96  if (Value < 0) {
97  if (needsLeadingZero((uint64_t)(-Value)))
98  return format("-0%" PRIx64 "h", -Value);
99  else
100  return format("-%" PRIx64 "h", -Value);
101  } else {
102  if (needsLeadingZero((uint64_t)(Value)))
103  return format("0%" PRIx64 "h", Value);
104  else
105  return format("%" PRIx64 "h", Value);
106  }
107  }
108  llvm_unreachable("unsupported print style");
109 }
110 
112  switch(PrintHexStyle) {
113  case HexStyle::C:
114  return format("0x%" PRIx64, Value);
115  case HexStyle::Asm:
116  if (needsLeadingZero(Value))
117  return format("0%" PRIx64 "h", Value);
118  else
119  return format("%" PRIx64 "h", Value);
120  }
121  llvm_unreachable("unsupported print style");
122 }
HexStyle::Style PrintHexStyle
Which style to use for printing hexadecimal values.
Definition: MCInstPrinter.h:57
static bool needsLeadingZero(uint64_t Value)
This class represents lattice values for constants.
Definition: AllocatorList.h:24
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:124
virtual void printRegName(raw_ostream &OS, unsigned RegNo) const
Print the assembler register name.
format_object< int64_t > formatDec(int64_t Value) const
Utility functions to print decimal/hexadecimal values.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
StringRef markup(StringRef s) const
Utility functions to make adding mark ups simpler.
virtual ~MCInstPrinter()
StringRef getCommentString() const
Definition: MCAsmInfo.h:486
StringRef getName(unsigned Opcode) const
Returns the name for the instructions with the given opcode.
Definition: MCInstrInfo.h:51
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
raw_ostream * CommentStream
A stream that comments can be emitted to if desired.
Definition: MCInstPrinter.h:45
const MCAsmInfo & MAI
Definition: MCInstPrinter.h:46
bool getUseMarkup() const
Definition: MCInstPrinter.h:82
StringRef getOpcodeName(unsigned Opcode) const
Return the name of the specified opcode enum (e.g.
format_object< int64_t > formatHex(int64_t Value) const
const MCInstrInfo & MII
Definition: MCInstPrinter.h:47
void dumpBytes(ArrayRef< uint8_t > Bytes, raw_ostream &OS)
Convert `Bytes&#39; to a hex string and output to `OS&#39;.
LLVM Value Representation.
Definition: Value.h:73
void printAnnotation(raw_ostream &OS, StringRef Annot)
Utility function for printing annotations.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49