2009-09-14 01:43:38 +00:00
|
|
|
//===-- MCInstPrinter.cpp - Convert an MCInst to target assembly syntax ---===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/MC/MCInstPrinter.h"
|
2010-02-11 22:39:10 +00:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
|
|
|
#include "llvm/MC/MCInstrInfo.h"
|
2012-02-07 05:05:23 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2012-12-05 18:13:19 +00:00
|
|
|
#include "llvm/Support/Format.h"
|
2011-09-15 18:36:29 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2009-09-14 01:43:38 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
MCInstPrinter::~MCInstPrinter() {
|
2009-10-05 18:43:19 +00:00
|
|
|
}
|
2010-02-11 22:39:10 +00:00
|
|
|
|
|
|
|
/// getOpcodeName - Return the name of the specified opcode enum (e.g.
|
|
|
|
/// "MOV32ri") or empty if we can't resolve it.
|
|
|
|
StringRef MCInstPrinter::getOpcodeName(unsigned Opcode) const {
|
2012-04-02 08:32:38 +00:00
|
|
|
return MII.getName(Opcode);
|
2010-02-11 22:39:10 +00:00
|
|
|
}
|
2011-03-05 18:43:32 +00:00
|
|
|
|
2011-06-02 02:34:55 +00:00
|
|
|
void MCInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
|
2012-02-07 05:05:23 +00:00
|
|
|
llvm_unreachable("Target should implement this");
|
2011-03-05 18:43:32 +00:00
|
|
|
}
|
2011-09-15 18:36:29 +00:00
|
|
|
|
2011-09-15 23:38:46 +00:00
|
|
|
void MCInstPrinter::printAnnotation(raw_ostream &OS, StringRef Annot) {
|
2011-09-21 00:25:23 +00:00
|
|
|
if (!Annot.empty()) {
|
2013-10-01 19:21:24 +00:00
|
|
|
if (CommentStream) {
|
2011-10-04 22:44:48 +00:00
|
|
|
(*CommentStream) << Annot;
|
2013-10-01 19:21:24 +00:00
|
|
|
// By definition (see MCInstPrinter.h), CommentStream must end with
|
|
|
|
// a newline after each comment.
|
|
|
|
if (Annot.back() != '\n')
|
|
|
|
(*CommentStream) << '\n';
|
|
|
|
} else
|
2011-10-04 22:44:48 +00:00
|
|
|
OS << " " << MAI.getCommentString() << " " << Annot;
|
2011-09-21 00:25:23 +00:00
|
|
|
}
|
2011-09-15 18:36:29 +00:00
|
|
|
}
|
2012-10-23 22:52:52 +00:00
|
|
|
|
|
|
|
/// Utility functions to make adding mark ups simpler.
|
|
|
|
StringRef MCInstPrinter::markup(StringRef s) const {
|
|
|
|
if (getUseMarkup())
|
|
|
|
return s;
|
|
|
|
else
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
StringRef MCInstPrinter::markup(StringRef a, StringRef b) const {
|
|
|
|
if (getUseMarkup())
|
|
|
|
return a;
|
|
|
|
else
|
|
|
|
return b;
|
|
|
|
}
|
2012-12-05 18:13:19 +00:00
|
|
|
|
2013-08-01 21:18:16 +00:00
|
|
|
// For asm-style hex (e.g. 0ffh) the first digit always has to be a number.
|
|
|
|
static bool needsLeadingZero(uint64_t Value)
|
|
|
|
{
|
|
|
|
while(Value)
|
|
|
|
{
|
|
|
|
uint64_t digit = (Value >> 60) & 0xf;
|
|
|
|
if (digit != 0)
|
|
|
|
return (digit >= 0xa);
|
|
|
|
Value <<= 4;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
format_object1<int64_t> MCInstPrinter::formatDec(const int64_t Value) const {
|
|
|
|
return format("%" PRId64, Value);
|
|
|
|
}
|
|
|
|
|
|
|
|
format_object1<int64_t> MCInstPrinter::formatHex(const int64_t Value) const {
|
|
|
|
switch(PrintHexStyle) {
|
|
|
|
case HexStyle::C:
|
|
|
|
if (Value < 0)
|
|
|
|
return format("-0x%" PRIx64, -Value);
|
|
|
|
else
|
|
|
|
return format("0x%" PRIx64, Value);
|
|
|
|
case HexStyle::Asm:
|
|
|
|
if (Value < 0) {
|
|
|
|
if (needsLeadingZero((uint64_t)(-Value)))
|
|
|
|
return format("-0%" PRIx64 "h", -Value);
|
|
|
|
else
|
|
|
|
return format("-%" PRIx64 "h", -Value);
|
|
|
|
} else {
|
|
|
|
if (needsLeadingZero((uint64_t)(Value)))
|
|
|
|
return format("0%" PRIx64 "h", Value);
|
|
|
|
else
|
|
|
|
return format("%" PRIx64 "h", Value);
|
|
|
|
}
|
|
|
|
}
|
2013-08-02 09:37:20 +00:00
|
|
|
llvm_unreachable("unsupported print style");
|
2013-08-01 21:18:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
format_object1<uint64_t> MCInstPrinter::formatHex(const uint64_t Value) const {
|
|
|
|
switch(PrintHexStyle) {
|
|
|
|
case HexStyle::C:
|
|
|
|
return format("0x%" PRIx64, Value);
|
|
|
|
case HexStyle::Asm:
|
|
|
|
if (needsLeadingZero(Value))
|
|
|
|
return format("0%" PRIx64 "h", Value);
|
|
|
|
else
|
|
|
|
return format("%" PRIx64 "h", Value);
|
|
|
|
}
|
2013-08-02 09:37:20 +00:00
|
|
|
llvm_unreachable("unsupported print style");
|
2012-12-05 18:13:19 +00:00
|
|
|
}
|