Skip to content

Commit dc0cd44

Browse files
dcodeIOkripken
authored andcommitted
Add const expression utilities to binaryen-c/.js (#1288)
1 parent 4deed1e commit dc0cd44

File tree

8 files changed

+1022
-908
lines changed

8 files changed

+1022
-908
lines changed

bin/binaryen.js

Lines changed: 540 additions & 537 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bin/wasm.js

Lines changed: 357 additions & 357 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build-js.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,11 @@ export_function "_BinaryenAtomicWake"
350350
export_function "_BinaryenExpressionGetId"
351351
export_function "_BinaryenExpressionGetType"
352352
export_function "_BinaryenExpressionPrint"
353+
export_function "_BinaryenConstGetValueI32"
354+
export_function "_BinaryenConstGetValueI64Low"
355+
export_function "_BinaryenConstGetValueI64High"
356+
export_function "_BinaryenConstGetValueF32"
357+
export_function "_BinaryenConstGetValueF64"
353358
export_function "_BinaryenAddFunction"
354359
export_function "_BinaryenAddGlobal"
355360
export_function "_BinaryenAddImport"

src/binaryen-c.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,60 @@ void BinaryenExpressionPrint(BinaryenExpressionRef expr) {
797797
WasmPrinter::printExpression((Expression*)expr, std::cout);
798798
std::cout << '\n';
799799
}
800+
int32_t BinaryenConstGetValueI32(BinaryenExpressionRef expr) {
801+
if (tracing) {
802+
std::cout << " BinaryenConstGetValueI32(expressions[" << expressions[expr] << "]);\n";
803+
}
804+
805+
auto* expression = (Expression*)expr;
806+
assert(expression->is<Const>());
807+
return static_cast<Const*>(expression)->value.geti32();
808+
}
809+
int64_t BinaryenConstGetValueI64(BinaryenExpressionRef expr) {
810+
if (tracing) {
811+
std::cout << " BinaryenConstGetValueI64(expressions[" << expressions[expr] << "]);\n";
812+
}
813+
814+
auto* expression = (Expression*)expr;
815+
assert(expression->is<Const>());
816+
return static_cast<Const*>(expression)->value.geti64();
817+
}
818+
int32_t BinaryenConstGetValueI64Low(BinaryenExpressionRef expr) {
819+
if (tracing) {
820+
std::cout << " BinaryenConstGetValueI64Low(expressions[" << expressions[expr] << "]);\n";
821+
}
822+
823+
auto* expression = (Expression*)expr;
824+
assert(expression->is<Const>());
825+
return (int32_t)(static_cast<Const*>(expression)->value.geti64() & 0xffffffff);
826+
}
827+
int32_t BinaryenConstGetValueI64High(BinaryenExpressionRef expr) {
828+
if (tracing) {
829+
std::cout << " BinaryenConstGetValueI64High(expressions[" << expressions[expr] << "]);\n";
830+
}
831+
832+
auto* expression = (Expression*)expr;
833+
assert(expression->is<Const>());
834+
return (int32_t)(static_cast<Const*>(expression)->value.geti64() >> 32);
835+
}
836+
float BinaryenConstGetValueF32(BinaryenExpressionRef expr) {
837+
if (tracing) {
838+
std::cout << " BinaryenConstGetValueF32(expressions[" << expressions[expr] << "]);\n";
839+
}
840+
841+
auto* expression = (Expression*)expr;
842+
assert(expression->is<Const>());
843+
return static_cast<Const*>(expression)->value.getf32();
844+
}
845+
double BinaryenConstGetValueF64(BinaryenExpressionRef expr) {
846+
if (tracing) {
847+
std::cout << " BinaryenConstGetValueF64(expressions[" << expressions[expr] << "]);\n";
848+
}
849+
850+
auto* expression = (Expression*)expr;
851+
assert(expression->is<Const>());
852+
return static_cast<Const*>(expression)->value.getf64();
853+
}
800854

801855
// Functions
802856

src/binaryen-c.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,18 @@ BinaryenExpressionId BinaryenExpressionGetId(BinaryenExpressionRef expr);
371371
BinaryenType BinaryenExpressionGetType(BinaryenExpressionRef expr);
372372
// Print an expression to stdout. Useful for debugging.
373373
void BinaryenExpressionPrint(BinaryenExpressionRef expr);
374+
// Gets the 32-bit integer value of the specified `Const` expression.
375+
int32_t BinaryenConstGetValueI32(BinaryenExpressionRef expr);
376+
// Gets the 64-bit integer value of the specified `Const` expression.
377+
int64_t BinaryenConstGetValueI64(BinaryenExpressionRef expr);
378+
// Gets the low 32-bits of a 64-bit integer value of the specified `Const` expression. Useful where I64 returning exports are illegal, i.e. binaryen.js.
379+
int32_t BinaryenConstGetValueI64Low(BinaryenExpressionRef expr);
380+
// Gets the high 32-bits of a 64-bit integer value of the specified `Const` expression. Useful where I64 returning exports are illegal, i.e. binaryen.js.
381+
int32_t BinaryenConstGetValueI64High(BinaryenExpressionRef expr);
382+
// Gets the 32-bit float value of the specified `Const` expression.
383+
float BinaryenConstGetValueF32(BinaryenExpressionRef expr);
384+
// Gets the 64-bit float value of the specified `Const` expression.
385+
double BinaryenConstGetValueF64(BinaryenExpressionRef expr);
374386

375387
// Functions
376388

src/js/binaryen.js-post.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,25 @@
11111111
return Module['_BinaryenExpressionGetType'](expr);
11121112
};
11131113

1114+
Module['getConstValueI32'] = function(expr) {
1115+
return Module['_BinaryenConstGetValueI32'](expr);
1116+
};
1117+
1118+
Module['getConstValueI64'] = function(expr) {
1119+
return {
1120+
'low': Module['_BinaryenConstGetValueI64Low'](expr),
1121+
'high': Module['_BinaryenConstGetValueI64High'](expr)
1122+
};
1123+
};
1124+
1125+
Module['getConstValueF32'] = function(expr) {
1126+
return Module['_BinaryenConstGetValueF32'](expr);
1127+
};
1128+
1129+
Module['getConstValueF64'] = function(expr) {
1130+
return Module['_BinaryenConstGetValueF64'](expr);
1131+
};
1132+
11141133
// emit text of an expression or a module
11151134
Module['emitText'] = function(expr) {
11161135
if (typeof expr === 'object') {

test/binaryen.js/kitchen-sink.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ function test_core() {
213213
console.log("getExpressionId=" + Binaryen.getExpressionId(valueList[3]));
214214
console.log("getExpressionType=" + Binaryen.getExpressionType(valueList[3]));
215215
console.log(Binaryen.emitText(valueList[3])); // test printing a standalone expression
216+
console.log(Binaryen.getConstValueI32(module.i32.const(5)));
217+
console.log(Binaryen.getConstValueI64(module.i64.const(6, 7)));
218+
console.log(Binaryen.getConstValueF32(module.f32.const(8.5)));
219+
console.log(Binaryen.getConstValueF64(module.f64.const(9.5)));
216220

217221
// Make the main body of the function. and one block with a return value, one without
218222
var value = module.block("the-value", valueList);

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

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ getExpressionType=3
3737
(f32.const -33.61199951171875)
3838
)
3939

40+
5
41+
{ low: 6, high: 7 }
42+
8.5
43+
9.5
4044
(module
4145
(type $iiIfF (func (param i32 i64 f32 f64) (result i32)))
4246
(type $fiF (func (param i32 f64) (result f32)))
@@ -497,7 +501,7 @@ getExpressionType=3
497501
)
498502
(drop
499503
(i32.eqz
500-
(call_indirect $iiIfF
504+
(call_indirect (type $iiIfF)
501505
(i32.const 13)
502506
(i64.const 37)
503507
(f32.const 1.2999999523162842)
@@ -1393,23 +1397,36 @@ getExpressionType=3
13931397
(f32.const -33.61199951171875)
13941398
)
13951399

1400+
expressions[247] = BinaryenConst(the_module, BinaryenLiteralInt32(5));
1401+
BinaryenConstGetValueI32(expressions[247]);
1402+
5
1403+
expressions[248] = BinaryenConst(the_module, BinaryenLiteralInt64(30064771078));
1404+
BinaryenConstGetValueI64Low(expressions[248]);
1405+
BinaryenConstGetValueI64High(expressions[248]);
1406+
{ low: 6, high: 7 }
1407+
expressions[249] = BinaryenConst(the_module, BinaryenLiteralFloat32(8.5));
1408+
BinaryenConstGetValueF32(expressions[249]);
1409+
8.5
1410+
expressions[250] = BinaryenConst(the_module, BinaryenLiteralFloat64(9.5));
1411+
BinaryenConstGetValueF64(expressions[250]);
1412+
9.5
13961413
{
13971414
BinaryenExpressionRef children[] = { expressions[24], expressions[26], expressions[28], expressions[30], expressions[32], expressions[34], expressions[36], expressions[38], expressions[40], expressions[42], expressions[44], expressions[46], expressions[48], expressions[50], expressions[52], expressions[54], expressions[56], expressions[58], expressions[60], expressions[62], expressions[64], expressions[66], expressions[68], expressions[70], expressions[72], expressions[74], expressions[76], expressions[78], expressions[80], expressions[82], expressions[84], expressions[86], expressions[88], expressions[90], expressions[92], expressions[94], expressions[97], expressions[100], expressions[103], expressions[106], expressions[109], expressions[112], expressions[115], expressions[118], expressions[121], expressions[124], expressions[127], expressions[130], expressions[133], expressions[136], expressions[139], expressions[142], expressions[145], expressions[148], expressions[151], expressions[154], expressions[157], expressions[160], expressions[163], expressions[166], expressions[169], expressions[172], expressions[175], expressions[178], expressions[181], expressions[184], expressions[187], expressions[190], expressions[191], expressions[192], expressions[193], expressions[195], expressions[197], expressions[198], expressions[200], expressions[202], expressions[203], expressions[204], expressions[206], expressions[212], expressions[217], expressions[224], expressions[226], expressions[228], expressions[231], expressions[233], expressions[235], expressions[237], expressions[239], expressions[240], expressions[241], expressions[242], expressions[244], expressions[245], expressions[246] };
1398-
expressions[247] = BinaryenBlock(the_module, "the-value", children, 95, BinaryenUndefined());
1415+
expressions[251] = BinaryenBlock(the_module, "the-value", children, 95, BinaryenUndefined());
13991416
}
1400-
expressions[248] = BinaryenDrop(the_module, expressions[247]);
1417+
expressions[252] = BinaryenDrop(the_module, expressions[251]);
14011418
{
1402-
BinaryenExpressionRef children[] = { expressions[248] };
1403-
expressions[249] = BinaryenBlock(the_module, "the-nothing", children, 1, BinaryenUndefined());
1419+
BinaryenExpressionRef children[] = { expressions[252] };
1420+
expressions[253] = BinaryenBlock(the_module, "the-nothing", children, 1, BinaryenUndefined());
14041421
}
1405-
expressions[250] = BinaryenConst(the_module, BinaryenLiteralInt32(42));
1422+
expressions[254] = BinaryenConst(the_module, BinaryenLiteralInt32(42));
14061423
{
1407-
BinaryenExpressionRef children[] = { expressions[249], expressions[250] };
1408-
expressions[251] = BinaryenBlock(the_module, "the-body", children, 2, BinaryenUndefined());
1424+
BinaryenExpressionRef children[] = { expressions[253], expressions[254] };
1425+
expressions[255] = BinaryenBlock(the_module, "the-body", children, 2, BinaryenUndefined());
14091426
}
14101427
{
14111428
BinaryenType varTypes[] = { 1 };
1412-
functions[0] = BinaryenAddFunction(the_module, "kitchen()sinker", functionTypes[0], varTypes, 1, expressions[251]);
1429+
functions[0] = BinaryenAddFunction(the_module, "kitchen()sinker", functionTypes[0], varTypes, 1, expressions[255]);
14131430
}
14141431
{
14151432
BinaryenType paramTypes[] = { 1, 4 };
@@ -1421,22 +1438,22 @@ getExpressionType=3
14211438
BinaryenFunctionRef funcs[] = { functions[0] };
14221439
BinaryenSetFunctionTable(the_module, funcs, 1);
14231440
}
1424-
expressions[252] = BinaryenConst(the_module, BinaryenLiteralInt32(10));
1441+
expressions[256] = BinaryenConst(the_module, BinaryenLiteralInt32(10));
14251442
{
14261443
const char segment0[] = { 104, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100 };
14271444
const char* segments[] = { segment0 };
1428-
BinaryenExpressionRef segmentOffsets[] = { expressions[252] };
1445+
BinaryenExpressionRef segmentOffsets[] = { expressions[256] };
14291446
BinaryenIndex segmentSizes[] = { 12 };
14301447
BinaryenSetMemory(the_module, 1, 256, "mem", segments, segmentOffsets, segmentSizes, 1);
14311448
}
14321449
{
14331450
BinaryenType paramTypes[] = { 0 };
14341451
functionTypes[2] = BinaryenAddFunctionType(the_module, "v", 0, paramTypes, 0);
14351452
}
1436-
expressions[253] = BinaryenNop(the_module);
1453+
expressions[257] = BinaryenNop(the_module);
14371454
{
14381455
BinaryenType varTypes[] = { 0 };
1439-
functions[1] = BinaryenAddFunction(the_module, "starter", functionTypes[2], varTypes, 0, expressions[253]);
1456+
functions[1] = BinaryenAddFunction(the_module, "starter", functionTypes[2], varTypes, 0, expressions[257]);
14401457
}
14411458
BinaryenSetStart(the_module, functions[1]);
14421459
{
@@ -1906,7 +1923,7 @@ getExpressionType=3
19061923
)
19071924
(drop
19081925
(i32.eqz
1909-
(call_indirect $iiIfF
1926+
(call_indirect (type $iiIfF)
19101927
(i32.const 13)
19111928
(i64.const 37)
19121929
(f32.const 1.2999999523162842)

0 commit comments

Comments
 (0)