1198090Srdivacky//===-- MCInstPrinter.cpp - Convert an MCInst to target assembly syntax ---===//
2198090Srdivacky//
3198090Srdivacky//                     The LLVM Compiler Infrastructure
4198090Srdivacky//
5198090Srdivacky// This file is distributed under the University of Illinois Open Source
6198090Srdivacky// License. See LICENSE.TXT for details.
7198090Srdivacky//
8198090Srdivacky//===----------------------------------------------------------------------===//
9198090Srdivacky
10198090Srdivacky#include "llvm/MC/MCInstPrinter.h"
11249423Sdim#include "llvm/ADT/StringRef.h"
12249423Sdim#include "llvm/MC/MCAsmInfo.h"
13234353Sdim#include "llvm/MC/MCInstrInfo.h"
14234353Sdim#include "llvm/Support/ErrorHandling.h"
15249423Sdim#include "llvm/Support/Format.h"
16226633Sdim#include "llvm/Support/raw_ostream.h"
17198090Srdivackyusing namespace llvm;
18198090Srdivacky
19198090SrdivackyMCInstPrinter::~MCInstPrinter() {
20198090Srdivacky}
21203954Srdivacky
22203954Srdivacky/// getOpcodeName - Return the name of the specified opcode enum (e.g.
23203954Srdivacky/// "MOV32ri") or empty if we can't resolve it.
24203954SrdivackyStringRef MCInstPrinter::getOpcodeName(unsigned Opcode) const {
25234353Sdim  return MII.getName(Opcode);
26203954Srdivacky}
27221345Sdim
28223017Sdimvoid MCInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
29234353Sdim  llvm_unreachable("Target should implement this");
30221345Sdim}
31226633Sdim
32226633Sdimvoid MCInstPrinter::printAnnotation(raw_ostream &OS, StringRef Annot) {
33226633Sdim  if (!Annot.empty()) {
34263508Sdim    if (CommentStream) {
35226633Sdim      (*CommentStream) << Annot;
36263508Sdim      // By definition (see MCInstPrinter.h), CommentStream must end with
37263508Sdim      // a newline after each comment.
38263508Sdim      if (Annot.back() != '\n')
39263508Sdim        (*CommentStream) << '\n';
40263508Sdim    } else
41226633Sdim      OS << " " << MAI.getCommentString() << " " << Annot;
42226633Sdim  }
43226633Sdim}
44243830Sdim
45243830Sdim/// Utility functions to make adding mark ups simpler.
46243830SdimStringRef MCInstPrinter::markup(StringRef s) const {
47243830Sdim  if (getUseMarkup())
48243830Sdim    return s;
49243830Sdim  else
50243830Sdim    return "";
51243830Sdim}
52243830SdimStringRef MCInstPrinter::markup(StringRef a, StringRef b) const {
53243830Sdim  if (getUseMarkup())
54243830Sdim    return a;
55243830Sdim  else
56243830Sdim    return b;
57243830Sdim}
58249423Sdim
59254790Semaste// For asm-style hex (e.g. 0ffh) the first digit always has to be a number.
60254790Semastestatic bool needsLeadingZero(uint64_t Value)
61254790Semaste{
62254790Semaste  while(Value)
63254790Semaste  {
64254790Semaste    uint64_t digit = (Value >> 60) & 0xf;
65254790Semaste    if (digit != 0)
66254790Semaste      return (digit >= 0xa);
67254790Semaste    Value <<= 4;
68254790Semaste  }
69254790Semaste  return false;
70249423Sdim}
71254790Semaste
72254790Semasteformat_object1<int64_t> MCInstPrinter::formatDec(const int64_t Value) const {
73254790Semaste  return format("%" PRId64, Value);
74254790Semaste}
75254790Semaste
76254790Semasteformat_object1<int64_t> MCInstPrinter::formatHex(const int64_t Value) const {
77254790Semaste  switch(PrintHexStyle) {
78254790Semaste  case HexStyle::C:
79254790Semaste    if (Value < 0)
80254790Semaste      return format("-0x%" PRIx64, -Value);
81254790Semaste    else
82254790Semaste      return format("0x%" PRIx64, Value);
83254790Semaste  case HexStyle::Asm:
84254790Semaste    if (Value < 0) {
85254790Semaste      if (needsLeadingZero((uint64_t)(-Value)))
86254790Semaste        return format("-0%" PRIx64 "h", -Value);
87254790Semaste      else
88254790Semaste        return format("-%" PRIx64 "h", -Value);
89254790Semaste    } else {
90254790Semaste      if (needsLeadingZero((uint64_t)(Value)))
91254790Semaste        return format("0%" PRIx64 "h", Value);
92254790Semaste      else
93254790Semaste        return format("%" PRIx64 "h", Value);
94254790Semaste    }
95254790Semaste  }
96263508Sdim  llvm_unreachable("unsupported print style");
97254790Semaste}
98254790Semaste
99254790Semasteformat_object1<uint64_t> MCInstPrinter::formatHex(const uint64_t Value) const {
100254790Semaste  switch(PrintHexStyle) {
101254790Semaste  case HexStyle::C:
102254790Semaste     return format("0x%" PRIx64, Value);
103254790Semaste  case HexStyle::Asm:
104254790Semaste    if (needsLeadingZero(Value))
105254790Semaste      return format("0%" PRIx64 "h", Value);
106254790Semaste    else
107254790Semaste      return format("%" PRIx64 "h", Value);
108254790Semaste  }
109263508Sdim  llvm_unreachable("unsupported print style");
110254790Semaste}
111