From da5035f893ce9e046f99cf3ede92b576024aa9da Mon Sep 17 00:00:00 2001
From: Thomas Lively <7121787+tlively@users.noreply.github.com>
Date: Wed, 20 Jul 2022 20:13:18 -0700
Subject: Remove basic reference types (#4802)
Basic reference types like `Type::funcref`, `Type::anyref`, etc. made it easy to
accidentally forget to handle reference types with the same basic HeapTypes but
the opposite nullability. In principle there is nothing special about the types
with shorthands except in the binary and text formats. Removing these shorthands
from the internal type representation by removing all basic reference types
makes some code more complicated locally, but simplifies code globally and
encourages properly handling both nullable and non-nullable reference types.
---
src/wasm-builder.h | 33 +++++++++++----------------------
1 file changed, 11 insertions(+), 22 deletions(-)
(limited to 'src/wasm-builder.h')
diff --git a/src/wasm-builder.h b/src/wasm-builder.h
index eec86bc50..d76d24b21 100644
--- a/src/wasm-builder.h
+++ b/src/wasm-builder.h
@@ -82,7 +82,8 @@ public:
}
static std::unique_ptr
makeTable(Name name,
- Type type = Type::funcref,
+ Type type = Type(HeapType::func,
+ Nullable),
Address initial = 0,
Address max = Table::kMaxSize) {
auto table = std::make_unique();
@@ -97,7 +98,7 @@ public:
makeElementSegment(Name name,
Name table,
Expression* offset = nullptr,
- Type type = Type::funcref) {
+ Type type = Type(HeapType::func, Nullable)) {
auto seg = std::make_unique();
seg->name = name;
seg->table = table;
@@ -1114,20 +1115,14 @@ public:
if (type.isFunction()) {
return makeRefFunc(value.getFunc(), type.getHeapType());
}
+ if (type.isRef() && type.getHeapType() == HeapType::i31) {
+ return makeI31New(makeConst(value.geti31()));
+ }
if (type.isRtt()) {
return makeRtt(value.type);
}
TODO_SINGLE_COMPOUND(type);
- switch (type.getBasic()) {
- case Type::anyref:
- case Type::eqref:
- assert(value.isNull() && "unexpected non-null reference type literal");
- return makeRefNull(type);
- case Type::i31ref:
- return makeI31New(makeConst(value.geti31()));
- default:
- WASM_UNREACHABLE("invalid constant expression");
- }
+ WASM_UNREACHABLE("unsupported constant expression");
}
Expression* makeConstantExpression(Literals values) {
@@ -1273,7 +1268,10 @@ public:
if (curr->type.isNullable()) {
return ExpressionManipulator::refNull(curr, curr->type);
}
- if (curr->type.isFunction() || !curr->type.isBasic()) {
+ if (curr->type.isRef() && curr->type.getHeapType() == HeapType::i31) {
+ return makeI31New(makeConst(0));
+ }
+ if (!curr->type.isBasic()) {
// We can't do any better, keep the original.
return curr;
}
@@ -1298,15 +1296,6 @@ public:
value = Literal(bytes.data());
break;
}
- case Type::funcref:
- WASM_UNREACHABLE("handled above");
- case Type::anyref:
- case Type::eqref:
- return ExpressionManipulator::refNull(curr, curr->type);
- case Type::i31ref:
- return makeI31New(makeConst(0));
- case Type::dataref:
- return curr;
case Type::none:
return ExpressionManipulator::nop(curr);
case Type::unreachable:
--
cgit v1.2.3