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/ADT/SmallSet.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/MC/MCExpr.h"
22 #include "llvm/MC/MCInst.h"
23 #include "llvm/MC/MCInstrInfo.h"
24 #include "llvm/MC/MCSubtargetInfo.h"
25 #include "llvm/MC/MCSymbol.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/FormattedStream.h"
28 #include "llvm/Target/TargetRegisterInfo.h"
31 #define DEBUG_TYPE "asm-printer"
33 #include "WebAssemblyGenAsmWriter.inc"
35 WebAssemblyInstPrinter::WebAssemblyInstPrinter(const MCAsmInfo &MAI,
36 const MCInstrInfo &MII,
37 const MCRegisterInfo &MRI)
38 : MCInstPrinter(MAI, MII, MRI), ControlFlowCounter(0) {}
40 void WebAssemblyInstPrinter::printRegName(raw_ostream &OS,
41 unsigned RegNo) const {
42 assert(RegNo != WebAssemblyFunctionInfo::UnusedReg);
43 // Note that there's an implicit get_local/set_local here!
47 void WebAssemblyInstPrinter::printInst(const MCInst *MI, raw_ostream &OS,
49 const MCSubtargetInfo & /*STI*/) {
50 // Print the instruction (this uses the AsmStrings from the .td files).
51 printInstruction(MI, OS);
53 // Print any additional variadic operands.
54 const MCInstrDesc &Desc = MII.get(MI->getOpcode());
55 if (Desc.isVariadic())
56 for (auto i = Desc.getNumOperands(), e = MI->getNumOperands(); i < e; ++i) {
59 printOperand(MI, i, OS);
62 // Print any added annotation.
63 printAnnotation(OS, Annot);
66 // Observe any effects on the control flow stack, for use in annotating
67 // control flow label references.
68 switch (MI->getOpcode()) {
71 case WebAssembly::LOOP: {
72 // Grab the TopLabel value first so that labels print in numeric order.
73 uint64_t TopLabel = ControlFlowCounter++;
74 ControlFlowStack.push_back(std::make_pair(ControlFlowCounter++, false));
75 printAnnotation(OS, "label" + utostr(TopLabel) + ':');
76 ControlFlowStack.push_back(std::make_pair(TopLabel, true));
79 case WebAssembly::BLOCK:
80 ControlFlowStack.push_back(std::make_pair(ControlFlowCounter++, false));
82 case WebAssembly::END_LOOP:
83 ControlFlowStack.pop_back();
85 OS, "label" + utostr(ControlFlowStack.pop_back_val().first) + ':');
87 case WebAssembly::END_BLOCK:
89 OS, "label" + utostr(ControlFlowStack.pop_back_val().first) + ':');
93 // Annotate any control flow label references.
94 unsigned NumFixedOperands = Desc.NumOperands;
95 SmallSet<uint64_t, 8> Printed;
96 for (unsigned i = 0, e = MI->getNumOperands(); i < e; ++i) {
97 const MCOperandInfo &Info = Desc.OpInfo[i];
98 if (!(i < NumFixedOperands
99 ? (Info.OperandType == WebAssembly::OPERAND_BASIC_BLOCK)
100 : (Desc.TSFlags & WebAssemblyII::VariableOpImmediateIsLabel)))
102 uint64_t Depth = MI->getOperand(i).getImm();
103 if (!Printed.insert(Depth).second)
105 const auto &Pair = ControlFlowStack.rbegin()[Depth];
106 printAnnotation(OS, utostr(Depth) + ": " + (Pair.second ? "up" : "down") +
107 " to label" + utostr(Pair.first));
112 static std::string toString(const APFloat &FP) {
113 static const size_t BufBytes = 128;
116 assert((FP.bitwiseIsEqual(APFloat::getQNaN(FP.getSemantics())) ||
118 APFloat::getQNaN(FP.getSemantics(), /*Negative=*/true))) &&
119 "convertToHexString handles neither SNaN nor NaN payloads");
120 // Use C99's hexadecimal floating-point representation.
121 auto Written = FP.convertToHexString(
122 buf, /*hexDigits=*/0, /*upperCase=*/false, APFloat::rmNearestTiesToEven);
124 assert(Written != 0);
125 assert(Written < BufBytes);
129 void WebAssemblyInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
131 const MCOperand &Op = MI->getOperand(OpNo);
133 assert((OpNo < MII.get(MI->getOpcode()).getNumOperands() ||
134 MII.get(MI->getOpcode()).TSFlags == 0) &&
135 "WebAssembly variable_ops register ops don't use TSFlags");
136 unsigned WAReg = Op.getReg();
138 printRegName(O, WAReg);
139 else if (OpNo >= MII.get(MI->getOpcode()).getNumDefs())
140 O << "$pop" << (WAReg & INT32_MAX);
141 else if (WAReg != WebAssemblyFunctionInfo::UnusedReg)
142 O << "$push" << (WAReg & INT32_MAX);
145 // Add a '=' suffix if this is a def.
146 if (OpNo < MII.get(MI->getOpcode()).getNumDefs())
148 } else if (Op.isImm()) {
149 assert((OpNo < MII.get(MI->getOpcode()).getNumOperands() ||
150 (MII.get(MI->getOpcode()).TSFlags &
151 WebAssemblyII::VariableOpIsImmediate)) &&
152 "WebAssemblyII::VariableOpIsImmediate should be set for "
153 "variable_ops immediate ops");
154 // TODO: (MII.get(MI->getOpcode()).TSFlags &
155 // WebAssemblyII::VariableOpImmediateIsLabel)
156 // can tell us whether this is an immediate referencing a label in the
157 // control flow stack, and it may be nice to pretty-print.
159 } else if (Op.isFPImm()) {
160 assert((OpNo < MII.get(MI->getOpcode()).getNumOperands() ||
161 MII.get(MI->getOpcode()).TSFlags == 0) &&
162 "WebAssembly variable_ops floating point ops don't use TSFlags");
163 O << toString(APFloat(Op.getFPImm()));
165 assert((OpNo < MII.get(MI->getOpcode()).getNumOperands() ||
166 (MII.get(MI->getOpcode()).TSFlags &
167 WebAssemblyII::VariableOpIsImmediate)) &&
168 "WebAssemblyII::VariableOpIsImmediate should be set for "
169 "variable_ops expr ops");
170 assert(Op.isExpr() && "unknown operand kind in printOperand");
171 Op.getExpr()->print(O, &MAI);
175 const char *llvm::WebAssembly::TypeToString(MVT Ty) {
176 switch (Ty.SimpleTy) {
186 llvm_unreachable("unsupported type");