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 "MCTargetDesc/WebAssemblyMCTargetDesc.h"
17 #include "WebAssembly.h"
18 #include "WebAssemblyMachineFunctionInfo.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"
29 #define DEBUG_TYPE "asm-printer"
31 #include "WebAssemblyGenAsmWriter.inc"
33 WebAssemblyInstPrinter::WebAssemblyInstPrinter(const MCAsmInfo &MAI,
34 const MCInstrInfo &MII,
35 const MCRegisterInfo &MRI)
36 : MCInstPrinter(MAI, MII, MRI) {}
38 void WebAssemblyInstPrinter::printRegName(raw_ostream &OS,
39 unsigned RegNo) const {
40 assert(RegNo != WebAssemblyFunctionInfo::UnusedReg);
41 // Note that there's an implicit get_local/set_local here!
45 void WebAssemblyInstPrinter::printInst(const MCInst *MI, raw_ostream &OS,
47 const MCSubtargetInfo & /*STI*/) {
48 // Print the instruction (this uses the AsmStrings from the .td files).
49 printInstruction(MI, OS);
51 // Print any additional variadic operands.
52 const MCInstrDesc &Desc = MII.get(MI->getOpcode());
53 if (Desc.isVariadic())
54 for (auto i = Desc.getNumOperands(), e = MI->getNumOperands(); i < e; ++i) {
57 printOperand(MI, i, OS);
60 // Print any added annotation.
61 printAnnotation(OS, Annot);
64 static std::string toString(const APFloat &FP) {
65 static const size_t BufBytes = 128;
68 assert((FP.bitwiseIsEqual(APFloat::getQNaN(FP.getSemantics())) ||
70 APFloat::getQNaN(FP.getSemantics(), /*Negative=*/true))) &&
71 "convertToHexString handles neither SNaN nor NaN payloads");
72 // Use C99's hexadecimal floating-point representation.
73 auto Written = FP.convertToHexString(
74 buf, /*hexDigits=*/0, /*upperCase=*/false, APFloat::rmNearestTiesToEven);
77 assert(Written < BufBytes);
81 void WebAssemblyInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
83 const MCOperand &Op = MI->getOperand(OpNo);
85 assert((OpNo < MII.get(MI->getOpcode()).getNumOperands() ||
86 MII.get(MI->getOpcode()).TSFlags == 0) &&
87 "WebAssembly variable_ops register ops don't use TSFlags");
88 unsigned WAReg = Op.getReg();
90 printRegName(O, WAReg);
91 else if (OpNo >= MII.get(MI->getOpcode()).getNumDefs())
92 O << "$pop" << (WAReg & INT32_MAX);
93 else if (WAReg != WebAssemblyFunctionInfo::UnusedReg)
94 O << "$push" << (WAReg & INT32_MAX);
97 // Add a '=' suffix if this is a def.
98 if (OpNo < MII.get(MI->getOpcode()).getNumDefs())
100 } else if (Op.isImm()) {
101 assert((OpNo < MII.get(MI->getOpcode()).getNumOperands() ||
102 (MII.get(MI->getOpcode()).TSFlags &
103 WebAssemblyII::VariableOpIsImmediate)) &&
104 "WebAssemblyII::VariableOpIsImmediate should be set for "
105 "variable_ops immediate ops");
106 if (MII.get(MI->getOpcode()).TSFlags &
107 WebAssemblyII::VariableOpImmediateIsType)
108 // The immediates represent types.
109 O << WebAssembly::TypeToString(MVT::SimpleValueType(Op.getImm()));
112 } else if (Op.isFPImm()) {
113 assert((OpNo < MII.get(MI->getOpcode()).getNumOperands() ||
114 MII.get(MI->getOpcode()).TSFlags == 0) &&
115 "WebAssembly variable_ops floating point ops don't use TSFlags");
116 O << toString(APFloat(Op.getFPImm()));
118 assert((OpNo < MII.get(MI->getOpcode()).getNumOperands() ||
119 (MII.get(MI->getOpcode()).TSFlags &
120 WebAssemblyII::VariableOpIsImmediate)) &&
121 "WebAssemblyII::VariableOpIsImmediate should be set for "
122 "variable_ops expr ops");
123 assert(Op.isExpr() && "unknown operand kind in printOperand");
124 Op.getExpr()->print(O, &MAI);
128 const char *llvm::WebAssembly::TypeToString(MVT Ty) {
129 switch (Ty.SimpleTy) {
139 llvm_unreachable("unsupported type");