blob: c5c06f323e680f4bf2aa66ba0415fd9efb4f6a0d [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";
Fangrui Song7d4ad142019-04-10 05:31:21 +000024 bool First = true;
Colin LeMahieu2048ea42015-05-28 18:39:50 +000025 for (char i: bytes) {
Fangrui Song7d4ad142019-04-10 05:31:21 +000026 if (First)
27 First = false;
28 else
29 OS << ' ';
Colin LeMahieu2048ea42015-05-28 18:39:50 +000030 OS << hex_rep[(i & 0xF0) >> 4];
31 OS << hex_rep[i & 0xF];
Colin LeMahieu2048ea42015-05-28 18:39:50 +000032 }
33}
34
Eugene Zelenkod3a6c892017-02-11 00:27:28 +000035MCInstPrinter::~MCInstPrinter() = default;
Chris Lattner52413812010-02-11 22:39:10 +000036
37/// getOpcodeName - Return the name of the specified opcode enum (e.g.
38/// "MOV32ri") or empty if we can't resolve it.
39StringRef MCInstPrinter::getOpcodeName(unsigned Opcode) const {
Benjamin Kramer1c0541b2012-04-02 08:32:38 +000040 return MII.getName(Opcode);
Chris Lattner52413812010-02-11 22:39:10 +000041}
Anton Korobeynikove7410dd2011-03-05 18:43:32 +000042
Rafael Espindolad6860522011-06-02 02:34:55 +000043void MCInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
Craig Toppera2886c22012-02-07 05:05:23 +000044 llvm_unreachable("Target should implement this");
Anton Korobeynikove7410dd2011-03-05 18:43:32 +000045}
Owen Andersond1814792011-09-15 18:36:29 +000046
Owen Andersona0c3b972011-09-15 23:38:46 +000047void MCInstPrinter::printAnnotation(raw_ostream &OS, StringRef Annot) {
Owen Anderson69fa8ff2011-09-21 00:25:23 +000048 if (!Annot.empty()) {
Quentin Colombet85f60ef2013-10-01 19:21:24 +000049 if (CommentStream) {
Kevin Enderby5dcda642011-10-04 22:44:48 +000050 (*CommentStream) << Annot;
Quentin Colombet85f60ef2013-10-01 19:21:24 +000051 // 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
Kevin Enderby5dcda642011-10-04 22:44:48 +000056 OS << " " << MAI.getCommentString() << " " << Annot;
Owen Anderson69fa8ff2011-09-21 00:25:23 +000057 }
Owen Andersond1814792011-09-15 18:36:29 +000058}
Kevin Enderbydccdac62012-10-23 22:52:52 +000059
60/// Utility functions to make adding mark ups simpler.
61StringRef MCInstPrinter::markup(StringRef s) const {
62 if (getUseMarkup())
63 return s;
64 else
65 return "";
66}
Kevin Enderby168ffb32012-12-05 18:13:19 +000067
Daniel Maleaa3d42452013-08-01 21:18:16 +000068// For asm-style hex (e.g. 0ffh) the first digit always has to be a number.
69static bool needsLeadingZero(uint64_t Value)
70{
Eugene Zelenkod3a6c892017-02-11 00:27:28 +000071 while (Value)
Daniel Maleaa3d42452013-08-01 21:18:16 +000072 {
73 uint64_t digit = (Value >> 60) & 0xf;
74 if (digit != 0)
75 return (digit >= 0xa);
76 Value <<= 4;
77 }
78 return false;
79}
80
Benjamin Krameraf09f222015-02-15 22:15:41 +000081format_object<int64_t> MCInstPrinter::formatDec(int64_t Value) const {
Daniel Maleaa3d42452013-08-01 21:18:16 +000082 return format("%" PRId64, Value);
83}
84
Benjamin Krameraf09f222015-02-15 22:15:41 +000085format_object<int64_t> MCInstPrinter::formatHex(int64_t Value) const {
Jonas Devliegherebee0f7d2019-09-06 01:13:32 +000086 switch (PrintHexStyle) {
Daniel Maleaa3d42452013-08-01 21:18:16 +000087 case HexStyle::C:
Jonas Devliegherebee0f7d2019-09-06 01:13:32 +000088 if (Value < 0) {
89 if (Value == std::numeric_limits<int64_t>::min())
90 return format<int64_t>("-0x8000000000000000", Value);
Daniel Maleaa3d42452013-08-01 21:18:16 +000091 return format("-0x%" PRIx64, -Value);
Jonas Devliegherebee0f7d2019-09-06 01:13:32 +000092 }
93 return format("0x%" PRIx64, Value);
Daniel Maleaa3d42452013-08-01 21:18:16 +000094 case HexStyle::Asm:
95 if (Value < 0) {
Jonas Devliegherebee0f7d2019-09-06 01:13:32 +000096 if (Value == std::numeric_limits<int64_t>::min())
97 return format<int64_t>("-8000000000000000h", Value);
98 if (needsLeadingZero(-(uint64_t)(Value)))
Daniel Maleaa3d42452013-08-01 21:18:16 +000099 return format("-0%" PRIx64 "h", -Value);
Jonas Devliegherebee0f7d2019-09-06 01:13:32 +0000100 return format("-%" PRIx64 "h", -Value);
Daniel Maleaa3d42452013-08-01 21:18:16 +0000101 }
Jonas Devliegherebee0f7d2019-09-06 01:13:32 +0000102 if (needsLeadingZero((uint64_t)(Value)))
103 return format("0%" PRIx64 "h", Value);
104 return format("%" PRIx64 "h", Value);
Daniel Maleaa3d42452013-08-01 21:18:16 +0000105 }
Duncan Sands3194fca2013-08-02 09:37:20 +0000106 llvm_unreachable("unsupported print style");
Daniel Maleaa3d42452013-08-01 21:18:16 +0000107}
108
Benjamin Krameraf09f222015-02-15 22:15:41 +0000109format_object<uint64_t> MCInstPrinter::formatHex(uint64_t Value) const {
Daniel Maleaa3d42452013-08-01 21:18:16 +0000110 switch(PrintHexStyle) {
111 case HexStyle::C:
112 return format("0x%" PRIx64, Value);
113 case HexStyle::Asm:
114 if (needsLeadingZero(Value))
115 return format("0%" PRIx64 "h", Value);
116 else
117 return format("%" PRIx64 "h", Value);
118 }
Duncan Sands3194fca2013-08-02 09:37:20 +0000119 llvm_unreachable("unsupported print style");
Kevin Enderby168ffb32012-12-05 18:13:19 +0000120}