diff options
author | Abbas Mashayekh <martianboy2005@gmail.com> | 2021-03-24 21:43:45 +0430 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-24 10:13:45 -0700 |
commit | ffac06650507ac413d60d72aadc1e33fb1f91ccf (patch) | |
tree | f581d02da66df8db2a775fc731d39baf15d96c82 /src/passes/Print.cpp | |
parent | 683c31381f5798016f683a6b42e2a8fad0f871cb (diff) | |
download | binaryen-ffac06650507ac413d60d72aadc1e33fb1f91ccf.tar.gz binaryen-ffac06650507ac413d60d72aadc1e33fb1f91ccf.tar.bz2 binaryen-ffac06650507ac413d60d72aadc1e33fb1f91ccf.zip |
[RT] Support expressions in element segments (#3666)
This PR adds support for `ref.null t` as a valid element segment
item. The abbreviated format of `(elem ... func $f $g...)` is kept in
both printing and binary emitting if all items are `ref.func`s. Public
APIs aren't updated in this PR.
Diffstat (limited to 'src/passes/Print.cpp')
-rw-r--r-- | src/passes/Print.cpp | 34 |
1 files changed, 27 insertions, 7 deletions
diff --git a/src/passes/Print.cpp b/src/passes/Print.cpp index 4346fe705..6dfd495c0 100644 --- a/src/passes/Print.cpp +++ b/src/passes/Print.cpp @@ -2704,6 +2704,18 @@ struct PrintSExpression : public UnifiedExpressionVisitor<PrintSExpression> { if (curr->data.empty()) { return; } + bool allElementsRefFunc = + std::all_of(curr->data.begin(), curr->data.end(), [](Expression* entry) { + return entry->is<RefFunc>(); + }); + auto printElemType = [&]() { + if (allElementsRefFunc) { + TypeNamePrinter(o, currModule).print(HeapType::func); + } else { + TypeNamePrinter(o, currModule).print(Type::funcref); + } + }; + doIndent(o, indent); o << '('; printMedium(o, "elem"); @@ -2714,7 +2726,7 @@ struct PrintSExpression : public UnifiedExpressionVisitor<PrintSExpression> { if (curr->table.is()) { // TODO(reference-types): check for old-style based on the complete spec - if (currModule->tables.size() > 1) { + if (!allElementsRefFunc || currModule->tables.size() > 1) { // tableuse o << " (table "; printName(curr->table, o); @@ -2724,18 +2736,26 @@ struct PrintSExpression : public UnifiedExpressionVisitor<PrintSExpression> { o << ' '; visit(curr->offset); - if (currModule->tables.size() > 1) { + if (!allElementsRefFunc || currModule->tables.size() > 1) { o << ' '; - TypeNamePrinter(o, currModule).print(HeapType::func); + printElemType(); } } else { o << ' '; - TypeNamePrinter(o, currModule).print(HeapType::func); + printElemType(); } - for (auto name : curr->data) { - o << ' '; - printName(name, o); + if (allElementsRefFunc) { + for (auto* entry : curr->data) { + auto* refFunc = entry->cast<RefFunc>(); + o << ' '; + printName(refFunc->func, o); + } + } else { + for (auto* entry : curr->data) { + o << ' '; + printExpression(entry, o); + } } o << ')' << maybeNewLine; } |