LLVM  14.0.0git
MCInst.cpp
Go to the documentation of this file.
1 //===- lib/MC/MCInst.cpp - MCInst implementation --------------------------===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/MC/MCInst.h"
10 #include "llvm/Config/llvm-config.h"
11 #include "llvm/MC/MCExpr.h"
12 #include "llvm/MC/MCInstPrinter.h"
13 #include "llvm/MC/MCRegisterInfo.h"
14 #include "llvm/Support/Casting.h"
15 #include "llvm/Support/Compiler.h"
16 #include "llvm/Support/Debug.h"
18 
19 using namespace llvm;
20 
21 void MCOperand::print(raw_ostream &OS, const MCRegisterInfo *RegInfo) const {
22  OS << "<MCOperand ";
23  if (!isValid())
24  OS << "INVALID";
25  else if (isReg()) {
26  OS << "Reg:";
27  if (RegInfo)
28  OS << RegInfo->getName(getReg());
29  else
30  OS << getReg();
31  } else if (isImm())
32  OS << "Imm:" << getImm();
33  else if (isSFPImm())
34  OS << "SFPImm:" << bit_cast<float>(getSFPImm());
35  else if (isDFPImm())
36  OS << "DFPImm:" << bit_cast<double>(getDFPImm());
37  else if (isExpr()) {
38  OS << "Expr:(" << *getExpr() << ")";
39  } else if (isInst()) {
40  OS << "Inst:(";
41  getInst()->print(OS, RegInfo);
42  OS << ")";
43  } else
44  OS << "UNDEFINED";
45  OS << ">";
46 }
47 
48 bool MCOperand::evaluateAsConstantImm(int64_t &Imm) const {
49  if (isImm()) {
50  Imm = getImm();
51  return true;
52  }
53  return false;
54 }
55 
57  assert(isExpr() &&
58  "isBareSymbolRef expects only expressions");
59  const MCExpr *Expr = getExpr();
60  MCExpr::ExprKind Kind = getExpr()->getKind();
61  return Kind == MCExpr::SymbolRef &&
62  cast<MCSymbolRefExpr>(Expr)->getKind() == MCSymbolRefExpr::VK_None;
63 }
64 
65 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
67  print(dbgs());
68  dbgs() << "\n";
69 }
70 #endif
71 
72 void MCInst::print(raw_ostream &OS, const MCRegisterInfo *RegInfo) const {
73  OS << "<MCInst " << getOpcode();
74  for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
75  OS << " ";
76  getOperand(i).print(OS, RegInfo);
77  }
78  OS << ">";
79 }
80 
82  StringRef Separator,
83  const MCRegisterInfo *RegInfo) const {
84  StringRef InstName = Printer ? Printer->getOpcodeName(getOpcode()) : "";
85  dump_pretty(OS, InstName, Separator, RegInfo);
86 }
87 
89  const MCRegisterInfo *RegInfo) const {
90  OS << "<MCInst #" << getOpcode();
91 
92  // Show the instruction opcode name if we have it.
93  if (!Name.empty())
94  OS << ' ' << Name;
95 
96  for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
97  OS << Separator;
98  getOperand(i).print(OS, RegInfo);
99  }
100  OS << ">";
101 }
102 
103 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
105  print(dbgs());
106  dbgs() << "\n";
107 }
108 #endif
i
i
Definition: README.txt:29
LLVM_DUMP_METHOD
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:506
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
llvm::MCRegisterInfo::getName
const char * getName(MCRegister RegNo) const
Return the human-readable symbolic target-specific name for the specified physical register.
Definition: MCRegisterInfo.h:485
llvm::MCOperand::isReg
bool isReg() const
Definition: MCInst.h:61
llvm::MCInst::dump
void dump() const
Definition: MCInst.cpp:104
llvm::MCOperand::getSFPImm
uint32_t getSFPImm() const
Definition: MCInst.h:90
llvm::MCOperand::isSFPImm
bool isSFPImm() const
Definition: MCInst.h:63
llvm::MCInst::getNumOperands
unsigned getNumOperands() const
Definition: MCInst.h:208
llvm::MCOperand::isInst
bool isInst() const
Definition: MCInst.h:66
llvm::MCOperand::print
void print(raw_ostream &OS, const MCRegisterInfo *RegInfo=nullptr) const
Definition: MCInst.cpp:21
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
Printer
print alias Alias Set Printer
Definition: AliasSetTracker.cpp:762
llvm::MCOperand::dump
void dump() const
Definition: MCInst.cpp:66
llvm::MCOperand::getInst
const MCInst * getInst() const
Definition: MCInst.h:124
llvm::MCOperand::getImm
int64_t getImm() const
Definition: MCInst.h:80
MCInst.h
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::MCOperand::isBareSymbolRef
bool isBareSymbolRef() const
Definition: MCInst.cpp:56
llvm::MCExpr::getKind
ExprKind getKind() const
Definition: MCExpr.h:81
MCInstPrinter.h
llvm::MCOperand::isImm
bool isImm() const
Definition: MCInst.h:62
llvm::MCInst::print
void print(raw_ostream &OS, const MCRegisterInfo *RegInfo=nullptr) const
Definition: MCInst.cpp:72
llvm::MCInstPrinter
This is an instance of a target assembly language printer that converts an MCInst to valid target ass...
Definition: MCInstPrinter.h:43
llvm::MCOperand::isValid
bool isValid() const
Definition: MCInst.h:60
llvm::numbers::e
constexpr double e
Definition: MathExtras.h:57
MCRegisterInfo.h
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::MCOperand::evaluateAsConstantImm
bool evaluateAsConstantImm(int64_t &Imm) const
Definition: MCInst.cpp:48
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
Compiler.h
llvm::MCRegisterInfo
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
Definition: MCRegisterInfo.h:135
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
Casting.h
llvm::MCOperand::getExpr
const MCExpr * getExpr() const
Definition: MCInst.h:114
llvm::MCInst::getOpcode
unsigned getOpcode() const
Definition: MCInst.h:198
llvm::MCOperand::isExpr
bool isExpr() const
Definition: MCInst.h:65
llvm::MCOperand::getDFPImm
uint64_t getDFPImm() const
Definition: MCInst.h:100
llvm::MCExpr::SymbolRef
@ SymbolRef
References to labels and assigned expressions.
Definition: MCExpr.h:40
llvm::MCInst::getOperand
const MCOperand & getOperand(unsigned i) const
Definition: MCInst.h:206
llvm::MCInst::dump_pretty
void dump_pretty(raw_ostream &OS, const MCInstPrinter *Printer=nullptr, StringRef Separator=" ", const MCRegisterInfo *RegInfo=nullptr) const
Dump the MCInst as prettily as possible using the additional MC structures, if given.
Definition: MCInst.cpp:81
llvm::MCSymbolRefExpr::VK_None
@ VK_None
Definition: MCExpr.h:195
llvm::MCExpr::ExprKind
ExprKind
Definition: MCExpr.h:37
raw_ostream.h
MCExpr.h
llvm::MCOperand::isDFPImm
bool isDFPImm() const
Definition: MCInst.h:64
llvm::MCExpr
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
Debug.h
llvm::MCOperand::getReg
unsigned getReg() const
Returns the register number.
Definition: MCInst.h:69