Skip to content

Commit 1582c52

Browse files
authored
Add push/pop support for anyref (#2376)
This adds push/pop support for anyref. This also adds missing C API tests for push/pop.
1 parent 13725e5 commit 1582c52

File tree

10 files changed

+168
-58
lines changed

10 files changed

+168
-58
lines changed

scripts/gen-s-parser.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
("f32.pop", "makePop(f32)"),
5050
("f64.pop", "makePop(f64)"),
5151
("v128.pop", "makePop(v128)"),
52+
("anyref.pop", "makePop(anyref)"),
5253
("exnref.pop", "makePop(exnref)"),
5354
("i32.load", "makeLoad(s, i32, /*isAtomic=*/false)"),
5455
("i64.load", "makeLoad(s, i64, /*isAtomic=*/false)"),

src/gen-s-parser.inc

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,21 @@ char op[27] = {'\0'};
88
strncpy(op, s[0]->c_str(), 26);
99
switch (op[0]) {
1010
case 'a': {
11-
switch (op[7]) {
12-
case 'f':
13-
if (strcmp(op, "atomic.fence") == 0) { return makeAtomicFence(s); }
14-
goto parse_error;
11+
switch (op[1]) {
1512
case 'n':
16-
if (strcmp(op, "atomic.notify") == 0) { return makeAtomicNotify(s); }
13+
if (strcmp(op, "anyref.pop") == 0) { return makePop(anyref); }
1714
goto parse_error;
15+
case 't': {
16+
switch (op[7]) {
17+
case 'f':
18+
if (strcmp(op, "atomic.fence") == 0) { return makeAtomicFence(s); }
19+
goto parse_error;
20+
case 'n':
21+
if (strcmp(op, "atomic.notify") == 0) { return makeAtomicNotify(s); }
22+
goto parse_error;
23+
default: goto parse_error;
24+
}
25+
}
1826
default: goto parse_error;
1927
}
2028
}

src/js/binaryen.js-post.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,6 +1872,12 @@ function wrapModule(module, self) {
18721872
},
18731873
};
18741874

1875+
self['anyref'] = {
1876+
'pop': function() {
1877+
return Module['_BinaryenPop'](module, Module['anyref']);
1878+
}
1879+
};
1880+
18751881
self['exnref'] = {
18761882
'pop': function() {
18771883
return Module['_BinaryenPop'](module, Module['exnref']);

test/binaryen.js/kitchen-sink.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,7 @@ function test_core() {
488488
module.push(module.f32.pop()),
489489
module.push(module.f64.pop()),
490490
module.push(module.v128.pop()),
491+
module.push(module.anyref.pop()),
491492
module.push(module.exnref.pop()),
492493
// TODO: Host
493494
module.nop(),

test/binaryen.js/kitchen-sink.js.txt

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,6 +1658,9 @@ getExpressionInfo(f64.const)={"id":14,"type":4,"value":9.5}
16581658
(push
16591659
(v128.pop)
16601660
)
1661+
(push
1662+
(anyref.pop)
1663+
)
16611664
(push
16621665
(exnref.pop)
16631666
)
@@ -3261,6 +3264,9 @@ getExpressionInfo(f64.const)={"id":14,"type":4,"value":9.5}
32613264
(push
32623265
(v128.pop)
32633266
)
3267+
(push
3268+
(anyref.pop)
3269+
)
32643270
(push
32653271
(exnref.pop)
32663272
)
@@ -5336,10 +5342,12 @@ int main() {
53365342
expressions[758] = BinaryenPush(the_module, expressions[757]);
53375343
expressions[759] = BinaryenPop(the_module, 5);
53385344
expressions[760] = BinaryenPush(the_module, expressions[759]);
5339-
expressions[761] = BinaryenPop(the_module, 7);
5345+
expressions[761] = BinaryenPop(the_module, 6);
53405346
expressions[762] = BinaryenPush(the_module, expressions[761]);
5341-
expressions[763] = BinaryenNop(the_module);
5342-
expressions[764] = BinaryenUnreachable(the_module);
5347+
expressions[763] = BinaryenPop(the_module, 7);
5348+
expressions[764] = BinaryenPush(the_module, expressions[763]);
5349+
expressions[765] = BinaryenNop(the_module);
5350+
expressions[766] = BinaryenUnreachable(the_module);
53435351
BinaryenExpressionGetId(expressions[30]);
53445352
BinaryenExpressionGetType(expressions[30]);
53455353
BinaryenUnaryGetOp(expressions[30]);
@@ -5350,26 +5358,26 @@ getExpressionInfo={"id":15,"type":3,"op":6}
53505358
(f32.const -33.61199951171875)
53515359
)
53525360

5353-
expressions[765] = BinaryenConst(the_module, BinaryenLiteralInt32(5));
5354-
BinaryenExpressionGetId(expressions[765]);
5355-
BinaryenExpressionGetType(expressions[765]);
5356-
BinaryenConstGetValueI32(expressions[765]);
5357-
getExpressionInfo(i32.const)={"id":14,"type":1,"value":5}
5358-
expressions[766] = BinaryenConst(the_module, BinaryenLiteralInt64(30064771078));
5359-
BinaryenExpressionGetId(expressions[766]);
5360-
BinaryenExpressionGetType(expressions[766]);
5361-
BinaryenConstGetValueI64Low(expressions[766]);
5362-
BinaryenConstGetValueI64High(expressions[766]);
5363-
getExpressionInfo(i64.const)={"id":14,"type":2,"value":{"low":6,"high":7}}
5364-
expressions[767] = BinaryenConst(the_module, BinaryenLiteralFloat32(8.5));
5361+
expressions[767] = BinaryenConst(the_module, BinaryenLiteralInt32(5));
53655362
BinaryenExpressionGetId(expressions[767]);
53665363
BinaryenExpressionGetType(expressions[767]);
5367-
BinaryenConstGetValueF32(expressions[767]);
5368-
getExpressionInfo(f32.const)={"id":14,"type":3,"value":8.5}
5369-
expressions[768] = BinaryenConst(the_module, BinaryenLiteralFloat64(9.5));
5364+
BinaryenConstGetValueI32(expressions[767]);
5365+
getExpressionInfo(i32.const)={"id":14,"type":1,"value":5}
5366+
expressions[768] = BinaryenConst(the_module, BinaryenLiteralInt64(30064771078));
53705367
BinaryenExpressionGetId(expressions[768]);
53715368
BinaryenExpressionGetType(expressions[768]);
5372-
BinaryenConstGetValueF64(expressions[768]);
5369+
BinaryenConstGetValueI64Low(expressions[768]);
5370+
BinaryenConstGetValueI64High(expressions[768]);
5371+
getExpressionInfo(i64.const)={"id":14,"type":2,"value":{"low":6,"high":7}}
5372+
expressions[769] = BinaryenConst(the_module, BinaryenLiteralFloat32(8.5));
5373+
BinaryenExpressionGetId(expressions[769]);
5374+
BinaryenExpressionGetType(expressions[769]);
5375+
BinaryenConstGetValueF32(expressions[769]);
5376+
getExpressionInfo(f32.const)={"id":14,"type":3,"value":8.5}
5377+
expressions[770] = BinaryenConst(the_module, BinaryenLiteralFloat64(9.5));
5378+
BinaryenExpressionGetId(expressions[770]);
5379+
BinaryenExpressionGetType(expressions[770]);
5380+
BinaryenConstGetValueF64(expressions[770]);
53735381
getExpressionInfo(f64.const)={"id":14,"type":4,"value":9.5}
53745382
{
53755383
BinaryenExpressionRef children[] = { expressions[24], expressions[26], expressions[28], expressions[30], expressions[32],
@@ -5419,25 +5427,25 @@ getExpressionInfo(f64.const)={"id":14,"type":4,"value":9.5}
54195427
expressions[705], expressions[707], expressions[709], expressions[710], expressions[711], expressions[712],
54205428
expressions[714], expressions[719], expressions[725], expressions[736], expressions[740], expressions[745],
54215429
expressions[749], expressions[750], expressions[752], expressions[754], expressions[756], expressions[758],
5422-
expressions[760], expressions[762], expressions[763], expressions[764] };
5423-
expressions[769] = BinaryenBlock(the_module, "the-value", children, 285, 0);
5430+
expressions[760], expressions[762], expressions[764], expressions[765], expressions[766] };
5431+
expressions[771] = BinaryenBlock(the_module, "the-value", children, 286, 0);
54245432
}
5425-
expressions[770] = BinaryenDrop(the_module, expressions[769]);
5433+
expressions[772] = BinaryenDrop(the_module, expressions[771]);
54265434
{
5427-
BinaryenExpressionRef children[] = { expressions[770] };
5428-
expressions[771] = BinaryenBlock(the_module, "the-nothing", children, 1, 0);
5435+
BinaryenExpressionRef children[] = { expressions[772] };
5436+
expressions[773] = BinaryenBlock(the_module, "the-nothing", children, 1, 0);
54295437
}
5430-
expressions[772] = BinaryenConst(the_module, BinaryenLiteralInt32(42));
5438+
expressions[774] = BinaryenConst(the_module, BinaryenLiteralInt32(42));
54315439
{
5432-
BinaryenExpressionRef children[] = { expressions[771], expressions[772] };
5433-
expressions[773] = BinaryenBlock(the_module, "the-body", children, 2, 0);
5440+
BinaryenExpressionRef children[] = { expressions[773], expressions[774] };
5441+
expressions[775] = BinaryenBlock(the_module, "the-body", children, 2, 0);
54345442
}
54355443
{
54365444
BinaryenType varTypes[] = { 1, 7 };
5437-
functions[0] = BinaryenAddFunction(the_module, "kitchen()sinker", functionTypes[1], varTypes, 2, expressions[773]);
5445+
functions[0] = BinaryenAddFunction(the_module, "kitchen()sinker", functionTypes[1], varTypes, 2, expressions[775]);
54385446
}
5439-
expressions[774] = BinaryenConst(the_module, BinaryenLiteralInt32(1));
5440-
globals[0] = BinaryenAddGlobal(the_module, "a-global", 1, 0, expressions[774]);
5447+
expressions[776] = BinaryenConst(the_module, BinaryenLiteralInt32(1));
5448+
globals[0] = BinaryenAddGlobal(the_module, "a-global", 1, 0, expressions[776]);
54415449
{
54425450
BinaryenType paramTypes[] = { 1, 4 };
54435451
functionTypes[2] = BinaryenAddFunctionType(the_module, "fiF", 3, paramTypes, 2);
@@ -5467,24 +5475,24 @@ getExpressionInfo(f64.const)={"id":14,"type":4,"value":9.5}
54675475
const char* funcNames[] = { "kitchen()sinker" };
54685476
BinaryenSetFunctionTable(the_module, 1, 4294967295, funcNames, 1);
54695477
}
5470-
expressions[775] = BinaryenConst(the_module, BinaryenLiteralInt32(10));
5478+
expressions[777] = BinaryenConst(the_module, BinaryenLiteralInt32(10));
54715479
{
54725480
const char segment0[] = { 104, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100 };
54735481
const char segment1[] = { 73, 32, 97, 109, 32, 112, 97, 115, 115, 105, 118, 101 };
54745482
const char* segments[] = { segment0, segment1 };
54755483
int8_t segmentPassive[] = { 0, 1 };
5476-
BinaryenExpressionRef segmentOffsets[] = { expressions[775], expressions[0] };
5484+
BinaryenExpressionRef segmentOffsets[] = { expressions[777], expressions[0] };
54775485
BinaryenIndex segmentSizes[] = { 12, 12 };
54785486
BinaryenSetMemory(the_module, 1, 256, "mem", segments, segmentPassive, segmentOffsets, segmentSizes, 2, 1);
54795487
}
54805488
{
54815489
BinaryenType paramTypes[] = { 0 };
54825490
functionTypes[3] = BinaryenAddFunctionType(the_module, "v", 0, paramTypes, 0);
54835491
}
5484-
expressions[776] = BinaryenNop(the_module);
5492+
expressions[778] = BinaryenNop(the_module);
54855493
{
54865494
BinaryenType varTypes[] = { 0 };
5487-
functions[1] = BinaryenAddFunction(the_module, "starter", functionTypes[3], varTypes, 0, expressions[776]);
5495+
functions[1] = BinaryenAddFunction(the_module, "starter", functionTypes[3], varTypes, 0, expressions[778]);
54885496
}
54895497
BinaryenSetStart(the_module, functions[1]);
54905498
{
@@ -7082,6 +7090,9 @@ getExpressionInfo(f64.const)={"id":14,"type":4,"value":9.5}
70827090
(push
70837091
(v128.pop)
70847092
)
7093+
(push
7094+
(anyref.pop)
7095+
)
70857096
(push
70867097
(exnref.pop)
70877098
)
@@ -8687,6 +8698,9 @@ getExpressionInfo(f64.const)={"id":14,"type":4,"value":9.5}
86878698
(push
86888699
(v128.pop)
86898700
)
8701+
(push
8702+
(anyref.pop)
8703+
)
86908704
(push
86918705
(exnref.pop)
86928706
)

test/binaryen.js/push-pop.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ var func = module.addFunction("func", v, [],
2727
module.push(module.f32.pop()),
2828
module.push(module.f64.pop()),
2929
module.push(module.v128.pop()),
30+
module.push(module.anyref.pop()),
3031
module.push(module.exnref.pop())
3132
]
3233
)
@@ -40,5 +41,6 @@ console.log("getExpressionInfo(i64.pop) = " + stringify(module.i64.pop()));
4041
console.log("getExpressionInfo(f32.pop) = " + stringify(module.f32.pop()));
4142
console.log("getExpressionInfo(f64.pop) = " + stringify(module.f64.pop()));
4243
console.log("getExpressionInfo(v128.pop) = " + stringify(module.v128.pop()));
44+
console.log("getExpressionInfo(anyref.pop) = " + stringify(module.anyref.pop()));
4345
console.log("getExpressionInfo(exnref.pop) = " + stringify(module.exnref.pop()));
4446
console.log("getExpressionInfo(push) = " + stringify(module.push(module.i32.const(0))));

test/binaryen.js/push-pop.js.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
(push
1717
(v128.pop)
1818
)
19+
(push
20+
(anyref.pop)
21+
)
1922
(push
2023
(exnref.pop)
2124
)
@@ -27,5 +30,6 @@ getExpressionInfo(i64.pop) = {"id":39,"type":2}
2730
getExpressionInfo(f32.pop) = {"id":39,"type":3}
2831
getExpressionInfo(f64.pop) = {"id":39,"type":4}
2932
getExpressionInfo(v128.pop) = {"id":39,"type":5}
33+
getExpressionInfo(anyref.pop) = {"id":39,"type":6}
3034
getExpressionInfo(exnref.pop) = {"id":39,"type":7}
3135
getExpressionInfo(push) = {"id":38}

test/example/c-api-kitchen-sink.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,13 @@ void test_core() {
593593
BinaryenAtomicWait(module, temp6, temp6, temp16, BinaryenTypeInt32())),
594594
BinaryenDrop(module, BinaryenAtomicNotify(module, temp6, temp6)),
595595
BinaryenAtomicFence(module),
596+
// Push and pop
597+
BinaryenPush(module, BinaryenPop(module, BinaryenTypeInt32())),
598+
BinaryenPush(module, BinaryenPop(module, BinaryenTypeInt64())),
599+
BinaryenPush(module, BinaryenPop(module, BinaryenTypeFloat32())),
600+
BinaryenPush(module, BinaryenPop(module, BinaryenTypeFloat64())),
601+
BinaryenPush(module, BinaryenPop(module, BinaryenTypeAnyref())),
602+
BinaryenPush(module, BinaryenPop(module, BinaryenTypeExnref())),
596603

597604
// TODO: Host
598605
BinaryenNop(module),

0 commit comments

Comments
 (0)