xref: /aosp_15_r20/external/llvm/lib/MC/MCInstPrinter.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===-- MCInstPrinter.cpp - Convert an MCInst to target assembly syntax ---===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker 
10*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCInstPrinter.h"
11*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/StringRef.h"
12*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCAsmInfo.h"
13*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCInstrInfo.h"
14*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ErrorHandling.h"
15*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Format.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
17*9880d681SAndroid Build Coastguard Worker using namespace llvm;
18*9880d681SAndroid Build Coastguard Worker 
dumpBytes(ArrayRef<uint8_t> bytes,raw_ostream & OS)19*9880d681SAndroid Build Coastguard Worker void llvm::dumpBytes(ArrayRef<uint8_t> bytes, raw_ostream &OS) {
20*9880d681SAndroid Build Coastguard Worker   static const char hex_rep[] = "0123456789abcdef";
21*9880d681SAndroid Build Coastguard Worker   for (char i: bytes) {
22*9880d681SAndroid Build Coastguard Worker     OS << hex_rep[(i & 0xF0) >> 4];
23*9880d681SAndroid Build Coastguard Worker     OS << hex_rep[i & 0xF];
24*9880d681SAndroid Build Coastguard Worker     OS << ' ';
25*9880d681SAndroid Build Coastguard Worker   }
26*9880d681SAndroid Build Coastguard Worker }
27*9880d681SAndroid Build Coastguard Worker 
~MCInstPrinter()28*9880d681SAndroid Build Coastguard Worker MCInstPrinter::~MCInstPrinter() {
29*9880d681SAndroid Build Coastguard Worker }
30*9880d681SAndroid Build Coastguard Worker 
31*9880d681SAndroid Build Coastguard Worker /// getOpcodeName - Return the name of the specified opcode enum (e.g.
32*9880d681SAndroid Build Coastguard Worker /// "MOV32ri") or empty if we can't resolve it.
getOpcodeName(unsigned Opcode) const33*9880d681SAndroid Build Coastguard Worker StringRef MCInstPrinter::getOpcodeName(unsigned Opcode) const {
34*9880d681SAndroid Build Coastguard Worker   return MII.getName(Opcode);
35*9880d681SAndroid Build Coastguard Worker }
36*9880d681SAndroid Build Coastguard Worker 
printRegName(raw_ostream & OS,unsigned RegNo) const37*9880d681SAndroid Build Coastguard Worker void MCInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
38*9880d681SAndroid Build Coastguard Worker   llvm_unreachable("Target should implement this");
39*9880d681SAndroid Build Coastguard Worker }
40*9880d681SAndroid Build Coastguard Worker 
printAnnotation(raw_ostream & OS,StringRef Annot)41*9880d681SAndroid Build Coastguard Worker void MCInstPrinter::printAnnotation(raw_ostream &OS, StringRef Annot) {
42*9880d681SAndroid Build Coastguard Worker   if (!Annot.empty()) {
43*9880d681SAndroid Build Coastguard Worker     if (CommentStream) {
44*9880d681SAndroid Build Coastguard Worker       (*CommentStream) << Annot;
45*9880d681SAndroid Build Coastguard Worker       // By definition (see MCInstPrinter.h), CommentStream must end with
46*9880d681SAndroid Build Coastguard Worker       // a newline after each comment.
47*9880d681SAndroid Build Coastguard Worker       if (Annot.back() != '\n')
48*9880d681SAndroid Build Coastguard Worker         (*CommentStream) << '\n';
49*9880d681SAndroid Build Coastguard Worker     } else
50*9880d681SAndroid Build Coastguard Worker       OS << " " << MAI.getCommentString() << " " << Annot;
51*9880d681SAndroid Build Coastguard Worker   }
52*9880d681SAndroid Build Coastguard Worker }
53*9880d681SAndroid Build Coastguard Worker 
54*9880d681SAndroid Build Coastguard Worker /// Utility functions to make adding mark ups simpler.
markup(StringRef s) const55*9880d681SAndroid Build Coastguard Worker StringRef MCInstPrinter::markup(StringRef s) const {
56*9880d681SAndroid Build Coastguard Worker   if (getUseMarkup())
57*9880d681SAndroid Build Coastguard Worker     return s;
58*9880d681SAndroid Build Coastguard Worker   else
59*9880d681SAndroid Build Coastguard Worker     return "";
60*9880d681SAndroid Build Coastguard Worker }
markup(StringRef a,StringRef b) const61*9880d681SAndroid Build Coastguard Worker StringRef MCInstPrinter::markup(StringRef a, StringRef b) const {
62*9880d681SAndroid Build Coastguard Worker   if (getUseMarkup())
63*9880d681SAndroid Build Coastguard Worker     return a;
64*9880d681SAndroid Build Coastguard Worker   else
65*9880d681SAndroid Build Coastguard Worker     return b;
66*9880d681SAndroid Build Coastguard Worker }
67*9880d681SAndroid Build Coastguard Worker 
68*9880d681SAndroid Build Coastguard Worker // For asm-style hex (e.g. 0ffh) the first digit always has to be a number.
needsLeadingZero(uint64_t Value)69*9880d681SAndroid Build Coastguard Worker static bool needsLeadingZero(uint64_t Value)
70*9880d681SAndroid Build Coastguard Worker {
71*9880d681SAndroid Build Coastguard Worker   while(Value)
72*9880d681SAndroid Build Coastguard Worker   {
73*9880d681SAndroid Build Coastguard Worker     uint64_t digit = (Value >> 60) & 0xf;
74*9880d681SAndroid Build Coastguard Worker     if (digit != 0)
75*9880d681SAndroid Build Coastguard Worker       return (digit >= 0xa);
76*9880d681SAndroid Build Coastguard Worker     Value <<= 4;
77*9880d681SAndroid Build Coastguard Worker   }
78*9880d681SAndroid Build Coastguard Worker   return false;
79*9880d681SAndroid Build Coastguard Worker }
80*9880d681SAndroid Build Coastguard Worker 
formatDec(int64_t Value) const81*9880d681SAndroid Build Coastguard Worker format_object<int64_t> MCInstPrinter::formatDec(int64_t Value) const {
82*9880d681SAndroid Build Coastguard Worker   return format("%" PRId64, Value);
83*9880d681SAndroid Build Coastguard Worker }
84*9880d681SAndroid Build Coastguard Worker 
formatHex(int64_t Value) const85*9880d681SAndroid Build Coastguard Worker format_object<int64_t> MCInstPrinter::formatHex(int64_t Value) const {
86*9880d681SAndroid Build Coastguard Worker   switch(PrintHexStyle) {
87*9880d681SAndroid Build Coastguard Worker   case HexStyle::C:
88*9880d681SAndroid Build Coastguard Worker     if (Value < 0)
89*9880d681SAndroid Build Coastguard Worker       return format("-0x%" PRIx64, -Value);
90*9880d681SAndroid Build Coastguard Worker     else
91*9880d681SAndroid Build Coastguard Worker       return format("0x%" PRIx64, Value);
92*9880d681SAndroid Build Coastguard Worker   case HexStyle::Asm:
93*9880d681SAndroid Build Coastguard Worker     if (Value < 0) {
94*9880d681SAndroid Build Coastguard Worker       if (needsLeadingZero((uint64_t)(-Value)))
95*9880d681SAndroid Build Coastguard Worker         return format("-0%" PRIx64 "h", -Value);
96*9880d681SAndroid Build Coastguard Worker       else
97*9880d681SAndroid Build Coastguard Worker         return format("-%" PRIx64 "h", -Value);
98*9880d681SAndroid Build Coastguard Worker     } else {
99*9880d681SAndroid Build Coastguard Worker       if (needsLeadingZero((uint64_t)(Value)))
100*9880d681SAndroid Build Coastguard Worker         return format("0%" PRIx64 "h", Value);
101*9880d681SAndroid Build Coastguard Worker       else
102*9880d681SAndroid Build Coastguard Worker         return format("%" PRIx64 "h", Value);
103*9880d681SAndroid Build Coastguard Worker     }
104*9880d681SAndroid Build Coastguard Worker   }
105*9880d681SAndroid Build Coastguard Worker   llvm_unreachable("unsupported print style");
106*9880d681SAndroid Build Coastguard Worker }
107*9880d681SAndroid Build Coastguard Worker 
formatHex(uint64_t Value) const108*9880d681SAndroid Build Coastguard Worker format_object<uint64_t> MCInstPrinter::formatHex(uint64_t Value) const {
109*9880d681SAndroid Build Coastguard Worker   switch(PrintHexStyle) {
110*9880d681SAndroid Build Coastguard Worker   case HexStyle::C:
111*9880d681SAndroid Build Coastguard Worker      return format("0x%" PRIx64, Value);
112*9880d681SAndroid Build Coastguard Worker   case HexStyle::Asm:
113*9880d681SAndroid Build Coastguard Worker     if (needsLeadingZero(Value))
114*9880d681SAndroid Build Coastguard Worker       return format("0%" PRIx64 "h", Value);
115*9880d681SAndroid Build Coastguard Worker     else
116*9880d681SAndroid Build Coastguard Worker       return format("%" PRIx64 "h", Value);
117*9880d681SAndroid Build Coastguard Worker   }
118*9880d681SAndroid Build Coastguard Worker   llvm_unreachable("unsupported print style");
119*9880d681SAndroid Build Coastguard Worker }
120