LLVM  10.0.0svn
MCInstPrinter.cpp
Go to the documentation of this file.
1 //===- MCInstPrinter.cpp - Convert an MCInst to target assembly syntax ----===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8 
10 #include "llvm/ADT/ArrayRef.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 #include <cinttypes>
18 #include <cstdint>
19 
20 using namespace llvm;
21 
23  static const char hex_rep[] = "0123456789abcdef";
24  bool First = true;
25  for (char i: bytes) {
26  if (First)
27  First = false;
28  else
29  OS << ' ';
30  OS << hex_rep[(i & 0xF0) >> 4];
31  OS << hex_rep[i & 0xF];
32  }
33 }
34 
36 
37 /// getOpcodeName - Return the name of the specified opcode enum (e.g.
38 /// "MOV32ri") or empty if we can't resolve it.
39 StringRef MCInstPrinter::getOpcodeName(unsigned Opcode) const {
40  return MII.getName(Opcode);
41 }
42 
43 void MCInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
44  llvm_unreachable("Target should implement this");
45 }
46 
48  if (!Annot.empty()) {
49  if (CommentStream) {
50  (*CommentStream) << Annot;
51  // By definition (see MCInstPrinter.h), CommentStream must end with
52  // a newline after each comment.
53  if (Annot.back() != '\n')
54  (*CommentStream) << '\n';
55  } else
56  OS << " " << MAI.getCommentString() << " " << Annot;
57  }
58 }
59 
60 /// Utility functions to make adding mark ups simpler.
62  if (getUseMarkup())
63  return s;
64  else
65  return "";
66 }
68  if (getUseMarkup())
69  return a;
70  else
71  return b;
72 }
73 
74 // For asm-style hex (e.g. 0ffh) the first digit always has to be a number.
75 static bool needsLeadingZero(uint64_t Value)
76 {
77  while (Value)
78  {
79  uint64_t digit = (Value >> 60) & 0xf;
80  if (digit != 0)
81  return (digit >= 0xa);
82  Value <<= 4;
83  }
84  return false;
85 }
86 
88  return format("%" PRId64, Value);
89 }
90 
92  switch(PrintHexStyle) {
93  case HexStyle::C:
94  if (Value < 0)
95  return format("-0x%" PRIx64, -Value);
96  else
97  return format("0x%" PRIx64, Value);
98  case HexStyle::Asm:
99  if (Value < 0) {
100  if (needsLeadingZero((uint64_t)(-Value)))
101  return format("-0%" PRIx64 "h", -Value);
102  else
103  return format("-%" PRIx64 "h", -Value);
104  } else {
105  if (needsLeadingZero((uint64_t)(Value)))
106  return format("0%" PRIx64 "h", Value);
107  else
108  return format("%" PRIx64 "h", Value);
109  }
110  }
111  llvm_unreachable("unsupported print style");
112 }
113 
115  switch(PrintHexStyle) {
116  case HexStyle::C:
117  return format("0x%" PRIx64, Value);
118  case HexStyle::Asm:
119  if (needsLeadingZero(Value))
120  return format("0%" PRIx64 "h", Value);
121  else
122  return format("%" PRIx64 "h", Value);
123  }
124  llvm_unreachable("unsupported print style");
125 }
HexStyle::Style PrintHexStyle
Which style to use for printing hexadecimal values.
Definition: MCInstPrinter.h:56
static bool needsLeadingZero(uint64_t Value)
This class represents lattice values for constants.
Definition: AllocatorList.h:23
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:123
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 bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:126
StringRef markup(StringRef s) const
Utility functions to make adding mark ups simpler.
virtual ~MCInstPrinter()
StringRef getCommentString() const
Definition: MCAsmInfo.h:492
StringRef getName(unsigned Opcode) const
Returns the name for the instructions with the given opcode.
Definition: MCInstrInfo.h:50
#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:44
const MCAsmInfo & MAI
Definition: MCInstPrinter.h:45
bool getUseMarkup() const
Definition: MCInstPrinter.h:85
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:46
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:72
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:45
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48