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