diff options
author | Alon Zakai <azakai@google.com> | 2021-03-04 20:31:58 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-04 12:31:58 -0800 |
commit | 2bd5f60769506479316811da2b07913d0412abe0 (patch) | |
tree | 1477b7adda76385f219736946cf89c20307a289e /src/wasm/wasm-binary.cpp | |
parent | ecd05546466494973fcc196a6661e0d5dfff6aa1 (diff) | |
download | binaryen-2bd5f60769506479316811da2b07913d0412abe0.tar.gz binaryen-2bd5f60769506479316811da2b07913d0412abe0.tar.bz2 binaryen-2bd5f60769506479316811da2b07913d0412abe0.zip |
Emit "elem declare" for functions that need it (#3653)
This adds support for reading (elem declare func $foo .. in the text and
binary formats. We can simply ignore it: we don't need to represent it in
IR, rather we find what needs to be declared when writing. That part takes
a little more work, for which this adds a shared helper function.
Diffstat (limited to 'src/wasm/wasm-binary.cpp')
-rw-r--r-- | src/wasm/wasm-binary.cpp | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index d5579eedf..0993eb124 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -18,6 +18,7 @@ #include <fstream> #include "ir/module-utils.h" +#include "ir/table-utils.h" #include "support/bits.h" #include "support/debug.h" #include "wasm-binary.h" @@ -548,6 +549,10 @@ void WasmBinaryWriter::writeTableElements() { for (auto& table : wasm->tables) { elemCount += table->segments.size(); } + auto needingElemDecl = TableUtils::getFunctionsNeedingElemDeclare(*wasm); + if (!needingElemDecl.empty()) { + elemCount++; + } if (elemCount == 0) { return; } @@ -588,6 +593,16 @@ void WasmBinaryWriter::writeTableElements() { } } } + + if (!needingElemDecl.empty()) { + o << U32LEB(BinaryConsts::IsPassive | BinaryConsts::IsDeclarative); + o << U32LEB(0); // type (indicating funcref) + o << U32LEB(needingElemDecl.size()); + for (auto name : needingElemDecl) { + o << U32LEB(indexes.functionIndexes[name]); + } + } + finishSection(start); } @@ -2677,6 +2692,19 @@ void WasmBinaryBuilder::readTableElements() { bool usesExpressions = (flags & BinaryConsts::UsesExpressions) != 0; if (isPassive) { + bool isDeclarative = (flags & BinaryConsts::IsDeclarative) != 0; + if (isDeclarative) { + // "elem declare" is needed in wasm text and binary, but not in Binaryen + // IR; read and ignore the contents. + auto type = getU32LEB(); + WASM_UNUSED(type); + auto num = getU32LEB(); + for (Index i = 0; i < num; i++) { + getU32LEB(); + } + continue; + } + throwError("Only active elem segments are supported."); } |