LLVM  14.0.0git
WebAssemblyAsmTypeCheck.cpp
Go to the documentation of this file.
1 //==- WebAssemblyAsmTypeCheck.cpp - 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 
22 #include "WebAssembly.h"
23 #include "llvm/MC/MCContext.h"
24 #include "llvm/MC/MCExpr.h"
25 #include "llvm/MC/MCInst.h"
26 #include "llvm/MC/MCInstrInfo.h"
29 #include "llvm/MC/MCSectionWasm.h"
30 #include "llvm/MC/MCStreamer.h"
32 #include "llvm/MC/MCSymbol.h"
33 #include "llvm/MC/MCSymbolWasm.h"
34 #include "llvm/Support/Compiler.h"
35 #include "llvm/Support/Endian.h"
36 #include "llvm/Support/SourceMgr.h"
38 
39 using namespace llvm;
40 
41 #define DEBUG_TYPE "wasm-asm-parser"
42 
43 extern StringRef GetMnemonic(unsigned Opc);
44 
45 namespace llvm {
46 
48  const MCInstrInfo &MII, bool is64)
49  : Parser(Parser), MII(MII), is64(is64) {
50 }
51 
53  LocalTypes.assign(Sig.Params.begin(), Sig.Params.end());
54  ReturnTypes.assign(Sig.Returns.begin(), Sig.Returns.end());
55 }
56 
58  LocalTypes.insert(LocalTypes.end(), Locals.begin(), Locals.end());
59 }
60 
61 void WebAssemblyAsmTypeCheck::dumpTypeStack(Twine Msg) {
62  LLVM_DEBUG({
63  std::string s;
64  for (auto VT : Stack) {
66  s += " ";
67  }
68  dbgs() << Msg << s << '\n';
69  });
70 }
71 
72 bool WebAssemblyAsmTypeCheck::typeError(SMLoc ErrorLoc, const Twine &Msg) {
73  // Once you get one type error in a function, it will likely trigger more
74  // which are mostly not helpful.
75  if (TypeErrorThisFunction)
76  return true;
77  TypeErrorThisFunction = true;
78  dumpTypeStack("current stack: ");
79  return Parser.Error(ErrorLoc, Msg);
80 }
81 
82 bool WebAssemblyAsmTypeCheck::popType(SMLoc ErrorLoc,
84  if (Stack.empty()) {
85  return typeError(ErrorLoc,
86  EVT.hasValue()
87  ? StringRef("empty stack while popping ") +
88  WebAssembly::typeToString(EVT.getValue())
89  : StringRef(
90  "empty stack while popping value"));
91  }
92  auto PVT = Stack.pop_back_val();
93  if (EVT.hasValue() && EVT.getValue() != PVT) {
94  return typeError(
95  ErrorLoc, StringRef("popped ") + WebAssembly::typeToString(PVT) +
96  ", expected " +
97  WebAssembly::typeToString(EVT.getValue()));
98  }
99  return false;
100 }
101 
102 bool WebAssemblyAsmTypeCheck::getLocal(SMLoc ErrorLoc, const MCInst &Inst,
103  wasm::ValType &Type) {
104  auto Local = static_cast<size_t>(Inst.getOperand(0).getImm());
105  if (Local >= LocalTypes.size())
106  return typeError(ErrorLoc, StringRef("no local type specified for index ") +
107  std::to_string(Local));
108  Type = LocalTypes[Local];
109  return false;
110 }
111 
112 bool WebAssemblyAsmTypeCheck::checkEnd(SMLoc ErrorLoc) {
113  if (LastSig.Returns.size() > Stack.size())
114  return typeError(ErrorLoc, "end: insufficient values on the type stack");
115  for (size_t i = 0; i < LastSig.Returns.size(); i++) {
116  auto EVT = LastSig.Returns[i];
117  auto PVT = Stack[Stack.size() - LastSig.Returns.size() + i];
118  if (PVT != EVT)
119  return typeError(
120  ErrorLoc, StringRef("end got ") + WebAssembly::typeToString(PVT) +
121  ", expected " + WebAssembly::typeToString(EVT));
122  }
123  return false;
124 }
125 
126 bool WebAssemblyAsmTypeCheck::checkSig(SMLoc ErrorLoc,
127  const wasm::WasmSignature& Sig) {
128  for (auto VT : llvm::reverse(Sig.Params))
129  if (popType(ErrorLoc, VT)) return true;
130  Stack.insert(Stack.end(), Sig.Returns.begin(), Sig.Returns.end());
131  return false;
132 }
133 
134 bool WebAssemblyAsmTypeCheck::getSymRef(SMLoc ErrorLoc, const MCInst &Inst,
135  const MCSymbolRefExpr *&SymRef) {
136  auto Op = Inst.getOperand(0);
137  if (!Op.isExpr())
138  return typeError(ErrorLoc, StringRef("expected expression operand"));
139  SymRef = dyn_cast<MCSymbolRefExpr>(Op.getExpr());
140  if (!SymRef)
141  return typeError(ErrorLoc, StringRef("expected symbol operand"));
142  return false;
143 }
144 
145 bool WebAssemblyAsmTypeCheck::getGlobal(SMLoc ErrorLoc, const MCInst &Inst,
146  wasm::ValType &Type) {
147  const MCSymbolRefExpr *SymRef;
148  if (getSymRef(ErrorLoc, Inst, SymRef))
149  return true;
150  auto WasmSym = cast<MCSymbolWasm>(&SymRef->getSymbol());
151  switch (WasmSym->getType().getValueOr(wasm::WASM_SYMBOL_TYPE_DATA)) {
153  Type = static_cast<wasm::ValType>(WasmSym->getGlobalType().Type);
154  break;
157  switch (SymRef->getKind()) {
161  return false;
162  default:
163  break;
164  }
166  default:
167  return typeError(ErrorLoc, StringRef("symbol ") + WasmSym->getName() +
168  " missing .globaltype");
169  }
170  return false;
171 }
172 
174  // Check the return types.
175  for (auto RVT : llvm::reverse(ReturnTypes)) {
176  popType(ErrorLoc, RVT);
177  }
178  if (!Stack.empty()) {
179  typeError(ErrorLoc,
180  std::to_string(Stack.size()) + " superfluous return values");
181  }
182  // Reset the type checker state.
183  Clear();
184 }
185 
186 bool WebAssemblyAsmTypeCheck::typeCheck(SMLoc ErrorLoc, const MCInst &Inst) {
187  auto Opc = Inst.getOpcode();
188  auto Name = GetMnemonic(Opc);
189  dumpTypeStack("typechecking " + Name + ": ");
191  if (Name == "local.get") {
192  if (getLocal(ErrorLoc, Inst, Type))
193  return true;
194  Stack.push_back(Type);
195  } else if (Name == "local.set") {
196  if (getLocal(ErrorLoc, Inst, Type))
197  return true;
198  if (popType(ErrorLoc, Type))
199  return true;
200  } else if (Name == "local.tee") {
201  if (getLocal(ErrorLoc, Inst, Type))
202  return true;
203  if (popType(ErrorLoc, Type))
204  return true;
205  Stack.push_back(Type);
206  } else if (Name == "global.get") {
207  if (getGlobal(ErrorLoc, Inst, Type))
208  return true;
209  Stack.push_back(Type);
210  } else if (Name == "global.set") {
211  if (getGlobal(ErrorLoc, Inst, Type))
212  return true;
213  if (popType(ErrorLoc, Type))
214  return true;
215  } else if (Name == "drop") {
216  if (popType(ErrorLoc, {}))
217  return true;
218  } else if (Name == "end_block" || Name == "end_loop" || Name == "end_if" ||
219  Name == "else" || Name == "end_try") {
220  if (checkEnd(ErrorLoc))
221  return true;
222  } else if (Name == "call_indirect" || Name == "return_call_indirect") {
223  // Function value.
224  if (popType(ErrorLoc, wasm::ValType::I32)) return true;
225  if (checkSig(ErrorLoc, LastSig)) return true;
226  } else if (Name == "call" || Name == "return_call") {
227  const MCSymbolRefExpr *SymRef;
228  if (getSymRef(ErrorLoc, Inst, SymRef))
229  return true;
230  auto WasmSym = cast<MCSymbolWasm>(&SymRef->getSymbol());
231  auto Sig = WasmSym->getSignature();
232  if (!Sig || WasmSym->getType() != wasm::WASM_SYMBOL_TYPE_FUNCTION)
233  return typeError(ErrorLoc, StringRef("symbol ") + WasmSym->getName() +
234  " missing .functype");
235  if (checkSig(ErrorLoc, *Sig)) return true;
236  } else if (Name == "catch") {
237  const MCSymbolRefExpr *SymRef;
238  if (getSymRef(ErrorLoc, Inst, SymRef))
239  return true;
240  const auto *WasmSym = cast<MCSymbolWasm>(&SymRef->getSymbol());
241  const auto *Sig = WasmSym->getSignature();
242  if (!Sig || WasmSym->getType() != wasm::WASM_SYMBOL_TYPE_TAG)
243  return typeError(ErrorLoc, StringRef("symbol ") + WasmSym->getName() +
244  " missing .tagtype");
245  // catch instruction pushes values whose types are specified in the tag's
246  // "params" part
247  Stack.insert(Stack.end(), Sig->Params.begin(), Sig->Params.end());
248  } else if (Name == "ref.null") {
249  auto VT = static_cast<wasm::ValType>(Inst.getOperand(0).getImm());
250  Stack.push_back(VT);
251  } else {
252  // The current instruction is a stack instruction which doesn't have
253  // explicit operands that indicate push/pop types, so we get those from
254  // the register version of the same instruction.
255  auto RegOpc = WebAssembly::getRegisterOpcode(Opc);
256  assert(RegOpc != -1 && "Failed to get register version of MC instruction");
257  const auto &II = MII.get(RegOpc);
258  // First pop all the uses off the stack and check them.
259  for (unsigned I = II.getNumOperands(); I > II.getNumDefs(); I--) {
260  const auto &Op = II.OpInfo[I - 1];
261  if (Op.OperandType == MCOI::OPERAND_REGISTER) {
262  auto VT = WebAssembly::regClassToValType(Op.RegClass);
263  if (popType(ErrorLoc, VT))
264  return true;
265  }
266  }
267  // Now push all the defs onto the stack.
268  for (unsigned I = 0; I < II.getNumDefs(); I++) {
269  const auto &Op = II.OpInfo[I];
270  assert(Op.OperandType == MCOI::OPERAND_REGISTER && "Register expected");
271  auto VT = WebAssembly::regClassToValType(Op.RegClass);
272  Stack.push_back(VT);
273  }
274  }
275  return false;
276 }
277 
278 } // end namespace llvm
llvm::MCSymbolRefExpr::getKind
VariantKind getKind() const
Definition: MCExpr.h:400
i
i
Definition: README.txt:29
llvm::MCAsmParser
Generic assembler parser interface, for use by target specific assembly parsers.
Definition: MCAsmParser.h:124
llvm::MCAsmParser::Error
bool Error(SMLoc L, const Twine &Msg, SMRange Range=None)
Return an error at the location L, with the message Msg.
Definition: MCAsmParser.cpp:99
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::wasm::ValType::I32
@ I32
WebAssembly.h
llvm::MCSymbolRefExpr::VK_GOT
@ VK_GOT
Definition: MCExpr.h:198
llvm::SmallVector< wasm::ValType, 4 >
MCParsedAsmOperand.h
llvm::wasm::WASM_SYMBOL_TYPE_GLOBAL
@ WASM_SYMBOL_TYPE_GLOBAL
Definition: Wasm.h:369
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::reverse
auto reverse(ContainerTy &&C, std::enable_if_t< has_rbegin< ContainerTy >::value > *=nullptr)
Definition: STLExtras.h:333
llvm::Optional
Definition: APInt.h:33
llvm::MCInst
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
llvm::SmallVectorImpl::pop_back_val
LLVM_NODISCARD T pop_back_val()
Definition: SmallVector.h:635
MCTargetAsmParser.h
llvm::WebAssembly::regClassToValType
wasm::ValType regClassToValType(unsigned RC)
Definition: WebAssemblyTypeUtilities.cpp:157
llvm::WebAssemblyAsmTypeCheck::WebAssemblyAsmTypeCheck
WebAssemblyAsmTypeCheck(MCAsmParser &Parser, const MCInstrInfo &MII, bool is64)
Definition: WebAssemblyAsmTypeCheck.cpp:47
LLVM_DEBUG
#define LLVM_DEBUG(X)
Definition: Debug.h:101
GetMnemonic
StringRef GetMnemonic(unsigned Opc)
Definition: WebAssemblyAsmParser.cpp:1145
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
llvm::WebAssemblyAsmTypeCheck::localDecl
void localDecl(const SmallVector< wasm::ValType, 4 > &Locals)
Definition: WebAssemblyAsmTypeCheck.cpp:57
WebAssemblyAsmTypeCheck.h
llvm::WebAssemblyAsmTypeCheck::endOfFunction
void endOfFunction(SMLoc ErrorLoc)
Definition: WebAssemblyAsmTypeCheck.cpp:173
llvm::SMLoc
Represents a location in source code.
Definition: SMLoc.h:23
llvm::EVT
Extended Value Type.
Definition: ValueTypes.h:35
MCSymbolWasm.h
MCContext.h
MCInstrInfo.h
llvm::MCOperand::getImm
int64_t getImm() const
Definition: MCInst.h:80
MCSymbol.h
MCInst.h
MCSubtargetInfo.h
WebAssemblyTypeUtilities.h
llvm::MCSymbolRefExpr::getSymbol
const MCSymbol & getSymbol() const
Definition: MCExpr.h:398
llvm::wasm::WASM_SYMBOL_TYPE_FUNCTION
@ WASM_SYMBOL_TYPE_FUNCTION
Definition: Wasm.h:367
llvm::wasm::ValType
ValType
Definition: Wasm.h:408
SourceMgr.h
WebAssemblyUtilities.h
WebAssemblyMCTargetDesc.h
WebAssemblyTargetStreamer.h
llvm::MCSymbolRefExpr::VK_WASM_GOT_TLS
@ VK_WASM_GOT_TLS
Definition: MCExpr.h:331
s
multiplies can be turned into SHL s
Definition: README.txt:370
llvm::wasm::ValType::I64
@ I64
llvm::WebAssembly::getRegisterOpcode
int getRegisterOpcode(unsigned short Opcode)
I
#define I(x, y, z)
Definition: MD5.cpp:59
WebAssemblyTargetInfo.h
llvm::wasm::WasmSignature::Returns
SmallVector< ValType, 1 > Returns
Definition: Wasm.h:419
llvm::MCOI::OPERAND_REGISTER
@ OPERAND_REGISTER
Definition: MCInstrDesc.h:59
llvm::wasm::WASM_SYMBOL_TYPE_TAG
@ WASM_SYMBOL_TYPE_TAG
Definition: Wasm.h:371
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::MCSymbolRefExpr
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:192
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
Compiler.h
llvm::WebAssemblyAsmTypeCheck::typeCheck
bool typeCheck(SMLoc ErrorLoc, const MCInst &Inst)
Definition: WebAssemblyAsmTypeCheck.cpp:186
LLVM_FALLTHROUGH
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
Definition: Compiler.h:273
llvm::pdb::PDB_DataKind::Local
@ Local
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:83
llvm::MCInstrInfo
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:25
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
llvm::SmallVectorImpl::assign
void assign(size_type NumElts, ValueParamT Elt)
Definition: SmallVector.h:669
llvm::AMDGPU::SendMsg::Op
Op
Definition: SIDefines.h:321
llvm::MCInst::getOpcode
unsigned getOpcode() const
Definition: MCInst.h:198
llvm::WebAssembly::typeToString
const char * typeToString(wasm::ValType Type)
Definition: WebAssemblyTypeUtilities.cpp:108
MCStreamer.h
llvm::MCInst::getOperand
const MCOperand & getOperand(unsigned i) const
Definition: MCInst.h:206
llvm::to_string
std::string to_string(const T &Value)
Definition: ScopedPrinter.h:63
llvm::WebAssemblyAsmTypeCheck::funcDecl
void funcDecl(const wasm::WasmSignature &Sig)
Definition: WebAssemblyAsmTypeCheck.cpp:52
llvm::MCInstrInfo::get
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode.
Definition: MCInstrInfo.h:62
Endian.h
TargetRegistry.h
MCExpr.h
llvm::wasm::WasmSignature::Params
SmallVector< ValType, 4 > Params
Definition: Wasm.h:420
llvm::wasm::WASM_SYMBOL_TYPE_DATA
@ WASM_SYMBOL_TYPE_DATA
Definition: Wasm.h:368
llvm::wasm::WasmSignature
Definition: Wasm.h:418
MCSectionWasm.h
llvm::SmallVectorImpl::insert
iterator insert(iterator I, T &&Elt)
Definition: SmallVector.h:773