2017-02-11 01:27:28 +01:00
|
|
|
//===- MCInstPrinter.cpp - Convert an MCInst to target assembly syntax ----===//
|
2009-09-14 03:43:38 +02:00
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2009-09-14 03:43:38 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-06-06 13:49:48 +02:00
|
|
|
#include "llvm/MC/MCInstPrinter.h"
|
2017-02-11 01:27:28 +01:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2010-02-11 23:39:10 +01:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2012-12-03 17:50:05 +01:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
|
|
|
#include "llvm/MC/MCInstrInfo.h"
|
2012-02-07 06:05:23 +01:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2012-12-05 19:13:19 +01:00
|
|
|
#include "llvm/Support/Format.h"
|
2011-09-15 20:36:29 +02:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-02-11 01:27:28 +01:00
|
|
|
#include <cinttypes>
|
|
|
|
#include <cstdint>
|
|
|
|
|
2009-09-14 03:43:38 +02:00
|
|
|
using namespace llvm;
|
|
|
|
|
2015-05-28 20:39:50 +02:00
|
|
|
void llvm::dumpBytes(ArrayRef<uint8_t> bytes, raw_ostream &OS) {
|
|
|
|
static const char hex_rep[] = "0123456789abcdef";
|
|
|
|
for (char i: bytes) {
|
|
|
|
OS << hex_rep[(i & 0xF0) >> 4];
|
|
|
|
OS << hex_rep[i & 0xF];
|
|
|
|
OS << ' ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-11 01:27:28 +01:00
|
|
|
MCInstPrinter::~MCInstPrinter() = default;
|
2010-02-11 23:39:10 +01: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 10:32:38 +02:00
|
|
|
return MII.getName(Opcode);
|
2010-02-11 23:39:10 +01:00
|
|
|
}
|
2011-03-05 19:43:32 +01:00
|
|
|
|
2011-06-02 04:34:55 +02:00
|
|
|
void MCInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
|
2012-02-07 06:05:23 +01:00
|
|
|
llvm_unreachable("Target should implement this");
|
2011-03-05 19:43:32 +01:00
|
|
|
}
|
2011-09-15 20:36:29 +02:00
|
|
|
|
2011-09-16 01:38:46 +02:00
|
|
|
void MCInstPrinter::printAnnotation(raw_ostream &OS, StringRef Annot) {
|
2011-09-21 02:25:23 +02:00
|
|
|
if (!Annot.empty()) {
|
2013-10-01 21:21:24 +02:00
|
|
|
if (CommentStream) {
|
2011-10-05 00:44:48 +02:00
|
|
|
(*CommentStream) << Annot;
|
2013-10-01 21:21:24 +02:00
|
|
|
// By definition (see MCInstPrinter.h), CommentStream must end with
|
|
|
|
// a newline after each comment.
|
|
|
|
if (Annot.back() != '\n')
|
|
|
|
(*CommentStream) << '\n';
|
|
|
|
} else
|
2011-10-05 00:44:48 +02:00
|
|
|
OS << " " << MAI.getCommentString() << " " << Annot;
|
2011-09-21 02:25:23 +02:00
|
|
|
}
|
2011-09-15 20:36:29 +02:00
|
|
|
}
|
2012-10-24 00:52:52 +02: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 19:13:19 +01:00
|
|
|
|
2013-08-01 23:18:16 +02:00
|
|
|
// For asm-style hex (e.g. 0ffh) the first digit always has to be a number.
|
|
|
|
static bool needsLeadingZero(uint64_t Value)
|
|
|
|
{
|
2017-02-11 01:27:28 +01:00
|
|
|
while (Value)
|
2013-08-01 23:18:16 +02:00
|
|
|
{
|
|
|
|
uint64_t digit = (Value >> 60) & 0xf;
|
|
|
|
if (digit != 0)
|
|
|
|
return (digit >= 0xa);
|
|
|
|
Value <<= 4;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-15 23:15:41 +01:00
|
|
|
format_object<int64_t> MCInstPrinter::formatDec(int64_t Value) const {
|
2013-08-01 23:18:16 +02:00
|
|
|
return format("%" PRId64, Value);
|
|
|
|
}
|
|
|
|
|
2015-02-15 23:15:41 +01:00
|
|
|
format_object<int64_t> MCInstPrinter::formatHex(int64_t Value) const {
|
2013-08-01 23:18:16 +02:00
|
|
|
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 11:37:20 +02:00
|
|
|
llvm_unreachable("unsupported print style");
|
2013-08-01 23:18:16 +02:00
|
|
|
}
|
|
|
|
|
2015-02-15 23:15:41 +01:00
|
|
|
format_object<uint64_t> MCInstPrinter::formatHex(uint64_t Value) const {
|
2013-08-01 23:18:16 +02:00
|
|
|
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 11:37:20 +02:00
|
|
|
llvm_unreachable("unsupported print style");
|
2012-12-05 19:13:19 +01:00
|
|
|
}
|