Path: blob/main/contrib/llvm-project/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchInstPrinter.cpp
35294 views
//===- LoongArchInstPrinter.cpp - Convert LoongArch MCInst to asm syntax --===//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//===----------------------------------------------------------------------===//7//8// This class prints an LoongArch MCInst to a .s file.9//10//===----------------------------------------------------------------------===//1112#include "LoongArchInstPrinter.h"13#include "LoongArchBaseInfo.h"14#include "LoongArchMCTargetDesc.h"15#include "llvm/MC/MCAsmInfo.h"16#include "llvm/MC/MCInst.h"17#include "llvm/MC/MCRegisterInfo.h"18#include "llvm/MC/MCSubtargetInfo.h"19#include "llvm/MC/MCSymbol.h"20#include "llvm/Support/CommandLine.h"21using namespace llvm;2223#define DEBUG_TYPE "loongarch-asm-printer"2425// Include the auto-generated portion of the assembly writer.26#define PRINT_ALIAS_INSTR27#include "LoongArchGenAsmWriter.inc"2829static cl::opt<bool>30NumericReg("loongarch-numeric-reg",31cl::desc("Print numeric register names rather than the ABI "32"names (such as $r0 instead of $zero)"),33cl::init(false), cl::Hidden);3435// The command-line flag above is used by llvm-mc and llc. It can be used by36// `llvm-objdump`, but we override the value here to handle options passed to37// `llvm-objdump` with `-M` (which matches GNU objdump). There did not seem to38// be an easier way to allow these options in all these tools, without doing it39// this way.40bool LoongArchInstPrinter::applyTargetSpecificCLOption(StringRef Opt) {41if (Opt == "numeric") {42NumericReg = true;43return true;44}4546return false;47}4849void LoongArchInstPrinter::printInst(const MCInst *MI, uint64_t Address,50StringRef Annot,51const MCSubtargetInfo &STI,52raw_ostream &O) {53if (!printAliasInstr(MI, Address, STI, O))54printInstruction(MI, Address, STI, O);55printAnnotation(O, Annot);56}5758void LoongArchInstPrinter::printRegName(raw_ostream &O, MCRegister Reg) const {59O << '$' << getRegisterName(Reg);60}6162void LoongArchInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,63const MCSubtargetInfo &STI,64raw_ostream &O) {65const MCOperand &MO = MI->getOperand(OpNo);6667if (MO.isReg()) {68printRegName(O, MO.getReg());69return;70}7172if (MO.isImm()) {73O << MO.getImm();74return;75}7677assert(MO.isExpr() && "Unknown operand kind in printOperand");78MO.getExpr()->print(O, &MAI);79}8081void LoongArchInstPrinter::printAtomicMemOp(const MCInst *MI, unsigned OpNo,82const MCSubtargetInfo &STI,83raw_ostream &O) {84const MCOperand &MO = MI->getOperand(OpNo);85assert(MO.isReg() && "printAtomicMemOp can only print register operands");86printRegName(O, MO.getReg());87}8889const char *LoongArchInstPrinter::getRegisterName(MCRegister Reg) {90// Default print reg alias name91return getRegisterName(Reg, NumericReg ? LoongArch::NoRegAltName92: LoongArch::RegAliasName);93}949596