diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/binaryen-c.cpp | 54 | ||||
-rw-r--r-- | src/binaryen-c.h | 12 | ||||
-rw-r--r-- | src/js/binaryen.js-post.js | 19 |
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') { |