blob: 6a452c8d4a7f50fd5ae42f31ea3f8cc4c4733b33 [file] [log] [blame]
Chris Lattner65b0b292009-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 Lattner7e851802010-02-11 22:39:10 +000011#include "llvm/ADT/StringRef.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000012#include "llvm/MC/MCAsmInfo.h"
13#include "llvm/MC/MCInstrInfo.h"
Craig Topper85814382012-02-07 05:05:23 +000014#include "llvm/Support/ErrorHandling.h"
Kevin Enderby14ccc902012-12-05 18:13:19 +000015#include "llvm/Support/Format.h"
Owen Andersonede042d2011-09-15 18:36:29 +000016#include "llvm/Support/raw_ostream.h"
Chris Lattner65b0b292009-09-14 01:43:38 +000017using namespace llvm;
18
19MCInstPrinter::~MCInstPrinter() {
Edward O'Callaghanaa5c1b72009-10-05 18:43:19 +000020}
Chris Lattner7e851802010-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 Kramerc97ef612012-04-02 08:32:38 +000025 return MII.getName(Opcode);
Chris Lattner7e851802010-02-11 22:39:10 +000026}
Anton Korobeynikov57caad72011-03-05 18:43:32 +000027
Rafael Espindolacde4ce42011-06-02 02:34:55 +000028void MCInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
Craig Topper85814382012-02-07 05:05:23 +000029 llvm_unreachable("Target should implement this");
Anton Korobeynikov57caad72011-03-05 18:43:32 +000030}
Owen Andersonede042d2011-09-15 18:36:29 +000031
Owen Anderson98c5dda2011-09-15 23:38:46 +000032void MCInstPrinter::printAnnotation(raw_ostream &OS, StringRef Annot) {
Owen Anderson317eaf12011-09-21 00:25:23 +000033 if (!Annot.empty()) {
34 if (CommentStream)
Kevin Enderby9e5887b2011-10-04 22:44:48 +000035 (*CommentStream) << Annot;
Owen Anderson317eaf12011-09-21 00:25:23 +000036 else
Kevin Enderby9e5887b2011-10-04 22:44:48 +000037 OS << " " << MAI.getCommentString() << " " << Annot;
Owen Anderson317eaf12011-09-21 00:25:23 +000038 }
Owen Andersonede042d2011-09-15 18:36:29 +000039}
Kevin Enderbye1d4a882012-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 Enderby14ccc902012-12-05 18:13:19 +000054
Daniel Malea44c8e342013-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 }
Duncan Sands1cd2ed82013-08-02 09:37:20 +000092 llvm_unreachable("unsupported print style");
Daniel Malea44c8e342013-08-01 21:18:16 +000093}
94
95format_object1<uint64_t> MCInstPrinter::formatHex(const uint64_t Value) const {
96 switch(PrintHexStyle) {
97 case HexStyle::C:
98 return format("0x%" PRIx64, Value);
99 case HexStyle::Asm:
100 if (needsLeadingZero(Value))
101 return format("0%" PRIx64 "h", Value);
102 else
103 return format("%" PRIx64 "h", Value);
104 }
Duncan Sands1cd2ed82013-08-02 09:37:20 +0000105 llvm_unreachable("unsupported print style");
Kevin Enderby14ccc902012-12-05 18:13:19 +0000106}