MCInst.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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/Support/Casting.h"
  13. #include "llvm/Support/Compiler.h"
  14. #include "llvm/Support/Debug.h"
  15. #include "llvm/Support/raw_ostream.h"
  16. using namespace llvm;
  17. void MCOperand::print(raw_ostream &OS) const {
  18. OS << "<MCOperand ";
  19. if (!isValid())
  20. OS << "INVALID";
  21. else if (isReg())
  22. OS << "Reg:" << getReg();
  23. else if (isImm())
  24. OS << "Imm:" << getImm();
  25. else if (isFPImm())
  26. OS << "FPImm:" << getFPImm();
  27. else if (isExpr()) {
  28. OS << "Expr:(" << *getExpr() << ")";
  29. } else if (isInst()) {
  30. OS << "Inst:(" << *getInst() << ")";
  31. } else
  32. OS << "UNDEFINED";
  33. OS << ">";
  34. }
  35. bool MCOperand::evaluateAsConstantImm(int64_t &Imm) const {
  36. if (isImm()) {
  37. Imm = getImm();
  38. return true;
  39. }
  40. return false;
  41. }
  42. bool MCOperand::isBareSymbolRef() const {
  43. assert(isExpr() &&
  44. "isBareSymbolRef expects only expressions");
  45. const MCExpr *Expr = getExpr();
  46. MCExpr::ExprKind Kind = getExpr()->getKind();
  47. return Kind == MCExpr::SymbolRef &&
  48. cast<MCSymbolRefExpr>(Expr)->getKind() == MCSymbolRefExpr::VK_None;
  49. }
  50. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  51. LLVM_DUMP_METHOD void MCOperand::dump() const {
  52. print(dbgs());
  53. dbgs() << "\n";
  54. }
  55. #endif
  56. void MCInst::print(raw_ostream &OS) const {
  57. OS << "<MCInst " << getOpcode();
  58. for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
  59. OS << " ";
  60. getOperand(i).print(OS);
  61. }
  62. OS << ">";
  63. }
  64. void MCInst::dump_pretty(raw_ostream &OS, const MCInstPrinter *Printer,
  65. StringRef Separator) const {
  66. StringRef InstName = Printer ? Printer->getOpcodeName(getOpcode()) : "";
  67. dump_pretty(OS, InstName, Separator);
  68. }
  69. void MCInst::dump_pretty(raw_ostream &OS, StringRef Name,
  70. StringRef Separator) const {
  71. OS << "<MCInst #" << getOpcode();
  72. // Show the instruction opcode name if we have it.
  73. if (!Name.empty())
  74. OS << ' ' << Name;
  75. for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
  76. OS << Separator;
  77. getOperand(i).print(OS);
  78. }
  79. OS << ">";
  80. }
  81. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  82. LLVM_DUMP_METHOD void MCInst::dump() const {
  83. print(dbgs());
  84. dbgs() << "\n";
  85. }
  86. #endif