MCInstPrinter.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. #include "llvm/MC/MCInstPrinter.h"
  10. #include "llvm/ADT/StringRef.h"
  11. #include "llvm/MC/MCAsmInfo.h"
  12. #include "llvm/MC/MCInstrInfo.h"
  13. #include "llvm/Support/ErrorHandling.h"
  14. #include "llvm/Support/Format.h"
  15. #include "llvm/Support/raw_ostream.h"
  16. using namespace llvm;
  17. void llvm::dumpBytes(ArrayRef<uint8_t> bytes, raw_ostream &OS) {
  18. static const char hex_rep[] = "0123456789abcdef";
  19. for (char i: bytes) {
  20. OS << hex_rep[(i & 0xF0) >> 4];
  21. OS << hex_rep[i & 0xF];
  22. OS << ' ';
  23. }
  24. }
  25. MCInstPrinter::~MCInstPrinter() {
  26. }
  27. /// getOpcodeName - Return the name of the specified opcode enum (e.g.
  28. /// "MOV32ri") or empty if we can't resolve it.
  29. StringRef MCInstPrinter::getOpcodeName(unsigned Opcode) const {
  30. return MII.getName(Opcode);
  31. }
  32. void MCInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
  33. llvm_unreachable("Target should implement this");
  34. }
  35. void MCInstPrinter::printAnnotation(raw_ostream &OS, StringRef Annot) {
  36. if (!Annot.empty()) {
  37. if (CommentStream) {
  38. (*CommentStream) << Annot;
  39. // By definition (see MCInstPrinter.h), CommentStream must end with
  40. // a newline after each comment.
  41. if (Annot.back() != '\n')
  42. (*CommentStream) << '\n';
  43. } else
  44. OS << " " << MAI.getCommentString() << " " << Annot;
  45. }
  46. }
  47. /// Utility functions to make adding mark ups simpler.
  48. StringRef MCInstPrinter::markup(StringRef s) const {
  49. if (getUseMarkup())
  50. return s;
  51. else
  52. return "";
  53. }
  54. StringRef MCInstPrinter::markup(StringRef a, StringRef b) const {
  55. if (getUseMarkup())
  56. return a;
  57. else
  58. return b;
  59. }
  60. // For asm-style hex (e.g. 0ffh) the first digit always has to be a number.
  61. static bool needsLeadingZero(uint64_t Value)
  62. {
  63. while(Value)
  64. {
  65. uint64_t digit = (Value >> 60) & 0xf;
  66. if (digit != 0)
  67. return (digit >= 0xa);
  68. Value <<= 4;
  69. }
  70. return false;
  71. }
  72. format_object<int64_t> MCInstPrinter::formatDec(int64_t Value) const {
  73. return format("%" PRId64, Value);
  74. }
  75. format_object<int64_t> MCInstPrinter::formatHex(int64_t Value) const {
  76. switch(PrintHexStyle) {
  77. case HexStyle::C:
  78. if (Value < 0)
  79. return format("-0x%" PRIx64, -Value);
  80. else
  81. return format("0x%" PRIx64, Value);
  82. case HexStyle::Asm:
  83. if (Value < 0) {
  84. if (needsLeadingZero((uint64_t)(-Value)))
  85. return format("-0%" PRIx64 "h", -Value);
  86. else
  87. return format("-%" PRIx64 "h", -Value);
  88. } else {
  89. if (needsLeadingZero((uint64_t)(Value)))
  90. return format("0%" PRIx64 "h", Value);
  91. else
  92. return format("%" PRIx64 "h", Value);
  93. }
  94. }
  95. llvm_unreachable("unsupported print style");
  96. }
  97. format_object<uint64_t> MCInstPrinter::formatHex(uint64_t Value) const {
  98. switch(PrintHexStyle) {
  99. case HexStyle::C:
  100. return format("0x%" PRIx64, Value);
  101. case HexStyle::Asm:
  102. if (needsLeadingZero(Value))
  103. return format("0%" PRIx64 "h", Value);
  104. else
  105. return format("%" PRIx64 "h", Value);
  106. }
  107. llvm_unreachable("unsupported print style");
  108. }