Home | History | Annotate | Download | only in MC
      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"
     11 #include "llvm/ADT/StringRef.h"
     12 #include "llvm/MC/MCAsmInfo.h"
     13 #include "llvm/MC/MCInstrInfo.h"
     14 #include "llvm/Support/ErrorHandling.h"
     15 #include "llvm/Support/Format.h"
     16 #include "llvm/Support/raw_ostream.h"
     17 using namespace llvm;
     18 
     19 MCInstPrinter::~MCInstPrinter() {
     20 }
     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 {
     25   return MII.getName(Opcode);
     26 }
     27 
     28 void MCInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
     29   llvm_unreachable("Target should implement this");
     30 }
     31 
     32 void MCInstPrinter::printAnnotation(raw_ostream &OS, StringRef Annot) {
     33   if (!Annot.empty()) {
     34     if (CommentStream)
     35       (*CommentStream) << Annot;
     36     else
     37       OS << " " << MAI.getCommentString() << " " << Annot;
     38   }
     39 }
     40 
     41 /// Utility functions to make adding mark ups simpler.
     42 StringRef MCInstPrinter::markup(StringRef s) const {
     43   if (getUseMarkup())
     44     return s;
     45   else
     46     return "";
     47 }
     48 StringRef MCInstPrinter::markup(StringRef a, StringRef b) const {
     49   if (getUseMarkup())
     50     return a;
     51   else
     52     return b;
     53 }
     54 
     55 // For asm-style hex (e.g. 0ffh) the first digit always has to be a number.
     56 static 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 
     68 format_object1<int64_t> MCInstPrinter::formatDec(const int64_t Value) const {
     69   return format("%" PRId64, Value);
     70 }
     71 
     72 format_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   llvm_unreachable("unsupported print style");
     93 }
     94 
     95 format_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   }
    105   llvm_unreachable("unsupported print style");
    106 }
    107