Chris Lattner | de57d8e | 2009-09-14 01:43:38 +0000 | [diff] [blame] | 1 | //===-- 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 Lattner | 5241381 | 2010-02-11 22:39:10 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/StringRef.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 12 | #include "llvm/MC/MCAsmInfo.h" |
| 13 | #include "llvm/MC/MCInstrInfo.h" |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 14 | #include "llvm/Support/ErrorHandling.h" |
Kevin Enderby | 168ffb3 | 2012-12-05 18:13:19 +0000 | [diff] [blame] | 15 | #include "llvm/Support/Format.h" |
Owen Anderson | d181479 | 2011-09-15 18:36:29 +0000 | [diff] [blame] | 16 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | de57d8e | 2009-09-14 01:43:38 +0000 | [diff] [blame] | 17 | using namespace llvm; |
| 18 | |
| 19 | MCInstPrinter::~MCInstPrinter() { |
Edward O'Callaghan | 50d75a6 | 2009-10-05 18:43:19 +0000 | [diff] [blame] | 20 | } |
Chris Lattner | 5241381 | 2010-02-11 22:39:10 +0000 | [diff] [blame] | 21 | |
| 22 | /// getOpcodeName - Return the name of the specified opcode enum (e.g. |
| 23 | /// "MOV32ri") or empty if we can't resolve it. |
| 24 | StringRef MCInstPrinter::getOpcodeName(unsigned Opcode) const { |
Benjamin Kramer | 1c0541b | 2012-04-02 08:32:38 +0000 | [diff] [blame] | 25 | return MII.getName(Opcode); |
Chris Lattner | 5241381 | 2010-02-11 22:39:10 +0000 | [diff] [blame] | 26 | } |
Anton Korobeynikov | e7410dd | 2011-03-05 18:43:32 +0000 | [diff] [blame] | 27 | |
Rafael Espindola | d686052 | 2011-06-02 02:34:55 +0000 | [diff] [blame] | 28 | void MCInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const { |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 29 | llvm_unreachable("Target should implement this"); |
Anton Korobeynikov | e7410dd | 2011-03-05 18:43:32 +0000 | [diff] [blame] | 30 | } |
Owen Anderson | d181479 | 2011-09-15 18:36:29 +0000 | [diff] [blame] | 31 | |
Owen Anderson | a0c3b97 | 2011-09-15 23:38:46 +0000 | [diff] [blame] | 32 | void MCInstPrinter::printAnnotation(raw_ostream &OS, StringRef Annot) { |
Owen Anderson | 69fa8ff | 2011-09-21 00:25:23 +0000 | [diff] [blame] | 33 | if (!Annot.empty()) { |
Quentin Colombet | 85f60ef | 2013-10-01 19:21:24 +0000 | [diff] [blame^] | 34 | if (CommentStream) { |
Kevin Enderby | 5dcda64 | 2011-10-04 22:44:48 +0000 | [diff] [blame] | 35 | (*CommentStream) << Annot; |
Quentin Colombet | 85f60ef | 2013-10-01 19:21:24 +0000 | [diff] [blame^] | 36 | // By definition (see MCInstPrinter.h), CommentStream must end with |
| 37 | // a newline after each comment. |
| 38 | if (Annot.back() != '\n') |
| 39 | (*CommentStream) << '\n'; |
| 40 | } else |
Kevin Enderby | 5dcda64 | 2011-10-04 22:44:48 +0000 | [diff] [blame] | 41 | OS << " " << MAI.getCommentString() << " " << Annot; |
Owen Anderson | 69fa8ff | 2011-09-21 00:25:23 +0000 | [diff] [blame] | 42 | } |
Owen Anderson | d181479 | 2011-09-15 18:36:29 +0000 | [diff] [blame] | 43 | } |
Kevin Enderby | dccdac6 | 2012-10-23 22:52:52 +0000 | [diff] [blame] | 44 | |
| 45 | /// Utility functions to make adding mark ups simpler. |
| 46 | StringRef MCInstPrinter::markup(StringRef s) const { |
| 47 | if (getUseMarkup()) |
| 48 | return s; |
| 49 | else |
| 50 | return ""; |
| 51 | } |
| 52 | StringRef MCInstPrinter::markup(StringRef a, StringRef b) const { |
| 53 | if (getUseMarkup()) |
| 54 | return a; |
| 55 | else |
| 56 | return b; |
| 57 | } |
Kevin Enderby | 168ffb3 | 2012-12-05 18:13:19 +0000 | [diff] [blame] | 58 | |
Daniel Malea | a3d4245 | 2013-08-01 21:18:16 +0000 | [diff] [blame] | 59 | // For asm-style hex (e.g. 0ffh) the first digit always has to be a number. |
| 60 | static bool needsLeadingZero(uint64_t Value) |
| 61 | { |
| 62 | while(Value) |
| 63 | { |
| 64 | uint64_t digit = (Value >> 60) & 0xf; |
| 65 | if (digit != 0) |
| 66 | return (digit >= 0xa); |
| 67 | Value <<= 4; |
| 68 | } |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | format_object1<int64_t> MCInstPrinter::formatDec(const int64_t Value) const { |
| 73 | return format("%" PRId64, Value); |
| 74 | } |
| 75 | |
| 76 | format_object1<int64_t> MCInstPrinter::formatHex(const int64_t Value) const { |
| 77 | switch(PrintHexStyle) { |
| 78 | case HexStyle::C: |
| 79 | if (Value < 0) |
| 80 | return format("-0x%" PRIx64, -Value); |
| 81 | else |
| 82 | return format("0x%" PRIx64, Value); |
| 83 | case HexStyle::Asm: |
| 84 | if (Value < 0) { |
| 85 | if (needsLeadingZero((uint64_t)(-Value))) |
| 86 | return format("-0%" PRIx64 "h", -Value); |
| 87 | else |
| 88 | return format("-%" PRIx64 "h", -Value); |
| 89 | } else { |
| 90 | if (needsLeadingZero((uint64_t)(Value))) |
| 91 | return format("0%" PRIx64 "h", Value); |
| 92 | else |
| 93 | return format("%" PRIx64 "h", Value); |
| 94 | } |
| 95 | } |
Duncan Sands | 3194fca | 2013-08-02 09:37:20 +0000 | [diff] [blame] | 96 | llvm_unreachable("unsupported print style"); |
Daniel Malea | a3d4245 | 2013-08-01 21:18:16 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | format_object1<uint64_t> MCInstPrinter::formatHex(const uint64_t Value) const { |
| 100 | switch(PrintHexStyle) { |
| 101 | case HexStyle::C: |
| 102 | return format("0x%" PRIx64, Value); |
| 103 | case HexStyle::Asm: |
| 104 | if (needsLeadingZero(Value)) |
| 105 | return format("0%" PRIx64 "h", Value); |
| 106 | else |
| 107 | return format("%" PRIx64 "h", Value); |
| 108 | } |
Duncan Sands | 3194fca | 2013-08-02 09:37:20 +0000 | [diff] [blame] | 109 | llvm_unreachable("unsupported print style"); |
Kevin Enderby | 168ffb3 | 2012-12-05 18:13:19 +0000 | [diff] [blame] | 110 | } |