Alex Bradbury | 2fee9ea | 2017-08-15 13:08:29 +0000 | [diff] [blame] | 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" |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 15 | #include "MCTargetDesc/RISCVBaseInfo.h" |
Alex Bradbury | 2fee9ea | 2017-08-15 13:08:29 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCAsmInfo.h" |
| 17 | #include "llvm/MC/MCExpr.h" |
| 18 | #include "llvm/MC/MCInst.h" |
Alex Bradbury | 0d6cf90 | 2017-12-07 10:26:05 +0000 | [diff] [blame^] | 19 | #include "llvm/MC/MCRegisterInfo.h" |
Alex Bradbury | 2fee9ea | 2017-08-15 13:08:29 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCSymbol.h" |
| 21 | #include "llvm/Support/ErrorHandling.h" |
| 22 | #include "llvm/Support/FormattedStream.h" |
| 23 | using namespace llvm; |
| 24 | |
| 25 | #define DEBUG_TYPE "asm-printer" |
| 26 | |
| 27 | // Include the auto-generated portion of the assembly writer. |
Alex Bradbury | 0d6cf90 | 2017-12-07 10:26:05 +0000 | [diff] [blame^] | 28 | #define PRINT_ALIAS_INSTR |
Alex Bradbury | 2fee9ea | 2017-08-15 13:08:29 +0000 | [diff] [blame] | 29 | #include "RISCVGenAsmWriter.inc" |
| 30 | |
| 31 | void RISCVInstPrinter::printInst(const MCInst *MI, raw_ostream &O, |
| 32 | StringRef Annot, const MCSubtargetInfo &STI) { |
Alex Bradbury | 0d6cf90 | 2017-12-07 10:26:05 +0000 | [diff] [blame^] | 33 | if (!printAliasInstr(MI, O)) |
| 34 | printInstruction(MI, O); |
Alex Bradbury | 2fee9ea | 2017-08-15 13:08:29 +0000 | [diff] [blame] | 35 | printAnnotation(O, Annot); |
| 36 | } |
| 37 | |
| 38 | void RISCVInstPrinter::printRegName(raw_ostream &O, unsigned RegNo) const { |
| 39 | O << getRegisterName(RegNo); |
| 40 | } |
| 41 | |
| 42 | void RISCVInstPrinter::printOperand(const MCInst *MI, unsigned OpNo, |
Alex Bradbury | 21c8adf | 2017-08-20 06:58:43 +0000 | [diff] [blame] | 43 | raw_ostream &O, const char *Modifier) { |
Alex Bradbury | 2fee9ea | 2017-08-15 13:08:29 +0000 | [diff] [blame] | 44 | 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 Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 60 | |
| 61 | void 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 Bradbury | 0d6cf90 | 2017-12-07 10:26:05 +0000 | [diff] [blame^] | 73 | |
| 74 | void 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 | } |