MCInst.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //===- lib/MC/MCInst.cpp - MCInst implementation --------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/MC/MCInst.h"
  9. #include "llvm/Config/llvm-config.h"
  10. #include "llvm/MC/MCExpr.h"
  11. #include "llvm/MC/MCInstPrinter.h"
  12. #include "llvm/MC/MCRegisterInfo.h"
  13. #include "llvm/Support/Casting.h"
  14. #include "llvm/Support/Compiler.h"
  15. #include "llvm/Support/Debug.h"
  16. #include "llvm/Support/raw_ostream.h"
  17. using namespace llvm;
  18. void MCOperand::print(raw_ostream &OS, const MCRegisterInfo *RegInfo) const {
  19. OS << "<MCOperand ";
  20. if (!isValid())
  21. OS << "INVALID";
  22. else if (isReg()) {
  23. OS << "Reg:";
  24. if (RegInfo)
  25. OS << RegInfo->getName(getReg());
  26. else
  27. OS << getReg();
  28. } else if (isImm())
  29. OS << "Imm:" << getImm();
  30. else if (isSFPImm())
  31. OS << "SFPImm:" << bit_cast<float>(getSFPImm());
  32. else if (isDFPImm())
  33. OS << "DFPImm:" << bit_cast<double>(getDFPImm());
  34. else if (isExpr()) {
  35. OS << "Expr:(" << *getExpr() << ")";
  36. } else if (isInst()) {
  37. OS << "Inst:(";
  38. getInst()->print(OS, RegInfo);
  39. OS << ")";
  40. } else
  41. OS << "UNDEFINED";
  42. OS << ">";
  43. }
  44. bool MCOperand::evaluateAsConstantImm(int64_t &Imm) const {
  45. if (isImm()) {
  46. Imm = getImm();
  47. return true;
  48. }
  49. return false;
  50. }
  51. bool MCOperand::isBareSymbolRef() const {
  52. assert(isExpr() &&
  53. "isBareSymbolRef expects only expressions");
  54. const MCExpr *Expr = getExpr();
  55. MCExpr::ExprKind Kind = getExpr()->getKind();
  56. return Kind == MCExpr::SymbolRef &&
  57. cast<MCSymbolRefExpr>(Expr)->getKind() == MCSymbolRefExpr::VK_None;
  58. }
  59. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  60. LLVM_DUMP_METHOD void MCOperand::dump() const {
  61. print(dbgs());
  62. dbgs() << "\n";
  63. }
  64. #endif
  65. void MCInst::print(raw_ostream &OS, const MCRegisterInfo *RegInfo) const {
  66. OS << "<MCInst " << getOpcode();
  67. for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
  68. OS << " ";
  69. getOperand(i).print(OS, RegInfo);
  70. }
  71. OS << ">";
  72. }
  73. void MCInst::dump_pretty(raw_ostream &OS, const MCInstPrinter *Printer,
  74. StringRef Separator,
  75. const MCRegisterInfo *RegInfo) const {
  76. StringRef InstName = Printer ? Printer->getOpcodeName(getOpcode()) : "";
  77. dump_pretty(OS, InstName, Separator, RegInfo);
  78. }
  79. void MCInst::dump_pretty(raw_ostream &OS, StringRef Name, StringRef Separator,
  80. const MCRegisterInfo *RegInfo) const {
  81. OS << "<MCInst #" << getOpcode();
  82. // Show the instruction opcode name if we have it.
  83. if (!Name.empty())
  84. OS << ' ' << Name;
  85. for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
  86. OS << Separator;
  87. getOperand(i).print(OS, RegInfo);
  88. }
  89. OS << ">";
  90. }
  91. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  92. LLVM_DUMP_METHOD void MCInst::dump() const {
  93. print(dbgs());
  94. dbgs() << "\n";
  95. }
  96. #endif