blob: 64f111fc7114847f4a39c5a60b16b7349185d8a2 [file] [log] [blame]
Daniel Dunbar4b770c22009-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 Weber0f38c602018-04-30 14:59:11 +000011#include "llvm/Config/llvm-config.h"
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000012#include "llvm/MC/MCExpr.h"
Daniel Dunbar67c076c2010-03-22 21:49:34 +000013#include "llvm/MC/MCInstPrinter.h"
Sameer AbuAsal839cd7f2018-04-06 21:07:05 +000014#include "llvm/Support/Casting.h"
Eugene Zelenkoa700a602017-02-11 00:27:28 +000015#include "llvm/Support/Compiler.h"
David Greene593b6e42010-01-05 01:28:22 +000016#include "llvm/Support/Debug.h"
Daniel Dunbar4b770c22009-08-27 07:57:12 +000017#include "llvm/Support/raw_ostream.h"
18
19using namespace llvm;
20
Sean Silvaa875e3d2015-02-05 00:58:51 +000021void MCOperand::print(raw_ostream &OS) const {
Daniel Dunbar4b770c22009-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 Gohman1a81ad92015-12-21 16:47:10 +000029 else if (isFPImm())
30 OS << "FPImm:" << getFPImm();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000031 else if (isExpr()) {
Chris Lattner8cb9a3b2010-01-18 00:37:40 +000032 OS << "Expr:(" << *getExpr() << ")";
Owen Anderson27ff6b52012-01-19 19:32:20 +000033 } else if (isInst()) {
34 OS << "Inst:(" << *getInst() << ")";
Daniel Dunbar4b770c22009-08-27 07:57:12 +000035 } else
36 OS << "UNDEFINED";
37 OS << ">";
38}
39
Sameer AbuAsal839cd7f2018-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 Ballman1d03d382017-10-15 14:32:27 +000057#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Keren55307982016-01-29 20:50:44 +000058LLVM_DUMP_METHOD void MCOperand::dump() const {
Sean Silvabd59cd42015-02-05 01:13:47 +000059 print(dbgs());
David Greene593b6e42010-01-05 01:28:22 +000060 dbgs() << "\n";
Daniel Dunbar4b770c22009-08-27 07:57:12 +000061}
Matthias Braun88d20752017-01-28 02:02:38 +000062#endif
Daniel Dunbar4b770c22009-08-27 07:57:12 +000063
Sean Silvaa875e3d2015-02-05 00:58:51 +000064void MCInst::print(raw_ostream &OS) const {
Daniel Dunbar4b770c22009-08-27 07:57:12 +000065 OS << "<MCInst " << getOpcode();
66 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
67 OS << " ";
Sean Silvaa875e3d2015-02-05 00:58:51 +000068 getOperand(i).print(OS);
Daniel Dunbar4b770c22009-08-27 07:57:12 +000069 }
70 OS << ">";
71}
72
Sean Silvaa875e3d2015-02-05 00:58:51 +000073void MCInst::dump_pretty(raw_ostream &OS, const MCInstPrinter *Printer,
Daniel Dunbar67c076c2010-03-22 21:49:34 +000074 StringRef Separator) const {
Oliver Stannard9b840092018-12-03 10:21:28 +000075 StringRef InstName = Printer ? Printer->getOpcodeName(getOpcode()) : "";
76 dump_pretty(OS, InstName, Separator);
77}
78
79void MCInst::dump_pretty(raw_ostream &OS, StringRef Name,
80 StringRef Separator) const {
Daniel Dunbar67c076c2010-03-22 21:49:34 +000081 OS << "<MCInst #" << getOpcode();
82
Oliver Stannard9b840092018-12-03 10:21:28 +000083 // Show the instruction opcode name if we have it.
84 if (!Name.empty())
85 OS << ' ' << Name;
Daniel Dunbar67c076c2010-03-22 21:49:34 +000086
87 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
88 OS << Separator;
Sean Silvaa875e3d2015-02-05 00:58:51 +000089 getOperand(i).print(OS);
Daniel Dunbar67c076c2010-03-22 21:49:34 +000090 }
Daniel Dunbarc9adb8c2010-05-26 15:18:13 +000091 OS << ">";
Daniel Dunbar67c076c2010-03-22 21:49:34 +000092}
93
Aaron Ballman1d03d382017-10-15 14:32:27 +000094#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Keren55307982016-01-29 20:50:44 +000095LLVM_DUMP_METHOD void MCInst::dump() const {
Sean Silvabd59cd42015-02-05 01:13:47 +000096 print(dbgs());
David Greene593b6e42010-01-05 01:28:22 +000097 dbgs() << "\n";
Daniel Dunbar4b770c22009-08-27 07:57:12 +000098}
Matthias Braun88d20752017-01-28 02:02:38 +000099#endif