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