10b57cec5SDimitry Andric //===- lib/MC/MCInst.cpp - MCInst implementation --------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #include "llvm/MC/MCInst.h"
100b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h"
110b57cec5SDimitry Andric #include "llvm/MC/MCExpr.h"
120b57cec5SDimitry Andric #include "llvm/MC/MCInstPrinter.h"
130b57cec5SDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
140b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
150b57cec5SDimitry Andric #include "llvm/Support/Compiler.h"
160b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
170b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
180b57cec5SDimitry Andric
190b57cec5SDimitry Andric using namespace llvm;
200b57cec5SDimitry Andric
print(raw_ostream & OS,const MCRegisterInfo * RegInfo) const210b57cec5SDimitry Andric void MCOperand::print(raw_ostream &OS, const MCRegisterInfo *RegInfo) const {
220b57cec5SDimitry Andric OS << "<MCOperand ";
230b57cec5SDimitry Andric if (!isValid())
240b57cec5SDimitry Andric OS << "INVALID";
250b57cec5SDimitry Andric else if (isReg()) {
260b57cec5SDimitry Andric OS << "Reg:";
270b57cec5SDimitry Andric if (RegInfo)
280b57cec5SDimitry Andric OS << RegInfo->getName(getReg());
290b57cec5SDimitry Andric else
300b57cec5SDimitry Andric OS << getReg();
310b57cec5SDimitry Andric } else if (isImm())
320b57cec5SDimitry Andric OS << "Imm:" << getImm();
330b57cec5SDimitry Andric else if (isSFPImm())
340b57cec5SDimitry Andric OS << "SFPImm:" << bit_cast<float>(getSFPImm());
350b57cec5SDimitry Andric else if (isDFPImm())
360b57cec5SDimitry Andric OS << "DFPImm:" << bit_cast<double>(getDFPImm());
370b57cec5SDimitry Andric else if (isExpr()) {
380b57cec5SDimitry Andric OS << "Expr:(" << *getExpr() << ")";
390b57cec5SDimitry Andric } else if (isInst()) {
400b57cec5SDimitry Andric OS << "Inst:(";
410b57cec5SDimitry Andric getInst()->print(OS, RegInfo);
420b57cec5SDimitry Andric OS << ")";
430b57cec5SDimitry Andric } else
440b57cec5SDimitry Andric OS << "UNDEFINED";
450b57cec5SDimitry Andric OS << ">";
460b57cec5SDimitry Andric }
470b57cec5SDimitry Andric
evaluateAsConstantImm(int64_t & Imm) const480b57cec5SDimitry Andric bool MCOperand::evaluateAsConstantImm(int64_t &Imm) const {
490b57cec5SDimitry Andric if (isImm()) {
500b57cec5SDimitry Andric Imm = getImm();
510b57cec5SDimitry Andric return true;
520b57cec5SDimitry Andric }
530b57cec5SDimitry Andric return false;
540b57cec5SDimitry Andric }
550b57cec5SDimitry Andric
isBareSymbolRef() const560b57cec5SDimitry Andric bool MCOperand::isBareSymbolRef() const {
570b57cec5SDimitry Andric assert(isExpr() &&
580b57cec5SDimitry Andric "isBareSymbolRef expects only expressions");
590b57cec5SDimitry Andric const MCExpr *Expr = getExpr();
600b57cec5SDimitry Andric MCExpr::ExprKind Kind = getExpr()->getKind();
610b57cec5SDimitry Andric return Kind == MCExpr::SymbolRef &&
620b57cec5SDimitry Andric cast<MCSymbolRefExpr>(Expr)->getKind() == MCSymbolRefExpr::VK_None;
630b57cec5SDimitry Andric }
640b57cec5SDimitry Andric
650b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const660b57cec5SDimitry Andric LLVM_DUMP_METHOD void MCOperand::dump() const {
670b57cec5SDimitry Andric print(dbgs());
680b57cec5SDimitry Andric dbgs() << "\n";
690b57cec5SDimitry Andric }
700b57cec5SDimitry Andric #endif
710b57cec5SDimitry Andric
print(raw_ostream & OS,const MCRegisterInfo * RegInfo) const720b57cec5SDimitry Andric void MCInst::print(raw_ostream &OS, const MCRegisterInfo *RegInfo) const {
730b57cec5SDimitry Andric OS << "<MCInst " << getOpcode();
740b57cec5SDimitry Andric for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
750b57cec5SDimitry Andric OS << " ";
760b57cec5SDimitry Andric getOperand(i).print(OS, RegInfo);
770b57cec5SDimitry Andric }
780b57cec5SDimitry Andric OS << ">";
790b57cec5SDimitry Andric }
800b57cec5SDimitry Andric
dump_pretty(raw_ostream & OS,const MCInstPrinter * Printer,StringRef Separator,const MCRegisterInfo * RegInfo) const810b57cec5SDimitry Andric void MCInst::dump_pretty(raw_ostream &OS, const MCInstPrinter *Printer,
820b57cec5SDimitry Andric StringRef Separator,
830b57cec5SDimitry Andric const MCRegisterInfo *RegInfo) const {
840b57cec5SDimitry Andric StringRef InstName = Printer ? Printer->getOpcodeName(getOpcode()) : "";
850b57cec5SDimitry Andric dump_pretty(OS, InstName, Separator, RegInfo);
860b57cec5SDimitry Andric }
870b57cec5SDimitry Andric
dump_pretty(raw_ostream & OS,StringRef Name,StringRef Separator,const MCRegisterInfo * RegInfo) const880b57cec5SDimitry Andric void MCInst::dump_pretty(raw_ostream &OS, StringRef Name, StringRef Separator,
890b57cec5SDimitry Andric const MCRegisterInfo *RegInfo) const {
900b57cec5SDimitry Andric OS << "<MCInst #" << getOpcode();
910b57cec5SDimitry Andric
920b57cec5SDimitry Andric // Show the instruction opcode name if we have it.
930b57cec5SDimitry Andric if (!Name.empty())
940b57cec5SDimitry Andric OS << ' ' << Name;
950b57cec5SDimitry Andric
960b57cec5SDimitry Andric for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
970b57cec5SDimitry Andric OS << Separator;
980b57cec5SDimitry Andric getOperand(i).print(OS, RegInfo);
99 }
100 OS << ">";
101 }
102
103 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const104 LLVM_DUMP_METHOD void MCInst::dump() const {
105 print(dbgs());
106 dbgs() << "\n";
107 }
108 #endif
109