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<wasm::ValType, 16> LocalTypes;
33   SmallVector<wasm::ValType, 4> ReturnTypes;
34   wasm::WasmSignature LastSig;
35   bool TypeErrorThisFunction = false;
36   bool Unreachable = false;
37   bool is64;
38 
39   void dumpTypeStack(Twine Msg);
40   bool typeError(SMLoc ErrorLoc, const Twine &Msg);
41   bool popType(SMLoc ErrorLoc, Optional<wasm::ValType> EVT);
42   bool popRefType(SMLoc ErrorLoc);
43   bool getLocal(SMLoc ErrorLoc, const MCInst &Inst, wasm::ValType &Type);
44   bool checkEnd(SMLoc ErrorLoc, bool PopVals = false);
45   bool checkSig(SMLoc ErrorLoc, const wasm::WasmSignature &Sig);
46   bool getSymRef(SMLoc ErrorLoc, const MCInst &Inst,
47                  const MCSymbolRefExpr *&SymRef);
48   bool getGlobal(SMLoc ErrorLoc, const MCInst &Inst, wasm::ValType &Type);
49   bool getTable(SMLoc ErrorLoc, const MCInst &Inst, wasm::ValType &Type);
50 
51 public:
52   WebAssemblyAsmTypeCheck(MCAsmParser &Parser, const MCInstrInfo &MII, bool is64);
53 
54   void funcDecl(const wasm::WasmSignature &Sig);
55   void localDecl(const SmallVector<wasm::ValType, 4> &Locals);
56   void setLastSig(const wasm::WasmSignature &Sig) { LastSig = Sig; }
57   bool endOfFunction(SMLoc ErrorLoc);
58   bool typeCheck(SMLoc ErrorLoc, const MCInst &Inst, OperandVector &Operands);
59 
60   void Clear() {
61     Stack.clear();
62     LocalTypes.clear();
63     ReturnTypes.clear();
64     TypeErrorThisFunction = false;
65     Unreachable = false;
66   }
67 };
68 
69 } // end namespace llvm
70 
71 #endif  // LLVM_LIB_TARGET_WEBASSEMBLY_ASMPARSER_TYPECHECK_H
72