1 // WebAssemblyInstPrinter.h - Print wasm MCInst to assembly syntax -*- C++ -*-//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// This class prints an WebAssembly MCInst to wasm file syntax.
11 ///
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_LIB_TARGET_WEBASSEMBLY_INSTPRINTER_WEBASSEMBLYINSTPRINTER_H
15 #define LLVM_LIB_TARGET_WEBASSEMBLY_INSTPRINTER_WEBASSEMBLYINSTPRINTER_H
16 
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/BinaryFormat/Wasm.h"
19 #include "llvm/MC/MCInstPrinter.h"
20 #include "llvm/Support/MachineValueType.h"
21 
22 namespace llvm {
23 
24 class MCSubtargetInfo;
25 
26 class WebAssemblyInstPrinter final : public MCInstPrinter {
27   uint64_t ControlFlowCounter = 0;
28   SmallVector<std::pair<uint64_t, bool>, 4> ControlFlowStack;
29   SmallVector<uint64_t, 4> EHPadStack;
30   // 'delegate' can target any block-like structure, but in case the target is
31   // 'try', it rethrows to the corresponding 'catch'. Because it can target all
32   // blocks but with a slightly different semantics with branches, we need a
33   // separate stack for 'delegate'.
34   SmallVector<uint64_t, 4> DelegateStack;
35 
36   enum EHInstKind { TRY, CATCH, CATCH_ALL };
37   SmallVector<EHInstKind, 4> EHInstStack;
38 
39 public:
40   WebAssemblyInstPrinter(const MCAsmInfo &MAI, const MCInstrInfo &MII,
41                          const MCRegisterInfo &MRI);
42 
43   void printRegName(raw_ostream &OS, unsigned RegNo) const override;
44   void printInst(const MCInst *MI, uint64_t Address, StringRef Annot,
45                  const MCSubtargetInfo &STI, raw_ostream &OS) override;
46 
47   // Used by tblegen code.
48   void printOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O,
49                     bool IsVariadicDef = false);
50   void printBrList(const MCInst *MI, unsigned OpNo, raw_ostream &O);
51   void printWebAssemblyP2AlignOperand(const MCInst *MI, unsigned OpNo,
52                                       raw_ostream &O);
53   void printWebAssemblySignatureOperand(const MCInst *MI, unsigned OpNo,
54                                         raw_ostream &O);
55   void printWebAssemblyHeapTypeOperand(const MCInst *MI, unsigned OpNo,
56                                        raw_ostream &O);
57 
58   // Autogenerated by tblgen.
59   std::pair<const char *, uint64_t> getMnemonic(const MCInst *MI) override;
60   void printInstruction(const MCInst *MI, uint64_t Address, raw_ostream &O);
61   static const char *getRegisterName(unsigned RegNo);
62 };
63 
64 namespace WebAssembly {
65 
66 const char *typeToString(wasm::ValType Ty);
67 const char *anyTypeToString(unsigned Ty);
68 
69 std::string typeListToString(ArrayRef<wasm::ValType> List);
70 std::string signatureToString(const wasm::WasmSignature *Sig);
71 
72 } // end namespace WebAssembly
73 
74 } // end namespace llvm
75 
76 #endif
77