Skip to content

Commit 365f12e

Browse files
authored
Typed continuations: nocont and cont basic heap types (#6468)
This PR is part of a series that adds basic support for the typed continuations/wasmfx proposal. This particular PR adds cont and nocont as top and bottom types for continuation types, completely analogous to func and nofunc for function types (also: exn and noexn).
1 parent 83d3059 commit 365f12e

File tree

13 files changed

+232
-19
lines changed

13 files changed

+232
-19
lines changed

src/binaryen-c.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ BinaryenLiteral toBinaryenLiteral(Literal x) {
8686
case HeapType::any:
8787
case HeapType::eq:
8888
case HeapType::func:
89+
case HeapType::cont:
8990
case HeapType::struct_:
9091
case HeapType::array:
9192
case HeapType::exn:
@@ -98,6 +99,7 @@ BinaryenLiteral toBinaryenLiteral(Literal x) {
9899
case HeapType::none:
99100
case HeapType::noext:
100101
case HeapType::nofunc:
102+
case HeapType::nocont:
101103
case HeapType::noexn:
102104
// Null.
103105
return ret;
@@ -141,6 +143,7 @@ Literal fromBinaryenLiteral(BinaryenLiteral x) {
141143
WASM_UNREACHABLE("TODO: extern literals");
142144
case HeapType::eq:
143145
case HeapType::func:
146+
case HeapType::cont:
144147
case HeapType::struct_:
145148
case HeapType::array:
146149
case HeapType::exn:
@@ -153,6 +156,7 @@ Literal fromBinaryenLiteral(BinaryenLiteral x) {
153156
case HeapType::none:
154157
case HeapType::noext:
155158
case HeapType::nofunc:
159+
case HeapType::nocont:
156160
case HeapType::noexn:
157161
assert(type.isNullable());
158162
return Literal::makeNull(heapType);

src/tools/fuzzing/fuzzing.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2525,6 +2525,9 @@ Expression* TranslateToFuzzReader::makeBasicRef(Type type) {
25252525
case HeapType::func: {
25262526
return makeRefFuncConst(type);
25272527
}
2528+
case HeapType::cont: {
2529+
WASM_UNREACHABLE("not implemented");
2530+
}
25282531
case HeapType::any: {
25292532
// Choose a subtype we can materialize a constant for. We cannot
25302533
// materialize non-nullable refs to func or i31 in global contexts.
@@ -2652,6 +2655,7 @@ Expression* TranslateToFuzzReader::makeBasicRef(Type type) {
26522655
case HeapType::none:
26532656
case HeapType::noext:
26542657
case HeapType::nofunc:
2658+
case HeapType::nocont:
26552659
case HeapType::noexn: {
26562660
auto null = builder.makeRefNull(heapType);
26572661
if (!type.isNullable()) {
@@ -4076,6 +4080,8 @@ HeapType TranslateToFuzzReader::getSubType(HeapType type) {
40764080
return pick(FeatureOptions<HeapType>()
40774081
.add(FeatureSet::ReferenceTypes, HeapType::func)
40784082
.add(FeatureSet::GC, HeapType::nofunc));
4083+
case HeapType::cont:
4084+
return pick(HeapType::cont, HeapType::nocont);
40794085
case HeapType::ext:
40804086
return pick(FeatureOptions<HeapType>()
40814087
.add(FeatureSet::ReferenceTypes, HeapType::ext)
@@ -4116,6 +4122,7 @@ HeapType TranslateToFuzzReader::getSubType(HeapType type) {
41164122
case HeapType::none:
41174123
case HeapType::noext:
41184124
case HeapType::nofunc:
4125+
case HeapType::nocont:
41194126
case HeapType::noexn:
41204127
break;
41214128
}

src/tools/fuzzing/heap-types.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,8 @@ struct HeapTypeGeneratorImpl {
380380
switch (type.getBasic()) {
381381
case HeapType::func:
382382
return pickSubFunc();
383+
case HeapType::cont:
384+
WASM_UNREACHABLE("not implemented");
383385
case HeapType::any:
384386
return pickSubAny();
385387
case HeapType::eq:
@@ -399,6 +401,7 @@ struct HeapTypeGeneratorImpl {
399401
case HeapType::none:
400402
case HeapType::noext:
401403
case HeapType::nofunc:
404+
case HeapType::nocont:
402405
case HeapType::noexn:
403406
return type;
404407
}
@@ -442,6 +445,7 @@ struct HeapTypeGeneratorImpl {
442445
case HeapType::ext:
443446
case HeapType::func:
444447
case HeapType::exn:
448+
case HeapType::cont:
445449
case HeapType::any:
446450
break;
447451
case HeapType::eq:
@@ -470,6 +474,8 @@ struct HeapTypeGeneratorImpl {
470474
return pickSubAny();
471475
case HeapType::nofunc:
472476
return pickSubFunc();
477+
case HeapType::nocont:
478+
WASM_UNREACHABLE("not implemented");
473479
case HeapType::noext:
474480
candidates.push_back(HeapType::ext);
475481
break;

src/wasm-binary.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,8 @@ enum EncodedType {
372372
eqref = -0x13, // 0x6d
373373
nonnullable = -0x1c, // 0x64
374374
nullable = -0x1d, // 0x63
375+
contref = -0x18, // 0x68
376+
nullcontref = -0x0b, // 0x75
375377
// exception handling
376378
exnref = -0x17, // 0x69
377379
nullexnref = -0xc, // 0x74
@@ -403,6 +405,8 @@ enum EncodedHeapType {
403405
eq = -0x13, // 0x6d
404406
exn = -0x17, // 0x69
405407
noexn = -0xc, // 0x74
408+
cont = -0x18, // 0x68
409+
nocont = -0x0b, // 0x75
406410
i31 = -0x14, // 0x6c
407411
struct_ = -0x15, // 0x6b
408412
array = -0x16, // 0x6a

src/wasm-type.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ class HeapType {
321321
enum BasicHeapType : uint32_t {
322322
ext,
323323
func,
324+
cont,
324325
any,
325326
eq,
326327
i31,
@@ -334,6 +335,7 @@ class HeapType {
334335
none,
335336
noext,
336337
nofunc,
338+
nocont,
337339
noexn,
338340
};
339341
static constexpr BasicHeapType _last_basic_type = noexn;
@@ -367,6 +369,10 @@ class HeapType {
367369
bool isFunction() const;
368370
bool isData() const;
369371
bool isSignature() const;
372+
// Indicates whether the given type was defined to be of the form
373+
// `(cont $ft)`. Returns false for `cont`, the top type of the continuation
374+
// type hierarchy (and all other types). In other words, this is analogous to
375+
// `isSignature`, but for continuation types.
370376
bool isContinuation() const;
371377
bool isStruct() const;
372378
bool isArray() const;

src/wasm/literal.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,12 @@ Literal::Literal(const Literal& other) : type(other.type) {
138138
case HeapType::noext:
139139
case HeapType::nofunc:
140140
case HeapType::noexn:
141+
case HeapType::nocont:
141142
WASM_UNREACHABLE("null literals should already have been handled");
142143
case HeapType::any:
143144
case HeapType::eq:
144145
case HeapType::func:
146+
case HeapType::cont:
145147
case HeapType::struct_:
146148
case HeapType::array:
147149
case HeapType::exn:
@@ -622,6 +624,9 @@ std::ostream& operator<<(std::ostream& o, Literal literal) {
622624
case HeapType::noexn:
623625
o << "nullexnref";
624626
break;
627+
case HeapType::nocont:
628+
o << "nullcontref";
629+
break;
625630
case HeapType::ext:
626631
o << "externref";
627632
break;
@@ -631,6 +636,7 @@ std::ostream& operator<<(std::ostream& o, Literal literal) {
631636
case HeapType::any:
632637
case HeapType::eq:
633638
case HeapType::func:
639+
case HeapType::cont:
634640
case HeapType::struct_:
635641
case HeapType::array:
636642
WASM_UNREACHABLE("invalid type");

src/wasm/wasm-binary.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,6 +1500,9 @@ void WasmBinaryWriter::writeType(Type type) {
15001500
case HeapType::func:
15011501
o << S32LEB(BinaryConsts::EncodedType::funcref);
15021502
return;
1503+
case HeapType::cont:
1504+
o << S32LEB(BinaryConsts::EncodedType::contref);
1505+
return;
15031506
case HeapType::eq:
15041507
o << S32LEB(BinaryConsts::EncodedType::eqref);
15051508
return;
@@ -1539,6 +1542,9 @@ void WasmBinaryWriter::writeType(Type type) {
15391542
case HeapType::noexn:
15401543
o << S32LEB(BinaryConsts::EncodedType::nullexnref);
15411544
return;
1545+
case HeapType::nocont:
1546+
o << S32LEB(BinaryConsts::EncodedType::nullcontref);
1547+
return;
15421548
}
15431549
}
15441550
if (type.isNullable()) {
@@ -1612,6 +1618,9 @@ void WasmBinaryWriter::writeHeapType(HeapType type) {
16121618
case HeapType::func:
16131619
ret = BinaryConsts::EncodedHeapType::func;
16141620
break;
1621+
case HeapType::cont:
1622+
ret = BinaryConsts::EncodedHeapType::cont;
1623+
break;
16151624
case HeapType::any:
16161625
ret = BinaryConsts::EncodedHeapType::any;
16171626
break;
@@ -1654,6 +1663,9 @@ void WasmBinaryWriter::writeHeapType(HeapType type) {
16541663
case HeapType::noexn:
16551664
ret = BinaryConsts::EncodedHeapType::noexn;
16561665
break;
1666+
case HeapType::nocont:
1667+
ret = BinaryConsts::EncodedHeapType::nocont;
1668+
break;
16571669
}
16581670
o << S64LEB(ret); // TODO: Actually s33
16591671
}
@@ -1986,6 +1998,9 @@ bool WasmBinaryReader::getBasicType(int32_t code, Type& out) {
19861998
case BinaryConsts::EncodedType::funcref:
19871999
out = Type(HeapType::func, Nullable);
19882000
return true;
2001+
case BinaryConsts::EncodedType::contref:
2002+
out = Type(HeapType::cont, Nullable);
2003+
return true;
19892004
case BinaryConsts::EncodedType::externref:
19902005
out = Type(HeapType::ext, Nullable);
19912006
return true;
@@ -2031,6 +2046,9 @@ bool WasmBinaryReader::getBasicType(int32_t code, Type& out) {
20312046
case BinaryConsts::EncodedType::nullexnref:
20322047
out = Type(HeapType::noexn, Nullable);
20332048
return true;
2049+
case BinaryConsts::EncodedType::nullcontref:
2050+
out = Type(HeapType::nocont, Nullable);
2051+
return true;
20342052
default:
20352053
return false;
20362054
}
@@ -2041,6 +2059,9 @@ bool WasmBinaryReader::getBasicHeapType(int64_t code, HeapType& out) {
20412059
case BinaryConsts::EncodedHeapType::func:
20422060
out = HeapType::func;
20432061
return true;
2062+
case BinaryConsts::EncodedHeapType::cont:
2063+
out = HeapType::func;
2064+
return true;
20442065
case BinaryConsts::EncodedHeapType::ext:
20452066
out = HeapType::ext;
20462067
return true;
@@ -2086,6 +2107,9 @@ bool WasmBinaryReader::getBasicHeapType(int64_t code, HeapType& out) {
20862107
case BinaryConsts::EncodedHeapType::noexn:
20872108
out = HeapType::noexn;
20882109
return true;
2110+
case BinaryConsts::EncodedHeapType::nocont:
2111+
out = HeapType::nocont;
2112+
return true;
20892113
default:
20902114
return false;
20912115
}

src/wasm/wasm-s-parser.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,6 +1257,9 @@ Type SExpressionWasmBuilder::stringToType(std::string_view str,
12571257
if (str.substr(0, 7) == "funcref" && (prefix || str.size() == 7)) {
12581258
return Type(HeapType::func, Nullable);
12591259
}
1260+
if (str.substr(0, 7) == "contref" && (prefix || str.size() == 7)) {
1261+
return Type(HeapType::cont, Nullable);
1262+
}
12601263
if (str.substr(0, 9) == "externref" && (prefix || str.size() == 9)) {
12611264
return Type(HeapType::ext, Nullable);
12621265
}
@@ -1302,6 +1305,9 @@ Type SExpressionWasmBuilder::stringToType(std::string_view str,
13021305
if (str.substr(0, 10) == "nullexnref" && (prefix || str.size() == 10)) {
13031306
return Type(HeapType::noexn, Nullable);
13041307
}
1308+
if (str.substr(0, 11) == "nullcontref" && (prefix || str.size() == 11)) {
1309+
return Type(HeapType::nocont, Nullable);
1310+
}
13051311
if (allowError) {
13061312
return Type::none;
13071313
}
@@ -1314,6 +1320,9 @@ HeapType SExpressionWasmBuilder::stringToHeapType(std::string_view str,
13141320
if (str.substr(0, 4) == "func" && (prefix || str.size() == 4)) {
13151321
return HeapType::func;
13161322
}
1323+
if (str.substr(0, 4) == "cont" && (prefix || str.size() == 4)) {
1324+
return HeapType::cont;
1325+
}
13171326
if (str.substr(0, 2) == "eq" && (prefix || str.size() == 2)) {
13181327
return HeapType::eq;
13191328
}
@@ -1362,6 +1371,9 @@ HeapType SExpressionWasmBuilder::stringToHeapType(std::string_view str,
13621371
if (str.substr(0, 5) == "noexn" && (prefix || str.size() == 5)) {
13631372
return HeapType::noexn;
13641373
}
1374+
if (str.substr(0, 6) == "nocont" && (prefix || str.size() == 6)) {
1375+
return HeapType::nocont;
1376+
}
13651377
throw ParseException(std::string("invalid wasm heap type: ") +
13661378
std::string(str.data(), str.size()));
13671379
}

0 commit comments

Comments
 (0)