Skip to content

Commit

Permalink
Add const expression utilities to binaryen-c/.js (WebAssembly#1288)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO authored and kripken committed Nov 15, 2017
1 parent 4deed1e commit dc0cd44
Show file tree
Hide file tree
Showing 8 changed files with 1,022 additions and 908 deletions.
1,077 changes: 540 additions & 537 deletions bin/binaryen.js

Large diffs are not rendered by default.

714 changes: 357 additions & 357 deletions bin/wasm.js

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions build-js.sh
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,11 @@ export_function "_BinaryenAtomicWake"
export_function "_BinaryenExpressionGetId"
export_function "_BinaryenExpressionGetType"
export_function "_BinaryenExpressionPrint"
export_function "_BinaryenConstGetValueI32"
export_function "_BinaryenConstGetValueI64Low"
export_function "_BinaryenConstGetValueI64High"
export_function "_BinaryenConstGetValueF32"
export_function "_BinaryenConstGetValueF64"
export_function "_BinaryenAddFunction"
export_function "_BinaryenAddGlobal"
export_function "_BinaryenAddImport"
Expand Down
54 changes: 54 additions & 0 deletions src/binaryen-c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,60 @@ void BinaryenExpressionPrint(BinaryenExpressionRef expr) {
WasmPrinter::printExpression((Expression*)expr, std::cout);
std::cout << '\n';
}
int32_t BinaryenConstGetValueI32(BinaryenExpressionRef expr) {
if (tracing) {
std::cout << " BinaryenConstGetValueI32(expressions[" << expressions[expr] << "]);\n";
}

auto* expression = (Expression*)expr;
assert(expression->is<Const>());
return static_cast<Const*>(expression)->value.geti32();
}
int64_t BinaryenConstGetValueI64(BinaryenExpressionRef expr) {
if (tracing) {
std::cout << " BinaryenConstGetValueI64(expressions[" << expressions[expr] << "]);\n";
}

auto* expression = (Expression*)expr;
assert(expression->is<Const>());
return static_cast<Const*>(expression)->value.geti64();
}
int32_t BinaryenConstGetValueI64Low(BinaryenExpressionRef expr) {
if (tracing) {
std::cout << " BinaryenConstGetValueI64Low(expressions[" << expressions[expr] << "]);\n";
}

auto* expression = (Expression*)expr;
assert(expression->is<Const>());
return (int32_t)(static_cast<Const*>(expression)->value.geti64() & 0xffffffff);
}
int32_t BinaryenConstGetValueI64High(BinaryenExpressionRef expr) {
if (tracing) {
std::cout << " BinaryenConstGetValueI64High(expressions[" << expressions[expr] << "]);\n";
}

auto* expression = (Expression*)expr;
assert(expression->is<Const>());
return (int32_t)(static_cast<Const*>(expression)->value.geti64() >> 32);
}
float BinaryenConstGetValueF32(BinaryenExpressionRef expr) {
if (tracing) {
std::cout << " BinaryenConstGetValueF32(expressions[" << expressions[expr] << "]);\n";
}

auto* expression = (Expression*)expr;
assert(expression->is<Const>());
return static_cast<Const*>(expression)->value.getf32();
}
double BinaryenConstGetValueF64(BinaryenExpressionRef expr) {
if (tracing) {
std::cout << " BinaryenConstGetValueF64(expressions[" << expressions[expr] << "]);\n";
}

auto* expression = (Expression*)expr;
assert(expression->is<Const>());
return static_cast<Const*>(expression)->value.getf64();
}

// Functions

Expand Down
12 changes: 12 additions & 0 deletions src/binaryen-c.h
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,18 @@ BinaryenExpressionId BinaryenExpressionGetId(BinaryenExpressionRef expr);
BinaryenType BinaryenExpressionGetType(BinaryenExpressionRef expr);
// Print an expression to stdout. Useful for debugging.
void BinaryenExpressionPrint(BinaryenExpressionRef expr);
// Gets the 32-bit integer value of the specified `Const` expression.
int32_t BinaryenConstGetValueI32(BinaryenExpressionRef expr);
// Gets the 64-bit integer value of the specified `Const` expression.
int64_t BinaryenConstGetValueI64(BinaryenExpressionRef expr);
// 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.
int32_t BinaryenConstGetValueI64Low(BinaryenExpressionRef expr);
// 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.
int32_t BinaryenConstGetValueI64High(BinaryenExpressionRef expr);
// Gets the 32-bit float value of the specified `Const` expression.
float BinaryenConstGetValueF32(BinaryenExpressionRef expr);
// Gets the 64-bit float value of the specified `Const` expression.
double BinaryenConstGetValueF64(BinaryenExpressionRef expr);

// Functions

Expand Down
19 changes: 19 additions & 0 deletions src/js/binaryen.js-post.js
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,25 @@
return Module['_BinaryenExpressionGetType'](expr);
};

Module['getConstValueI32'] = function(expr) {
return Module['_BinaryenConstGetValueI32'](expr);
};

Module['getConstValueI64'] = function(expr) {
return {
'low': Module['_BinaryenConstGetValueI64Low'](expr),
'high': Module['_BinaryenConstGetValueI64High'](expr)
};
};

Module['getConstValueF32'] = function(expr) {
return Module['_BinaryenConstGetValueF32'](expr);
};

Module['getConstValueF64'] = function(expr) {
return Module['_BinaryenConstGetValueF64'](expr);
};

// emit text of an expression or a module
Module['emitText'] = function(expr) {
if (typeof expr === 'object') {
Expand Down
4 changes: 4 additions & 0 deletions test/binaryen.js/kitchen-sink.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ function test_core() {
console.log("getExpressionId=" + Binaryen.getExpressionId(valueList[3]));
console.log("getExpressionType=" + Binaryen.getExpressionType(valueList[3]));
console.log(Binaryen.emitText(valueList[3])); // test printing a standalone expression
console.log(Binaryen.getConstValueI32(module.i32.const(5)));
console.log(Binaryen.getConstValueI64(module.i64.const(6, 7)));
console.log(Binaryen.getConstValueF32(module.f32.const(8.5)));
console.log(Binaryen.getConstValueF64(module.f64.const(9.5)));

// Make the main body of the function. and one block with a return value, one without
var value = module.block("the-value", valueList);
Expand Down
45 changes: 31 additions & 14 deletions test/binaryen.js/kitchen-sink.js.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ getExpressionType=3
(f32.const -33.61199951171875)
)

5
{ low: 6, high: 7 }
8.5
9.5
(module
(type $iiIfF (func (param i32 i64 f32 f64) (result i32)))
(type $fiF (func (param i32 f64) (result f32)))
Expand Down Expand Up @@ -497,7 +501,7 @@ getExpressionType=3
)
(drop
(i32.eqz
(call_indirect $iiIfF
(call_indirect (type $iiIfF)
(i32.const 13)
(i64.const 37)
(f32.const 1.2999999523162842)
Expand Down Expand Up @@ -1393,23 +1397,36 @@ getExpressionType=3
(f32.const -33.61199951171875)
)

expressions[247] = BinaryenConst(the_module, BinaryenLiteralInt32(5));
BinaryenConstGetValueI32(expressions[247]);
5
expressions[248] = BinaryenConst(the_module, BinaryenLiteralInt64(30064771078));
BinaryenConstGetValueI64Low(expressions[248]);
BinaryenConstGetValueI64High(expressions[248]);
{ low: 6, high: 7 }
expressions[249] = BinaryenConst(the_module, BinaryenLiteralFloat32(8.5));
BinaryenConstGetValueF32(expressions[249]);
8.5
expressions[250] = BinaryenConst(the_module, BinaryenLiteralFloat64(9.5));
BinaryenConstGetValueF64(expressions[250]);
9.5
{
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] };
expressions[247] = BinaryenBlock(the_module, "the-value", children, 95, BinaryenUndefined());
expressions[251] = BinaryenBlock(the_module, "the-value", children, 95, BinaryenUndefined());
}
expressions[248] = BinaryenDrop(the_module, expressions[247]);
expressions[252] = BinaryenDrop(the_module, expressions[251]);
{
BinaryenExpressionRef children[] = { expressions[248] };
expressions[249] = BinaryenBlock(the_module, "the-nothing", children, 1, BinaryenUndefined());
BinaryenExpressionRef children[] = { expressions[252] };
expressions[253] = BinaryenBlock(the_module, "the-nothing", children, 1, BinaryenUndefined());
}
expressions[250] = BinaryenConst(the_module, BinaryenLiteralInt32(42));
expressions[254] = BinaryenConst(the_module, BinaryenLiteralInt32(42));
{
BinaryenExpressionRef children[] = { expressions[249], expressions[250] };
expressions[251] = BinaryenBlock(the_module, "the-body", children, 2, BinaryenUndefined());
BinaryenExpressionRef children[] = { expressions[253], expressions[254] };
expressions[255] = BinaryenBlock(the_module, "the-body", children, 2, BinaryenUndefined());
}
{
BinaryenType varTypes[] = { 1 };
functions[0] = BinaryenAddFunction(the_module, "kitchen()sinker", functionTypes[0], varTypes, 1, expressions[251]);
functions[0] = BinaryenAddFunction(the_module, "kitchen()sinker", functionTypes[0], varTypes, 1, expressions[255]);
}
{
BinaryenType paramTypes[] = { 1, 4 };
Expand All @@ -1421,22 +1438,22 @@ getExpressionType=3
BinaryenFunctionRef funcs[] = { functions[0] };
BinaryenSetFunctionTable(the_module, funcs, 1);
}
expressions[252] = BinaryenConst(the_module, BinaryenLiteralInt32(10));
expressions[256] = BinaryenConst(the_module, BinaryenLiteralInt32(10));
{
const char segment0[] = { 104, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100 };
const char* segments[] = { segment0 };
BinaryenExpressionRef segmentOffsets[] = { expressions[252] };
BinaryenExpressionRef segmentOffsets[] = { expressions[256] };
BinaryenIndex segmentSizes[] = { 12 };
BinaryenSetMemory(the_module, 1, 256, "mem", segments, segmentOffsets, segmentSizes, 1);
}
{
BinaryenType paramTypes[] = { 0 };
functionTypes[2] = BinaryenAddFunctionType(the_module, "v", 0, paramTypes, 0);
}
expressions[253] = BinaryenNop(the_module);
expressions[257] = BinaryenNop(the_module);
{
BinaryenType varTypes[] = { 0 };
functions[1] = BinaryenAddFunction(the_module, "starter", functionTypes[2], varTypes, 0, expressions[253]);
functions[1] = BinaryenAddFunction(the_module, "starter", functionTypes[2], varTypes, 0, expressions[257]);
}
BinaryenSetStart(the_module, functions[1]);
{
Expand Down Expand Up @@ -1906,7 +1923,7 @@ getExpressionType=3
)
(drop
(i32.eqz
(call_indirect $iiIfF
(call_indirect (type $iiIfF)
(i32.const 13)
(i64.const 37)
(f32.const 1.2999999523162842)
Expand Down

0 comments on commit dc0cd44

Please sign in to comment.