LLVM  7.0.0
RISCVInstPrinter.cpp
Go to the documentation of this file.
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"
17 #include "llvm/MC/MCAsmInfo.h"
18 #include "llvm/MC/MCExpr.h"
19 #include "llvm/MC/MCInst.h"
20 #include "llvm/MC/MCRegisterInfo.h"
22 #include "llvm/MC/MCSymbol.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 
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 
58 void RISCVInstPrinter::printRegName(raw_ostream &O, unsigned RegNo) const {
59  O << getRegisterName(RegNo);
60 }
61 
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 
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 
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());
102 }
bool isImm() const
Definition: MCInst.h:59
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
bool isReg() const
Definition: MCInst.h:58
unsigned getReg() const
Returns the register number.
Definition: MCInst.h:65
const MCExpr * getExpr() const
Definition: MCInst.h:96
void printFRMArg(const MCInst *MI, unsigned OpNo, const MCSubtargetInfo &STI, raw_ostream &O)
void printInst(const MCInst *MI, raw_ostream &O, StringRef Annot, const MCSubtargetInfo &STI) override
Print the specified MCInst to the specified raw_ostream.
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:161
int64_t getImm() const
Definition: MCInst.h:76
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:410
void print(raw_ostream &OS, const MCAsmInfo *MAI, bool InParens=false) const
Definition: MCExpr.cpp:42
bool isExpr() const
Definition: MCInst.h:61
static const char * getRegisterName(unsigned RegNo, unsigned AltIdx=RISCV::ABIRegAltName)
void printFenceArg(const MCInst *MI, unsigned OpNo, const MCSubtargetInfo &STI, raw_ostream &O)
const MCOperand & getOperand(unsigned i) const
Definition: MCInst.h:182
static cl::opt< bool > NoAliases("riscv-no-aliases", cl::desc("Disable the emission of assembler pseudo instructions"), cl::init(false), cl::Hidden)
bool printAliasInstr(const MCInst *MI, const MCSubtargetInfo &STI, raw_ostream &O)
void printOperand(const MCInst *MI, unsigned OpNo, const MCSubtargetInfo &STI, raw_ostream &O, const char *Modifier=nullptr)
const MCAsmInfo & MAI
Definition: MCInstPrinter.h:46
void printInstruction(const MCInst *MI, const MCSubtargetInfo &STI, raw_ostream &O)
Generic base class for all target subtargets.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static StringRef roundingModeToString(RoundingMode RndMode)
Definition: RISCVBaseInfo.h:78
void printAnnotation(raw_ostream &OS, StringRef Annot)
Utility function for printing annotations.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
void printRegName(raw_ostream &O, unsigned RegNo) const override
Print the assembler register name.
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:35
const MCRegisterInfo & MRI
Definition: MCInstPrinter.h:48