1 //===-- RISCVInstPrinter.cpp - Convert RISCV MCInst to asm syntax ---------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This class prints an RISCV MCInst to a .s file.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "RISCVInstPrinter.h"
15 #include "MCTargetDesc/RISCVBaseInfo.h"
16 #include "llvm/MC/MCAsmInfo.h"
17 #include "llvm/MC/MCExpr.h"
18 #include "llvm/MC/MCInst.h"
19 #include "llvm/MC/MCRegisterInfo.h"
20 #include "llvm/MC/MCSymbol.h"
21 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/FormattedStream.h"
24 using namespace llvm;
25 
26 #define DEBUG_TYPE "asm-printer"
27 
28 // Include the auto-generated portion of the assembly writer.
29 #define PRINT_ALIAS_INSTR
30 #include "RISCVGenAsmWriter.inc"
31 
32 static cl::opt<bool>
33 NoAliases("riscv-no-aliases",
34             cl::desc("Disable the emission of assembler pseudo instructions"),
35             cl::init(false),
36             cl::Hidden);
37 
38 void RISCVInstPrinter::printInst(const MCInst *MI, raw_ostream &O,
39                                  StringRef Annot, const MCSubtargetInfo &STI) {
40   if (NoAliases || !printAliasInstr(MI, O))
41     printInstruction(MI, O);
42   printAnnotation(O, Annot);
43 }
44 
45 void RISCVInstPrinter::printRegName(raw_ostream &O, unsigned RegNo) const {
46   O << getRegisterName(RegNo);
47 }
48 
49 void RISCVInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
50                                     raw_ostream &O, const char *Modifier) {
51   assert((Modifier == 0 || Modifier[0] == 0) && "No modifiers supported");
52   const MCOperand &MO = MI->getOperand(OpNo);
53 
54   if (MO.isReg()) {
55     printRegName(O, MO.getReg());
56     return;
57   }
58 
59   if (MO.isImm()) {
60     O << MO.getImm();
61     return;
62   }
63 
64   assert(MO.isExpr() && "Unknown operand kind in printOperand");
65   MO.getExpr()->print(O, &MAI);
66 }
67 
68 void RISCVInstPrinter::printFenceArg(const MCInst *MI, unsigned OpNo,
69                                      raw_ostream &O) {
70   unsigned FenceArg = MI->getOperand(OpNo).getImm();
71   if ((FenceArg & RISCVFenceField::I) != 0)
72     O << 'i';
73   if ((FenceArg & RISCVFenceField::O) != 0)
74     O << 'o';
75   if ((FenceArg & RISCVFenceField::R) != 0)
76     O << 'r';
77   if ((FenceArg & RISCVFenceField::W) != 0)
78     O << 'w';
79 }
80 
81 void RISCVInstPrinter::printFRMArg(const MCInst *MI, unsigned OpNo,
82                                    raw_ostream &O) {
83   auto FRMArg =
84       static_cast<RISCVFPRndMode::RoundingMode>(MI->getOperand(OpNo).getImm());
85   O << RISCVFPRndMode::roundingModeToString(FRMArg);
86 }
87