Skip to content

Commit 6b2482f

Browse files
nokotanpmatos
authored andcommitted
[WebAssembly] Update WebAssemblyAsmTypeCheck for table.get
This patch is aimed to resolve [[ #53789 | GitHub Issue #53789 ]]. Reviewed By: sbc100 Differential Revision: https://reviews.llvm.org/D120229
1 parent 42b4a62 commit 6b2482f

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmTypeCheck.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,20 @@ bool WebAssemblyAsmTypeCheck::getGlobal(SMLoc ErrorLoc, const MCInst &Inst,
182182
return false;
183183
}
184184

185+
bool WebAssemblyAsmTypeCheck::getTable(SMLoc ErrorLoc, const MCInst &Inst,
186+
wasm::ValType &Type) {
187+
const MCSymbolRefExpr *SymRef;
188+
if (getSymRef(ErrorLoc, Inst, SymRef))
189+
return true;
190+
auto WasmSym = cast<MCSymbolWasm>(&SymRef->getSymbol());
191+
if (WasmSym->getType().getValueOr(wasm::WASM_SYMBOL_TYPE_DATA) !=
192+
wasm::WASM_SYMBOL_TYPE_TABLE)
193+
return typeError(ErrorLoc, StringRef("symbol ") + WasmSym->getName() +
194+
" missing .tabletype");
195+
Type = static_cast<wasm::ValType>(WasmSym->getTableType().ElemType);
196+
return false;
197+
}
198+
185199
bool WebAssemblyAsmTypeCheck::endOfFunction(SMLoc ErrorLoc) {
186200
// Check the return types.
187201
for (auto RVT : llvm::reverse(ReturnTypes)) {
@@ -225,6 +239,19 @@ bool WebAssemblyAsmTypeCheck::typeCheck(SMLoc ErrorLoc, const MCInst &Inst) {
225239
return true;
226240
if (popType(ErrorLoc, Type))
227241
return true;
242+
} else if (Name == "table.get") {
243+
if (getTable(ErrorLoc, Inst, Type))
244+
return true;
245+
if (popType(ErrorLoc, wasm::ValType::I32))
246+
return true;
247+
Stack.push_back(Type);
248+
} else if (Name == "table.set") {
249+
if (getTable(ErrorLoc, Inst, Type))
250+
return true;
251+
if (popType(ErrorLoc, Type))
252+
return true;
253+
if (popType(ErrorLoc, wasm::ValType::I32))
254+
return true;
228255
} else if (Name == "drop") {
229256
if (popType(ErrorLoc, {}))
230257
return true;

llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmTypeCheck.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class WebAssemblyAsmTypeCheck final {
4444
bool getSymRef(SMLoc ErrorLoc, const MCInst &Inst,
4545
const MCSymbolRefExpr *&SymRef);
4646
bool getGlobal(SMLoc ErrorLoc, const MCInst &Inst, wasm::ValType &Type);
47+
bool getTable(SMLoc ErrorLoc, const MCInst &Inst, wasm::ValType &Type);
4748

4849
public:
4950
WebAssemblyAsmTypeCheck(MCAsmParser &Parser, const MCInstrInfo &MII, bool is64);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# RUN: llvm-mc -mattr=+reference-types -triple=wasm32-unknown-unknown -filetype=obj -o %t.o %s
2+
# RUN: wasm-ld --no-entry --export obtain_funcref_from_table_index %t.o -o %t.wasm
3+
# RUN: obj2yaml %t.wasm | FileCheck %s
4+
5+
.globl __indirect_function_table
6+
.tabletype __indirect_function_table, funcref
7+
8+
.globl obtain_funcref_from_table_index
9+
10+
obtain_funcref_from_table_index:
11+
.functype obtain_funcref_from_table_index(i32) -> (funcref)
12+
local.get 0
13+
table.get __indirect_function_table
14+
end_function
15+
16+
# CHECK: Sections:
17+
# CHECK-NEXT: - Type: TYPE
18+
# CHECK-NEXT: Signatures:
19+
# CHECK-NEXT: - Index: 0
20+
# CHECK-NEXT: ParamTypes:
21+
# CHECK-NEXT: - I32
22+
# CHECK-NEXT: ReturnTypes:
23+
# CHECK-NEXT: - FUNCREF

0 commit comments

Comments
 (0)