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" 13*fe6060f1SDimitry 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 21*fe6060f1SDimitry Andric void MCOperand::print(raw_ostream &OS, const MCRegisterInfo *RegInfo) const { 220b57cec5SDimitry Andric OS << "<MCOperand "; 230b57cec5SDimitry Andric if (!isValid()) 240b57cec5SDimitry Andric OS << "INVALID"; 25*fe6060f1SDimitry Andric else if (isReg()) { 26*fe6060f1SDimitry Andric OS << "Reg:"; 27*fe6060f1SDimitry Andric if (RegInfo) 28*fe6060f1SDimitry Andric OS << RegInfo->getName(getReg()); 29*fe6060f1SDimitry Andric else 30*fe6060f1SDimitry Andric OS << getReg(); 31*fe6060f1SDimitry Andric } else if (isImm()) 320b57cec5SDimitry Andric OS << "Imm:" << getImm(); 33*fe6060f1SDimitry Andric else if (isSFPImm()) 34*fe6060f1SDimitry Andric OS << "SFPImm:" << bit_cast<float>(getSFPImm()); 35*fe6060f1SDimitry Andric else if (isDFPImm()) 36*fe6060f1SDimitry Andric OS << "DFPImm:" << bit_cast<double>(getDFPImm()); 370b57cec5SDimitry Andric else if (isExpr()) { 380b57cec5SDimitry Andric OS << "Expr:(" << *getExpr() << ")"; 390b57cec5SDimitry Andric } else if (isInst()) { 40*fe6060f1SDimitry Andric OS << "Inst:("; 41*fe6060f1SDimitry Andric getInst()->print(OS, RegInfo); 42*fe6060f1SDimitry Andric OS << ")"; 430b57cec5SDimitry Andric } else 440b57cec5SDimitry Andric OS << "UNDEFINED"; 450b57cec5SDimitry Andric OS << ">"; 460b57cec5SDimitry Andric } 470b57cec5SDimitry Andric 480b57cec5SDimitry 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 560b57cec5SDimitry 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) 660b57cec5SDimitry Andric LLVM_DUMP_METHOD void MCOperand::dump() const { 670b57cec5SDimitry Andric print(dbgs()); 680b57cec5SDimitry Andric dbgs() << "\n"; 690b57cec5SDimitry Andric } 700b57cec5SDimitry Andric #endif 710b57cec5SDimitry Andric 72*fe6060f1SDimitry 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 << " "; 76*fe6060f1SDimitry Andric getOperand(i).print(OS, RegInfo); 770b57cec5SDimitry Andric } 780b57cec5SDimitry Andric OS << ">"; 790b57cec5SDimitry Andric } 800b57cec5SDimitry Andric 810b57cec5SDimitry Andric void MCInst::dump_pretty(raw_ostream &OS, const MCInstPrinter *Printer, 82*fe6060f1SDimitry Andric StringRef Separator, 83*fe6060f1SDimitry Andric const MCRegisterInfo *RegInfo) const { 840b57cec5SDimitry Andric StringRef InstName = Printer ? Printer->getOpcodeName(getOpcode()) : ""; 85*fe6060f1SDimitry Andric dump_pretty(OS, InstName, Separator, RegInfo); 860b57cec5SDimitry Andric } 870b57cec5SDimitry Andric 88*fe6060f1SDimitry Andric void MCInst::dump_pretty(raw_ostream &OS, StringRef Name, StringRef Separator, 89*fe6060f1SDimitry 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; 98*fe6060f1SDimitry Andric getOperand(i).print(OS, RegInfo); 990b57cec5SDimitry Andric } 1000b57cec5SDimitry Andric OS << ">"; 1010b57cec5SDimitry Andric } 1020b57cec5SDimitry Andric 1030b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 1040b57cec5SDimitry Andric LLVM_DUMP_METHOD void MCInst::dump() const { 1050b57cec5SDimitry Andric print(dbgs()); 1060b57cec5SDimitry Andric dbgs() << "\n"; 1070b57cec5SDimitry Andric } 1080b57cec5SDimitry Andric #endif 109