Revert r354244 "[DAGCombiner] Eliminate dead stores to stack."
[llvm-complete.git] / lib / MC / MCInstPrinter.cpp
blobab8773e7bbb2fa1ed5e18407ac428cd5e453518e
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 //===----------------------------------------------------------------------===//
9 #include "llvm/MC/MCInstPrinter.h"
10 #include "llvm/ADT/ArrayRef.h"
11 #include "llvm/ADT/StringRef.h"
12 #include "llvm/MC/MCAsmInfo.h"
13 #include "llvm/MC/MCInstrInfo.h"
14 #include "llvm/Support/ErrorHandling.h"
15 #include "llvm/Support/Format.h"
16 #include "llvm/Support/raw_ostream.h"
17 #include <cinttypes>
18 #include <cstdint>
20 using namespace llvm;
22 void llvm::dumpBytes(ArrayRef<uint8_t> bytes, raw_ostream &OS) {
23 static const char hex_rep[] = "0123456789abcdef";
24 for (char i: bytes) {
25 OS << hex_rep[(i & 0xF0) >> 4];
26 OS << hex_rep[i & 0xF];
27 OS << ' ';
31 MCInstPrinter::~MCInstPrinter() = default;
33 /// getOpcodeName - Return the name of the specified opcode enum (e.g.
34 /// "MOV32ri") or empty if we can't resolve it.
35 StringRef MCInstPrinter::getOpcodeName(unsigned Opcode) const {
36 return MII.getName(Opcode);
39 void MCInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
40 llvm_unreachable("Target should implement this");
43 void MCInstPrinter::printAnnotation(raw_ostream &OS, StringRef Annot) {
44 if (!Annot.empty()) {
45 if (CommentStream) {
46 (*CommentStream) << Annot;
47 // By definition (see MCInstPrinter.h), CommentStream must end with
48 // a newline after each comment.
49 if (Annot.back() != '\n')
50 (*CommentStream) << '\n';
51 } else
52 OS << " " << MAI.getCommentString() << " " << Annot;
56 /// Utility functions to make adding mark ups simpler.
57 StringRef MCInstPrinter::markup(StringRef s) const {
58 if (getUseMarkup())
59 return s;
60 else
61 return "";
63 StringRef MCInstPrinter::markup(StringRef a, StringRef b) const {
64 if (getUseMarkup())
65 return a;
66 else
67 return b;
70 // For asm-style hex (e.g. 0ffh) the first digit always has to be a number.
71 static bool needsLeadingZero(uint64_t Value)
73 while (Value)
75 uint64_t digit = (Value >> 60) & 0xf;
76 if (digit != 0)
77 return (digit >= 0xa);
78 Value <<= 4;
80 return false;
83 format_object<int64_t> MCInstPrinter::formatDec(int64_t Value) const {
84 return format("%" PRId64, Value);
87 format_object<int64_t> MCInstPrinter::formatHex(int64_t Value) const {
88 switch(PrintHexStyle) {
89 case HexStyle::C:
90 if (Value < 0)
91 return format("-0x%" PRIx64, -Value);
92 else
93 return format("0x%" PRIx64, Value);
94 case HexStyle::Asm:
95 if (Value < 0) {
96 if (needsLeadingZero((uint64_t)(-Value)))
97 return format("-0%" PRIx64 "h", -Value);
98 else
99 return format("-%" PRIx64 "h", -Value);
100 } else {
101 if (needsLeadingZero((uint64_t)(Value)))
102 return format("0%" PRIx64 "h", Value);
103 else
104 return format("%" PRIx64 "h", Value);
107 llvm_unreachable("unsupported print style");
110 format_object<uint64_t> MCInstPrinter::formatHex(uint64_t Value) const {
111 switch(PrintHexStyle) {
112 case HexStyle::C:
113 return format("0x%" PRIx64, Value);
114 case HexStyle::Asm:
115 if (needsLeadingZero(Value))
116 return format("0%" PRIx64 "h", Value);
117 else
118 return format("%" PRIx64 "h", Value);
120 llvm_unreachable("unsupported print style");