Path: blob/main/contrib/llvm-project/llvm/lib/MC/MCInst.cpp
35234 views
//===- lib/MC/MCInst.cpp - MCInst implementation --------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "llvm/MC/MCInst.h"9#include "llvm/Config/llvm-config.h"10#include "llvm/MC/MCExpr.h"11#include "llvm/MC/MCInstPrinter.h"12#include "llvm/MC/MCRegisterInfo.h"13#include "llvm/Support/Casting.h"14#include "llvm/Support/Compiler.h"15#include "llvm/Support/Debug.h"16#include "llvm/Support/raw_ostream.h"1718using namespace llvm;1920void MCOperand::print(raw_ostream &OS, const MCRegisterInfo *RegInfo) const {21OS << "<MCOperand ";22if (!isValid())23OS << "INVALID";24else if (isReg()) {25OS << "Reg:";26if (RegInfo)27OS << RegInfo->getName(getReg());28else29OS << getReg();30} else if (isImm())31OS << "Imm:" << getImm();32else if (isSFPImm())33OS << "SFPImm:" << bit_cast<float>(getSFPImm());34else if (isDFPImm())35OS << "DFPImm:" << bit_cast<double>(getDFPImm());36else if (isExpr()) {37OS << "Expr:(" << *getExpr() << ")";38} else if (isInst()) {39OS << "Inst:(";40if (const auto *Inst = getInst())41Inst->print(OS, RegInfo);42else43OS << "NULL";44OS << ")";45} else46OS << "UNDEFINED";47OS << ">";48}4950bool MCOperand::evaluateAsConstantImm(int64_t &Imm) const {51if (isImm()) {52Imm = getImm();53return true;54}55return false;56}5758bool MCOperand::isBareSymbolRef() const {59assert(isExpr() &&60"isBareSymbolRef expects only expressions");61const MCExpr *Expr = getExpr();62MCExpr::ExprKind Kind = getExpr()->getKind();63return Kind == MCExpr::SymbolRef &&64cast<MCSymbolRefExpr>(Expr)->getKind() == MCSymbolRefExpr::VK_None;65}6667#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)68LLVM_DUMP_METHOD void MCOperand::dump() const {69print(dbgs());70dbgs() << "\n";71}72#endif7374void MCInst::print(raw_ostream &OS, const MCRegisterInfo *RegInfo) const {75OS << "<MCInst " << getOpcode();76for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {77OS << " ";78getOperand(i).print(OS, RegInfo);79}80OS << ">";81}8283void MCInst::dump_pretty(raw_ostream &OS, const MCInstPrinter *Printer,84StringRef Separator,85const MCRegisterInfo *RegInfo) const {86StringRef InstName = Printer ? Printer->getOpcodeName(getOpcode()) : "";87dump_pretty(OS, InstName, Separator, RegInfo);88}8990void MCInst::dump_pretty(raw_ostream &OS, StringRef Name, StringRef Separator,91const MCRegisterInfo *RegInfo) const {92OS << "<MCInst #" << getOpcode();9394// Show the instruction opcode name if we have it.95if (!Name.empty())96OS << ' ' << Name;9798for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {99OS << Separator;100getOperand(i).print(OS, RegInfo);101}102OS << ">";103}104105#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)106LLVM_DUMP_METHOD void MCInst::dump() const {107print(dbgs());108dbgs() << "\n";109}110#endif111112113