Skip to content

Commit e46df67

Browse files
committed
Add initial support for anyref as an opaque type
1 parent 12add6f commit e46df67

37 files changed

+231
-32
lines changed

build-js.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ export_function "_BinaryenTypeInt64"
185185
export_function "_BinaryenTypeFloat32"
186186
export_function "_BinaryenTypeFloat64"
187187
export_function "_BinaryenTypeVec128"
188+
export_function "_BinaryenTypeAnyref"
188189
export_function "_BinaryenTypeExnref"
189190
export_function "_BinaryenTypeUnreachable"
190191
export_function "_BinaryenTypeAuto"

src/asmjs/asm_v_wasm.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@ AsmType wasmToAsmType(Type type) {
5353
return ASM_INT64;
5454
case v128:
5555
assert(false && "v128 not implemented yet");
56+
case anyref:
57+
assert(false && "anyref is not supported by asm2wasm");
5658
case exnref:
57-
assert(false && "exnref is not in asm2wasm");
59+
assert(false && "exnref is not supported by asm2wasm");
5860
case none:
5961
return ASM_NONE;
6062
case unreachable:
@@ -75,6 +77,8 @@ char getSig(Type type) {
7577
return 'd';
7678
case v128:
7779
return 'V';
80+
case anyref:
81+
return 'a';
7882
case exnref:
7983
return 'e';
8084
case none:
@@ -105,6 +109,8 @@ Type sigToType(char sig) {
105109
return f64;
106110
case 'V':
107111
return v128;
112+
case 'a':
113+
return anyref;
108114
case 'e':
109115
return exnref;
110116
case 'v':

src/binaryen-c.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ BinaryenLiteral toBinaryenLiteral(Literal x) {
7070
break;
7171
}
7272

73+
case Type::anyref: // there's no anyref literals
7374
case Type::exnref: // there's no exnref literals
7475
case Type::none:
7576
case Type::unreachable:
@@ -90,6 +91,7 @@ Literal fromBinaryenLiteral(BinaryenLiteral x) {
9091
return Literal(x.i64).castToF64();
9192
case Type::v128:
9293
return Literal(x.v128);
94+
case Type::anyref: // there's no anyref literals
9395
case Type::exnref: // there's no exnref literals
9496
case Type::none:
9597
case Type::unreachable:
@@ -210,6 +212,7 @@ void printArg(std::ostream& setup, std::ostream& out, BinaryenLiteral arg) {
210212
out << "BinaryenLiteralVec128(" << array << ")";
211213
break;
212214
}
215+
case Type::anyref: // there's no anyref literals
213216
case Type::exnref: // there's no exnref literals
214217
case Type::none:
215218
case Type::unreachable:
@@ -265,6 +268,7 @@ BinaryenType BinaryenTypeInt64(void) { return i64; }
265268
BinaryenType BinaryenTypeFloat32(void) { return f32; }
266269
BinaryenType BinaryenTypeFloat64(void) { return f64; }
267270
BinaryenType BinaryenTypeVec128(void) { return v128; }
271+
BinaryenType BinaryenTypeAnyref(void) { return anyref; }
268272
BinaryenType BinaryenTypeExnref(void) { return exnref; }
269273
BinaryenType BinaryenTypeUnreachable(void) { return unreachable; }
270274
BinaryenType BinaryenTypeAuto(void) { return uint32_t(-1); }

src/binaryen-c.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ BinaryenType BinaryenTypeInt64(void);
7474
BinaryenType BinaryenTypeFloat32(void);
7575
BinaryenType BinaryenTypeFloat64(void);
7676
BinaryenType BinaryenTypeVec128(void);
77+
BinaryenType BinaryenTypeAnyref(void);
7778
BinaryenType BinaryenTypeExnref(void);
7879
BinaryenType BinaryenTypeUnreachable(void);
7980
// Not a real type. Used as the last parameter to BinaryenBlock to let

src/ir/abstract.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ inline UnaryOp getUnary(Type type, Op op) {
8181
assert(false && "v128 not implemented yet");
8282
WASM_UNREACHABLE();
8383
}
84+
case anyref: // there's no unary instructions for anyref
8485
case exnref: // there's no unary instructions for exnref
8586
case none:
8687
case unreachable: {
@@ -212,6 +213,7 @@ inline BinaryOp getBinary(Type type, Op op) {
212213
assert(false && "v128 not implemented yet");
213214
WASM_UNREACHABLE();
214215
}
216+
case anyref: // there's no binary instructions for anyref
215217
case exnref: // there's no binary instructions for exnref
216218
case none:
217219
case unreachable: {

src/js/binaryen.js-post.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Module['i64'] = Module['_BinaryenTypeInt64']();
3737
Module['f32'] = Module['_BinaryenTypeFloat32']();
3838
Module['f64'] = Module['_BinaryenTypeFloat64']();
3939
Module['v128'] = Module['_BinaryenTypeVec128']();
40+
Module['anyref'] = Module['_BinaryenTypeAnyref']();
4041
Module['exnref'] = Module['_BinaryenTypeExnref']();
4142
Module['unreachable'] = Module['_BinaryenTypeUnreachable']();
4243
Module['auto'] = /* deprecated */ Module['undefined'] = Module['_BinaryenTypeAuto']();

src/literal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class Literal {
8080
Literal(int32_t(0)),
8181
Literal(int32_t(0)),
8282
Literal(int32_t(0))}});
83+
case Type::anyref: // there's no anyref literals
8384
case Type::exnref: // there's no exnref literals
8485
case none:
8586
case unreachable:
@@ -430,6 +431,7 @@ template<> struct less<wasm::Literal> {
430431
return a.reinterpreti64() < b.reinterpreti64();
431432
case wasm::Type::v128:
432433
return memcmp(a.getv128Ptr(), b.getv128Ptr(), 16) < 0;
434+
case wasm::Type::anyref: // anyref is an opaque value
433435
case wasm::Type::exnref: // exnref is an opaque value
434436
case wasm::Type::none:
435437
case wasm::Type::unreachable:

src/parsing.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ parseConst(cashew::IString s, Type type, MixedArena& allocator) {
263263
break;
264264
}
265265
case v128:
266+
case anyref: // there's no anyref.const
266267
case exnref: // there's no exnref.const
267268
WASM_UNREACHABLE();
268269
case none:

src/passes/ConstHoisting.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,10 @@ struct ConstHoisting : public WalkerPass<PostWalker<ConstHoisting>> {
9191
size = getTypeSize(value.type);
9292
break;
9393
}
94-
case v128: {
95-
// v128 not implemented yet
94+
case v128: // v128 not implemented yet
95+
case anyref: // anyref cannot have literals
96+
case exnref: // exnref cannot have literals
9697
return false;
97-
}
98-
case exnref: {
99-
// exnref cannot have literals
100-
return false;
101-
}
10298
case none:
10399
case unreachable: {
104100
WASM_UNREACHABLE();

src/passes/FuncCastEmulation.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ static Expression* toABI(Expression* value, Module* module) {
6666
assert(false && "v128 not implemented yet");
6767
WASM_UNREACHABLE();
6868
}
69+
case anyref: {
70+
assert(false && "anyref cannot be converted to i64");
71+
WASM_UNREACHABLE();
72+
}
6973
case exnref: {
7074
assert(false && "exnref cannot be converted to i64");
7175
WASM_UNREACHABLE();
@@ -108,6 +112,10 @@ static Expression* fromABI(Expression* value, Type type, Module* module) {
108112
assert(false && "v128 not implemented yet");
109113
WASM_UNREACHABLE();
110114
}
115+
case anyref: {
116+
assert(false && "anyref cannot be converted from i64");
117+
WASM_UNREACHABLE();
118+
}
111119
case exnref: {
112120
assert(false && "exnref cannot be converted from i64");
113121
WASM_UNREACHABLE();

src/passes/InstrumentLocals.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,10 @@ struct InstrumentLocals : public WalkerPass<PostWalker<InstrumentLocals>> {
8181
break;
8282
case v128:
8383
assert(false && "v128 not implemented yet");
84+
case anyref:
85+
assert(false && "anyref not implemented yet");
8486
case exnref:
85-
assert(false && "not implemented yet");
87+
assert(false && "exnref not implemented yet");
8688
case none:
8789
WASM_UNREACHABLE();
8890
case unreachable:
@@ -113,6 +115,8 @@ struct InstrumentLocals : public WalkerPass<PostWalker<InstrumentLocals>> {
113115
break;
114116
case v128:
115117
assert(false && "v128 not implemented yet");
118+
case anyref:
119+
assert(false && "anyref not implemented yet");
116120
case exnref:
117121
assert(false && "exnref not implemented yet");
118122
case unreachable:

src/shell-interface.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ struct ShellExternalInterface : ModuleInstance::ExternalInterface {
114114
break;
115115
case v128:
116116
assert(false && "v128 not implemented yet");
117+
case anyref:
118+
assert(false && "anyref not implemented yet");
117119
case exnref:
118120
assert(false && "exnref not implemented yet");
119121
case none:

src/tools/fuzzing.h

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,7 @@ class TranslateToFuzzReader {
851851
case f32:
852852
case f64:
853853
case v128:
854+
case anyref:
854855
case exnref:
855856
ret = _makeConcrete(type);
856857
break;
@@ -1370,6 +1371,7 @@ class TranslateToFuzzReader {
13701371
return builder.makeLoad(
13711372
16, false, offset, pick(1, 2, 4, 8, 16), ptr, type);
13721373
}
1374+
case anyref: // anyref cannot be loaded from memory
13731375
case exnref: // exnref cannot be loaded from memory
13741376
case none:
13751377
case unreachable:
@@ -1471,6 +1473,7 @@ class TranslateToFuzzReader {
14711473
return builder.makeStore(
14721474
16, offset, pick(1, 2, 4, 8, 16), ptr, value, type);
14731475
}
1476+
case anyref: // anyref cannot be stored in memory
14741477
case exnref: // exnref cannot be stored in memory
14751478
case none:
14761479
case unreachable:
@@ -1481,7 +1484,7 @@ class TranslateToFuzzReader {
14811484

14821485
Expression* makeStore(Type type) {
14831486
// exnref type cannot be stored in memory
1484-
if (!allowMemory || type == exnref) {
1487+
if (!allowMemory || type == exnref || type == anyref) {
14851488
return makeTrivial(type);
14861489
}
14871490
auto* ret = makeNonAtomicStore(type);
@@ -1566,6 +1569,7 @@ class TranslateToFuzzReader {
15661569
case f64:
15671570
return Literal(getDouble());
15681571
case v128:
1572+
case anyref: // anyref cannot have literals
15691573
case exnref: // exnref cannot have literals
15701574
case none:
15711575
case unreachable:
@@ -1608,6 +1612,7 @@ class TranslateToFuzzReader {
16081612
case f64:
16091613
return Literal(double(small));
16101614
case v128:
1615+
case anyref: // anyref cannot have literals
16111616
case exnref: // exnref cannot have literals
16121617
case none:
16131618
case unreachable:
@@ -1673,6 +1678,7 @@ class TranslateToFuzzReader {
16731678
std::numeric_limits<uint64_t>::max()));
16741679
break;
16751680
case v128:
1681+
case anyref: // anyref cannot have literals
16761682
case exnref: // exnref cannot have literals
16771683
case none:
16781684
case unreachable:
@@ -1704,6 +1710,7 @@ class TranslateToFuzzReader {
17041710
value = Literal(double(int64_t(1) << upTo(64)));
17051711
break;
17061712
case v128:
1713+
case anyref: // anyref cannot have literals
17071714
case exnref: // exnref cannot have literals
17081715
case none:
17091716
case unreachable:
@@ -1728,12 +1735,18 @@ class TranslateToFuzzReader {
17281735
}
17291736

17301737
Expression* makeConst(Type type) {
1731-
if (type == exnref) {
1732-
// There's no exnref.const.
1733-
// TODO We should return a nullref once we implement instructions for
1734-
// reference types proposal.
1735-
assert(false && "exnref const is not implemented yet");
1738+
switch (type) {
1739+
case anyref:
1740+
assert(false && "anyref const is not implemented yet");
1741+
case exnref:
1742+
// There's no exnref.const.
1743+
// TODO We should return a nullref once we implement instructions for
1744+
// reference types proposal.
1745+
assert(false && "exnref const is not implemented yet");
1746+
default:
1747+
break;
17361748
}
1749+
17371750
auto* ret = wasm.allocator.alloc<Const>();
17381751
ret->value = makeLiteral(type);
17391752
ret->type = type;
@@ -1802,6 +1815,7 @@ class TranslateToFuzzReader {
18021815
AllTrueVecI64x2),
18031816
make(v128)});
18041817
}
1818+
case anyref: // there's no unary ops for anyref
18051819
case exnref: // there's no unary ops for exnref
18061820
case none:
18071821
case unreachable:
@@ -1933,6 +1947,7 @@ class TranslateToFuzzReader {
19331947
}
19341948
WASM_UNREACHABLE();
19351949
}
1950+
case anyref: // there's no unary ops for anyref
19361951
case exnref: // there's no unary ops for exnref
19371952
case none:
19381953
case unreachable:
@@ -1955,7 +1970,7 @@ class TranslateToFuzzReader {
19551970
return makeTrivial(type);
19561971
}
19571972
// There's no binary ops for exnref
1958-
if (type == exnref) {
1973+
if (type == exnref || type == anyref) {
19591974
makeTrivial(type);
19601975
}
19611976

@@ -2146,6 +2161,7 @@ class TranslateToFuzzReader {
21462161
make(v128),
21472162
make(v128)});
21482163
}
2164+
case anyref: // there's no binary ops for anyref
21492165
case exnref: // there's no binary ops for exnref
21502166
case none:
21512167
case unreachable:
@@ -2340,6 +2356,7 @@ class TranslateToFuzzReader {
23402356
op = ExtractLaneVecF64x2;
23412357
break;
23422358
case v128:
2359+
case anyref:
23432360
case exnref:
23442361
case none:
23452362
case unreachable:

src/tools/spec-wrapper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ static std::string generateSpecWrapper(Module& wasm) {
4848
case v128:
4949
ret += "(v128.const i32x4 0 0 0 0)";
5050
break;
51+
case anyref: // there's no anyref.const
5152
case exnref: // there's no exnref.const
5253
case none:
5354
case unreachable:

src/tools/tool-options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ struct ToolOptions : public Options {
7171
.addFeature(FeatureSet::ExceptionHandling,
7272
"exception handling operations")
7373
.addFeature(FeatureSet::TailCall, "tail call operations")
74+
.addFeature(FeatureSet::ReferenceTypes, "reference types")
7475
.add("--no-validation",
7576
"-n",
7677
"Disables validation, assumes inputs are correct",

src/tools/wasm-reduce.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,7 @@ struct Reducer
592592
fixed = builder->makeUnary(TruncSFloat64ToInt32, child);
593593
break;
594594
case v128:
595+
case anyref:
595596
case exnref:
596597
continue; // not implemented yet
597598
case none:
@@ -614,6 +615,7 @@ struct Reducer
614615
fixed = builder->makeUnary(TruncSFloat64ToInt64, child);
615616
break;
616617
case v128:
618+
case anyref:
617619
case exnref:
618620
continue; // not implemented yet
619621
case none:
@@ -636,6 +638,7 @@ struct Reducer
636638
fixed = builder->makeUnary(DemoteFloat64, child);
637639
break;
638640
case v128:
641+
case anyref:
639642
case exnref:
640643
continue; // not implemented yet
641644
case none:
@@ -658,6 +661,7 @@ struct Reducer
658661
case f64:
659662
WASM_UNREACHABLE();
660663
case v128:
664+
case anyref:
661665
case exnref:
662666
continue; // not implemented yet
663667
case none:
@@ -667,6 +671,7 @@ struct Reducer
667671
break;
668672
}
669673
case v128:
674+
case anyref:
670675
case exnref:
671676
continue; // not implemented yet
672677
case none:

0 commit comments

Comments
 (0)