blob: f6f6edee5822e0c04172b5d15a4af7ea496120c0 [file] [log] [blame]
Daniel Dunbar212b6d82009-08-27 07:57:12 +00001//===- lib/MC/MCInst.cpp - MCInst implementation --------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Daniel Dunbar212b6d82009-08-27 07:57:12 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/MC/MCInst.h"
Nico Weber432a3882018-04-30 14:59:11 +000010#include "llvm/Config/llvm-config.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 {
Oliver Stannardc5881102018-12-03 10:21:28 +000074 StringRef InstName = Printer ? Printer->getOpcodeName(getOpcode()) : "";
75 dump_pretty(OS, InstName, Separator);
76}
77
78void MCInst::dump_pretty(raw_ostream &OS, StringRef Name,
79 StringRef Separator) const {
Daniel Dunbar04047fb2010-03-22 21:49:34 +000080 OS << "<MCInst #" << getOpcode();
81
Oliver Stannardc5881102018-12-03 10:21:28 +000082 // Show the instruction opcode name if we have it.
83 if (!Name.empty())
84 OS << ' ' << Name;
Daniel Dunbar04047fb2010-03-22 21:49:34 +000085
86 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
87 OS << Separator;
Sean Silva0e1fe182015-02-05 00:58:51 +000088 getOperand(i).print(OS);
Daniel Dunbar04047fb2010-03-22 21:49:34 +000089 }
Daniel Dunbar3627af52010-05-26 15:18:13 +000090 OS << ">";
Daniel Dunbar04047fb2010-03-22 21:49:34 +000091}
92
Aaron Ballman615eb472017-10-15 14:32:27 +000093#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +000094LLVM_DUMP_METHOD void MCInst::dump() const {
Sean Silva32f24c42015-02-05 01:13:47 +000095 print(dbgs());
David Greene7b42d702010-01-05 01:28:22 +000096 dbgs() << "\n";
Daniel Dunbar212b6d82009-08-27 07:57:12 +000097}
Matthias Braun8c209aa2017-01-28 02:02:38 +000098#endif