blob: ab8773e7bbb2fa1ed5e18407ac428cd5e453518e [file] [log] [blame]
Eugene Zelenkod3a6c892017-02-11 00:27:28 +00001//===- MCInstPrinter.cpp - Convert an MCInst to target assembly syntax ----===//
Chris Lattnerde57d8e2009-09-14 01:43:38 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Chris Lattnerde57d8e2009-09-14 01:43:38 +00006//
7//===----------------------------------------------------------------------===//
8
Chandler Carruth6bda14b2017-06-06 11:49:48 +00009#include "llvm/MC/MCInstPrinter.h"
Eugene Zelenkod3a6c892017-02-11 00:27:28 +000010#include "llvm/ADT/ArrayRef.h"
Chris Lattner52413812010-02-11 22:39:10 +000011#include "llvm/ADT/StringRef.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000012#include "llvm/MC/MCAsmInfo.h"
13#include "llvm/MC/MCInstrInfo.h"
Craig Toppera2886c22012-02-07 05:05:23 +000014#include "llvm/Support/ErrorHandling.h"
Kevin Enderby168ffb32012-12-05 18:13:19 +000015#include "llvm/Support/Format.h"
Owen Andersond1814792011-09-15 18:36:29 +000016#include "llvm/Support/raw_ostream.h"
Eugene Zelenkod3a6c892017-02-11 00:27:28 +000017#include <cinttypes>
18#include <cstdint>
19
Chris Lattnerde57d8e2009-09-14 01:43:38 +000020using namespace llvm;
21
Colin LeMahieu2048ea42015-05-28 18:39:50 +000022void 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 << ' ';
28 }
29}
30
Eugene Zelenkod3a6c892017-02-11 00:27:28 +000031MCInstPrinter::~MCInstPrinter() = default;
Chris Lattner52413812010-02-11 22:39:10 +000032
33/// getOpcodeName - Return the name of the specified opcode enum (e.g.
34/// "MOV32ri") or empty if we can't resolve it.
35StringRef MCInstPrinter::getOpcodeName(unsigned Opcode) const {
Benjamin Kramer1c0541b2012-04-02 08:32:38 +000036 return MII.getName(Opcode);
Chris Lattner52413812010-02-11 22:39:10 +000037}
Anton Korobeynikove7410dd2011-03-05 18:43:32 +000038
Rafael Espindolad6860522011-06-02 02:34:55 +000039void MCInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
Craig Toppera2886c22012-02-07 05:05:23 +000040 llvm_unreachable("Target should implement this");
Anton Korobeynikove7410dd2011-03-05 18:43:32 +000041}
Owen Andersond1814792011-09-15 18:36:29 +000042
Owen Andersona0c3b972011-09-15 23:38:46 +000043void MCInstPrinter::printAnnotation(raw_ostream &OS, StringRef Annot) {
Owen Anderson69fa8ff2011-09-21 00:25:23 +000044 if (!Annot.empty()) {
Quentin Colombet85f60ef2013-10-01 19:21:24 +000045 if (CommentStream) {
Kevin Enderby5dcda642011-10-04 22:44:48 +000046 (*CommentStream) << Annot;
Quentin Colombet85f60ef2013-10-01 19:21:24 +000047 // 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
Kevin Enderby5dcda642011-10-04 22:44:48 +000052 OS << " " << MAI.getCommentString() << " " << Annot;
Owen Anderson69fa8ff2011-09-21 00:25:23 +000053 }
Owen Andersond1814792011-09-15 18:36:29 +000054}
Kevin Enderbydccdac62012-10-23 22:52:52 +000055
56/// Utility functions to make adding mark ups simpler.
57StringRef MCInstPrinter::markup(StringRef s) const {
58 if (getUseMarkup())
59 return s;
60 else
61 return "";
62}
63StringRef MCInstPrinter::markup(StringRef a, StringRef b) const {
64 if (getUseMarkup())
65 return a;
66 else
67 return b;
68}
Kevin Enderby168ffb32012-12-05 18:13:19 +000069
Daniel Maleaa3d42452013-08-01 21:18:16 +000070// For asm-style hex (e.g. 0ffh) the first digit always has to be a number.
71static bool needsLeadingZero(uint64_t Value)
72{
Eugene Zelenkod3a6c892017-02-11 00:27:28 +000073 while (Value)
Daniel Maleaa3d42452013-08-01 21:18:16 +000074 {
75 uint64_t digit = (Value >> 60) & 0xf;
76 if (digit != 0)
77 return (digit >= 0xa);
78 Value <<= 4;
79 }
80 return false;
81}
82
Benjamin Krameraf09f222015-02-15 22:15:41 +000083format_object<int64_t> MCInstPrinter::formatDec(int64_t Value) const {
Daniel Maleaa3d42452013-08-01 21:18:16 +000084 return format("%" PRId64, Value);
85}
86
Benjamin Krameraf09f222015-02-15 22:15:41 +000087format_object<int64_t> MCInstPrinter::formatHex(int64_t Value) const {
Daniel Maleaa3d42452013-08-01 21:18:16 +000088 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);
105 }
106 }
Duncan Sands3194fca2013-08-02 09:37:20 +0000107 llvm_unreachable("unsupported print style");
Daniel Maleaa3d42452013-08-01 21:18:16 +0000108}
109
Benjamin Krameraf09f222015-02-15 22:15:41 +0000110format_object<uint64_t> MCInstPrinter::formatHex(uint64_t Value) const {
Daniel Maleaa3d42452013-08-01 21:18:16 +0000111 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);
119 }
Duncan Sands3194fca2013-08-02 09:37:20 +0000120 llvm_unreachable("unsupported print style");
Kevin Enderby168ffb32012-12-05 18:13:19 +0000121}