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 "MCTargetDesc/RISCVMCExpr.h"
17 #include "llvm/MC/MCAsmInfo.h"
18 #include "llvm/MC/MCExpr.h"
19 #include "llvm/MC/MCInst.h"
20 #include "llvm/MC/MCRegisterInfo.h"
21 #include "llvm/MC/MCSubtargetInfo.h"
22 #include "llvm/MC/MCSymbol.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/FormattedStream.h"
26 using namespace llvm;
27
28 #define DEBUG_TYPE "asm-printer"
29
30 // Include the auto-generated portion of the assembly writer.
31 #define PRINT_ALIAS_INSTR
32 #include "RISCVGenAsmWriter.inc"
33
34 // Include the auto-generated portion of the compress emitter.
35 #define GEN_UNCOMPRESS_INSTR
36 #include "RISCVGenCompressInstEmitter.inc"
37
38 static cl::opt<bool>
39 NoAliases("riscv-no-aliases",
40 cl::desc("Disable the emission of assembler pseudo instructions"),
41 cl::init(false),
42 cl::Hidden);
43
printInst(const MCInst * MI,raw_ostream & O,StringRef Annot,const MCSubtargetInfo & STI)44 void RISCVInstPrinter::printInst(const MCInst *MI, raw_ostream &O,
45 StringRef Annot, const MCSubtargetInfo &STI) {
46 bool Res = false;
47 const MCInst *NewMI = MI;
48 MCInst UncompressedMI;
49 if (!NoAliases)
50 Res = uncompressInst(UncompressedMI, *MI, MRI, STI);
51 if (Res)
52 NewMI = const_cast<MCInst*>(&UncompressedMI);
53 if (NoAliases || !printAliasInstr(NewMI, STI, O))
54 printInstruction(NewMI, STI, O);
55 printAnnotation(O, Annot);
56 }
57
printRegName(raw_ostream & O,unsigned RegNo) const58 void RISCVInstPrinter::printRegName(raw_ostream &O, unsigned RegNo) const {
59 O << getRegisterName(RegNo);
60 }
61
printOperand(const MCInst * MI,unsigned OpNo,const MCSubtargetInfo & STI,raw_ostream & O,const char * Modifier)62 void RISCVInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
63 const MCSubtargetInfo &STI,
64 raw_ostream &O, const char *Modifier) {
65 assert((Modifier == 0 || Modifier[0] == 0) && "No modifiers supported");
66 const MCOperand &MO = MI->getOperand(OpNo);
67
68 if (MO.isReg()) {
69 printRegName(O, MO.getReg());
70 return;
71 }
72
73 if (MO.isImm()) {
74 O << MO.getImm();
75 return;
76 }
77
78 assert(MO.isExpr() && "Unknown operand kind in printOperand");
79 MO.getExpr()->print(O, &MAI);
80 }
81
printFenceArg(const MCInst * MI,unsigned OpNo,const MCSubtargetInfo & STI,raw_ostream & O)82 void RISCVInstPrinter::printFenceArg(const MCInst *MI, unsigned OpNo,
83 const MCSubtargetInfo &STI,
84 raw_ostream &O) {
85 unsigned FenceArg = MI->getOperand(OpNo).getImm();
86 if ((FenceArg & RISCVFenceField::I) != 0)
87 O << 'i';
88 if ((FenceArg & RISCVFenceField::O) != 0)
89 O << 'o';
90 if ((FenceArg & RISCVFenceField::R) != 0)
91 O << 'r';
92 if ((FenceArg & RISCVFenceField::W) != 0)
93 O << 'w';
94 }
95
printFRMArg(const MCInst * MI,unsigned OpNo,const MCSubtargetInfo & STI,raw_ostream & O)96 void RISCVInstPrinter::printFRMArg(const MCInst *MI, unsigned OpNo,
97 const MCSubtargetInfo &STI,
98 raw_ostream &O) {
99 auto FRMArg =
100 static_cast<RISCVFPRndMode::RoundingMode>(MI->getOperand(OpNo).getImm());
101 O << RISCVFPRndMode::roundingModeToString(FRMArg);
102 }
103