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 "WebAssemblyMachineFunctionInfo.h"
18 #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
19 #include "llvm/MC/MCExpr.h"
20 #include "llvm/MC/MCInst.h"
21 #include "llvm/MC/MCInstrInfo.h"
22 #include "llvm/MC/MCSubtargetInfo.h"
23 #include "llvm/MC/MCSymbol.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/FormattedStream.h"
26 #include "llvm/Target/TargetRegisterInfo.h"
30 #define DEBUG_TYPE "asm-printer"
32 #include "WebAssemblyGenAsmWriter.inc"
34 WebAssemblyInstPrinter::WebAssemblyInstPrinter(const MCAsmInfo &MAI,
35 const MCInstrInfo &MII,
36 const MCRegisterInfo &MRI)
37 : MCInstPrinter(MAI, MII, MRI) {}
39 void WebAssemblyInstPrinter::printRegName(raw_ostream &OS,
40 unsigned RegNo) const {
41 assert(RegNo != WebAssemblyFunctionInfo::UnusedReg);
42 // Note that there's an implicit get_local/set_local here!
46 void WebAssemblyInstPrinter::printInst(const MCInst *MI, raw_ostream &OS,
48 const MCSubtargetInfo &STI) {
49 printInstruction(MI, OS);
51 const MCInstrDesc &Desc = MII.get(MI->getOpcode());
52 if (Desc.isVariadic())
53 for (unsigned i = Desc.getNumOperands(), e = MI->getNumOperands(); i < e;
57 printOperand(MI, i, OS);
60 printAnnotation(OS, Annot);
63 static std::string toString(const APFloat &FP) {
64 static const size_t BufBytes = 128;
67 assert((FP.bitwiseIsEqual(APFloat::getQNaN(FP.getSemantics())) ||
69 APFloat::getQNaN(FP.getSemantics(), /*Negative=*/true))) &&
70 "convertToHexString handles neither SNaN nor NaN payloads");
71 // Use C99's hexadecimal floating-point representation.
72 auto Written = FP.convertToHexString(
73 buf, /*hexDigits=*/0, /*upperCase=*/false, APFloat::rmNearestTiesToEven);
76 assert(Written < BufBytes);
80 void WebAssemblyInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
82 const MCOperand &Op = MI->getOperand(OpNo);
84 unsigned WAReg = Op.getReg();
86 printRegName(O, WAReg);
87 else if (OpNo >= MII.get(MI->getOpcode()).getNumDefs())
88 O << "$pop" << (WAReg & INT32_MAX);
89 else if (WAReg != WebAssemblyFunctionInfo::UnusedReg)
90 O << "$push" << (WAReg & INT32_MAX);
93 } else if (Op.isImm()) {
94 switch (MI->getOpcode()) {
95 case WebAssembly::PARAM:
96 case WebAssembly::RESULT:
97 case WebAssembly::LOCAL:
98 switch (Op.getImm()) {
99 case MVT::i32: O << "i32"; break;
100 case MVT::i64: O << "i64"; break;
101 case MVT::f32: O << "f32"; break;
102 case MVT::f64: O << "f64"; break;
103 default: llvm_unreachable("unexpected type");
110 } else if (Op.isFPImm())
111 O << toString(APFloat(Op.getFPImm()));
113 assert(Op.isExpr() && "unknown operand kind in printOperand");
114 Op.getExpr()->print(O, &MAI);