blob: c729d498044befb6030e83e9a910c55ef347dbbd [file] [log] [blame]
Chris Lattnerde57d8e2009-09-14 01:43:38 +00001//===-- MCInstPrinter.cpp - Convert an MCInst to target assembly syntax ---===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/MC/MCInstPrinter.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"
Chris Lattnerde57d8e2009-09-14 01:43:38 +000017using namespace llvm;
18
19MCInstPrinter::~MCInstPrinter() {
Edward O'Callaghan50d75a62009-10-05 18:43:19 +000020}
Chris Lattner52413812010-02-11 22:39:10 +000021
22/// getOpcodeName - Return the name of the specified opcode enum (e.g.
23/// "MOV32ri") or empty if we can't resolve it.
24StringRef MCInstPrinter::getOpcodeName(unsigned Opcode) const {
Benjamin Kramer1c0541b2012-04-02 08:32:38 +000025 return MII.getName(Opcode);
Chris Lattner52413812010-02-11 22:39:10 +000026}
Anton Korobeynikove7410dd2011-03-05 18:43:32 +000027
Rafael Espindolad6860522011-06-02 02:34:55 +000028void MCInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
Craig Toppera2886c22012-02-07 05:05:23 +000029 llvm_unreachable("Target should implement this");
Anton Korobeynikove7410dd2011-03-05 18:43:32 +000030}
Owen Andersond1814792011-09-15 18:36:29 +000031
Owen Andersona0c3b972011-09-15 23:38:46 +000032void MCInstPrinter::printAnnotation(raw_ostream &OS, StringRef Annot) {
Owen Anderson69fa8ff2011-09-21 00:25:23 +000033 if (!Annot.empty()) {
34 if (CommentStream)
Kevin Enderby5dcda642011-10-04 22:44:48 +000035 (*CommentStream) << Annot;
Owen Anderson69fa8ff2011-09-21 00:25:23 +000036 else
Kevin Enderby5dcda642011-10-04 22:44:48 +000037 OS << " " << MAI.getCommentString() << " " << Annot;
Owen Anderson69fa8ff2011-09-21 00:25:23 +000038 }
Owen Andersond1814792011-09-15 18:36:29 +000039}
Kevin Enderbydccdac62012-10-23 22:52:52 +000040
41/// Utility functions to make adding mark ups simpler.
42StringRef MCInstPrinter::markup(StringRef s) const {
43 if (getUseMarkup())
44 return s;
45 else
46 return "";
47}
48StringRef MCInstPrinter::markup(StringRef a, StringRef b) const {
49 if (getUseMarkup())
50 return a;
51 else
52 return b;
53}
Kevin Enderby168ffb32012-12-05 18:13:19 +000054
Daniel Maleaa3d42452013-08-01 21:18:16 +000055// For asm-style hex (e.g. 0ffh) the first digit always has to be a number.
56static bool needsLeadingZero(uint64_t Value)
57{
58 while(Value)
59 {
60 uint64_t digit = (Value >> 60) & 0xf;
61 if (digit != 0)
62 return (digit >= 0xa);
63 Value <<= 4;
64 }
65 return false;
66}
67
68format_object1<int64_t> MCInstPrinter::formatDec(const int64_t Value) const {
69 return format("%" PRId64, Value);
70}
71
72format_object1<int64_t> MCInstPrinter::formatHex(const int64_t Value) const {
73 switch(PrintHexStyle) {
74 case HexStyle::C:
75 if (Value < 0)
76 return format("-0x%" PRIx64, -Value);
77 else
78 return format("0x%" PRIx64, Value);
79 case HexStyle::Asm:
80 if (Value < 0) {
81 if (needsLeadingZero((uint64_t)(-Value)))
82 return format("-0%" PRIx64 "h", -Value);
83 else
84 return format("-%" PRIx64 "h", -Value);
85 } else {
86 if (needsLeadingZero((uint64_t)(Value)))
87 return format("0%" PRIx64 "h", Value);
88 else
89 return format("%" PRIx64 "h", Value);
90 }
91 }
92}
93
94format_object1<uint64_t> MCInstPrinter::formatHex(const uint64_t Value) const {
95 switch(PrintHexStyle) {
96 case HexStyle::C:
97 return format("0x%" PRIx64, Value);
98 case HexStyle::Asm:
99 if (needsLeadingZero(Value))
100 return format("0%" PRIx64 "h", Value);
101 else
102 return format("%" PRIx64 "h", Value);
103 }
Kevin Enderby168ffb32012-12-05 18:13:19 +0000104}