1 //===- LoongArchInstPrinter.cpp - Convert LoongArch MCInst to asm syntax --===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This class prints an LoongArch MCInst to a .s file.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "LoongArchInstPrinter.h"
14 #include "LoongArchBaseInfo.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 using namespace llvm;
21 
22 #define DEBUG_TYPE "loongarch-asm-printer"
23 
24 // Include the auto-generated portion of the assembly writer.
25 #define PRINT_ALIAS_INSTR
26 #include "LoongArchGenAsmWriter.inc"
27 
28 void LoongArchInstPrinter::printInst(const MCInst *MI, uint64_t Address,
29                                      StringRef Annot,
30                                      const MCSubtargetInfo &STI,
31                                      raw_ostream &O) {
32   if (!printAliasInstr(MI, Address, STI, O))
33     printInstruction(MI, Address, STI, O);
34   printAnnotation(O, Annot);
35 }
36 
37 void LoongArchInstPrinter::printRegName(raw_ostream &O, unsigned RegNo) const {
38   O << '$' << getRegisterName(RegNo);
39 }
40 
41 void LoongArchInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
42                                         const MCSubtargetInfo &STI,
43                                         raw_ostream &O) {
44   const MCOperand &MO = MI->getOperand(OpNo);
45 
46   if (MO.isReg()) {
47     printRegName(O, MO.getReg());
48     return;
49   }
50 
51   if (MO.isImm()) {
52     O << MO.getImm();
53     return;
54   }
55 
56   assert(MO.isExpr() && "Unknown operand kind in printOperand");
57   MO.getExpr()->print(O, &MAI);
58 }
59 
60 const char *LoongArchInstPrinter::getRegisterName(unsigned RegNo) {
61   // Default print reg alias name
62   return getRegisterName(RegNo, LoongArch::RegAliasName);
63 }
64