LLVM 22.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/MCAsmInfo.h"
12#include "llvm/MC/MCContext.h"
13#include "llvm/MC/MCExpr.h"
18#include "llvm/Support/Debug.h"
20
21using namespace llvm;
22
23void MCOperand::print(raw_ostream &OS, const MCContext *Ctx) const {
24 OS << "<MCOperand ";
25 if (!isValid())
26 OS << "INVALID";
27 else if (isReg()) {
28 OS << "Reg:";
29 if (Ctx && Ctx->getRegisterInfo())
30 OS << Ctx->getRegisterInfo()->getName(getReg());
31 else
32 OS << getReg();
33 } else if (isImm())
34 OS << "Imm:" << getImm();
35 else if (isSFPImm())
36 OS << "SFPImm:" << bit_cast<float>(getSFPImm());
37 else if (isDFPImm())
38 OS << "DFPImm:" << bit_cast<double>(getDFPImm());
39 else if (isExpr()) {
40 OS << "Expr:";
41 if (Ctx)
42 Ctx->getAsmInfo()->printExpr(OS, *getExpr());
43 else
44 getExpr()->print(OS, nullptr);
45 } else if (isInst()) {
46 OS << "Inst:(";
47 if (const auto *Inst = getInst())
48 Inst->print(OS, Ctx);
49 else
50 OS << "NULL";
51 OS << ")";
52 } else
53 OS << "UNDEFINED";
54 OS << ">";
55}
56
57bool MCOperand::evaluateAsConstantImm(int64_t &Imm) const {
58 if (isImm()) {
59 Imm = getImm();
60 return true;
61 }
62 return false;
63}
64
66 assert(isExpr() &&
67 "isBareSymbolRef expects only expressions");
68 const MCExpr *Expr = getExpr();
70 return Kind == MCExpr::SymbolRef &&
71 cast<MCSymbolRefExpr>(Expr)->getSpecifier() == 0;
72}
73
74#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
76 print(dbgs());
77 dbgs() << "\n";
78}
79#endif
80
81void MCInst::print(raw_ostream &OS, const MCContext *Ctx) const {
82 OS << "<MCInst " << getOpcode();
83 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
84 OS << " ";
85 getOperand(i).print(OS, Ctx);
86 }
87 OS << ">";
88}
89
91 StringRef Separator, const MCContext *Ctx) const {
92 StringRef InstName = Printer ? Printer->getOpcodeName(getOpcode()) : "";
93 dump_pretty(OS, InstName, Separator, Ctx);
94}
95
97 const MCContext *Ctx) const {
98 OS << "<MCInst #" << getOpcode();
99
100 // Show the instruction opcode name if we have it.
101 if (!Name.empty())
102 OS << ' ' << Name;
103
104 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
105 OS << Separator;
106 getOperand(i).print(OS, Ctx);
107 }
108 OS << ">";
109}
110
111#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
113 print(dbgs());
114 dbgs() << "\n";
115}
116#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:638
dxil pretty DXIL Metadata Pretty Printer
std::string Name
raw_pwrite_stream & OS
void printExpr(raw_ostream &, const MCExpr &) const
Definition: MCAsmInfo.cpp:153
Context object for machine code objects.
Definition: MCContext.h:83
const MCRegisterInfo * getRegisterInfo() const
Definition: MCContext.h:414
const MCAsmInfo * getAsmInfo() const
Definition: MCContext.h:412
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
@ SymbolRef
References to labels and assigned expressions.
Definition: MCExpr.h:43
ExprKind getKind() const
Definition: MCExpr.h:85
This is an instance of a target assembly language printer that converts an MCInst to valid target ass...
Definition: MCInstPrinter.h:46
LLVM_ABI void print(raw_ostream &OS, const MCContext *Ctx=nullptr) const
Definition: MCInst.cpp:81
unsigned getNumOperands() const
Definition: MCInst.h:212
unsigned getOpcode() const
Definition: MCInst.h:202
LLVM_ABI void dump_pretty(raw_ostream &OS, const MCInstPrinter *Printer=nullptr, StringRef Separator=" ", const MCContext *Ctx=nullptr) const
Dump the MCInst as prettily as possible using the additional MC structures, if given.
Definition: MCInst.cpp:90
LLVM_ABI void dump() const
Definition: MCInst.cpp:112
const MCOperand & getOperand(unsigned i) const
Definition: MCInst.h:210
bool isSFPImm() const
Definition: MCInst.h:67
LLVM_ABI bool isBareSymbolRef() const
Definition: MCInst.cpp:65
LLVM_ABI void print(raw_ostream &OS, const MCContext *Ctx=nullptr) const
Definition: MCInst.cpp:23
int64_t getImm() const
Definition: MCInst.h:84
bool isImm() const
Definition: MCInst.h:66
bool isInst() const
Definition: MCInst.h:70
bool isReg() const
Definition: MCInst.h:65
MCRegister getReg() const
Returns the register number.
Definition: MCInst.h:73
LLVM_ABI bool evaluateAsConstantImm(int64_t &Imm) const
Definition: MCInst.cpp:57
bool isDFPImm() const
Definition: MCInst.h:68
LLVM_ABI void dump() const
Definition: MCInst.cpp:75
const MCInst * getInst() const
Definition: MCInst.h:128
bool isValid() const
Definition: MCInst.h:64
const MCExpr * getExpr() const
Definition: MCInst.h:118
uint32_t getSFPImm() const
Definition: MCInst.h:94
uint64_t getDFPImm() const
Definition: MCInst.h:104
bool isExpr() const
Definition: MCInst.h:69
const char * getName(MCRegister RegNo) const
Return the human-readable symbolic target-specific name for the specified physical register.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:207