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/MC/MCParser/MCAsmParser.h"
20 #include "llvm/MC/MCInstrInfo.h"
21 #include "llvm/BinaryFormat/Wasm.h"
22 #include "llvm/MC/MCSymbol.h"
23 
24 namespace llvm {
25 
26 class WebAssemblyAsmTypeCheck final {
27   MCAsmParser &Parser;
28   const MCInstrInfo &MII;
29 
30   SmallVector<wasm::ValType, 8> Stack;
31   SmallVector<wasm::ValType, 16> LocalTypes;
32   SmallVector<wasm::ValType, 4> ReturnTypes;
33   wasm::WasmSignature LastSig;
34   bool TypeErrorThisFunction = false;
35   bool is64;
36 
Clear()37   void Clear() {
38     Stack.clear();
39     LocalTypes.clear();
40     ReturnTypes.clear();
41     TypeErrorThisFunction = false;
42   }
43 
44   void dumpTypeStack(Twine Msg);
45   bool typeError(SMLoc ErrorLoc, const Twine &Msg);
46   bool popType(SMLoc ErrorLoc, Optional<wasm::ValType> EVT);
47   bool getLocal(SMLoc ErrorLoc, const MCInst &Inst, wasm::ValType &Type);
48   bool checkEnd(SMLoc ErrorLoc);
49   bool checkSig(SMLoc ErrorLoc, const wasm::WasmSignature &Sig);
50   bool getSymRef(SMLoc ErrorLoc, const MCInst &Inst,
51                  const MCSymbolRefExpr *&SymRef);
52   bool getGlobal(SMLoc ErrorLoc, const MCInst &Inst, wasm::ValType &Type);
53 
54 public:
55   WebAssemblyAsmTypeCheck(MCAsmParser &Parser, const MCInstrInfo &MII, bool is64);
56 
57   void funcDecl(const wasm::WasmSignature &Sig);
58   void localDecl(const SmallVector<wasm::ValType, 4> &Locals);
setLastSig(const wasm::WasmSignature & Sig)59   void setLastSig(const wasm::WasmSignature &Sig) { LastSig = Sig; }
60   void endOfFunction(SMLoc ErrorLoc);
61   bool typeCheck(SMLoc ErrorLoc, const MCInst &Inst);
62 };
63 
64 } // end namespace llvm
65 
66 #endif  // LLVM_LIB_TARGET_WEBASSEMBLY_ASMPARSER_TYPECHECK_H
67