blob: f1fa2ecbcb22b4680d7318bf90ce401af1854a10 [file] [log] [blame]
Alex Bradbury2fee9ea2017-08-15 13:08:29 +00001//===-- 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"
Alex Bradbury6758ecb2017-09-17 14:27:35 +000015#include "MCTargetDesc/RISCVBaseInfo.h"
Alex Bradbury2fee9ea2017-08-15 13:08:29 +000016#include "llvm/MC/MCAsmInfo.h"
17#include "llvm/MC/MCExpr.h"
18#include "llvm/MC/MCInst.h"
Alex Bradbury0d6cf902017-12-07 10:26:05 +000019#include "llvm/MC/MCRegisterInfo.h"
Ana Pazose3d24832018-01-12 02:27:00 +000020#include "llvm/MC/MCSubtargetInfo.h"
Alex Bradbury2fee9ea2017-08-15 13:08:29 +000021#include "llvm/MC/MCSymbol.h"
Alex Bradbury9ed84c82017-12-12 15:46:15 +000022#include "llvm/Support/CommandLine.h"
Alex Bradbury2fee9ea2017-08-15 13:08:29 +000023#include "llvm/Support/ErrorHandling.h"
24#include "llvm/Support/FormattedStream.h"
25using namespace llvm;
26
27#define DEBUG_TYPE "asm-printer"
28
29// Include the auto-generated portion of the assembly writer.
Alex Bradbury0d6cf902017-12-07 10:26:05 +000030#define PRINT_ALIAS_INSTR
Alex Bradbury2fee9ea2017-08-15 13:08:29 +000031#include "RISCVGenAsmWriter.inc"
32
Alex Bradbury9ed84c82017-12-12 15:46:15 +000033static cl::opt<bool>
34NoAliases("riscv-no-aliases",
35 cl::desc("Disable the emission of assembler pseudo instructions"),
Alex Bradbury59136ff2017-12-15 09:47:01 +000036 cl::init(false),
Alex Bradbury9ed84c82017-12-12 15:46:15 +000037 cl::Hidden);
38
Alex Bradbury2fee9ea2017-08-15 13:08:29 +000039void RISCVInstPrinter::printInst(const MCInst *MI, raw_ostream &O,
40 StringRef Annot, const MCSubtargetInfo &STI) {
Ana Pazose3d24832018-01-12 02:27:00 +000041 if (NoAliases || !printAliasInstr(MI, STI, O))
42 printInstruction(MI, STI, O);
Alex Bradbury2fee9ea2017-08-15 13:08:29 +000043 printAnnotation(O, Annot);
44}
45
46void RISCVInstPrinter::printRegName(raw_ostream &O, unsigned RegNo) const {
47 O << getRegisterName(RegNo);
48}
49
50void RISCVInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
Ana Pazose3d24832018-01-12 02:27:00 +000051 const MCSubtargetInfo &STI,
Alex Bradbury21c8adf2017-08-20 06:58:43 +000052 raw_ostream &O, const char *Modifier) {
Alex Bradbury2fee9ea2017-08-15 13:08:29 +000053 assert((Modifier == 0 || Modifier[0] == 0) && "No modifiers supported");
54 const MCOperand &MO = MI->getOperand(OpNo);
55
56 if (MO.isReg()) {
57 printRegName(O, MO.getReg());
58 return;
59 }
60
61 if (MO.isImm()) {
62 O << MO.getImm();
63 return;
64 }
65
66 assert(MO.isExpr() && "Unknown operand kind in printOperand");
67 MO.getExpr()->print(O, &MAI);
68}
Alex Bradbury6758ecb2017-09-17 14:27:35 +000069
70void RISCVInstPrinter::printFenceArg(const MCInst *MI, unsigned OpNo,
Ana Pazose3d24832018-01-12 02:27:00 +000071 const MCSubtargetInfo &STI,
Alex Bradbury6758ecb2017-09-17 14:27:35 +000072 raw_ostream &O) {
73 unsigned FenceArg = MI->getOperand(OpNo).getImm();
74 if ((FenceArg & RISCVFenceField::I) != 0)
75 O << 'i';
76 if ((FenceArg & RISCVFenceField::O) != 0)
77 O << 'o';
78 if ((FenceArg & RISCVFenceField::R) != 0)
79 O << 'r';
80 if ((FenceArg & RISCVFenceField::W) != 0)
81 O << 'w';
82}
Alex Bradbury0d6cf902017-12-07 10:26:05 +000083
84void RISCVInstPrinter::printFRMArg(const MCInst *MI, unsigned OpNo,
Ana Pazose3d24832018-01-12 02:27:00 +000085 const MCSubtargetInfo &STI,
Alex Bradbury0d6cf902017-12-07 10:26:05 +000086 raw_ostream &O) {
87 auto FRMArg =
88 static_cast<RISCVFPRndMode::RoundingMode>(MI->getOperand(OpNo).getImm());
89 O << RISCVFPRndMode::roundingModeToString(FRMArg);
90}