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 /// 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/CodeGen/TargetRegisterInfo.h"
22 #include "llvm/MC/MCExpr.h"
23 #include "llvm/MC/MCInst.h"
24 #include "llvm/MC/MCInstrInfo.h"
25 #include "llvm/MC/MCSubtargetInfo.h"
26 #include "llvm/MC/MCSymbol.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/FormattedStream.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) {}
40 void WebAssemblyInstPrinter::printRegName(raw_ostream &OS,
41 unsigned RegNo) const {
42 assert(RegNo != WebAssemblyFunctionInfo::UnusedReg);
43 // Note that there's an implicit local.get/local.set 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) {
57 // FIXME: For CALL_INDIRECT_VOID, don't print a leading comma, because
58 // we have an extra flags operand which is not currently printed, for
59 // compatiblity reasons.
60 if (i != 0 && ((MI->getOpcode() != WebAssembly::CALL_INDIRECT_VOID &&
61 MI->getOpcode() != WebAssembly::CALL_INDIRECT_VOID_S) ||
62 i != Desc.getNumOperands()))
64 printOperand(MI, i, OS);
67 // Print any added annotation.
68 printAnnotation(OS, Annot);
71 // Observe any effects on the control flow stack, for use in annotating
72 // control flow label references.
73 unsigned Opc = MI->getOpcode();
78 case WebAssembly::LOOP:
79 case WebAssembly::LOOP_S:
80 printAnnotation(OS, "label" + utostr(ControlFlowCounter) + ':');
81 ControlFlowStack.push_back(std::make_pair(ControlFlowCounter++, true));
84 case WebAssembly::BLOCK:
85 case WebAssembly::BLOCK_S:
86 ControlFlowStack.push_back(std::make_pair(ControlFlowCounter++, false));
89 case WebAssembly::TRY:
90 case WebAssembly::TRY_S:
91 ControlFlowStack.push_back(std::make_pair(ControlFlowCounter++, false));
92 EHPadStack.push_back(EHPadStackCounter++);
96 case WebAssembly::END_LOOP:
97 case WebAssembly::END_LOOP_S:
98 if (ControlFlowStack.empty()) {
99 printAnnotation(OS, "End marker mismatch!");
101 ControlFlowStack.pop_back();
105 case WebAssembly::END_BLOCK:
106 case WebAssembly::END_BLOCK_S:
107 if (ControlFlowStack.empty()) {
108 printAnnotation(OS, "End marker mismatch!");
111 OS, "label" + utostr(ControlFlowStack.pop_back_val().first) + ':');
115 case WebAssembly::END_TRY:
116 case WebAssembly::END_TRY_S:
117 if (ControlFlowStack.empty()) {
118 printAnnotation(OS, "End marker mismatch!");
121 OS, "label" + utostr(ControlFlowStack.pop_back_val().first) + ':');
122 LastSeenEHInst = END_TRY;
126 case WebAssembly::CATCH_I32:
127 case WebAssembly::CATCH_I32_S:
128 case WebAssembly::CATCH_I64:
129 case WebAssembly::CATCH_I64_S:
130 case WebAssembly::CATCH_ALL:
131 case WebAssembly::CATCH_ALL_S:
132 // There can be multiple catch instructions for one try instruction, so we
133 // print a label only for the first 'catch' label.
134 if (LastSeenEHInst != CATCH) {
135 if (EHPadStack.empty()) {
136 printAnnotation(OS, "try-catch mismatch!");
139 "catch" + utostr(EHPadStack.pop_back_val()) + ':');
142 LastSeenEHInst = CATCH;
146 // Annotate any control flow label references.
147 unsigned NumFixedOperands = Desc.NumOperands;
148 SmallSet<uint64_t, 8> Printed;
149 for (unsigned i = 0, e = MI->getNumOperands(); i < e; ++i) {
150 // See if this operand denotes a basic block target.
151 if (i < NumFixedOperands) {
152 // A non-variable_ops operand, check its type.
153 if (Desc.OpInfo[i].OperandType != WebAssembly::OPERAND_BASIC_BLOCK)
156 // A variable_ops operand, which currently can be immediates (used in
157 // br_table) which are basic block targets, or for call instructions
158 // when using -wasm-keep-registers (in which case they are registers,
159 // and should not be processed).
160 if (!MI->getOperand(i).isImm())
163 uint64_t Depth = MI->getOperand(i).getImm();
164 if (!Printed.insert(Depth).second)
167 if (Opc == WebAssembly::RETHROW || Opc == WebAssembly::RETHROW_S) {
168 if (Depth > EHPadStack.size()) {
169 printAnnotation(OS, "Invalid depth argument!");
170 } else if (Depth == EHPadStack.size()) {
171 // This can happen when rethrow instruction breaks out of all nests
172 // and throws up to the current function's caller.
173 printAnnotation(OS, utostr(Depth) + ": " + "to caller");
175 uint64_t CatchNo = EHPadStack.rbegin()[Depth];
176 printAnnotation(OS, utostr(Depth) + ": " + "down to catch" +
181 if (Depth >= ControlFlowStack.size()) {
182 printAnnotation(OS, "Invalid depth argument!");
184 const auto &Pair = ControlFlowStack.rbegin()[Depth];
185 printAnnotation(OS, utostr(Depth) + ": " +
186 (Pair.second ? "up" : "down") + " to label" +
194 static std::string toString(const APFloat &FP) {
195 // Print NaNs with custom payloads specially.
196 if (FP.isNaN() && !FP.bitwiseIsEqual(APFloat::getQNaN(FP.getSemantics())) &&
198 APFloat::getQNaN(FP.getSemantics(), /*Negative=*/true))) {
199 APInt AI = FP.bitcastToAPInt();
200 return std::string(AI.isNegative() ? "-" : "") + "nan:0x" +
201 utohexstr(AI.getZExtValue() &
202 (AI.getBitWidth() == 32 ? INT64_C(0x007fffff)
203 : INT64_C(0x000fffffffffffff)),
207 // Use C99's hexadecimal floating-point representation.
208 static const size_t BufBytes = 128;
210 auto Written = FP.convertToHexString(
211 buf, /*hexDigits=*/0, /*upperCase=*/false, APFloat::rmNearestTiesToEven);
213 assert(Written != 0);
214 assert(Written < BufBytes);
218 void WebAssemblyInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
220 const MCOperand &Op = MI->getOperand(OpNo);
222 unsigned WAReg = Op.getReg();
224 printRegName(O, WAReg);
225 else if (OpNo >= MII.get(MI->getOpcode()).getNumDefs())
226 O << "$pop" << WebAssemblyFunctionInfo::getWARegStackId(WAReg);
227 else if (WAReg != WebAssemblyFunctionInfo::UnusedReg)
228 O << "$push" << WebAssemblyFunctionInfo::getWARegStackId(WAReg);
231 // Add a '=' suffix if this is a def.
232 if (OpNo < MII.get(MI->getOpcode()).getNumDefs())
234 } else if (Op.isImm()) {
236 } else if (Op.isFPImm()) {
237 const MCInstrDesc &Desc = MII.get(MI->getOpcode());
238 const MCOperandInfo &Info = Desc.OpInfo[OpNo];
239 if (Info.OperandType == WebAssembly::OPERAND_F32IMM) {
240 // TODO: MC converts all floating point immediate operands to double.
241 // This is fine for numeric values, but may cause NaNs to change bits.
242 O << ::toString(APFloat(float(Op.getFPImm())));
244 assert(Info.OperandType == WebAssembly::OPERAND_F64IMM);
245 O << ::toString(APFloat(Op.getFPImm()));
248 assert(Op.isExpr() && "unknown operand kind in printOperand");
249 Op.getExpr()->print(O, &MAI);
253 void WebAssemblyInstPrinter::printBrList(const MCInst *MI, unsigned OpNo,
256 for (unsigned I = OpNo, E = MI->getNumOperands(); I != E; ++I) {
259 O << MI->getOperand(I).getImm();
264 void WebAssemblyInstPrinter::printWebAssemblyP2AlignOperand(const MCInst *MI,
267 int64_t Imm = MI->getOperand(OpNo).getImm();
268 if (Imm == WebAssembly::GetDefaultP2Align(MI->getOpcode()))
270 O << ":p2align=" << Imm;
273 void WebAssemblyInstPrinter::printWebAssemblySignatureOperand(const MCInst *MI,
276 auto Imm = static_cast<unsigned>(MI->getOperand(OpNo).getImm());
277 if (Imm != wasm::WASM_TYPE_NORESULT)
278 O << WebAssembly::anyTypeToString(Imm);
281 // We have various enums representing a subset of these types, use this
282 // function to convert any of them to text.
283 const char *llvm::WebAssembly::anyTypeToString(unsigned Ty) {
285 case wasm::WASM_TYPE_I32:
287 case wasm::WASM_TYPE_I64:
289 case wasm::WASM_TYPE_F32:
291 case wasm::WASM_TYPE_F64:
293 case wasm::WASM_TYPE_V128:
295 case wasm::WASM_TYPE_FUNCREF:
297 case wasm::WASM_TYPE_FUNC:
299 case wasm::WASM_TYPE_EXCEPT_REF:
301 case wasm::WASM_TYPE_NORESULT:
304 return "invalid_type";
308 const char *llvm::WebAssembly::typeToString(wasm::ValType Ty) {
309 return anyTypeToString(static_cast<unsigned>(Ty));