summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDaniel Wirtz <dcode@dcode.io>2017-11-15 18:55:50 +0100
committerAlon Zakai <alonzakai@gmail.com>2017-11-15 09:55:50 -0800
commitdc0cd445c08ede80fa2b1747cffcd2254c97a0f6 (patch)
treedea167a459ceab94c64aa947eae7a2c90db12552 /src
parent4deed1eb310993391fa0e7e06a18cf2303674f98 (diff)
downloadbinaryen-dc0cd445c08ede80fa2b1747cffcd2254c97a0f6.tar.gz
binaryen-dc0cd445c08ede80fa2b1747cffcd2254c97a0f6.tar.bz2
binaryen-dc0cd445c08ede80fa2b1747cffcd2254c97a0f6.zip
Add const expression utilities to binaryen-c/.js (#1288)
Diffstat (limited to 'src')
-rw-r--r--src/binaryen-c.cpp54
-rw-r--r--src/binaryen-c.h12
-rw-r--r--src/js/binaryen.js-post.js19
3 files changed, 85 insertions, 0 deletions
diff --git a/src/binaryen-c.cpp b/src/binaryen-c.cpp
index 9883aef71..f4084eb82 100644
--- a/src/binaryen-c.cpp
+++ b/src/binaryen-c.cpp
@@ -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
diff --git a/src/binaryen-c.h b/src/binaryen-c.h
index c136b5f70..88fe8904c 100644
--- a/src/binaryen-c.h
+++ b/src/binaryen-c.h
@@ -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
diff --git a/src/js/binaryen.js-post.js b/src/js/binaryen.js-post.js
index a29a035c6..352da1c0a 100644
--- a/src/js/binaryen.js-post.js
+++ b/src/js/binaryen.js-post.js
@@ -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') {