1 //==- WebAssemblyAsmTypeCheck.h - Assembler for WebAssembly -*- 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 file is part of the WebAssembly Assembler.
11 ///
12 /// It contains code to translate a parsed .s file into MCInsts.
13 ///
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_LIB_TARGET_WEBASSEMBLY_ASMPARSER_TYPECHECK_H
17 #define LLVM_LIB_TARGET_WEBASSEMBLY_ASMPARSER_TYPECHECK_H
18 
19 #include "llvm/BinaryFormat/Wasm.h"
20 #include "llvm/MC/MCInstrInfo.h"
21 #include "llvm/MC/MCParser/MCAsmParser.h"
22 #include "llvm/MC/MCParser/MCTargetAsmParser.h"
23 #include "llvm/MC/MCSymbol.h"
24 
25 namespace llvm {
26 
27 class WebAssemblyAsmTypeCheck final {
28   MCAsmParser &Parser;
29   const MCInstrInfo &MII;
30 
31   SmallVector<wasm::ValType, 8> Stack;
32   SmallVector<SmallVector<wasm::ValType, 4>, 8> BrStack;
33   SmallVector<wasm::ValType, 16> LocalTypes;
34   SmallVector<wasm::ValType, 4> ReturnTypes;
35   wasm::WasmSignature LastSig;
36   bool TypeErrorThisFunction = false;
37   bool Unreachable = false;
38   bool is64;
39 
40   void dumpTypeStack(Twine Msg);
41   bool typeError(SMLoc ErrorLoc, const Twine &Msg);
42   bool popType(SMLoc ErrorLoc, std::optional<wasm::ValType> EVT);
43   bool popRefType(SMLoc ErrorLoc);
44   bool getLocal(SMLoc ErrorLoc, const MCInst &Inst, wasm::ValType &Type);
45   bool checkEnd(SMLoc ErrorLoc, bool PopVals = false);
46   bool checkBr(SMLoc ErrorLoc, size_t Level);
47   bool checkSig(SMLoc ErrorLoc, const wasm::WasmSignature &Sig);
48   bool getSymRef(SMLoc ErrorLoc, const MCInst &Inst,
49                  const MCSymbolRefExpr *&SymRef);
50   bool getGlobal(SMLoc ErrorLoc, const MCInst &Inst, wasm::ValType &Type);
51   bool getTable(SMLoc ErrorLoc, const MCInst &Inst, wasm::ValType &Type);
52 
53 public:
54   WebAssemblyAsmTypeCheck(MCAsmParser &Parser, const MCInstrInfo &MII,
55                           bool is64);
56 
57   void funcDecl(const wasm::WasmSignature &Sig);
58   void localDecl(const SmallVectorImpl<wasm::ValType> &Locals);
setLastSig(const wasm::WasmSignature & Sig)59   void setLastSig(const wasm::WasmSignature &Sig) { LastSig = Sig; }
60   bool endOfFunction(SMLoc ErrorLoc);
61   bool typeCheck(SMLoc ErrorLoc, const MCInst &Inst, OperandVector &Operands);
62 
Clear()63   void Clear() {
64     Stack.clear();
65     BrStack.clear();
66     LocalTypes.clear();
67     ReturnTypes.clear();
68     TypeErrorThisFunction = false;
69     Unreachable = false;
70   }
71 };
72 
73 } // end namespace llvm
74 
75 #endif // LLVM_LIB_TARGET_WEBASSEMBLY_ASMPARSER_TYPECHECK_H
76