diff options
author | Thomas Lively <tlively@google.com> | 2022-12-09 17:56:10 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-09 23:56:10 +0000 |
commit | 082dbe25b7377809b1b3dc429cb334fc80fac286 (patch) | |
tree | 62478deb4383c8f78ca648fd6ea8629494a9a113 /src/wasm/wasm-s-parser.cpp | |
parent | 48959ab5a74d849e9782f54b3535c6fca69d51d7 (diff) | |
download | binaryen-082dbe25b7377809b1b3dc429cb334fc80fac286.tar.gz binaryen-082dbe25b7377809b1b3dc429cb334fc80fac286.tar.bz2 binaryen-082dbe25b7377809b1b3dc429cb334fc80fac286.zip |
Use non-nullable ref.cast for non-nullable input (#5335)
We switched from emitting the legacy `ref.cast_static` instruction to emitting
`ref.cast null` in #5331, but that wasn't quite correct. The legacy instruction
had polymorphic typing so that its output type was nullable if and only if its
input type was nullable. In contrast, `ref.cast null` always has a a nullable
output type.
Fix our output by instead emitting non-nullable `ref.cast` if the output should
be non-nullable. Parse `ref.cast` in binary and text forms as well. Since the IR
can only represent the legacy polymorphic semantics, disallow unsupported casts
from nullable to non-nullable references or vice versa for now.
Diffstat (limited to 'src/wasm/wasm-s-parser.cpp')
-rw-r--r-- | src/wasm/wasm-s-parser.cpp | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/src/wasm/wasm-s-parser.cpp b/src/wasm/wasm-s-parser.cpp index 507cf3b6c..98f7c1e87 100644 --- a/src/wasm/wasm-s-parser.cpp +++ b/src/wasm/wasm-s-parser.cpp @@ -2783,13 +2783,25 @@ Expression* SExpressionWasmBuilder::makeRefTest(Element& s) { Expression* SExpressionWasmBuilder::makeRefCast(Element& s) { int i = 1; + std::optional<Nullability> nullability; if (s[0]->str().str != "ref.cast_static") { - if (s[i++]->str().str != "null") { - throw ParseException("ref.cast not yet supported. Use ref.cast null."); + nullability = NonNullable; + if (s[i]->str().str == "null") { + nullability = Nullable; + ++i; } } auto heapType = parseHeapType(*s[i++]); auto* ref = parseExpression(*s[i++]); + if (nullability && ref->type.isRef()) { + if (*nullability == NonNullable && ref->type.isNullable()) { + throw ParseException( + "ref.cast on nullable input not yet supported", s.line, s.col); + } else if (*nullability == Nullable && ref->type.isNonNullable()) { + throw ParseException( + "ref.cast null on non-nullable input not yet supported", s.line, s.col); + } + } return Builder(wasm).makeRefCast(ref, heapType, RefCast::Safe); } |