@@ -112,9 +112,9 @@ bool WebAssemblyAsmTypeCheck::popRefType(SMLoc ErrorLoc) {
112
112
return false ;
113
113
}
114
114
115
- bool WebAssemblyAsmTypeCheck::getLocal (SMLoc ErrorLoc, const MCInst &Inst ,
115
+ bool WebAssemblyAsmTypeCheck::getLocal (SMLoc ErrorLoc, const MCOperand &LocalOp ,
116
116
wasm::ValType &Type) {
117
- auto Local = static_cast <size_t >(Inst. getOperand ( 0 ) .getImm ());
117
+ auto Local = static_cast <size_t >(LocalOp .getImm ());
118
118
if (Local >= LocalTypes.size ())
119
119
return typeError (ErrorLoc, StringRef (" no local type specified for index " ) +
120
120
std::to_string (Local));
@@ -178,21 +178,21 @@ bool WebAssemblyAsmTypeCheck::checkSig(SMLoc ErrorLoc,
178
178
return false ;
179
179
}
180
180
181
- bool WebAssemblyAsmTypeCheck::getSymRef (SMLoc ErrorLoc, const MCInst &Inst ,
181
+ bool WebAssemblyAsmTypeCheck::getSymRef (SMLoc ErrorLoc, const MCOperand &SymOp ,
182
182
const MCSymbolRefExpr *&SymRef) {
183
- auto Op = Inst.getOperand (0 );
184
- if (!Op.isExpr ())
183
+ if (!SymOp.isExpr ())
185
184
return typeError (ErrorLoc, StringRef (" expected expression operand" ));
186
- SymRef = dyn_cast<MCSymbolRefExpr>(Op .getExpr ());
185
+ SymRef = dyn_cast<MCSymbolRefExpr>(SymOp .getExpr ());
187
186
if (!SymRef)
188
187
return typeError (ErrorLoc, StringRef (" expected symbol operand" ));
189
188
return false ;
190
189
}
191
190
192
- bool WebAssemblyAsmTypeCheck::getGlobal (SMLoc ErrorLoc, const MCInst &Inst,
191
+ bool WebAssemblyAsmTypeCheck::getGlobal (SMLoc ErrorLoc,
192
+ const MCOperand &GlobalOp,
193
193
wasm::ValType &Type) {
194
194
const MCSymbolRefExpr *SymRef;
195
- if (getSymRef (ErrorLoc, Inst , SymRef))
195
+ if (getSymRef (ErrorLoc, GlobalOp , SymRef))
196
196
return true ;
197
197
auto WasmSym = cast<MCSymbolWasm>(&SymRef->getSymbol ());
198
198
switch (WasmSym->getType ().value_or (wasm::WASM_SYMBOL_TYPE_DATA)) {
@@ -217,10 +217,10 @@ bool WebAssemblyAsmTypeCheck::getGlobal(SMLoc ErrorLoc, const MCInst &Inst,
217
217
return false ;
218
218
}
219
219
220
- bool WebAssemblyAsmTypeCheck::getTable (SMLoc ErrorLoc, const MCInst &Inst ,
220
+ bool WebAssemblyAsmTypeCheck::getTable (SMLoc ErrorLoc, const MCOperand &TableOp ,
221
221
wasm::ValType &Type) {
222
222
const MCSymbolRefExpr *SymRef;
223
- if (getSymRef (ErrorLoc, Inst , SymRef))
223
+ if (getSymRef (ErrorLoc, TableOp , SymRef))
224
224
return true ;
225
225
auto WasmSym = cast<MCSymbolWasm>(&SymRef->getSymbol ());
226
226
if (WasmSym->getType ().value_or (wasm::WASM_SYMBOL_TYPE_DATA) !=
@@ -231,6 +231,34 @@ bool WebAssemblyAsmTypeCheck::getTable(SMLoc ErrorLoc, const MCInst &Inst,
231
231
return false ;
232
232
}
233
233
234
+ bool WebAssemblyAsmTypeCheck::getSignature (SMLoc ErrorLoc,
235
+ const MCOperand &SigOp,
236
+ wasm::WasmSymbolType Type,
237
+ const wasm::WasmSignature *&Sig) {
238
+ const MCSymbolRefExpr *SymRef = nullptr ;
239
+ if (getSymRef (ErrorLoc, SigOp, SymRef))
240
+ return true ;
241
+ const auto *WasmSym = cast<MCSymbolWasm>(&SymRef->getSymbol ());
242
+ Sig = WasmSym->getSignature ();
243
+
244
+ if (!Sig || WasmSym->getType () != Type) {
245
+ const char *TypeName = nullptr ;
246
+ switch (Type) {
247
+ case wasm::WASM_SYMBOL_TYPE_FUNCTION:
248
+ TypeName = " func" ;
249
+ break ;
250
+ case wasm::WASM_SYMBOL_TYPE_TAG:
251
+ TypeName = " tag" ;
252
+ break ;
253
+ default :
254
+ return true ;
255
+ }
256
+ return typeError (ErrorLoc, StringRef (" symbol " ) + WasmSym->getName () +
257
+ " : missing ." + TypeName + " type" );
258
+ }
259
+ return false ;
260
+ }
261
+
234
262
bool WebAssemblyAsmTypeCheck::endOfFunction (SMLoc ErrorLoc) {
235
263
// Check the return types.
236
264
for (auto RVT : llvm::reverse (ReturnTypes)) {
@@ -252,56 +280,56 @@ bool WebAssemblyAsmTypeCheck::typeCheck(SMLoc ErrorLoc, const MCInst &Inst,
252
280
dumpTypeStack (" typechecking " + Name + " : " );
253
281
wasm::ValType Type;
254
282
if (Name == " local.get" ) {
255
- if (getLocal (Operands[1 ]->getStartLoc (), Inst, Type))
283
+ if (getLocal (Operands[1 ]->getStartLoc (), Inst. getOperand ( 0 ) , Type))
256
284
return true ;
257
285
Stack.push_back (Type);
258
286
} else if (Name == " local.set" ) {
259
- if (getLocal (Operands[1 ]->getStartLoc (), Inst, Type))
287
+ if (getLocal (Operands[1 ]->getStartLoc (), Inst. getOperand ( 0 ) , Type))
260
288
return true ;
261
289
if (popType (ErrorLoc, Type))
262
290
return true ;
263
291
} else if (Name == " local.tee" ) {
264
- if (getLocal (Operands[1 ]->getStartLoc (), Inst, Type))
292
+ if (getLocal (Operands[1 ]->getStartLoc (), Inst. getOperand ( 0 ) , Type))
265
293
return true ;
266
294
if (popType (ErrorLoc, Type))
267
295
return true ;
268
296
Stack.push_back (Type);
269
297
} else if (Name == " global.get" ) {
270
- if (getGlobal (Operands[1 ]->getStartLoc (), Inst, Type))
298
+ if (getGlobal (Operands[1 ]->getStartLoc (), Inst. getOperand ( 0 ) , Type))
271
299
return true ;
272
300
Stack.push_back (Type);
273
301
} else if (Name == " global.set" ) {
274
- if (getGlobal (Operands[1 ]->getStartLoc (), Inst, Type))
302
+ if (getGlobal (Operands[1 ]->getStartLoc (), Inst. getOperand ( 0 ) , Type))
275
303
return true ;
276
304
if (popType (ErrorLoc, Type))
277
305
return true ;
278
306
} else if (Name == " table.get" ) {
279
- if (getTable (Operands[1 ]->getStartLoc (), Inst, Type))
307
+ if (getTable (Operands[1 ]->getStartLoc (), Inst. getOperand ( 0 ) , Type))
280
308
return true ;
281
309
if (popType (ErrorLoc, wasm::ValType::I32))
282
310
return true ;
283
311
Stack.push_back (Type);
284
312
} else if (Name == " table.set" ) {
285
- if (getTable (Operands[1 ]->getStartLoc (), Inst, Type))
313
+ if (getTable (Operands[1 ]->getStartLoc (), Inst. getOperand ( 0 ) , Type))
286
314
return true ;
287
315
if (popType (ErrorLoc, Type))
288
316
return true ;
289
317
if (popType (ErrorLoc, wasm::ValType::I32))
290
318
return true ;
291
319
} else if (Name == " table.size" ) {
292
- if (getTable (Operands[1 ]->getStartLoc (), Inst, Type))
320
+ if (getTable (Operands[1 ]->getStartLoc (), Inst. getOperand ( 0 ) , Type))
293
321
return true ;
294
322
Stack.push_back (wasm::ValType::I32);
295
323
} else if (Name == " table.grow" ) {
296
- if (getTable (Operands[1 ]->getStartLoc (), Inst, Type))
324
+ if (getTable (Operands[1 ]->getStartLoc (), Inst. getOperand ( 0 ) , Type))
297
325
return true ;
298
326
if (popType (ErrorLoc, wasm::ValType::I32))
299
327
return true ;
300
328
if (popType (ErrorLoc, Type))
301
329
return true ;
302
330
Stack.push_back (wasm::ValType::I32);
303
331
} else if (Name == " table.fill" ) {
304
- if (getTable (Operands[1 ]->getStartLoc (), Inst, Type))
332
+ if (getTable (Operands[1 ]->getStartLoc (), Inst. getOperand ( 0 ) , Type))
305
333
return true ;
306
334
if (popType (ErrorLoc, wasm::ValType::I32))
307
335
return true ;
@@ -352,15 +380,10 @@ bool WebAssemblyAsmTypeCheck::typeCheck(SMLoc ErrorLoc, const MCInst &Inst,
352
380
return true ;
353
381
Unreachable = false ;
354
382
if (Name == " catch" ) {
355
- const MCSymbolRefExpr *SymRef;
356
- if (getSymRef (Operands[1 ]->getStartLoc (), Inst, SymRef))
383
+ const wasm::WasmSignature *Sig = nullptr ;
384
+ if (getSignature (Operands[1 ]->getStartLoc (), Inst.getOperand (0 ),
385
+ wasm::WASM_SYMBOL_TYPE_TAG, Sig))
357
386
return true ;
358
- const auto *WasmSym = cast<MCSymbolWasm>(&SymRef->getSymbol ());
359
- const auto *Sig = WasmSym->getSignature ();
360
- if (!Sig || WasmSym->getType () != wasm::WASM_SYMBOL_TYPE_TAG)
361
- return typeError (Operands[1 ]->getStartLoc (), StringRef (" symbol " ) +
362
- WasmSym->getName () +
363
- " : missing .tagtype" );
364
387
// catch instruction pushes values whose types are specified in the tag's
365
388
// "params" part
366
389
Stack.insert (Stack.end (), Sig->Params .begin (), Sig->Params .end ());
@@ -383,15 +406,10 @@ bool WebAssemblyAsmTypeCheck::typeCheck(SMLoc ErrorLoc, const MCInst &Inst,
383
406
if (Name == " return_call_indirect" && endOfFunction (ErrorLoc))
384
407
return true ;
385
408
} else if (Name == " call" || Name == " return_call" ) {
386
- const MCSymbolRefExpr *SymRef;
387
- if (getSymRef (Operands[1 ]->getStartLoc (), Inst, SymRef))
388
- return true ;
389
- auto WasmSym = cast<MCSymbolWasm>(&SymRef->getSymbol ());
390
- auto Sig = WasmSym->getSignature ();
391
- if (!Sig || WasmSym->getType () != wasm::WASM_SYMBOL_TYPE_FUNCTION)
392
- return typeError (Operands[1 ]->getStartLoc (), StringRef (" symbol " ) +
393
- WasmSym->getName () +
394
- " : missing .functype" );
409
+ const wasm::WasmSignature *Sig = nullptr ;
410
+ if (getSignature (Operands[1 ]->getStartLoc (), Inst.getOperand (0 ),
411
+ wasm::WASM_SYMBOL_TYPE_FUNCTION, Sig))
412
+ return true ;
395
413
if (checkSig (ErrorLoc, *Sig))
396
414
return true ;
397
415
if (Name == " return_call" && endOfFunction (ErrorLoc))
0 commit comments