181ad6265SDimitry Andric //===- LoongArchInstPrinter.cpp - Convert LoongArch MCInst to asm syntax --===//
281ad6265SDimitry Andric //
381ad6265SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
481ad6265SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
581ad6265SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
681ad6265SDimitry Andric //
781ad6265SDimitry Andric //===----------------------------------------------------------------------===//
881ad6265SDimitry Andric //
981ad6265SDimitry Andric // This class prints an LoongArch MCInst to a .s file.
1081ad6265SDimitry Andric //
1181ad6265SDimitry Andric //===----------------------------------------------------------------------===//
1281ad6265SDimitry Andric
1381ad6265SDimitry Andric #include "LoongArchInstPrinter.h"
1481ad6265SDimitry Andric #include "LoongArchBaseInfo.h"
15*bdd1243dSDimitry Andric #include "LoongArchMCTargetDesc.h"
1681ad6265SDimitry Andric #include "llvm/MC/MCAsmInfo.h"
1781ad6265SDimitry Andric #include "llvm/MC/MCInst.h"
1881ad6265SDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
1981ad6265SDimitry Andric #include "llvm/MC/MCSubtargetInfo.h"
2081ad6265SDimitry Andric #include "llvm/MC/MCSymbol.h"
21*bdd1243dSDimitry Andric #include "llvm/Support/CommandLine.h"
2281ad6265SDimitry Andric using namespace llvm;
2381ad6265SDimitry Andric
2481ad6265SDimitry Andric #define DEBUG_TYPE "loongarch-asm-printer"
2581ad6265SDimitry Andric
2681ad6265SDimitry Andric // Include the auto-generated portion of the assembly writer.
2781ad6265SDimitry Andric #define PRINT_ALIAS_INSTR
2881ad6265SDimitry Andric #include "LoongArchGenAsmWriter.inc"
2981ad6265SDimitry Andric
30*bdd1243dSDimitry Andric static cl::opt<bool>
31*bdd1243dSDimitry Andric NumericReg("loongarch-numeric-reg",
32*bdd1243dSDimitry Andric cl::desc("Print numeric register names rather than the ABI "
33*bdd1243dSDimitry Andric "names (such as $r0 instead of $zero)"),
34*bdd1243dSDimitry Andric cl::init(false), cl::Hidden);
35*bdd1243dSDimitry Andric
36*bdd1243dSDimitry Andric // The command-line flag above is used by llvm-mc and llc. It can be used by
37*bdd1243dSDimitry Andric // `llvm-objdump`, but we override the value here to handle options passed to
38*bdd1243dSDimitry Andric // `llvm-objdump` with `-M` (which matches GNU objdump). There did not seem to
39*bdd1243dSDimitry Andric // be an easier way to allow these options in all these tools, without doing it
40*bdd1243dSDimitry Andric // this way.
applyTargetSpecificCLOption(StringRef Opt)41*bdd1243dSDimitry Andric bool LoongArchInstPrinter::applyTargetSpecificCLOption(StringRef Opt) {
42*bdd1243dSDimitry Andric if (Opt == "numeric") {
43*bdd1243dSDimitry Andric NumericReg = true;
44*bdd1243dSDimitry Andric return true;
45*bdd1243dSDimitry Andric }
46*bdd1243dSDimitry Andric
47*bdd1243dSDimitry Andric return false;
48*bdd1243dSDimitry Andric }
49*bdd1243dSDimitry Andric
printInst(const MCInst * MI,uint64_t Address,StringRef Annot,const MCSubtargetInfo & STI,raw_ostream & O)5081ad6265SDimitry Andric void LoongArchInstPrinter::printInst(const MCInst *MI, uint64_t Address,
5181ad6265SDimitry Andric StringRef Annot,
5281ad6265SDimitry Andric const MCSubtargetInfo &STI,
5381ad6265SDimitry Andric raw_ostream &O) {
5481ad6265SDimitry Andric if (!printAliasInstr(MI, Address, STI, O))
5581ad6265SDimitry Andric printInstruction(MI, Address, STI, O);
5681ad6265SDimitry Andric printAnnotation(O, Annot);
5781ad6265SDimitry Andric }
5881ad6265SDimitry Andric
printRegName(raw_ostream & O,MCRegister Reg) const59*bdd1243dSDimitry Andric void LoongArchInstPrinter::printRegName(raw_ostream &O, MCRegister Reg) const {
60*bdd1243dSDimitry Andric O << '$' << getRegisterName(Reg);
6181ad6265SDimitry Andric }
6281ad6265SDimitry Andric
printOperand(const MCInst * MI,unsigned OpNo,const MCSubtargetInfo & STI,raw_ostream & O)6381ad6265SDimitry Andric void LoongArchInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
6481ad6265SDimitry Andric const MCSubtargetInfo &STI,
6581ad6265SDimitry Andric raw_ostream &O) {
6681ad6265SDimitry Andric const MCOperand &MO = MI->getOperand(OpNo);
6781ad6265SDimitry Andric
6881ad6265SDimitry Andric if (MO.isReg()) {
6981ad6265SDimitry Andric printRegName(O, MO.getReg());
7081ad6265SDimitry Andric return;
7181ad6265SDimitry Andric }
7281ad6265SDimitry Andric
7381ad6265SDimitry Andric if (MO.isImm()) {
7481ad6265SDimitry Andric O << MO.getImm();
7581ad6265SDimitry Andric return;
7681ad6265SDimitry Andric }
7781ad6265SDimitry Andric
7881ad6265SDimitry Andric assert(MO.isExpr() && "Unknown operand kind in printOperand");
7981ad6265SDimitry Andric MO.getExpr()->print(O, &MAI);
8081ad6265SDimitry Andric }
8181ad6265SDimitry Andric
printAtomicMemOp(const MCInst * MI,unsigned OpNo,const MCSubtargetInfo & STI,raw_ostream & O)82*bdd1243dSDimitry Andric void LoongArchInstPrinter::printAtomicMemOp(const MCInst *MI, unsigned OpNo,
83*bdd1243dSDimitry Andric const MCSubtargetInfo &STI,
84*bdd1243dSDimitry Andric raw_ostream &O) {
85*bdd1243dSDimitry Andric const MCOperand &MO = MI->getOperand(OpNo);
86*bdd1243dSDimitry Andric assert(MO.isReg() && "printAtomicMemOp can only print register operands");
87*bdd1243dSDimitry Andric printRegName(O, MO.getReg());
88*bdd1243dSDimitry Andric }
89*bdd1243dSDimitry Andric
getRegisterName(MCRegister Reg)90*bdd1243dSDimitry Andric const char *LoongArchInstPrinter::getRegisterName(MCRegister Reg) {
9181ad6265SDimitry Andric // Default print reg alias name
92*bdd1243dSDimitry Andric return getRegisterName(Reg, NumericReg ? LoongArch::NoRegAltName
93*bdd1243dSDimitry Andric : LoongArch::RegAliasName);
9481ad6265SDimitry Andric }
95