blob: a396025ccc409ebc6e1c7001e71d0ed542778645 [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"
Alex Bradbury2fee9ea2017-08-15 13:08:29 +000020#include "llvm/MC/MCSymbol.h"
21#include "llvm/Support/ErrorHandling.h"
22#include "llvm/Support/FormattedStream.h"
23using namespace llvm;
24
25#define DEBUG_TYPE "asm-printer"
26
27// Include the auto-generated portion of the assembly writer.
Alex Bradbury0d6cf902017-12-07 10:26:05 +000028#define PRINT_ALIAS_INSTR
Alex Bradbury2fee9ea2017-08-15 13:08:29 +000029#include "RISCVGenAsmWriter.inc"
30
31void RISCVInstPrinter::printInst(const MCInst *MI, raw_ostream &O,
32 StringRef Annot, const MCSubtargetInfo &STI) {
Alex Bradbury0d6cf902017-12-07 10:26:05 +000033 if (!printAliasInstr(MI, O))
34 printInstruction(MI, O);
Alex Bradbury2fee9ea2017-08-15 13:08:29 +000035 printAnnotation(O, Annot);
36}
37
38void RISCVInstPrinter::printRegName(raw_ostream &O, unsigned RegNo) const {
39 O << getRegisterName(RegNo);
40}
41
42void RISCVInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
Alex Bradbury21c8adf2017-08-20 06:58:43 +000043 raw_ostream &O, const char *Modifier) {
Alex Bradbury2fee9ea2017-08-15 13:08:29 +000044 assert((Modifier == 0 || Modifier[0] == 0) && "No modifiers supported");
45 const MCOperand &MO = MI->getOperand(OpNo);
46
47 if (MO.isReg()) {
48 printRegName(O, MO.getReg());
49 return;
50 }
51
52 if (MO.isImm()) {
53 O << MO.getImm();
54 return;
55 }
56
57 assert(MO.isExpr() && "Unknown operand kind in printOperand");
58 MO.getExpr()->print(O, &MAI);
59}
Alex Bradbury6758ecb2017-09-17 14:27:35 +000060
61void RISCVInstPrinter::printFenceArg(const MCInst *MI, unsigned OpNo,
62 raw_ostream &O) {
63 unsigned FenceArg = MI->getOperand(OpNo).getImm();
64 if ((FenceArg & RISCVFenceField::I) != 0)
65 O << 'i';
66 if ((FenceArg & RISCVFenceField::O) != 0)
67 O << 'o';
68 if ((FenceArg & RISCVFenceField::R) != 0)
69 O << 'r';
70 if ((FenceArg & RISCVFenceField::W) != 0)
71 O << 'w';
72}
Alex Bradbury0d6cf902017-12-07 10:26:05 +000073
74void RISCVInstPrinter::printFRMArg(const MCInst *MI, unsigned OpNo,
75 raw_ostream &O) {
76 auto FRMArg =
77 static_cast<RISCVFPRndMode::RoundingMode>(MI->getOperand(OpNo).getImm());
78 O << RISCVFPRndMode::roundingModeToString(FRMArg);
79}