1 //===- MCInstPrinter.cpp - Convert an MCInst to target assembly syntax ----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
15 #include "llvm/Support/ErrorHandling.h"
16 #include "llvm/Support/Format.h"
17 #include "llvm/Support/raw_ostream.h"
23 void llvm::dumpBytes(ArrayRef
<uint8_t> bytes
, raw_ostream
&OS
) {
24 static const char hex_rep
[] = "0123456789abcdef";
26 OS
<< hex_rep
[(i
& 0xF0) >> 4];
27 OS
<< hex_rep
[i
& 0xF];
32 MCInstPrinter::~MCInstPrinter() = default;
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
);
40 void MCInstPrinter::printRegName(raw_ostream
&OS
, unsigned RegNo
) const {
41 llvm_unreachable("Target should implement this");
44 void MCInstPrinter::printAnnotation(raw_ostream
&OS
, StringRef Annot
) {
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';
53 OS
<< " " << MAI
.getCommentString() << " " << Annot
;
57 /// Utility functions to make adding mark ups simpler.
58 StringRef
MCInstPrinter::markup(StringRef s
) const {
64 StringRef
MCInstPrinter::markup(StringRef a
, StringRef b
) const {
71 // For asm-style hex (e.g. 0ffh) the first digit always has to be a number.
72 static bool needsLeadingZero(uint64_t Value
)
76 uint64_t digit
= (Value
>> 60) & 0xf;
78 return (digit
>= 0xa);
84 format_object
<int64_t> MCInstPrinter::formatDec(int64_t Value
) const {
85 return format("%" PRId64
, Value
);
88 format_object
<int64_t> MCInstPrinter::formatHex(int64_t Value
) const {
89 switch(PrintHexStyle
) {
92 return format("-0x%" PRIx64
, -Value
);
94 return format("0x%" PRIx64
, Value
);
97 if (needsLeadingZero((uint64_t)(-Value
)))
98 return format("-0%" PRIx64
"h", -Value
);
100 return format("-%" PRIx64
"h", -Value
);
102 if (needsLeadingZero((uint64_t)(Value
)))
103 return format("0%" PRIx64
"h", Value
);
105 return format("%" PRIx64
"h", Value
);
108 llvm_unreachable("unsupported print style");
111 format_object
<uint64_t> MCInstPrinter::formatHex(uint64_t Value
) const {
112 switch(PrintHexStyle
) {
114 return format("0x%" PRIx64
, Value
);
116 if (needsLeadingZero(Value
))
117 return format("0%" PRIx64
"h", Value
);
119 return format("%" PRIx64
"h", Value
);
121 llvm_unreachable("unsupported print style");