1 //=- WebAssemblyInstPrinter.cpp - WebAssembly assembly instruction printing -=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief Print MCInst instructions to wasm format.
13 //===----------------------------------------------------------------------===//
15 #include "InstPrinter/WebAssemblyInstPrinter.h"
16 #include "WebAssembly.h"
17 #include "llvm/MC/MCExpr.h"
18 #include "llvm/MC/MCInst.h"
19 #include "llvm/MC/MCInstrInfo.h"
20 #include "llvm/MC/MCSubtargetInfo.h"
21 #include "llvm/MC/MCSymbol.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/FormattedStream.h"
24 #include "llvm/Target/TargetRegisterInfo.h"
28 #define DEBUG_TYPE "asm-printer"
30 #include "WebAssemblyGenAsmWriter.inc"
32 WebAssemblyInstPrinter::WebAssemblyInstPrinter(const MCAsmInfo &MAI,
33 const MCInstrInfo &MII,
34 const MCRegisterInfo &MRI)
35 : MCInstPrinter(MAI, MII, MRI) {}
37 void WebAssemblyInstPrinter::printRegName(raw_ostream &OS,
38 unsigned RegNo) const {
39 if (TargetRegisterInfo::isPhysicalRegister(RegNo))
40 OS << getRegisterName(RegNo);
42 OS << TargetRegisterInfo::virtReg2Index(RegNo);
45 void WebAssemblyInstPrinter::printInst(const MCInst *MI, raw_ostream &OS,
47 const MCSubtargetInfo &STI) {
48 printInstruction(MI, OS);
49 printAnnotation(OS, Annot);
51 unsigned NumDefs = MII.get(MI->getOpcode()).getNumDefs();
52 assert(NumDefs <= 1 &&
53 "Instructions with multiple result values not implemented");
58 printRegName(OS, MI->getOperand(0).getReg());
63 void WebAssemblyInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
65 const MCOperand &Op = MI->getOperand(OpNo);
67 if (OpNo < MII.get(MI->getOpcode()).getNumDefs())
70 printRegName(O, Op.getReg());
71 } else if (Op.isImm())
72 O << '#' << Op.getImm();
73 else if (Op.isFPImm())
74 O << '#' << Op.getFPImm();
76 assert(Op.isExpr() && "unknown operand kind in printOperand");
77 Op.getExpr()->print(O, &MAI);