Skip to content

Commit

Permalink
[WebAssembly] Update WebAssemblyAsmTypeCheck for table.get
Browse files Browse the repository at this point in the history
This patch is aimed to resolve [[ #53789 | GitHub Issue #53789 ]].

Reviewed By: sbc100

Differential Revision: https://reviews.llvm.org/D120229
  • Loading branch information
nokotan authored and pmatos committed Mar 4, 2022
1 parent 42b4a62 commit 6b2482f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
27 changes: 27 additions & 0 deletions llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmTypeCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,20 @@ bool WebAssemblyAsmTypeCheck::getGlobal(SMLoc ErrorLoc, const MCInst &Inst,
return false;
}

bool WebAssemblyAsmTypeCheck::getTable(SMLoc ErrorLoc, const MCInst &Inst,
wasm::ValType &Type) {
const MCSymbolRefExpr *SymRef;
if (getSymRef(ErrorLoc, Inst, SymRef))
return true;
auto WasmSym = cast<MCSymbolWasm>(&SymRef->getSymbol());
if (WasmSym->getType().getValueOr(wasm::WASM_SYMBOL_TYPE_DATA) !=
wasm::WASM_SYMBOL_TYPE_TABLE)
return typeError(ErrorLoc, StringRef("symbol ") + WasmSym->getName() +
" missing .tabletype");
Type = static_cast<wasm::ValType>(WasmSym->getTableType().ElemType);
return false;
}

bool WebAssemblyAsmTypeCheck::endOfFunction(SMLoc ErrorLoc) {
// Check the return types.
for (auto RVT : llvm::reverse(ReturnTypes)) {
Expand Down Expand Up @@ -225,6 +239,19 @@ bool WebAssemblyAsmTypeCheck::typeCheck(SMLoc ErrorLoc, const MCInst &Inst) {
return true;
if (popType(ErrorLoc, Type))
return true;
} else if (Name == "table.get") {
if (getTable(ErrorLoc, Inst, Type))
return true;
if (popType(ErrorLoc, wasm::ValType::I32))
return true;
Stack.push_back(Type);
} else if (Name == "table.set") {
if (getTable(ErrorLoc, Inst, Type))
return true;
if (popType(ErrorLoc, Type))
return true;
if (popType(ErrorLoc, wasm::ValType::I32))
return true;
} else if (Name == "drop") {
if (popType(ErrorLoc, {}))
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class WebAssemblyAsmTypeCheck final {
bool getSymRef(SMLoc ErrorLoc, const MCInst &Inst,
const MCSymbolRefExpr *&SymRef);
bool getGlobal(SMLoc ErrorLoc, const MCInst &Inst, wasm::ValType &Type);
bool getTable(SMLoc ErrorLoc, const MCInst &Inst, wasm::ValType &Type);

public:
WebAssemblyAsmTypeCheck(MCAsmParser &Parser, const MCInstrInfo &MII, bool is64);
Expand Down
23 changes: 23 additions & 0 deletions llvm/test/MC/WebAssembly/funcref-from-table.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# RUN: llvm-mc -mattr=+reference-types -triple=wasm32-unknown-unknown -filetype=obj -o %t.o %s
# RUN: wasm-ld --no-entry --export obtain_funcref_from_table_index %t.o -o %t.wasm
# RUN: obj2yaml %t.wasm | FileCheck %s

.globl __indirect_function_table
.tabletype __indirect_function_table, funcref

.globl obtain_funcref_from_table_index

obtain_funcref_from_table_index:
.functype obtain_funcref_from_table_index(i32) -> (funcref)
local.get 0
table.get __indirect_function_table
end_function

# CHECK: Sections:
# CHECK-NEXT: - Type: TYPE
# CHECK-NEXT: Signatures:
# CHECK-NEXT: - Index: 0
# CHECK-NEXT: ParamTypes:
# CHECK-NEXT: - I32
# CHECK-NEXT: ReturnTypes:
# CHECK-NEXT: - FUNCREF

0 comments on commit 6b2482f

Please sign in to comment.