blob: 5f026dc6b26082aac405af3097c53dcb85959bdc [file] [log] [blame]
Daniel Dunbar212b6d82009-08-27 07:57:12 +00001//===- 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"
Daniel Dunbar73da11e2009-08-31 08:08:38 +000011#include "llvm/MC/MCExpr.h"
Daniel Dunbar04047fb2010-03-22 21:49:34 +000012#include "llvm/MC/MCInstPrinter.h"
Sameer AbuAsalc1b0e662018-04-06 21:07:05 +000013#include "llvm/Support/Casting.h"
Eugene Zelenkod3a6c892017-02-11 00:27:28 +000014#include "llvm/Support/Compiler.h"
David Greene7b42d702010-01-05 01:28:22 +000015#include "llvm/Support/Debug.h"
Daniel Dunbar212b6d82009-08-27 07:57:12 +000016#include "llvm/Support/raw_ostream.h"
17
18using namespace llvm;
19
Sean Silva0e1fe182015-02-05 00:58:51 +000020void MCOperand::print(raw_ostream &OS) const {
Daniel Dunbar212b6d82009-08-27 07:57:12 +000021 OS << "<MCOperand ";
22 if (!isValid())
23 OS << "INVALID";
24 else if (isReg())
25 OS << "Reg:" << getReg();
26 else if (isImm())
27 OS << "Imm:" << getImm();
Dan Gohman216e0c22015-12-21 16:47:10 +000028 else if (isFPImm())
29 OS << "FPImm:" << getFPImm();
Daniel Dunbar73da11e2009-08-31 08:08:38 +000030 else if (isExpr()) {
Chris Lattnerc8f77172010-01-18 00:37:40 +000031 OS << "Expr:(" << *getExpr() << ")";
Owen Anderson4b53e182012-01-19 19:32:20 +000032 } else if (isInst()) {
33 OS << "Inst:(" << *getInst() << ")";
Daniel Dunbar212b6d82009-08-27 07:57:12 +000034 } else
35 OS << "UNDEFINED";
36 OS << ">";
37}
38
Sameer AbuAsalc1b0e662018-04-06 21:07:05 +000039bool MCOperand::evaluateAsConstantImm(int64_t &Imm) const {
40 if (isImm()) {
41 Imm = getImm();
42 return true;
43 }
44 return false;
45}
46
47bool 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
Aaron Ballman615eb472017-10-15 14:32:27 +000056#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +000057LLVM_DUMP_METHOD void MCOperand::dump() const {
Sean Silva32f24c42015-02-05 01:13:47 +000058 print(dbgs());
David Greene7b42d702010-01-05 01:28:22 +000059 dbgs() << "\n";
Daniel Dunbar212b6d82009-08-27 07:57:12 +000060}
Matthias Braun8c209aa2017-01-28 02:02:38 +000061#endif
Daniel Dunbar212b6d82009-08-27 07:57:12 +000062
Sean Silva0e1fe182015-02-05 00:58:51 +000063void MCInst::print(raw_ostream &OS) const {
Daniel Dunbar212b6d82009-08-27 07:57:12 +000064 OS << "<MCInst " << getOpcode();
65 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
66 OS << " ";
Sean Silva0e1fe182015-02-05 00:58:51 +000067 getOperand(i).print(OS);
Daniel Dunbar212b6d82009-08-27 07:57:12 +000068 }
69 OS << ">";
70}
71
Sean Silva0e1fe182015-02-05 00:58:51 +000072void MCInst::dump_pretty(raw_ostream &OS, const MCInstPrinter *Printer,
Daniel Dunbar04047fb2010-03-22 21:49:34 +000073 StringRef Separator) const {
74 OS << "<MCInst #" << getOpcode();
75
76 // Show the instruction opcode name if we have access to a printer.
77 if (Printer)
78 OS << ' ' << Printer->getOpcodeName(getOpcode());
79
80 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
81 OS << Separator;
Sean Silva0e1fe182015-02-05 00:58:51 +000082 getOperand(i).print(OS);
Daniel Dunbar04047fb2010-03-22 21:49:34 +000083 }
Daniel Dunbar3627af52010-05-26 15:18:13 +000084 OS << ">";
Daniel Dunbar04047fb2010-03-22 21:49:34 +000085}
86
Aaron Ballman615eb472017-10-15 14:32:27 +000087#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +000088LLVM_DUMP_METHOD void MCInst::dump() const {
Sean Silva32f24c42015-02-05 01:13:47 +000089 print(dbgs());
David Greene7b42d702010-01-05 01:28:22 +000090 dbgs() << "\n";
Daniel Dunbar212b6d82009-08-27 07:57:12 +000091}
Matthias Braun8c209aa2017-01-28 02:02:38 +000092#endif