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