1a263aa25SDavid L. Jones //=- WebAssemblyInstPrinter.cpp - WebAssembly assembly instruction printing -=//
2a263aa25SDavid L. Jones //
3a263aa25SDavid L. Jones // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4a263aa25SDavid L. Jones // See https://llvm.org/LICENSE.txt for license information.
5a263aa25SDavid L. Jones // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6a263aa25SDavid L. Jones //
7a263aa25SDavid L. Jones //===----------------------------------------------------------------------===//
8a263aa25SDavid L. Jones ///
9a263aa25SDavid L. Jones /// \file
10a263aa25SDavid L. Jones /// Print MCInst instructions to wasm format.
11a263aa25SDavid L. Jones ///
12a263aa25SDavid L. Jones //===----------------------------------------------------------------------===//
13a263aa25SDavid L. Jones 
14a263aa25SDavid L. Jones #include "MCTargetDesc/WebAssemblyInstPrinter.h"
15a263aa25SDavid L. Jones #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
16*0b2bc69bSHeejin Ahn #include "Utils/WebAssemblyTypeUtilities.h"
17*0b2bc69bSHeejin Ahn #include "Utils/WebAssemblyUtilities.h"
18a263aa25SDavid L. Jones #include "WebAssembly.h"
19a263aa25SDavid L. Jones #include "WebAssemblyMachineFunctionInfo.h"
20a263aa25SDavid L. Jones #include "llvm/ADT/SmallSet.h"
21a263aa25SDavid L. Jones #include "llvm/ADT/StringExtras.h"
22a263aa25SDavid L. Jones #include "llvm/CodeGen/TargetRegisterInfo.h"
23a263aa25SDavid L. Jones #include "llvm/MC/MCExpr.h"
24a263aa25SDavid L. Jones #include "llvm/MC/MCInst.h"
25a263aa25SDavid L. Jones #include "llvm/MC/MCInstrInfo.h"
26a263aa25SDavid L. Jones #include "llvm/MC/MCSubtargetInfo.h"
27a263aa25SDavid L. Jones #include "llvm/MC/MCSymbol.h"
28a263aa25SDavid L. Jones #include "llvm/Support/ErrorHandling.h"
29a263aa25SDavid L. Jones #include "llvm/Support/FormattedStream.h"
30a263aa25SDavid L. Jones using namespace llvm;
31a263aa25SDavid L. Jones 
32a263aa25SDavid L. Jones #define DEBUG_TYPE "asm-printer"
33a263aa25SDavid L. Jones 
34a263aa25SDavid L. Jones #include "WebAssemblyGenAsmWriter.inc"
35a263aa25SDavid L. Jones 
WebAssemblyInstPrinter(const MCAsmInfo & MAI,const MCInstrInfo & MII,const MCRegisterInfo & MRI)36a263aa25SDavid L. Jones WebAssemblyInstPrinter::WebAssemblyInstPrinter(const MCAsmInfo &MAI,
37a263aa25SDavid L. Jones                                                const MCInstrInfo &MII,
38a263aa25SDavid L. Jones                                                const MCRegisterInfo &MRI)
39a263aa25SDavid L. Jones     : MCInstPrinter(MAI, MII, MRI) {}
40a263aa25SDavid L. Jones 
printRegName(raw_ostream & OS,unsigned RegNo) const41a263aa25SDavid L. Jones void WebAssemblyInstPrinter::printRegName(raw_ostream &OS,
42a263aa25SDavid L. Jones                                           unsigned RegNo) const {
43a263aa25SDavid L. Jones   assert(RegNo != WebAssemblyFunctionInfo::UnusedReg);
44a263aa25SDavid L. Jones   // Note that there's an implicit local.get/local.set here!
45a263aa25SDavid L. Jones   OS << "$" << RegNo;
46a263aa25SDavid L. Jones }
47a263aa25SDavid L. Jones 
printInst(const MCInst * MI,uint64_t Address,StringRef Annot,const MCSubtargetInfo & STI,raw_ostream & OS)48aa708763SFangrui Song void WebAssemblyInstPrinter::printInst(const MCInst *MI, uint64_t Address,
49a263aa25SDavid L. Jones                                        StringRef Annot,
50aa708763SFangrui Song                                        const MCSubtargetInfo &STI,
51aa708763SFangrui Song                                        raw_ostream &OS) {
524307069dSAndy Wingo   switch (MI->getOpcode()) {
534307069dSAndy Wingo   case WebAssembly::CALL_INDIRECT_S:
544307069dSAndy Wingo   case WebAssembly::RET_CALL_INDIRECT_S: {
554307069dSAndy Wingo     // A special case for call_indirect (and ret_call_indirect), if the table
564307069dSAndy Wingo     // operand is a symbol: the order of the type and table operands is inverted
574307069dSAndy Wingo     // in the text format relative to the binary format.  Otherwise if table the
584307069dSAndy Wingo     // operand isn't a symbol, then we have an MVP compilation unit, and the
594307069dSAndy Wingo     // table shouldn't appear in the output.
604307069dSAndy Wingo     OS << "\t";
614307069dSAndy Wingo     OS << getMnemonic(MI).first;
624307069dSAndy Wingo     OS << " ";
634307069dSAndy Wingo 
644307069dSAndy Wingo     assert(MI->getNumOperands() == 2);
654307069dSAndy Wingo     const unsigned TypeOperand = 0;
664307069dSAndy Wingo     const unsigned TableOperand = 1;
674307069dSAndy Wingo     if (MI->getOperand(TableOperand).isExpr()) {
684307069dSAndy Wingo       printOperand(MI, TableOperand, OS);
694307069dSAndy Wingo       OS << ", ";
704307069dSAndy Wingo     } else {
714307069dSAndy Wingo       assert(MI->getOperand(TableOperand).getImm() == 0);
724307069dSAndy Wingo     }
734307069dSAndy Wingo     printOperand(MI, TypeOperand, OS);
744307069dSAndy Wingo     break;
754307069dSAndy Wingo   }
764307069dSAndy Wingo   default:
77a263aa25SDavid L. Jones     // Print the instruction (this uses the AsmStrings from the .td files).
783d87d0b9SFangrui Song     printInstruction(MI, Address, OS);
794307069dSAndy Wingo     break;
804307069dSAndy Wingo   }
81a263aa25SDavid L. Jones 
82a263aa25SDavid L. Jones   // Print any additional variadic operands.
83a263aa25SDavid L. Jones   const MCInstrDesc &Desc = MII.get(MI->getOpcode());
8400f9e5aaSThomas Lively   if (Desc.isVariadic()) {
85ca9ba764SThomas Lively     if ((Desc.getNumOperands() == 0 && MI->getNumOperands() > 0) ||
86ca9ba764SThomas Lively         Desc.variadicOpsAreDefs())
8700f9e5aaSThomas Lively       OS << "\t";
88ca9ba764SThomas Lively     unsigned Start = Desc.getNumOperands();
89ca9ba764SThomas Lively     unsigned NumVariadicDefs = 0;
90ca9ba764SThomas Lively     if (Desc.variadicOpsAreDefs()) {
91ca9ba764SThomas Lively       // The number of variadic defs is encoded in an immediate by MCInstLower
92ca9ba764SThomas Lively       NumVariadicDefs = MI->getOperand(0).getImm();
93ca9ba764SThomas Lively       Start = 1;
94ca9ba764SThomas Lively     }
95ca9ba764SThomas Lively     bool NeedsComma = Desc.getNumOperands() > 0 && !Desc.variadicOpsAreDefs();
96ca9ba764SThomas Lively     for (auto I = Start, E = MI->getNumOperands(); I < E; ++I) {
97ca9ba764SThomas Lively       if (MI->getOpcode() == WebAssembly::CALL_INDIRECT &&
98ca9ba764SThomas Lively           I - Start == NumVariadicDefs) {
992632ba6aSAndy Wingo         // Skip type and table arguments when printing for tests.
100ca9ba764SThomas Lively         ++I;
101ca9ba764SThomas Lively         continue;
102ca9ba764SThomas Lively       }
103ca9ba764SThomas Lively       if (NeedsComma)
104a263aa25SDavid L. Jones         OS << ", ";
105ca9ba764SThomas Lively       printOperand(MI, I, OS, I - Start < NumVariadicDefs);
106ca9ba764SThomas Lively       NeedsComma = true;
107a263aa25SDavid L. Jones     }
10800f9e5aaSThomas Lively   }
109a263aa25SDavid L. Jones 
110a263aa25SDavid L. Jones   // Print any added annotation.
111a263aa25SDavid L. Jones   printAnnotation(OS, Annot);
112a263aa25SDavid L. Jones 
113a263aa25SDavid L. Jones   if (CommentStream) {
114a263aa25SDavid L. Jones     // Observe any effects on the control flow stack, for use in annotating
115a263aa25SDavid L. Jones     // control flow label references.
116a263aa25SDavid L. Jones     unsigned Opc = MI->getOpcode();
117a263aa25SDavid L. Jones     switch (Opc) {
118a263aa25SDavid L. Jones     default:
119a263aa25SDavid L. Jones       break;
120a263aa25SDavid L. Jones 
121a263aa25SDavid L. Jones     case WebAssembly::LOOP:
122a263aa25SDavid L. Jones     case WebAssembly::LOOP_S:
123a263aa25SDavid L. Jones       printAnnotation(OS, "label" + utostr(ControlFlowCounter) + ':');
1242968611fSHeejin Ahn       ControlFlowStack.push_back(std::make_pair(ControlFlowCounter++, true));
1250d8dfbb4SHeejin Ahn       return;
126a263aa25SDavid L. Jones 
127a263aa25SDavid L. Jones     case WebAssembly::BLOCK:
128a263aa25SDavid L. Jones     case WebAssembly::BLOCK_S:
1292968611fSHeejin Ahn       ControlFlowStack.push_back(std::make_pair(ControlFlowCounter++, false));
1300d8dfbb4SHeejin Ahn       return;
131a263aa25SDavid L. Jones 
132a263aa25SDavid L. Jones     case WebAssembly::TRY:
133a263aa25SDavid L. Jones     case WebAssembly::TRY_S:
1340d8dfbb4SHeejin Ahn       ControlFlowStack.push_back(std::make_pair(ControlFlowCounter, false));
13535f5f797SHeejin Ahn       TryStack.push_back(ControlFlowCounter++);
1365afdd64aSHeejin Ahn       EHInstStack.push_back(TRY);
1370d8dfbb4SHeejin Ahn       return;
138a263aa25SDavid L. Jones 
139a263aa25SDavid L. Jones     case WebAssembly::END_LOOP:
140a263aa25SDavid L. Jones     case WebAssembly::END_LOOP_S:
1412968611fSHeejin Ahn       if (ControlFlowStack.empty()) {
142a263aa25SDavid L. Jones         printAnnotation(OS, "End marker mismatch!");
143a263aa25SDavid L. Jones       } else {
144a263aa25SDavid L. Jones         ControlFlowStack.pop_back();
145a263aa25SDavid L. Jones       }
1460d8dfbb4SHeejin Ahn       return;
147a263aa25SDavid L. Jones 
148a263aa25SDavid L. Jones     case WebAssembly::END_BLOCK:
149a263aa25SDavid L. Jones     case WebAssembly::END_BLOCK_S:
1502968611fSHeejin Ahn       if (ControlFlowStack.empty()) {
151a263aa25SDavid L. Jones         printAnnotation(OS, "End marker mismatch!");
152a263aa25SDavid L. Jones       } else {
153a263aa25SDavid L. Jones         printAnnotation(
154a263aa25SDavid L. Jones             OS, "label" + utostr(ControlFlowStack.pop_back_val().first) + ':');
155a263aa25SDavid L. Jones       }
1560d8dfbb4SHeejin Ahn       return;
157a263aa25SDavid L. Jones 
158a263aa25SDavid L. Jones     case WebAssembly::END_TRY:
159a263aa25SDavid L. Jones     case WebAssembly::END_TRY_S:
1605afdd64aSHeejin Ahn       if (ControlFlowStack.empty() || EHInstStack.empty()) {
161a263aa25SDavid L. Jones         printAnnotation(OS, "End marker mismatch!");
162a263aa25SDavid L. Jones       } else {
163a263aa25SDavid L. Jones         printAnnotation(
164a263aa25SDavid L. Jones             OS, "label" + utostr(ControlFlowStack.pop_back_val().first) + ':');
1655afdd64aSHeejin Ahn         EHInstStack.pop_back();
166a263aa25SDavid L. Jones       }
1670d8dfbb4SHeejin Ahn       return;
168a263aa25SDavid L. Jones 
169a263aa25SDavid L. Jones     case WebAssembly::CATCH:
170a263aa25SDavid L. Jones     case WebAssembly::CATCH_S:
1710d8dfbb4SHeejin Ahn     case WebAssembly::CATCH_ALL:
1720d8dfbb4SHeejin Ahn     case WebAssembly::CATCH_ALL_S:
1735afdd64aSHeejin Ahn       // There can be multiple catch instructions for one try instruction, so
1745afdd64aSHeejin Ahn       // we print a label only for the first 'catch' label.
1755afdd64aSHeejin Ahn       if (EHInstStack.empty()) {
1765afdd64aSHeejin Ahn         printAnnotation(OS, "try-catch mismatch!");
1775afdd64aSHeejin Ahn       } else if (EHInstStack.back() == CATCH_ALL) {
1785afdd64aSHeejin Ahn         printAnnotation(OS, "catch/catch_all cannot occur after catch_all");
1795afdd64aSHeejin Ahn       } else if (EHInstStack.back() == TRY) {
18035f5f797SHeejin Ahn         if (TryStack.empty()) {
181a263aa25SDavid L. Jones           printAnnotation(OS, "try-catch mismatch!");
182a263aa25SDavid L. Jones         } else {
18335f5f797SHeejin Ahn           printAnnotation(OS, "catch" + utostr(TryStack.pop_back_val()) + ':');
184a263aa25SDavid L. Jones         }
1855afdd64aSHeejin Ahn         EHInstStack.pop_back();
1865afdd64aSHeejin Ahn         if (Opc == WebAssembly::CATCH || Opc == WebAssembly::CATCH_S) {
1875afdd64aSHeejin Ahn           EHInstStack.push_back(CATCH);
1885afdd64aSHeejin Ahn         } else {
1895afdd64aSHeejin Ahn           EHInstStack.push_back(CATCH_ALL);
1905afdd64aSHeejin Ahn         }
1915afdd64aSHeejin Ahn       }
1920d8dfbb4SHeejin Ahn       return;
193a263aa25SDavid L. Jones 
1940d8dfbb4SHeejin Ahn     case WebAssembly::RETHROW:
1950d8dfbb4SHeejin Ahn     case WebAssembly::RETHROW_S:
1960d8dfbb4SHeejin Ahn       // 'rethrow' rethrows to the nearest enclosing catch scope, if any. If
1970d8dfbb4SHeejin Ahn       // there's no enclosing catch scope, it throws up to the caller.
19835f5f797SHeejin Ahn       if (TryStack.empty()) {
199a263aa25SDavid L. Jones         printAnnotation(OS, "to caller");
200a263aa25SDavid L. Jones       } else {
20135f5f797SHeejin Ahn         printAnnotation(OS, "down to catch" + utostr(TryStack.back()));
202a263aa25SDavid L. Jones       }
2030d8dfbb4SHeejin Ahn       return;
204ed41945fSHeejin Ahn 
205ed41945fSHeejin Ahn     case WebAssembly::DELEGATE:
206ed41945fSHeejin Ahn     case WebAssembly::DELEGATE_S:
20735f5f797SHeejin Ahn       if (ControlFlowStack.empty() || TryStack.empty() || EHInstStack.empty()) {
208ed41945fSHeejin Ahn         printAnnotation(OS, "try-delegate mismatch!");
209ed41945fSHeejin Ahn       } else {
210ed41945fSHeejin Ahn         // 'delegate' is
211ed41945fSHeejin Ahn         // 1. A marker for the end of block label
212ed41945fSHeejin Ahn         // 2. A destination for throwing instructions
213ed41945fSHeejin Ahn         // 3. An instruction that itself rethrows to another 'catch'
21435f5f797SHeejin Ahn         assert(ControlFlowStack.back().first == TryStack.back());
215ed41945fSHeejin Ahn         std::string Label = "label/catch" +
216ed41945fSHeejin Ahn                             utostr(ControlFlowStack.pop_back_val().first) +
217ed41945fSHeejin Ahn                             ": ";
21835f5f797SHeejin Ahn         TryStack.pop_back();
2195afdd64aSHeejin Ahn         EHInstStack.pop_back();
220ed41945fSHeejin Ahn         uint64_t Depth = MI->getOperand(0).getImm();
2212968611fSHeejin Ahn         if (Depth >= ControlFlowStack.size()) {
222ed41945fSHeejin Ahn           Label += "to caller";
223ed41945fSHeejin Ahn         } else {
2242968611fSHeejin Ahn           const auto &Pair = ControlFlowStack.rbegin()[Depth];
2252968611fSHeejin Ahn           if (Pair.second)
2262968611fSHeejin Ahn             printAnnotation(OS, "delegate cannot target a loop");
2272968611fSHeejin Ahn           else
2282968611fSHeejin Ahn             Label += "down to catch" + utostr(Pair.first);
229ed41945fSHeejin Ahn         }
230ed41945fSHeejin Ahn         printAnnotation(OS, Label);
231ed41945fSHeejin Ahn       }
232ed41945fSHeejin Ahn       return;
2330d8dfbb4SHeejin Ahn     }
234a263aa25SDavid L. Jones 
2350d8dfbb4SHeejin Ahn     // Annotate any control flow label references.
2360d8dfbb4SHeejin Ahn 
237a263aa25SDavid L. Jones     unsigned NumFixedOperands = Desc.NumOperands;
238a263aa25SDavid L. Jones     SmallSet<uint64_t, 8> Printed;
239a263aa25SDavid L. Jones     for (unsigned I = 0, E = MI->getNumOperands(); I < E; ++I) {
240a263aa25SDavid L. Jones       // See if this operand denotes a basic block target.
241a263aa25SDavid L. Jones       if (I < NumFixedOperands) {
242a263aa25SDavid L. Jones         // A non-variable_ops operand, check its type.
243a263aa25SDavid L. Jones         if (Desc.OpInfo[I].OperandType != WebAssembly::OPERAND_BASIC_BLOCK)
244a263aa25SDavid L. Jones           continue;
245a263aa25SDavid L. Jones       } else {
246a263aa25SDavid L. Jones         // A variable_ops operand, which currently can be immediates (used in
247a263aa25SDavid L. Jones         // br_table) which are basic block targets, or for call instructions
248a263aa25SDavid L. Jones         // when using -wasm-keep-registers (in which case they are registers,
249a263aa25SDavid L. Jones         // and should not be processed).
250a263aa25SDavid L. Jones         if (!MI->getOperand(I).isImm())
251a263aa25SDavid L. Jones           continue;
252a263aa25SDavid L. Jones       }
253a263aa25SDavid L. Jones       uint64_t Depth = MI->getOperand(I).getImm();
254a263aa25SDavid L. Jones       if (!Printed.insert(Depth).second)
255a263aa25SDavid L. Jones         continue;
256a263aa25SDavid L. Jones       if (Depth >= ControlFlowStack.size()) {
257a263aa25SDavid L. Jones         printAnnotation(OS, "Invalid depth argument!");
258a263aa25SDavid L. Jones       } else {
259a263aa25SDavid L. Jones         const auto &Pair = ControlFlowStack.rbegin()[Depth];
260a263aa25SDavid L. Jones         printAnnotation(OS, utostr(Depth) + ": " +
261a263aa25SDavid L. Jones                                 (Pair.second ? "up" : "down") + " to label" +
262a263aa25SDavid L. Jones                                 utostr(Pair.first));
263a263aa25SDavid L. Jones       }
264a263aa25SDavid L. Jones     }
265a263aa25SDavid L. Jones   }
266a263aa25SDavid L. Jones }
267a263aa25SDavid L. Jones 
toString(const APFloat & FP)268a263aa25SDavid L. Jones static std::string toString(const APFloat &FP) {
269a263aa25SDavid L. Jones   // Print NaNs with custom payloads specially.
270a263aa25SDavid L. Jones   if (FP.isNaN() && !FP.bitwiseIsEqual(APFloat::getQNaN(FP.getSemantics())) &&
271a263aa25SDavid L. Jones       !FP.bitwiseIsEqual(
272a263aa25SDavid L. Jones           APFloat::getQNaN(FP.getSemantics(), /*Negative=*/true))) {
273a263aa25SDavid L. Jones     APInt AI = FP.bitcastToAPInt();
274a263aa25SDavid L. Jones     return std::string(AI.isNegative() ? "-" : "") + "nan:0x" +
275a263aa25SDavid L. Jones            utohexstr(AI.getZExtValue() &
276a263aa25SDavid L. Jones                          (AI.getBitWidth() == 32 ? INT64_C(0x007fffff)
277a263aa25SDavid L. Jones                                                  : INT64_C(0x000fffffffffffff)),
278a263aa25SDavid L. Jones                      /*LowerCase=*/true);
279a263aa25SDavid L. Jones   }
280a263aa25SDavid L. Jones 
281a263aa25SDavid L. Jones   // Use C99's hexadecimal floating-point representation.
282a263aa25SDavid L. Jones   static const size_t BufBytes = 128;
283a263aa25SDavid L. Jones   char Buf[BufBytes];
284a263aa25SDavid L. Jones   auto Written = FP.convertToHexString(
28549a3ad21SRui Ueyama       Buf, /*HexDigits=*/0, /*UpperCase=*/false, APFloat::rmNearestTiesToEven);
286a263aa25SDavid L. Jones   (void)Written;
287a263aa25SDavid L. Jones   assert(Written != 0);
288a263aa25SDavid L. Jones   assert(Written < BufBytes);
289a263aa25SDavid L. Jones   return Buf;
290a263aa25SDavid L. Jones }
291a263aa25SDavid L. Jones 
printOperand(const MCInst * MI,unsigned OpNo,raw_ostream & O,bool IsVariadicDef)292a263aa25SDavid L. Jones void WebAssemblyInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
293ca9ba764SThomas Lively                                           raw_ostream &O, bool IsVariadicDef) {
294a263aa25SDavid L. Jones   const MCOperand &Op = MI->getOperand(OpNo);
295a263aa25SDavid L. Jones   if (Op.isReg()) {
296ca9ba764SThomas Lively     const MCInstrDesc &Desc = MII.get(MI->getOpcode());
297a263aa25SDavid L. Jones     unsigned WAReg = Op.getReg();
298a263aa25SDavid L. Jones     if (int(WAReg) >= 0)
299a263aa25SDavid L. Jones       printRegName(O, WAReg);
300ca9ba764SThomas Lively     else if (OpNo >= Desc.getNumDefs() && !IsVariadicDef)
301a263aa25SDavid L. Jones       O << "$pop" << WebAssemblyFunctionInfo::getWARegStackId(WAReg);
302a263aa25SDavid L. Jones     else if (WAReg != WebAssemblyFunctionInfo::UnusedReg)
303a263aa25SDavid L. Jones       O << "$push" << WebAssemblyFunctionInfo::getWARegStackId(WAReg);
304a263aa25SDavid L. Jones     else
305a263aa25SDavid L. Jones       O << "$drop";
306a263aa25SDavid L. Jones     // Add a '=' suffix if this is a def.
307ca9ba764SThomas Lively     if (OpNo < MII.get(MI->getOpcode()).getNumDefs() || IsVariadicDef)
308a263aa25SDavid L. Jones       O << '=';
309a263aa25SDavid L. Jones   } else if (Op.isImm()) {
310a263aa25SDavid L. Jones     O << Op.getImm();
311698c6b0aSDan Gohman   } else if (Op.isSFPImm()) {
312c62dabc3SDan Gohman     O << ::toString(APFloat(APFloat::IEEEsingle(), APInt(32, Op.getSFPImm())));
313698c6b0aSDan Gohman   } else if (Op.isDFPImm()) {
314c62dabc3SDan Gohman     O << ::toString(APFloat(APFloat::IEEEdouble(), APInt(64, Op.getDFPImm())));
315a263aa25SDavid L. Jones   } else {
316a263aa25SDavid L. Jones     assert(Op.isExpr() && "unknown operand kind in printOperand");
31787af0b19SWouter van Oortmerssen     // call_indirect instructions have a TYPEINDEX operand that we print
31887af0b19SWouter van Oortmerssen     // as a signature here, such that the assembler can recover this
31987af0b19SWouter van Oortmerssen     // information.
32087af0b19SWouter van Oortmerssen     auto SRE = static_cast<const MCSymbolRefExpr *>(Op.getExpr());
32187af0b19SWouter van Oortmerssen     if (SRE->getKind() == MCSymbolRefExpr::VK_WASM_TYPEINDEX) {
32287af0b19SWouter van Oortmerssen       auto &Sym = static_cast<const MCSymbolWasm &>(SRE->getSymbol());
32387af0b19SWouter van Oortmerssen       O << WebAssembly::signatureToString(Sym.getSignature());
32487af0b19SWouter van Oortmerssen     } else {
325a263aa25SDavid L. Jones       Op.getExpr()->print(O, &MAI);
326a263aa25SDavid L. Jones     }
327a263aa25SDavid L. Jones   }
32887af0b19SWouter van Oortmerssen }
329a263aa25SDavid L. Jones 
printBrList(const MCInst * MI,unsigned OpNo,raw_ostream & O)330a263aa25SDavid L. Jones void WebAssemblyInstPrinter::printBrList(const MCInst *MI, unsigned OpNo,
331a263aa25SDavid L. Jones                                          raw_ostream &O) {
332a263aa25SDavid L. Jones   O << "{";
333a263aa25SDavid L. Jones   for (unsigned I = OpNo, E = MI->getNumOperands(); I != E; ++I) {
334a263aa25SDavid L. Jones     if (I != OpNo)
335a263aa25SDavid L. Jones       O << ", ";
336a263aa25SDavid L. Jones     O << MI->getOperand(I).getImm();
337a263aa25SDavid L. Jones   }
338a263aa25SDavid L. Jones   O << "}";
339a263aa25SDavid L. Jones }
340a263aa25SDavid L. Jones 
printWebAssemblyP2AlignOperand(const MCInst * MI,unsigned OpNo,raw_ostream & O)341a263aa25SDavid L. Jones void WebAssemblyInstPrinter::printWebAssemblyP2AlignOperand(const MCInst *MI,
342a263aa25SDavid L. Jones                                                             unsigned OpNo,
343a263aa25SDavid L. Jones                                                             raw_ostream &O) {
344a263aa25SDavid L. Jones   int64_t Imm = MI->getOperand(OpNo).getImm();
345a263aa25SDavid L. Jones   if (Imm == WebAssembly::GetDefaultP2Align(MI->getOpcode()))
346a263aa25SDavid L. Jones     return;
347a263aa25SDavid L. Jones   O << ":p2align=" << Imm;
348a263aa25SDavid L. Jones }
349a263aa25SDavid L. Jones 
printWebAssemblySignatureOperand(const MCInst * MI,unsigned OpNo,raw_ostream & O)350a263aa25SDavid L. Jones void WebAssemblyInstPrinter::printWebAssemblySignatureOperand(const MCInst *MI,
351a263aa25SDavid L. Jones                                                               unsigned OpNo,
352a263aa25SDavid L. Jones                                                               raw_ostream &O) {
3532cb27072SThomas Lively   const MCOperand &Op = MI->getOperand(OpNo);
3542cb27072SThomas Lively   if (Op.isImm()) {
3552cb27072SThomas Lively     auto Imm = static_cast<unsigned>(Op.getImm());
356a263aa25SDavid L. Jones     if (Imm != wasm::WASM_TYPE_NORESULT)
357a263aa25SDavid L. Jones       O << WebAssembly::anyTypeToString(Imm);
3582cb27072SThomas Lively   } else {
3592cb27072SThomas Lively     auto Expr = cast<MCSymbolRefExpr>(Op.getExpr());
3602cb27072SThomas Lively     auto *Sym = cast<MCSymbolWasm>(&Expr->getSymbol());
3612cb27072SThomas Lively     if (Sym->getSignature()) {
3622cb27072SThomas Lively       O << WebAssembly::signatureToString(Sym->getSignature());
3632cb27072SThomas Lively     } else {
3642cb27072SThomas Lively       // Disassembler does not currently produce a signature
3652cb27072SThomas Lively       O << "unknown_type";
3662cb27072SThomas Lively     }
3672cb27072SThomas Lively   }
368a263aa25SDavid L. Jones }
369