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