LLVM API Documentation

MCInstPrinter.cpp
Go to the documentation of this file.
00001 //===-- MCInstPrinter.cpp - Convert an MCInst to target assembly syntax ---===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #include "llvm/MC/MCInstPrinter.h"
00011 #include "llvm/ADT/StringRef.h"
00012 #include "llvm/MC/MCAsmInfo.h"
00013 #include "llvm/MC/MCInstrInfo.h"
00014 #include "llvm/Support/ErrorHandling.h"
00015 #include "llvm/Support/Format.h"
00016 #include "llvm/Support/raw_ostream.h"
00017 using namespace llvm;
00018 
00019 MCInstPrinter::~MCInstPrinter() {
00020 }
00021 
00022 /// getOpcodeName - Return the name of the specified opcode enum (e.g.
00023 /// "MOV32ri") or empty if we can't resolve it.
00024 StringRef MCInstPrinter::getOpcodeName(unsigned Opcode) const {
00025   return MII.getName(Opcode);
00026 }
00027 
00028 void MCInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
00029   llvm_unreachable("Target should implement this");
00030 }
00031 
00032 void MCInstPrinter::printAnnotation(raw_ostream &OS, StringRef Annot) {
00033   if (!Annot.empty()) {
00034     if (CommentStream) {
00035       (*CommentStream) << Annot;
00036       // By definition (see MCInstPrinter.h), CommentStream must end with
00037       // a newline after each comment.
00038       if (Annot.back() != '\n')
00039         (*CommentStream) << '\n';
00040     } else
00041       OS << " " << MAI.getCommentString() << " " << Annot;
00042   }
00043 }
00044 
00045 /// Utility functions to make adding mark ups simpler.
00046 StringRef MCInstPrinter::markup(StringRef s) const {
00047   if (getUseMarkup())
00048     return s;
00049   else
00050     return "";
00051 }
00052 StringRef MCInstPrinter::markup(StringRef a, StringRef b) const {
00053   if (getUseMarkup())
00054     return a;
00055   else
00056     return b;
00057 }
00058 
00059 // For asm-style hex (e.g. 0ffh) the first digit always has to be a number.
00060 static bool needsLeadingZero(uint64_t Value)
00061 {
00062   while(Value)
00063   {
00064     uint64_t digit = (Value >> 60) & 0xf;
00065     if (digit != 0)
00066       return (digit >= 0xa);
00067     Value <<= 4;
00068   }
00069   return false;
00070 }
00071 
00072 format_object1<int64_t> MCInstPrinter::formatDec(const int64_t Value) const {
00073   return format("%" PRId64, Value);
00074 }
00075 
00076 format_object1<int64_t> MCInstPrinter::formatHex(const int64_t Value) const {
00077   switch(PrintHexStyle) {
00078   case HexStyle::C:
00079     if (Value < 0)
00080       return format("-0x%" PRIx64, -Value);
00081     else
00082       return format("0x%" PRIx64, Value);
00083   case HexStyle::Asm:
00084     if (Value < 0) {
00085       if (needsLeadingZero((uint64_t)(-Value)))
00086         return format("-0%" PRIx64 "h", -Value);
00087       else
00088         return format("-%" PRIx64 "h", -Value);
00089     } else {
00090       if (needsLeadingZero((uint64_t)(Value)))
00091         return format("0%" PRIx64 "h", Value);
00092       else
00093         return format("%" PRIx64 "h", Value);
00094     }
00095   }
00096   llvm_unreachable("unsupported print style");
00097 }
00098 
00099 format_object1<uint64_t> MCInstPrinter::formatHex(const uint64_t Value) const {
00100   switch(PrintHexStyle) {
00101   case HexStyle::C:
00102      return format("0x%" PRIx64, Value);
00103   case HexStyle::Asm:
00104     if (needsLeadingZero(Value))
00105       return format("0%" PRIx64 "h", Value);
00106     else
00107       return format("%" PRIx64 "h", Value);
00108   }
00109   llvm_unreachable("unsupported print style");
00110 }