summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDaniel Wirtz <dcode@dcode.io>2017-12-20 20:46:14 +0100
committerAlon Zakai <alonzakai@gmail.com>2017-12-20 11:46:14 -0800
commitf4b7df08b27a2fe000424524957cf9cf10041193 (patch)
tree1123435d2ab3c858f9e0636ee930a06f12889406 /src
parenta0de358f7d73222501775e5f21ed4ec9838311cb (diff)
downloadbinaryen-f4b7df08b27a2fe000424524957cf9cf10041193.tar.gz
binaryen-f4b7df08b27a2fe000424524957cf9cf10041193.tar.bz2
binaryen-f4b7df08b27a2fe000424524957cf9cf10041193.zip
Add getters for various specific expression fields to C/JS (#1332)
Diffstat (limited to 'src')
-rw-r--r--src/binaryen-c.cpp980
-rw-r--r--src/binaryen-c.h253
-rw-r--r--src/js/binaryen.js-post.js363
3 files changed, 1503 insertions, 93 deletions
diff --git a/src/binaryen-c.cpp b/src/binaryen-c.cpp
index bd7528fb9..c91825882 100644
--- a/src/binaryen-c.cpp
+++ b/src/binaryen-c.cpp
@@ -82,6 +82,8 @@ void traceNameOrNULL(const char *name) {
std::map<BinaryenFunctionTypeRef, size_t> functionTypes;
std::map<BinaryenExpressionRef, size_t> expressions;
std::map<BinaryenFunctionRef, size_t> functions;
+std::map<BinaryenImportRef, size_t> imports;
+std::map<BinaryenExportRef, size_t> exports;
std::map<RelooperBlockRef, size_t> relooperBlocks;
size_t noteExpression(BinaryenExpressionRef expression) {
@@ -99,12 +101,20 @@ extern "C" {
// Core types
-BinaryenType BinaryenNone(void) { return none; }
-BinaryenType BinaryenInt32(void) { return i32; }
-BinaryenType BinaryenInt64(void) { return i64; }
-BinaryenType BinaryenFloat32(void) { return f32; }
-BinaryenType BinaryenFloat64(void) { return f64; }
-BinaryenType BinaryenUndefined(void) { return uint32_t(-1); }
+BinaryenType BinaryenTypeNone(void) { return none; }
+BinaryenType BinaryenTypeInt32(void) { return i32; }
+BinaryenType BinaryenTypeInt64(void) { return i64; }
+BinaryenType BinaryenTypeFloat32(void) { return f32; }
+BinaryenType BinaryenTypeFloat64(void) { return f64; }
+BinaryenType BinaryenTypeUnreachable(void) { return unreachable; }
+BinaryenType BinaryenTypeAuto(void) { return uint32_t(-1); }
+
+WASM_DEPRECATED BinaryenType BinaryenNone(void) { return none; }
+WASM_DEPRECATED BinaryenType BinaryenInt32(void) { return i32; }
+WASM_DEPRECATED BinaryenType BinaryenInt64(void) { return i64; }
+WASM_DEPRECATED BinaryenType BinaryenFloat32(void) { return f32; }
+WASM_DEPRECATED BinaryenType BinaryenFloat64(void) { return f64; }
+WASM_DEPRECATED BinaryenType BinaryenUndefined(void) { return uint32_t(-1); }
// Expression ids
@@ -161,6 +171,8 @@ void BinaryenModuleDispose(BinaryenModuleRef module) {
std::cout << " functionTypes.clear();\n";
std::cout << " expressions.clear();\n";
std::cout << " functions.clear();\n";
+ std::cout << " imports.clear();\n";
+ std::cout << " exports.clear();\n";
std::cout << " relooperBlocks.clear();\n";
functionTypes.clear();
expressions.clear();
@@ -359,7 +371,7 @@ BinaryenExpressionRef BinaryenBlock(BinaryenModuleRef module, const char* name,
for (BinaryenIndex i = 0; i < numChildren; i++) {
ret->list.push_back((Expression*)children[i]);
}
- if (type != BinaryenUndefined()) ret->finalize(WasmType(type));
+ if (type != BinaryenTypeAuto()) ret->finalize(WasmType(type));
else ret->finalize();
if (tracing) {
@@ -375,7 +387,7 @@ BinaryenExpressionRef BinaryenBlock(BinaryenModuleRef module, const char* name,
std::cout << " expressions[" << id << "] = BinaryenBlock(the_module, ";
traceNameOrNULL(name);
std::cout << ", children, " << numChildren << ", ";
- if (type == BinaryenUndefined()) std::cout << "BinaryenUndefined()";
+ if (type == BinaryenTypeAuto()) std::cout << "BinaryenTypeAuto()";
else std::cout << type;
std::cout << ");\n";
std::cout << " }\n";
@@ -706,11 +718,22 @@ BinaryenExpressionRef BinaryenReturn(BinaryenModuleRef module, BinaryenExpressio
return static_cast<Expression*>(ret);
}
BinaryenExpressionRef BinaryenHost(BinaryenModuleRef module, BinaryenOp op, const char* name, BinaryenExpressionRef* operands, BinaryenIndex numOperands) {
+ auto* ret = ((Module*)module)->allocator.alloc<Host>();
+
if (tracing) {
- std::cout << " TODO: host...\n";
+ std::cout << " {\n";
+ std::cout << " BinaryenExpressionRef operands[] = { ";
+ for (BinaryenIndex i = 0; i < numOperands; i++) {
+ if (i > 0) std::cout << ", ";
+ std::cout << "expressions[" << expressions[operands[i]] << "]";
+ }
+ if (numOperands == 0) std::cout << "0"; // ensure the array is not empty, otherwise a compiler error on VS
+ std::cout << " };\n";
+ auto id = noteExpression(ret);
+ std::cout << " expressions[" << id << "] = BinaryenHost(the_module, " << op << ", \"" << name << "\", operands, " << numOperands << ");\n";
+ std::cout << " }\n";
}
- auto* ret = ((Module*)module)->allocator.alloc<Host>();
ret->op = HostOp(op);
if (name) ret->nameOperand = name;
for (BinaryenIndex i = 0; i < numOperands; i++) {
@@ -824,6 +847,463 @@ void BinaryenExpressionPrint(BinaryenExpressionRef expr) {
WasmPrinter::printExpression((Expression*)expr, std::cout);
std::cout << '\n';
}
+
+// Specific expression utility
+
+// Block
+const char* BinaryenBlockGetName(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenBlockGetName(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Block>());
+ return static_cast<Block*>(expression)->name.c_str();
+}
+BinaryenIndex BinaryenBlockGetNumChildren(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenBlockGetNumChildren(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Block>());
+ return static_cast<Block*>(expression)->list.size();
+}
+BinaryenExpressionRef BinaryenBlockGetChild(BinaryenExpressionRef expr, BinaryenIndex index) {
+ if (tracing) {
+ std::cout << " BinaryenBlockGetChild(expressions[" << expressions[expr] << "], " << index << ");\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Block>());
+ assert(index < static_cast<Block*>(expression)->list.size());
+ return static_cast<Block*>(expression)->list[index];
+}
+// If
+BinaryenExpressionRef BinaryenIfGetCondition(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenIfGetCondition(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<If>());
+ return static_cast<If*>(expression)->condition;
+}
+BinaryenExpressionRef BinaryenIfGetIfTrue(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenIfGetIfTrue(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<If>());
+ return static_cast<If*>(expression)->ifTrue;
+}
+BinaryenExpressionRef BinaryenIfGetIfFalse(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenIfGetIfFalse(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<If>());
+ return static_cast<If*>(expression)->ifFalse;
+}
+// Loop
+const char* BinaryenLoopGetName(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenLoopGetName(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Loop>());
+ return static_cast<Loop*>(expression)->name.c_str();
+}
+BinaryenExpressionRef BinaryenLoopGetBody(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenLoopGetBody(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Loop>());
+ return static_cast<Loop*>(expression)->body;
+}
+// Break
+const char* BinaryenBreakGetName(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenBreakGetName(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Break>());
+ return static_cast<Break*>(expression)->name.c_str();
+}
+BinaryenExpressionRef BinaryenBreakGetCondition(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenBreakGetCondition(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Break>());
+ return static_cast<Break*>(expression)->condition;
+}
+BinaryenExpressionRef BinaryenBreakGetValue(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenBreakGetValue(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Break>());
+ return static_cast<Break*>(expression)->value;
+}
+// Switch
+BinaryenIndex BinaryenSwitchGetNumNames(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenSwitchGetNumNames(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Switch>());
+ return static_cast<Switch*>(expression)->targets.size();
+}
+const char* BinaryenSwitchGetName(BinaryenExpressionRef expr, BinaryenIndex index) {
+ if (tracing) {
+ std::cout << " BinaryenSwitchGetName(expressions[" << expressions[expr] << "], " << index << ");\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Switch>());
+ assert(index < static_cast<Switch*>(expression)->targets.size());
+ return static_cast<Switch*>(expression)->targets[index].c_str();
+}
+const char* BinaryenSwitchGetDefaultName(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenSwitchGetDefaultName(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Switch>());
+ return static_cast<Switch*>(expression)->default_.c_str();
+}
+BinaryenExpressionRef BinaryenSwitchGetCondition(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenSwitchGetCondition(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Switch>());
+ return static_cast<Switch*>(expression)->condition;
+}
+BinaryenExpressionRef BinaryenSwitchGetValue(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenSwitchGetValue(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Switch>());
+ return static_cast<Switch*>(expression)->value;
+}
+// Call
+const char* BinaryenCallGetTarget(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenCallGetTarget(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Call>());
+ return static_cast<Call*>(expression)->target.c_str();
+}
+BinaryenIndex BinaryenCallGetNumOperands(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenCallGetNumOperands(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Call>());
+ return static_cast<Call*>(expression)->operands.size();
+}
+BinaryenExpressionRef BinaryenCallGetOperand(BinaryenExpressionRef expr, BinaryenIndex index) {
+ if (tracing) {
+ std::cout << " BinaryenCallGetOperand(expressions[" << expressions[expr] << "], " << index << ");\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Call>());
+ assert(index < static_cast<Call*>(expression)->operands.size());
+ return static_cast<Call*>(expression)->operands[index];
+}
+// CallImport
+const char* BinaryenCallImportGetTarget(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenCallImportGetTarget(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<CallImport>());
+ return static_cast<CallImport*>(expression)->target.c_str();
+}
+BinaryenIndex BinaryenCallImportGetNumOperands(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenCallImportGetNumOperands(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<CallImport>());
+ return static_cast<CallImport*>(expression)->operands.size();
+}
+BinaryenExpressionRef BinaryenCallImportGetOperand(BinaryenExpressionRef expr, BinaryenIndex index) {
+ if (tracing) {
+ std::cout << " BinaryenCallImportGetOperand(expressions[" << expressions[expr] << "], " << index << ");\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<CallImport>());
+ assert(index < static_cast<CallImport*>(expression)->operands.size());
+ return static_cast<CallImport*>(expression)->operands[index];
+}
+// CallIndirect
+BinaryenExpressionRef BinaryenCallIndirectGetTarget(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenCallIndirectGetTarget(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<CallIndirect>());
+ return static_cast<CallIndirect*>(expression)->target;
+}
+BinaryenIndex BinaryenCallIndirectGetNumOperands(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenCallIndirectGetNumOperands(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<CallIndirect>());
+ return static_cast<CallIndirect*>(expression)->operands.size();
+}
+BinaryenExpressionRef BinaryenCallIndirectGetOperand(BinaryenExpressionRef expr, BinaryenIndex index) {
+ if (tracing) {
+ std::cout << " BinaryenCallIndirectGetOperand(expressions[" << expressions[expr] << "], " << index << ");\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<CallIndirect>());
+ assert(index < static_cast<CallIndirect*>(expression)->operands.size());
+ return static_cast<CallIndirect*>(expression)->operands[index];
+}
+// GetLocal
+BinaryenIndex BinaryenGetLocalGetIndex(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenGetLocalGetIndex(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<GetLocal>());
+ return static_cast<GetLocal*>(expression)->index;
+}
+// SetLocal
+int BinaryenSetLocalIsTee(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenSetLocalIsTee(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<SetLocal>());
+ return static_cast<SetLocal*>(expression)->isTee();
+}
+BinaryenIndex BinaryenSetLocalGetIndex(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenSetLocalGetIndex(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<SetLocal>());
+ return static_cast<SetLocal*>(expression)->index;
+}
+BinaryenExpressionRef BinaryenSetLocalGetValue(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenSetLocalGetValue(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<SetLocal>());
+ return static_cast<SetLocal*>(expression)->value;
+}
+// GetGlobal
+const char* BinaryenGetGlobalGetName(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenGetGlobalGetName(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<GetGlobal>());
+ return static_cast<GetGlobal*>(expression)->name.c_str();
+}
+// SetGlobal
+const char* BinaryenSetGlobalGetName(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenSetGlobalGetName(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<SetGlobal>());
+ return static_cast<SetGlobal*>(expression)->name.c_str();
+}
+BinaryenExpressionRef BinaryenSetGlobalGetValue(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenSetGlobalGetValue(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<SetGlobal>());
+ return static_cast<SetGlobal*>(expression)->value;
+}
+// Host
+BinaryenOp BinaryenHostGetOp(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenHostGetOp(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Host>());
+ return static_cast<Host*>(expression)->op;
+}
+const char* BinaryenHostGetNameOperand(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenHostGetNameOperand(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Host>());
+ return static_cast<Host*>(expression)->nameOperand.c_str();
+}
+BinaryenIndex BinaryenHostGetNumOperands(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenHostGetNumOperands(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Host>());
+ return static_cast<Host*>(expression)->operands.size();
+}
+BinaryenExpressionRef BinaryenHostGetOperand(BinaryenExpressionRef expr, BinaryenIndex index) {
+ if (tracing) {
+ std::cout << " BinaryenHostGetOperand(expressions[" << expressions[expr] << "], " << index << ");\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Call>());
+ assert(index < static_cast<Host*>(expression)->operands.size());
+ return static_cast<Host*>(expression)->operands[index];
+}
+// Load
+int BinaryenLoadIsAtomic(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenLoadIsAtomic(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Load>());
+ return static_cast<Load*>(expression)->isAtomic;
+}
+int BinaryenLoadIsSigned(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenLoadIsSigned(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Load>());
+ return static_cast<Load*>(expression)->signed_;
+}
+uint32_t BinaryenLoadGetBytes(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenLoadGetBytes(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Load>());
+ return static_cast<Load*>(expression)->bytes;
+}
+uint32_t BinaryenLoadGetOffset(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenLoadGetOffset(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Load>());
+ return static_cast<Load*>(expression)->offset;
+}
+uint32_t BinaryenLoadGetAlign(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenLoadGetAlign(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Load>());
+ return static_cast<Load*>(expression)->align;
+}
+BinaryenExpressionRef BinaryenLoadGetPtr(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenLoadGetPtr(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Load>());
+ return static_cast<Load*>(expression)->ptr;
+}
+// Store
+int BinaryenStoreIsAtomic(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenStoreIsAtomic(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Store>());
+ return static_cast<Store*>(expression)->isAtomic;
+}
+uint32_t BinaryenStoreGetBytes(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenStoreGetBytes(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Store>());
+ return static_cast<Store*>(expression)->bytes;
+}
+uint32_t BinaryenStoreGetOffset(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenStoreGetOffset(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Store>());
+ return static_cast<Store*>(expression)->offset;
+}
+uint32_t BinaryenStoreGetAlign(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenStoreGetAlign(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Store>());
+ return static_cast<Store*>(expression)->align;
+}
+BinaryenExpressionRef BinaryenStoreGetPtr(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenStoreGetPtr(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Store>());
+ return static_cast<Store*>(expression)->ptr;
+}
+BinaryenExpressionRef BinaryenStoreGetValue(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenStoreGetValue(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Store>());
+ return static_cast<Store*>(expression)->value;
+}
+// Const
int32_t BinaryenConstGetValueI32(BinaryenExpressionRef expr) {
if (tracing) {
std::cout << " BinaryenConstGetValueI32(expressions[" << expressions[expr] << "]);\n";
@@ -878,6 +1358,249 @@ double BinaryenConstGetValueF64(BinaryenExpressionRef expr) {
assert(expression->is<Const>());
return static_cast<Const*>(expression)->value.getf64();
}
+// Unary
+BinaryenOp BinaryenUnaryGetOp(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenUnaryGetOp(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Unary>());
+ return static_cast<Unary*>(expression)->op;
+}
+BinaryenExpressionRef BinaryenUnaryGetValue(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenUnaryGetValue(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Unary>());
+ return static_cast<Unary*>(expression)->value;
+}
+// Binary
+BinaryenOp BinaryenBinaryGetOp(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenBinaryGetOp(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Binary>());
+ return static_cast<Binary*>(expression)->op;
+}
+BinaryenExpressionRef BinaryenBinaryGetLeft(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenBinaryGetLeft(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Binary>());
+ return static_cast<Binary*>(expression)->left;
+}
+BinaryenExpressionRef BinaryenBinaryGetRight(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenBinaryGetRight(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Binary>());
+ return static_cast<Binary*>(expression)->right;
+}
+// Select
+BinaryenExpressionRef BinaryenSelectGetIfTrue(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenSelectGetIfTrue(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Select>());
+ return static_cast<Select*>(expression)->ifTrue;
+}
+BinaryenExpressionRef BinaryenSelectGetIfFalse(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenSelectGetIfFalse(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Select>());
+ return static_cast<Select*>(expression)->ifFalse;
+}
+BinaryenExpressionRef BinaryenSelectGetCondition(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenSelectGetCondition(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Select>());
+ return static_cast<Select*>(expression)->condition;
+}
+// Drop
+BinaryenExpressionRef BinaryenDropGetValue(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenDropGetValue(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Drop>());
+ return static_cast<Drop*>(expression)->value;
+}
+// Return
+BinaryenExpressionRef BinaryenReturnGetValue(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenReturnGetValue(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Return>());
+ return static_cast<Return*>(expression)->value;
+}
+// AtomicRMW
+BinaryenOp BinaryenAtomicRMWGetOp(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenAtomicRMWGetOp(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<AtomicRMW>());
+ return static_cast<AtomicRMW*>(expression)->op;
+}
+uint32_t BinaryenAtomicRMWGetBytes(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenAtomicRMWGetBytes(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<AtomicRMW>());
+ return static_cast<AtomicRMW*>(expression)->bytes;
+}
+uint32_t BinaryenAtomicRMWGetOffset(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenAtomicRMWGetOffset(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<AtomicRMW>());
+ return static_cast<AtomicRMW*>(expression)->offset;
+}
+BinaryenExpressionRef BinaryenAtomicRMWGetPtr(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenAtomicRMWGetPtr(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<AtomicRMW>());
+ return static_cast<AtomicRMW*>(expression)->ptr;
+}
+BinaryenExpressionRef BinaryenAtomicRMWGetValue(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenAtomicRMWGetValue(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<AtomicRMW>());
+ return static_cast<AtomicRMW*>(expression)->value;
+}
+// AtomicCmpxchg
+uint32_t BinaryenAtomicCmpxchgGetBytes(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenAtomicCmpxchgGetBytes(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<AtomicCmpxchg>());
+ return static_cast<AtomicCmpxchg*>(expression)->bytes;
+}
+uint32_t BinaryenAtomicCmpxchgGetOffset(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenAtomicCmpxchgGetOffset(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<AtomicCmpxchg>());
+ return static_cast<AtomicCmpxchg*>(expression)->offset;
+}
+BinaryenExpressionRef BinaryenAtomicCmpxchgGetPtr(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenAtomicCmpxchgGetPtr(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<AtomicCmpxchg>());
+ return static_cast<AtomicCmpxchg*>(expression)->ptr;
+}
+BinaryenExpressionRef BinaryenAtomicCmpxchgGetExpected(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenAtomicCmpxchgGetExpected(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<AtomicCmpxchg>());
+ return static_cast<AtomicCmpxchg*>(expression)->expected;
+}
+BinaryenExpressionRef BinaryenAtomicCmpxchgGetReplacement(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenAtomicCmpxchgGetReplacement(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<AtomicCmpxchg>());
+ return static_cast<AtomicCmpxchg*>(expression)->replacement;
+}
+// AtomicWait
+BinaryenExpressionRef BinaryenAtomicWaitGetPtr(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenAtomicWaitGetPtr(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<AtomicWait>());
+ return static_cast<AtomicWait*>(expression)->ptr;
+}
+BinaryenExpressionRef BinaryenAtomicWaitGetExpected(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenAtomicWaitGetExpected(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<AtomicWait>());
+ return static_cast<AtomicWait*>(expression)->expected;
+}
+BinaryenExpressionRef BinaryenAtomicWaitGetTimeout(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenAtomicWaitGetTimeout(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<AtomicWait>());
+ return static_cast<AtomicWait*>(expression)->timeout;
+}
+BinaryenType BinaryenAtomicWaitGetExpectedType(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenAtomicWaitGetExpectedType(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<AtomicWait>());
+ return static_cast<AtomicWait*>(expression)->expectedType;
+}
+// AtomicWake
+BinaryenExpressionRef BinaryenAtomicWakeGetPtr(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenAtomicWakeGetPtr(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<AtomicWake>());
+ return static_cast<AtomicWake*>(expression)->ptr;
+}
+BinaryenExpressionRef BinaryenAtomicWakeGetWakeCount(BinaryenExpressionRef expr) {
+ if (tracing) {
+ std::cout << " BinaryenAtomicWakeGetWakeCount(expressions[" << expressions[expr] << "]);\n";
+ }
+
+ auto* expression = (Expression*)expr;
+ assert(expression->is<AtomicWake>());
+ return static_cast<AtomicWake*>(expression)->wakeCount;
+}
// Functions
@@ -957,12 +1680,15 @@ WASM_DEPRECATED BinaryenImportRef BinaryenAddImport(BinaryenModuleRef module, co
return BinaryenAddFunctionImport(module, internalName, externalModuleName, externalBaseName, type);
}
BinaryenImportRef BinaryenAddFunctionImport(BinaryenModuleRef module, const char* internalName, const char* externalModuleName, const char *externalBaseName, BinaryenFunctionTypeRef functionType) {
+ auto* ret = new Import();
+ auto* wasm = (Module*)module;
+
if (tracing) {
- std::cout << " BinaryenAddFunctionImport(the_module, \"" << internalName << "\", \"" << externalModuleName << "\", \"" << externalBaseName << "\", functionTypes[" << functionTypes[functionType] << "]);\n";
+ auto id = imports.size();
+ imports[ret] = id;
+ std::cout << " imports[" << id << "] = BinaryenAddFunctionImport(the_module, \"" << internalName << "\", \"" << externalModuleName << "\", \"" << externalBaseName << "\", functionTypes[" << functionTypes[functionType] << "]);\n";
}
- auto* wasm = (Module*)module;
- auto* ret = new Import();
ret->name = internalName;
ret->module = externalModuleName;
ret->base = externalBaseName;
@@ -972,12 +1698,15 @@ BinaryenImportRef BinaryenAddFunctionImport(BinaryenModuleRef module, const char
return ret;
}
BinaryenImportRef BinaryenAddTableImport(BinaryenModuleRef module, const char* internalName, const char* externalModuleName, const char* externalBaseName) {
+ auto* wasm = (Module*)module;
+ auto* ret = new Import();
+
if (tracing) {
- std::cout << " BinaryenAddTableImport(the_module, \"" << internalName << "\", \"" << externalModuleName << "\", \"" << externalBaseName << "\");\n";
+ auto id = imports.size();
+ imports[ret] = id;
+ std::cout << " imports[" << id << "] = BinaryenAddTableImport(the_module, \"" << internalName << "\", \"" << externalModuleName << "\", \"" << externalBaseName << "\");\n";
}
- auto* wasm = (Module*)module;
- auto* ret = new Import();
ret->name = internalName;
ret->module = externalModuleName;
ret->base = externalBaseName;
@@ -989,12 +1718,15 @@ BinaryenImportRef BinaryenAddTableImport(BinaryenModuleRef module, const char* i
return ret;
}
BinaryenImportRef BinaryenAddMemoryImport(BinaryenModuleRef module, const char* internalName, const char* externalModuleName, const char* externalBaseName) {
+ auto* wasm = (Module*)module;
+ auto* ret = new Import();
+
if (tracing) {
- std::cout << " BinaryenAddMemoryImport(the_module, \"" << internalName << "\", \"" << externalModuleName << "\", \"" << externalBaseName << "\");\n";
+ auto id = imports.size();
+ imports[ret] = id;
+ std::cout << " imports[" << id << "] = BinaryenAddMemoryImport(the_module, \"" << internalName << "\", \"" << externalModuleName << "\", \"" << externalBaseName << "\");\n";
}
- auto* wasm = (Module*)module;
- auto* ret = new Import();
ret->name = internalName;
ret->module = externalModuleName;
ret->base = externalBaseName;
@@ -1006,12 +1738,15 @@ BinaryenImportRef BinaryenAddMemoryImport(BinaryenModuleRef module, const char*
return ret;
}
BinaryenImportRef BinaryenAddGlobalImport(BinaryenModuleRef module, const char* internalName, const char* externalModuleName, const char* externalBaseName, BinaryenType globalType) {
+ auto* wasm = (Module*)module;
+ auto* ret = new Import();
+
if (tracing) {
- std::cout << " BinaryenAddGlobalImport(the_module, \"" << internalName << "\", \"" << externalModuleName << "\", \"" << externalBaseName << "\", " << globalType << ");\n";
+ auto id = imports.size();
+ imports[ret] = id;
+ std::cout << " imports[" << id << "] = BinaryenAddGlobalImport(the_module, \"" << internalName << "\", \"" << externalModuleName << "\", \"" << externalBaseName << "\", " << globalType << ");\n";
}
- auto* wasm = (Module*)module;
- auto* ret = new Import();
ret->name = internalName;
ret->module = externalModuleName;
ret->base = externalBaseName;
@@ -1045,12 +1780,15 @@ WASM_DEPRECATED BinaryenExportRef BinaryenAddExport(BinaryenModuleRef module, co
return BinaryenAddFunctionExport(module, internalName, externalName);
}
BinaryenExportRef BinaryenAddFunctionExport(BinaryenModuleRef module, const char* internalName, const char* externalName) {
+ auto* wasm = (Module*)module;
+ auto* ret = new Export();
+
if (tracing) {
- std::cout << " BinaryenAddFunctionExport(the_module, \"" << internalName << "\", \"" << externalName << "\");\n";
+ auto id = exports.size();
+ exports[ret] = id;
+ std::cout << " exports[" << id << "] = BinaryenAddFunctionExport(the_module, \"" << internalName << "\", \"" << externalName << "\");\n";
}
- auto* wasm = (Module*)module;
- auto* ret = new Export();
ret->value = internalName;
ret->name = externalName;
ret->kind = ExternalKind::Function;
@@ -1058,12 +1796,15 @@ BinaryenExportRef BinaryenAddFunctionExport(BinaryenModuleRef module, const char
return ret;
}
BinaryenExportRef BinaryenAddTableExport(BinaryenModuleRef module, const char* internalName, const char* externalName) {
+ auto* wasm = (Module*)module;
+ auto* ret = new Export();
+
if (tracing) {
- std::cout << " BinaryenAddTableExport(the_module, \"" << internalName << "\", \"" << externalName << "\");\n";
+ auto id = exports.size();
+ exports[ret] = id;
+ std::cout << " exports[" << id << "] = BinaryenAddTableExport(the_module, \"" << internalName << "\", \"" << externalName << "\");\n";
}
- auto* wasm = (Module*)module;
- auto* ret = new Export();
ret->value = internalName;
ret->name = externalName;
ret->kind = ExternalKind::Table;
@@ -1071,12 +1812,15 @@ BinaryenExportRef BinaryenAddTableExport(BinaryenModuleRef module, const char* i
return ret;
}
BinaryenExportRef BinaryenAddMemoryExport(BinaryenModuleRef module, const char* internalName, const char* externalName) {
+ auto* wasm = (Module*)module;
+ auto* ret = new Export();
+
if (tracing) {
- std::cout << " BinaryenAddMemoryExport(the_module, \"" << internalName << "\", \"" << externalName << "\");\n";
+ auto id = exports.size();
+ exports[ret] = id;
+ std::cout << " exports[" << id << "] = BinaryenAddMemoryExport(the_module, \"" << internalName << "\", \"" << externalName << "\");\n";
}
- auto* wasm = (Module*)module;
- auto* ret = new Export();
ret->value = internalName;
ret->name = externalName;
ret->kind = ExternalKind::Memory;
@@ -1084,12 +1828,15 @@ BinaryenExportRef BinaryenAddMemoryExport(BinaryenModuleRef module, const char*
return ret;
}
BinaryenExportRef BinaryenAddGlobalExport(BinaryenModuleRef module, const char* internalName, const char* externalName) {
+ auto* wasm = (Module*)module;
+ auto* ret = new Export();
+
if (tracing) {
- std::cout << " BinaryenAddGlobalExport(the_module, \"" << internalName << "\", \"" << externalName << "\");\n";
+ auto id = exports.size();
+ exports[ret] = id;
+ std::cout << " exports[" << id << "] = BinaryenAddGlobalExport(the_module, \"" << internalName << "\", \"" << externalName << "\");\n";
}
- auto* wasm = (Module*)module;
- auto* ret = new Export();
ret->value = internalName;
ret->name = externalName;
ret->kind = ExternalKind::Global;
@@ -1340,9 +2087,97 @@ void BinaryenModuleInterpret(BinaryenModuleRef module) {
}
//
+// ======== FunctionType Operations ========
+//
+
+const char* BinaryenFunctionTypeGetName(BinaryenFunctionTypeRef ftype) {
+ if (tracing) {
+ std::cout << " BinaryenFunctionTypeGetName(functionsTypes[" << functions[ftype] << "]);\n";
+ }
+
+ return ((FunctionType*)ftype)->name.c_str();
+}
+BinaryenIndex BinaryenFunctionTypeGetNumParams(BinaryenFunctionTypeRef ftype) {
+ if (tracing) {
+ std::cout << " BinaryenFunctionTypeGetNumParams(functionsTypes[" << functions[ftype] << "]);\n";
+ }
+
+ return ((FunctionType*)ftype)->params.size();
+}
+BinaryenType BinaryenFunctionTypeGetParam(BinaryenFunctionTypeRef ftype, BinaryenIndex index) {
+ if (tracing) {
+ std::cout << " BinaryenFunctionTypeGetParam(functionsTypes[" << functions[ftype] << "], " << index << ");\n";
+ }
+
+ auto* ft = (FunctionType*)ftype;
+ assert(index < ft->params.size());
+ return ft->params[index];
+}
+BinaryenType BinaryenFunctionTypeGetResult(BinaryenFunctionTypeRef ftype) {
+ if (tracing) {
+ std::cout << " BinaryenFunctionTypeGetResult(functionsTypes[" << functions[ftype] << "]);\n";
+ }
+
+ return ((FunctionType*)ftype)->result;
+}
+
+//
// ========== Function Operations ==========
//
+const char* BinaryenFunctionGetName(BinaryenFunctionRef func) {
+ if (tracing) {
+ std::cout << " BinaryenFunctionGetName(functions[" << functions[func] << "]);\n";
+ }
+
+ return ((Function*)func)->name.c_str();
+}
+const char* BinaryenFunctionGetType(BinaryenFunctionRef func) {
+ if (tracing) {
+ std::cout << " BinaryenFunctionGetType(functions[" << functions[func] << "]);\n";
+ }
+
+ return ((Function*)func)->type.c_str();
+}
+BinaryenIndex BinaryenFunctionGetNumParams(BinaryenFunctionRef func) {
+ if (tracing) {
+ std::cout << " BinaryenFunctionGetNumParams(functions[" << functions[func] << "]);\n";
+ }
+
+ return ((Function*)func)->params.size();
+}
+BinaryenType BinaryenFunctionGetParam(BinaryenFunctionRef func, BinaryenIndex index) {
+ if (tracing) {
+ std::cout << " BinaryenFunctionGetParam(functions[" << functions[func] << "], " << index << ");\n";
+ }
+
+ auto* fn = (Function*)func;
+ assert(index < fn->params.size());
+ return fn->params[index];
+}
+BinaryenType BinaryenFunctionGetResult(BinaryenFunctionRef func) {
+ if (tracing) {
+ std::cout << " BinaryenFunctionGetResult(functions[" << functions[func] << "]);\n";
+ }
+
+ return ((Function*)func)->result;
+}
+BinaryenIndex BinaryenFunctionGetNumVars(BinaryenFunctionRef func) {
+ if (tracing) {
+ std::cout << " BinaryenFunctionGetNumVars(functions[" << functions[func] << "]);\n";
+ }
+
+ return ((Function*)func)->vars.size();
+}
+BinaryenType BinaryenFunctionGetVar(BinaryenFunctionRef func, BinaryenIndex index) {
+ if (tracing) {
+ std::cout << " BinaryenFunctionGetVar(functions[" << functions[func] << "], " << index << ");\n";
+ }
+
+ auto* fn = (Function*)func;
+ assert(index < fn->vars.size());
+ return fn->vars[index];
+}
BinaryenExpressionRef BinaryenFunctionGetBody(BinaryenFunctionRef func) {
if (tracing) {
std::cout << " BinaryenFunctionGetBody(functions[" << functions[func] << "]);\n";
@@ -1350,7 +2185,6 @@ BinaryenExpressionRef BinaryenFunctionGetBody(BinaryenFunctionRef func) {
return ((Function*)func)->body;
}
-
void BinaryenFunctionOptimize(BinaryenFunctionRef func, BinaryenModuleRef module) {
if (tracing) {
std::cout << " BinaryenFunctionOptimize(functions[" << functions[func] << "], the_module);\n";
@@ -1361,7 +2195,6 @@ void BinaryenFunctionOptimize(BinaryenFunctionRef func, BinaryenModuleRef module
passRunner.addDefaultOptimizationPasses();
passRunner.runFunction((Function*)func);
}
-
void BinaryenFunctionRunPasses(BinaryenFunctionRef func, BinaryenModuleRef module, const char **passes, BinaryenIndex numPasses) {
if (tracing) {
std::cout << " {\n";
@@ -1384,6 +2217,79 @@ void BinaryenFunctionRunPasses(BinaryenFunctionRef func, BinaryenModuleRef modul
}
//
+// =========== Import operations ===========
+//
+
+BinaryenExternalKind BinaryenImportGetKind(BinaryenImportRef import) {
+ if (tracing) {
+ std::cout << " BinaryenImportGetKind(imports[" << imports[import] << "]);\n";
+ }
+
+ return BinaryenExternalKind(((Import*)import)->kind);
+}
+const char* BinaryenImportGetModule(BinaryenImportRef import) {
+ if (tracing) {
+ std::cout << " BinaryenImportGetModule(imports[" << imports[import] << "]);\n";
+ }
+
+ return ((Import*)import)->module.c_str();
+}
+const char* BinaryenImportGetBase(BinaryenImportRef import) {
+ if (tracing) {
+ std::cout << " BinaryenImportGetBase(imports[" << imports[import] << "]);\n";
+ }
+
+ return ((Import*)import)->base.c_str();
+}
+const char* BinaryenImportGetName(BinaryenImportRef import) {
+ if (tracing) {
+ std::cout << " BinaryenImportGetName(imports[" << imports[import] << "]);\n";
+ }
+
+ return ((Import*)import)->name.c_str();
+}
+BinaryenType BinaryenImportGetGlobalType(BinaryenImportRef import) {
+ if (tracing) {
+ std::cout << " BinaryenImportGetGlobalType(imports[" << imports[import] << "]);\n";
+ }
+
+ return ((Import*)import)->globalType;
+}
+const char* BinaryenImportGetFunctionType(BinaryenImportRef import) {
+ if (tracing) {
+ std::cout << " BinaryenImportGetFunctionType(imports[" << imports[import] << "]);\n";
+ }
+
+ return ((Import*)import)->functionType.c_str();
+}
+
+//
+// =========== Export operations ===========
+//
+
+BinaryenExternalKind BinaryenExportGetKind(BinaryenExportRef export_) {
+ if (tracing) {
+ std::cout << " BinaryenExportGetKind(exports[" << exports[export_] << "]);\n";
+ }
+
+ return BinaryenExternalKind(((Export*)export_)->kind);
+}
+const char* BinaryenExportGetName(BinaryenExportRef export_) {
+ if (tracing) {
+ std::cout << " BinaryenExportGetName(exports[" << exports[export_] << "]);\n";
+ }
+
+ return ((Export*)export_)->name.c_str();
+}
+const char* BinaryenExportGetValue(BinaryenExportRef export_) {
+ if (tracing) {
+ std::cout << " BinaryenExportGetValue(exports[" << exports[export_] << "]);\n";
+ }
+
+ return ((Export*)export_)->value.c_str();
+}
+
+//
// ========== CFG / Relooper ==========
//
@@ -1486,6 +2392,8 @@ void BinaryenSetAPITracing(int on) {
" std::map<size_t, BinaryenFunctionTypeRef> functionTypes;\n"
" std::map<size_t, BinaryenExpressionRef> expressions;\n"
" std::map<size_t, BinaryenFunctionRef> functions;\n"
+ " std::map<size_t, BinaryenImportRef> imports;\n"
+ " std::map<size_t, BinaryenExportRef> exports;\n"
" std::map<size_t, RelooperBlockRef> relooperBlocks;\n"
" BinaryenModuleRef the_module = NULL;\n"
" RelooperRef the_relooper = NULL;\n";
diff --git a/src/binaryen-c.h b/src/binaryen-c.h
index 6bd61ed53..78866d189 100644
--- a/src/binaryen-c.h
+++ b/src/binaryen-c.h
@@ -68,15 +68,22 @@ typedef uint32_t BinaryenIndex;
typedef uint32_t BinaryenType;
-BinaryenType BinaryenNone(void);
-BinaryenType BinaryenInt32(void);
-BinaryenType BinaryenInt64(void);
-BinaryenType BinaryenFloat32(void);
-BinaryenType BinaryenFloat64(void);
-
+BinaryenType BinaryenTypeNone(void);
+BinaryenType BinaryenTypeInt32(void);
+BinaryenType BinaryenTypeInt64(void);
+BinaryenType BinaryenTypeFloat32(void);
+BinaryenType BinaryenTypeFloat64(void);
+BinaryenType BinaryenTypeUnreachable(void);
// Not a real type. Used as the last parameter to BinaryenBlock to let
// the API figure out the type instead of providing one.
-BinaryenType BinaryenUndefined(void);
+BinaryenType BinaryenTypeAuto(void);
+
+WASM_DEPRECATED BinaryenType BinaryenNone(void);
+WASM_DEPRECATED BinaryenType BinaryenInt32(void);
+WASM_DEPRECATED BinaryenType BinaryenInt64(void);
+WASM_DEPRECATED BinaryenType BinaryenFloat32(void);
+WASM_DEPRECATED BinaryenType BinaryenFloat64(void);
+WASM_DEPRECATED BinaryenType BinaryenUndefined(void);
// Expression ids (call to get the value of each; you can cache them)
@@ -382,8 +389,120 @@ BinaryenExpressionRef BinaryenAtomicWake(BinaryenModuleRef module, BinaryenExpre
BinaryenExpressionId BinaryenExpressionGetId(BinaryenExpressionRef expr);
// Gets the type of the specified expression.
BinaryenType BinaryenExpressionGetType(BinaryenExpressionRef expr);
-// Print an expression to stdout. Useful for debugging.
+// Prints an expression to stdout. Useful for debugging.
void BinaryenExpressionPrint(BinaryenExpressionRef expr);
+
+// Gets the name of the specified `Block` expression. May be `NULL`.
+const char* BinaryenBlockGetName(BinaryenExpressionRef expr);
+// Gets the number of nested child expressions within the specified `Block` expression.
+BinaryenIndex BinaryenBlockGetNumChildren(BinaryenExpressionRef expr);
+// Gets the nested child expression at the specified index within the specified `Block` expression.
+BinaryenExpressionRef BinaryenBlockGetChild(BinaryenExpressionRef expr, BinaryenIndex index);
+
+// Gets the nested condition expression within the specified `If` expression.
+BinaryenExpressionRef BinaryenIfGetCondition(BinaryenExpressionRef expr);
+// Gets the nested ifTrue expression within the specified `If` expression.
+BinaryenExpressionRef BinaryenIfGetIfTrue(BinaryenExpressionRef expr);
+// Gets the nested ifFalse expression within the specified `If` expression.
+BinaryenExpressionRef BinaryenIfGetIfFalse(BinaryenExpressionRef expr);
+
+// Gets the name of the specified `Loop` expression. May be `NULL`.
+const char* BinaryenLoopGetName(BinaryenExpressionRef expr);
+// Gets the nested body expression within the specified `Loop` expression.
+BinaryenExpressionRef BinaryenLoopGetBody(BinaryenExpressionRef expr);
+
+// Gets the name of the specified `Break` expression. May be `NULL`.
+const char* BinaryenBreakGetName(BinaryenExpressionRef expr);
+// Gets the nested condition expression within the specified `Break` expression. Returns `NULL` if this is a `br` and not a `br_if`.
+BinaryenExpressionRef BinaryenBreakGetCondition(BinaryenExpressionRef expr);
+// Gets the nested value expression within the specified `Break` expression. May be `NULL`.
+BinaryenExpressionRef BinaryenBreakGetValue(BinaryenExpressionRef expr);
+
+// Gets the number of names within the specified `Switch` expression.
+BinaryenIndex BinaryenSwitchGetNumNames(BinaryenExpressionRef expr);
+// Gets the name at the specified index within the specified `Switch` expression.
+const char* BinaryenSwitchGetName(BinaryenExpressionRef expr, BinaryenIndex index);
+// Gets the default name of the specified `Switch` expression.
+const char* BinaryenSwitchGetDefaultName(BinaryenExpressionRef expr);
+// Gets the nested condition expression within the specified `Switch` expression.
+BinaryenExpressionRef BinaryenSwitchGetCondition(BinaryenExpressionRef expr);
+// Gets the nested value expression within the specifiedd `Switch` expression. May be `NULL`.
+BinaryenExpressionRef BinaryenSwitchGetValue(BinaryenExpressionRef expr);
+
+// Gets the name of the target of the specified `Call` expression.
+const char* BinaryenCallGetTarget(BinaryenExpressionRef expr);
+// Gets the number of nested operand expressions within the specified `Call` expression.
+BinaryenIndex BinaryenCallGetNumOperands(BinaryenExpressionRef expr);
+// Gets the nested operand expression at the specified index within the specified `Call` expression.
+BinaryenExpressionRef BinaryenCallGetOperand(BinaryenExpressionRef expr, BinaryenIndex index);
+
+// Gets the name of the target of the specified `CallImport` expression.
+const char* BinaryenCallImportGetTarget(BinaryenExpressionRef expr);
+// Gets the number of nested operand expressions within the specified `CallImport` expression.
+BinaryenIndex BinaryenCallImportGetNumOperands(BinaryenExpressionRef expr);
+// Gets the nested operand expression at the specified index within the specified `CallImport` expression.
+BinaryenExpressionRef BinaryenCallImportGetOperand(BinaryenExpressionRef expr, BinaryenIndex index);
+
+// Gets the nested target expression of the specified `CallIndirect` expression.
+BinaryenExpressionRef BinaryenCallIndirectGetTarget(BinaryenExpressionRef expr);
+// Gets the number of nested operand expressions within the specified `CallIndirect` expression.
+BinaryenIndex BinaryenCallIndirectGetNumOperands(BinaryenExpressionRef expr);
+// Gets the nested operand expression at the specified index within the specified `CallIndirect` expression.
+BinaryenExpressionRef BinaryenCallIndirectGetOperand(BinaryenExpressionRef expr, BinaryenIndex index);
+
+// Gets the index of the specified `GetLocal` expression.
+BinaryenIndex BinaryenGetLocalGetIndex(BinaryenExpressionRef expr);
+
+// Tests if the specified `SetLocal` expression performs a `tee_local` instead of a `set_local`.
+int BinaryenSetLocalIsTee(BinaryenExpressionRef expr);
+// Gets the index of the specified `SetLocal` expression.
+BinaryenIndex BinaryenSetLocalGetIndex(BinaryenExpressionRef expr);
+// Gets the nested value expression within the specified `SetLocal` expression.
+BinaryenExpressionRef BinaryenSetLocalGetValue(BinaryenExpressionRef expr);
+
+// Gets the name of the specified `GetGlobal` expression.
+const char* BinaryenGetGlobalGetName(BinaryenExpressionRef expr);
+
+// Gets the name of the specified `SetGlobal` expression.
+const char* BinaryenSetGlobalGetName(BinaryenExpressionRef expr);
+// Gets the nested value expression within the specified `SetLocal` expression.
+BinaryenExpressionRef BinaryenSetGlobalGetValue(BinaryenExpressionRef expr);
+
+// Gets the operator of the specified `Host` expression.
+BinaryenOp BinaryenHostGetOp(BinaryenExpressionRef expr);
+// Gets the name operand of the specified `Host` expression. May be `NULL`.
+const char* BinaryenHostGetNameOperand(BinaryenExpressionRef expr);
+// Gets the number of nested operand expressions within the specified `Host` expression.
+BinaryenIndex BinaryenHostGetNumOperands(BinaryenExpressionRef expr);
+// Gets the nested operand expression at the specified index within the specified `Host` expression.
+BinaryenExpressionRef BinaryenHostGetOperand(BinaryenExpressionRef expr, BinaryenIndex index);
+
+// Tests if the specified `Load` expression is atomic.
+int BinaryenLoadIsAtomic(BinaryenExpressionRef expr);
+// Tests if the specified `Load` expression is signed.
+int BinaryenLoadIsSigned(BinaryenExpressionRef expr);
+// Gets the offset of the specified `Load` expression.
+uint32_t BinaryenLoadGetOffset(BinaryenExpressionRef expr);
+// Gets the byte size of the specified `Load` expression.
+uint32_t BinaryenLoadGetBytes(BinaryenExpressionRef expr);
+// Gets the alignment of the specified `Load` expression.
+uint32_t BinaryenLoadGetAlign(BinaryenExpressionRef expr);
+// Gets the nested pointer expression within the specified `Load` expression.
+BinaryenExpressionRef BinaryenLoadGetPtr(BinaryenExpressionRef expr);
+
+// Tests if the specified `Store` expression is atomic.
+int BinaryenStoreIsAtomic(BinaryenExpressionRef expr);
+// Gets the byte size of the specified `Store` expression.
+uint32_t BinaryenStoreGetBytes(BinaryenExpressionRef expr);
+// Gets the offset of the specified store expression.
+uint32_t BinaryenStoreGetOffset(BinaryenExpressionRef expr);
+// Gets the alignment of the specified `Store` expression.
+uint32_t BinaryenStoreGetAlign(BinaryenExpressionRef expr);
+// Gets the nested pointer expression within the specified `Store` expression.
+BinaryenExpressionRef BinaryenStoreGetPtr(BinaryenExpressionRef expr);
+// Gets the nested value expression within the specified `Store` expression.
+BinaryenExpressionRef BinaryenStoreGetValue(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.
@@ -397,6 +516,67 @@ float BinaryenConstGetValueF32(BinaryenExpressionRef expr);
// Gets the 64-bit float value of the specified `Const` expression.
double BinaryenConstGetValueF64(BinaryenExpressionRef expr);
+// Gets the operator of the specified `Unary` expression.
+BinaryenOp BinaryenUnaryGetOp(BinaryenExpressionRef expr);
+// Gets the nested value expression within the specified `Unary` expression.
+BinaryenExpressionRef BinaryenUnaryGetValue(BinaryenExpressionRef expr);
+
+// Gets the operator of the specified `Binary` expression.
+BinaryenOp BinaryenBinaryGetOp(BinaryenExpressionRef expr);
+// Gets the nested left expression within the specified `Binary` expression.
+BinaryenExpressionRef BinaryenBinaryGetLeft(BinaryenExpressionRef expr);
+// Gets the nested right expression within the specified `Binary` expression.
+BinaryenExpressionRef BinaryenBinaryGetRight(BinaryenExpressionRef expr);
+
+// Gets the nested ifTrue expression within the specified `Select` expression.
+BinaryenExpressionRef BinaryenSelectGetIfTrue(BinaryenExpressionRef expr);
+// Gets the nested ifFalse expression within the specified `Select` expression.
+BinaryenExpressionRef BinaryenSelectGetIfFalse(BinaryenExpressionRef expr);
+// Gets the nested condition expression within the specified `Select` expression.
+BinaryenExpressionRef BinaryenSelectGetCondition(BinaryenExpressionRef expr);
+
+// Gets the nested value expression within the specified `Drop` expression.
+BinaryenExpressionRef BinaryenDropGetValue(BinaryenExpressionRef expr);
+
+// Gets the nested value expression within the specified `Return` expression.
+BinaryenExpressionRef BinaryenReturnGetValue(BinaryenExpressionRef expr);
+
+// Gets the operator of the specified `AtomicRMW` expression.
+BinaryenOp BinaryenAtomicRMWGetOp(BinaryenExpressionRef expr);
+// Gets the byte size of the specified `AtomicRMW` expression.
+uint32_t BinaryenAtomicRMWGetBytes(BinaryenExpressionRef expr);
+// Gets the offset of the specified `AtomicRMW` expression.
+uint32_t BinaryenAtomicRMWGetOffset(BinaryenExpressionRef expr);
+// Gets the nested pointer expression within the specified `AtomicRMW` expression.
+BinaryenExpressionRef BinaryenAtomicRMWGetPtr(BinaryenExpressionRef expr);
+// Gets the nested value expression within the specified `AtomicRMW` expression.
+BinaryenExpressionRef BinaryenAtomicRMWGetValue(BinaryenExpressionRef expr);
+
+// Gets the byte size of the specified `AtomicCmpxchg` expression.
+uint32_t BinaryenAtomicCmpxchgGetBytes(BinaryenExpressionRef expr);
+// Gets the offset of the specified `AtomicCmpxchg` expression.
+uint32_t BinaryenAtomicCmpxchgGetOffset(BinaryenExpressionRef expr);
+// Gets the nested pointer expression within the specified `AtomicCmpxchg` expression.
+BinaryenExpressionRef BinaryenAtomicCmpxchgGetPtr(BinaryenExpressionRef expr);
+// Gets the nested expected value expression within the specified `AtomicCmpxchg` expression.
+BinaryenExpressionRef BinaryenAtomicCmpxchgGetExpected(BinaryenExpressionRef expr);
+// Gets the nested replacement value expression within the specified `AtomicCmpxchg` expression.
+BinaryenExpressionRef BinaryenAtomicCmpxchgGetReplacement(BinaryenExpressionRef expr);
+
+// Gets the nested pointer expression within the specified `AtomicWait` expression.
+BinaryenExpressionRef BinaryenAtomicWaitGetPtr(BinaryenExpressionRef expr);
+// Gets the nested expected value expression within the specified `AtomicWait` expression.
+BinaryenExpressionRef BinaryenAtomicWaitGetExpected(BinaryenExpressionRef expr);
+// Gets the nested timeout expression within the specified `AtomicWait` expression.
+BinaryenExpressionRef BinaryenAtomicWaitGetTimeout(BinaryenExpressionRef expr);
+// Gets the expected type of the specified `AtomicWait` expression.
+BinaryenType BinaryenAtomicWaitGetExpectedType(BinaryenExpressionRef expr);
+
+// Gets the nested pointer expression within the specified `AtomicWake` expression.
+BinaryenExpressionRef BinaryenAtomicWakeGetPtr(BinaryenExpressionRef expr);
+// Gets the nested wake count expression within the specified `AtomicWake` expression.
+BinaryenExpressionRef BinaryenAtomicWakeGetWakeCount(BinaryenExpressionRef expr);
+
// Functions
typedef void* BinaryenFunctionRef;
@@ -500,10 +680,37 @@ BinaryenModuleRef BinaryenModuleRead(char* input, size_t inputSize);
void BinaryenModuleInterpret(BinaryenModuleRef module);
//
+// ======== FunctionType Operations ========
+//
+
+// Gets the name of the specified `FunctionType`.
+const char* BinaryenFunctionTypeGetName(BinaryenFunctionTypeRef ftype);
+// Gets the number of parameters of the specified `FunctionType`.
+BinaryenIndex BinaryenFunctionTypeGetNumParams(BinaryenFunctionTypeRef ftype);
+// Gets the type of the parameter at the specified index of the specified `FunctionType`.
+BinaryenType BinaryenFunctionTypeGetParam(BinaryenFunctionTypeRef ftype, BinaryenIndex index);
+// Gets the result type of the specified `FunctionType`.
+BinaryenType BinaryenFunctionTypeGetResult(BinaryenFunctionTypeRef ftype);
+
+//
// ========== Function Operations ==========
//
-// Gets the body of the function.
+// Gets the name of the specified `Function`.
+const char* BinaryenFunctionGetName(BinaryenFunctionRef func);
+// Gets the name of the `FunctionType` associated with the specified `Function`. May be `NULL` if the signature is implicit.
+const char* BinaryenFunctionGetType(BinaryenFunctionRef func);
+// Gets the number of parameters of the specified `Function`.
+BinaryenIndex BinaryenFunctionGetNumParams(BinaryenFunctionRef func);
+// Gets the type of the parameter at the specified index of the specified `Function`.
+BinaryenType BinaryenFunctionGetParam(BinaryenFunctionRef func, BinaryenIndex index);
+// Gets the result type of the specified `Function`.
+BinaryenType BinaryenFunctionGetResult(BinaryenFunctionRef func);
+// Gets the number of additional locals within the specified `Function`.
+BinaryenIndex BinaryenFunctionGetNumVars(BinaryenFunctionRef func);
+// Gets the type of the additional local at the specified index within the specified `Function`.
+BinaryenType BinaryenFunctionGetVar(BinaryenFunctionRef func, BinaryenIndex index);
+// Gets the body of the specified `Function`.
BinaryenExpressionRef BinaryenFunctionGetBody(BinaryenFunctionRef func);
// Runs the standard optimization passes on the function.
@@ -513,6 +720,34 @@ void BinaryenFunctionOptimize(BinaryenFunctionRef func, BinaryenModuleRef module
void BinaryenFunctionRunPasses(BinaryenFunctionRef func, BinaryenModuleRef module, const char **passes, BinaryenIndex numPasses);
//
+// ========== Import Operations ==========
+//
+
+// Gets the external kind of the specified import.
+BinaryenExternalKind BinaryenImportGetKind(BinaryenImportRef import);
+// Gets the external module name of the specified import.
+const char* BinaryenImportGetModule(BinaryenImportRef import);
+// Gets the external base name of the specified import.
+const char* BinaryenImportGetBase(BinaryenImportRef import);
+// Gets the internal name of the specified import.
+const char* BinaryenImportGetName(BinaryenImportRef import);
+// Gets the type of the imported global, if referencing a `Global`.
+BinaryenType BinaryenImportGetGlobalType(BinaryenImportRef import);
+// Gets the name of the function type of the imported function, if referencing a `Function`.
+const char* BinaryenImportGetFunctionType(BinaryenImportRef import);
+
+//
+// ========== Export Operations ==========
+//
+
+// Gets the external kind of the specified export.
+BinaryenExternalKind BinaryenExportGetKind(BinaryenExportRef export_);
+// Gets the external name of the specified export.
+const char* BinaryenExportGetName(BinaryenExportRef export_);
+// Gets the internal name of the specified export.
+const char* BinaryenExportGetValue(BinaryenExportRef export_);
+
+//
// ========== CFG / Relooper ==========
//
// General usage is (1) create a relooper, (2) create blocks, (3) add
diff --git a/src/js/binaryen.js-post.js b/src/js/binaryen.js-post.js
index be0facdd5..7600f61f1 100644
--- a/src/js/binaryen.js-post.js
+++ b/src/js/binaryen.js-post.js
@@ -7,7 +7,7 @@
} finally {
Runtime.stackRestore(stack);
}
- };
+ }
function strToStack(str) {
if (!str) return 0;
@@ -22,13 +22,16 @@
return ret;
}
- Module['none'] = Module['_BinaryenNone']();
- Module['i32'] = Module['_BinaryenInt32']();
- Module['i64'] = Module['_BinaryenInt64']();
- Module['f32'] = Module['_BinaryenFloat32']();
- Module['f64'] = Module['_BinaryenFloat64']();
- Module['undefined'] = Module['_BinaryenUndefined']();
+ // Types
+ Module['none'] = Module['_BinaryenTypeNone']();
+ Module['i32'] = Module['_BinaryenTypeInt32']();
+ Module['i64'] = Module['_BinaryenTypeInt64']();
+ Module['f32'] = Module['_BinaryenTypeFloat32']();
+ Module['f64'] = Module['_BinaryenTypeFloat64']();
+ Module['unreachable'] = Module['_BinaryenTypeUnreachable']();
+ Module['auto'] = /* deprecated */ Module['undefined'] = Module['_BinaryenTypeAuto']();
+ // Expression ids
Module['InvalidId'] = Module['_BinaryenInvalidId']();
Module['BlockId'] = Module['_BinaryenBlockId']();
Module['IfId'] = Module['_BinaryenIfId']();
@@ -58,6 +61,13 @@
Module['AtomicWaitId'] = Module['_BinaryenAtomicWaitId']();
Module['AtomicWakeId'] = Module['_BinaryenAtomicWakeId']();
+ // External kinds
+ Module['ExternalFunction'] = Module['_BinaryenExternalFunction']();
+ Module['ExternalTable'] = Module['_BinaryenExternalTable']();
+ Module['ExternalMemory'] = Module['_BinaryenExternalMemory']();
+ Module['ExternalGlobal'] = Module['_BinaryenExternalGlobal']();
+
+ // Operations
Module['ClzInt32'] = Module['_BinaryenClzInt32']();
Module['CtzInt32'] = Module['_BinaryenCtzInt32']();
Module['PopcntInt32'] = Module['_BinaryenPopcntInt32']();
@@ -192,27 +202,12 @@
Module['AtomicRMWXor'] = Module['_BinaryenAtomicRMWXor']();
Module['AtomicRMWXchg'] = Module['_BinaryenAtomicRMWXchg']();
- // we provide a JS Module() object interface
+ // 'Module' interface
Module['Module'] = function(module) {
if (!module) module = Module['_BinaryenModuleCreate']();
this['ptr'] = module;
- this['dispose'] = function() {
- Module['_BinaryenModuleDispose'](module);
- };
- this['addFunctionType'] = function(name, result, paramTypes) {
- return preserveStack(function() {
- return Module['_BinaryenAddFunctionType'](module, strToStack(name), result,
- i32sToStack(paramTypes), paramTypes.length);
- });
- };
- this['getFunctionTypeBySignature'] = function(result, paramTypes) {
- return preserveStack(function() {
- return Module['_BinaryenGetFunctionTypeBySignature'](module, result,
- i32sToStack(paramTypes), paramTypes.length);
- });
- };
-
+ // 'Expression' creation
this['block'] = function(name, children, type) {
return preserveStack(function() {
return Module['_BinaryenBlock'](module, name ? strToStack(name) : 0,
@@ -1006,8 +1001,11 @@
this['return'] = function(value) {
return Module['_BinaryenReturn'](module, value);
};
- this['host'] = function() {
- throw 'TODO';
+ this['host'] = function(op, name, operands) {
+ if (!operands) operands = [];
+ return preserveStack(function() {
+ return Module['_BinaryenHost'](module, op, strToStack(name), i32sToStack(operands), operands.length);
+ });
};
this['nop'] = function() {
return Module['_BinaryenNop'](module);
@@ -1018,6 +1016,22 @@
this['wake'] = function(ptr, wakeCount) {
return Module['_BinaryenAtomicWake'](module, ptr, wakeCount);
};
+
+ // 'Module' operations
+ this['addFunctionType'] = function(name, result, paramTypes) {
+ if (!paramTypes) paramTypes = [];
+ return preserveStack(function() {
+ return Module['_BinaryenAddFunctionType'](module, strToStack(name), result,
+ i32sToStack(paramTypes), paramTypes.length);
+ });
+ };
+ this['getFunctionTypeBySignature'] = function(result, paramTypes) {
+ if (!paramTypes) paramTypes = [];
+ return preserveStack(function() {
+ return Module['_BinaryenGetFunctionTypeBySignature'](module, result,
+ i32sToStack(paramTypes), paramTypes.length);
+ });
+ };
this['addFunction'] = function(name, functionType, varTypes, body) {
return preserveStack(function() {
return Module['_BinaryenAddFunction'](module, strToStack(name), functionType, i32sToStack(varTypes), varTypes.length, body);
@@ -1167,9 +1181,10 @@
this['autoDrop'] = function() {
return Module['_BinaryenModuleAutoDrop'](module);
};
-
- // TODO: fix this hard-wired limit
- var MAX = 1024*1024;
+ this['dispose'] = function() {
+ Module['_BinaryenModuleDispose'](module);
+ };
+ var MAX = 1024*1024; // TODO: fix this hard-wired limit
var writeBuffer = null;
this['emitBinary'] = function() {
if (!writeBuffer) writeBuffer = _malloc(MAX);
@@ -1182,8 +1197,10 @@
};
};
- Module['Relooper'] = function() {
- var relooper = this.ptr = Module['_RelooperCreate']();
+ // 'Relooper' interface
+ Module['Relooper'] = function(relooper) {
+ if (!relooper) relooper = Module['_RelooperCreate']();
+ this.ptr = relooper;
this['addBlock'] = function(code) {
return Module['_RelooperAddBlock'](relooper, code);
@@ -1204,38 +1221,285 @@
};
};
+ function getAllNested(ref, numFn, getFn) {
+ var num = numFn(ref);
+ var ret = new Array(num);
+ for (var i = 0; i < num; ++i) ret[i] = getFn(ref, i);
+ return ret;
+ }
+
+ // Gets the specific id of an 'Expression'
Module['getExpressionId'] = function(expr) {
return Module['_BinaryenExpressionGetId'](expr);
};
+ // Gets the result type of an 'Expression'
Module['getExpressionType'] = function(expr) {
return Module['_BinaryenExpressionGetType'](expr);
};
- Module['getConstValueI32'] = function(expr) {
- return Module['_BinaryenConstGetValueI32'](expr);
+ // Obtains information about an 'Expression'
+ Module['getExpressionInfo'] = function(expr) {
+ var id = Module['_BinaryenExpressionGetId'](expr);
+ var type = Module['_BinaryenExpressionGetType'](expr);
+ switch (id) {
+ case Module['BlockId']:
+ return {
+ 'id': id,
+ 'type': type,
+ 'name': Pointer_stringify(Module['_BinaryenBlockGetName'](expr)),
+ 'children': getAllNested(expr, Module['_BinaryenBlockGetNumChildren'], Module['_BinaryenBlockGetChild'])
+ };
+ case Module['IfId']:
+ return {
+ 'id': id,
+ 'type': type,
+ 'condition': Module['_BinaryenIfGetCondition'](expr),
+ 'ifTrue': Module['_BinaryenIfGetIfTrue'](expr),
+ 'ifFalse': Module['_BinaryenIfGetIfFalse'](expr)
+ };
+ case Module['LoopId']:
+ return {
+ 'id': id,
+ 'type': type,
+ 'name': Pointer_stringify(Module['_BinaryenLoopGetName'](expr)),
+ 'body': Module['_BinaryenLoopGetBody'](expr)
+ };
+ case Module['BreakId']:
+ return {
+ 'id': id,
+ 'type': type,
+ 'name': Pointer_stringify(Module['_BinaryenBreakGetName'](expr)),
+ 'condition': Module['_BinaryenBreakGetCondition'](expr),
+ 'value': Module['_BinaryenBreakGetValue'](expr)
+ };
+ case Module['SwitchId']:
+ return {
+ 'id': id,
+ 'type': type,
+ 'names': getAllNested(expr, Module['_BinaryenSwitchGetNumNames'], Module['_BinaryenSwitchGetName']).map(Pointer_stringify),
+ 'defaultName': Pointer_stringify(Module['_BinaryenSwitchGetDefaultName'](expr)),
+ 'condition': Module['_BinaryenSwitchGetCondition'](expr),
+ 'value': Module['_BinaryenSwitchGetValue'](expr)
+ };
+ case Module['CallId']:
+ return {
+ 'id': id,
+ 'type': type,
+ 'target': Pointer_stringify(Module['_BinaryenCallGetTarget'](expr)),
+ 'operands': getAllNested(expr, Module[ '_BinaryenCallGetNumOperands'], Module['_BinaryenCallGetOperand'])
+ };
+ case Module['CallImportId']:
+ return {
+ 'id': id,
+ 'type': type,
+ 'target': Pointer_stringify(Module['_BinaryenCallImportGetTarget'](expr)),
+ 'operands': getAllNested(expr, Module['_BinaryenCallImportGetNumOperands'], Module['_BinaryenCallImportGetOperand']),
+ };
+ case Module['CallIndirectId']:
+ return {
+ 'id': id,
+ 'type': type,
+ 'target': Module['_BinaryenCallIndirectGetTarget'](expr),
+ 'operands': getAllNested(expr, Module['_BinaryenCallIndirectGetNumOperands'], Module['_BinaryenCallIndirectGetOperand'])
+ };
+ case Module['GetLocalId']:
+ return {
+ 'id': id,
+ 'type': type,
+ 'index': Module['_BinaryenGetLocalGetIndex'](expr)
+ };
+ case Module['SetLocalId']:
+ return {
+ 'id': id,
+ 'type': type,
+ 'isTee': Boolean(Module['_BinaryenSetLocalIsTee'](expr)),
+ 'index': Module['_BinaryenSetLocalGetIndex'](expr),
+ 'value': Module['_BinaryenSetLocalGetValue'](expr)
+ };
+ case Module['GetGlobalId']:
+ return {
+ 'id': id,
+ 'type': type,
+ 'name': Pointer_stringify(Module['_BinaryenGetGlobalGetName'](expr))
+ };
+ case Module['SetGlobalId']:
+ return {
+ 'id': id,
+ 'type': type,
+ 'name': Pointer_stringify(Module['_BinaryenSetGlobalGetName'](expr)),
+ 'value': Module['_BinaryenSetGlobalGetValue'](expr)
+ };
+ case Module['LoadId']:
+ return {
+ 'id': id,
+ 'type': type,
+ 'isAtomic': Boolean(Module['_BinaryenLoadIsAtomic'](expr)),
+ 'isSigned': Boolean(Module['_BinaryenLoadIsSigned'](expr)),
+ 'offset': Module['_BinaryenLoadGetOffset'](expr),
+ 'bytes': Module['_BinaryenLoadGetBytes'](expr),
+ 'align': Module['_BinaryenLoadGetAlign'](expr),
+ 'ptr': Module['_BinaryenLoadGetPtr'](expr)
+ };
+ case Module['StoreId']:
+ return {
+ 'id': id,
+ 'type': type,
+ 'isAtomic': Boolean(Module['_BinaryenStoreIsAtomic'](expr)),
+ 'offset': Module['_BinaryenStoreGetOffset'](expr),
+ 'bytes': Module['_BinaryenStoreGetBytes'](expr),
+ 'align': Module['_BinaryenStoreGetAlign'](expr),
+ 'ptr': Module['_BinaryenStoreGetPtr'](expr),
+ 'value': Module['_BinaryenStoreGetValue'](expr)
+ };
+ case Module['ConstId']: {
+ var value;
+ switch (type) {
+ case Module['i32']: value = Module['_BinaryenConstGetValueI32'](expr); break;
+ case Module['i64']: value = { 'low': Module['_BinaryenConstGetValueI64Low'](expr), 'high': Module['_BinaryenConstGetValueI64High'](expr) }; break;
+ case Module['f32']: value = Module['_BinaryenConstGetValueF32'](expr); break;
+ case Module['f64']: value = Module['_BinaryenConstGetValueF64'](expr); break;
+ default: throw Error('unexpected type: ' + type);
+ }
+ return {
+ 'id': id,
+ 'type': type,
+ 'value': value
+ };
+ }
+ case Module['UnaryId']:
+ return {
+ 'id': id,
+ 'type': type,
+ 'op': Module['_BinaryenUnaryGetOp'](expr),
+ 'value': Module['_BinaryenUnaryGetValue'](expr)
+ };
+ case Module['BinaryId']:
+ return {
+ 'id': id,
+ 'type': type,
+ 'op': Module['_BinaryenBinaryGetOp'](expr),
+ 'left': Module['_BinaryenBinaryGetLeft'](expr),
+ 'right': Module['_BinaryenBinaryGetRight'](expr)
+ };
+ case Module['SelectId']:
+ return {
+ 'id': id,
+ 'type': type,
+ 'ifTrue': Module['_BinaryenSelectGetIfTrue'](expr),
+ 'ifFalse': Module['_BinaryenSelectGetIfFalse'](expr),
+ 'condition': Module['_BinaryenSelectGetCondition'](expr)
+ };
+ case Module['DropId']:
+ return {
+ 'id': id,
+ 'type': type,
+ 'value': Module['_BinaryenDropGetValue'](expr)
+ };
+ case Module['ReturnId']:
+ return {
+ 'id': id,
+ 'type': type,
+ 'value': Module['_BinaryenReturnGetValue'](expr)
+ };
+ case Module['NopId']:
+ case Module['UnreachableId']:
+ return {
+ 'id': id,
+ 'type': type
+ };
+ case Module['HostId']:
+ return {
+ 'id': id,
+ 'type': type,
+ 'op': Module['_BinaryenHostGetOp'](expr),
+ 'nameOperand': Pointer_stringify(Module['_BinaryenHostGetNameOperand'](expr)),
+ 'operands': getAllNested(expr, Module['_BinaryenHostGetNumOperands'], Module['_BinaryenHostGetOperand'])
+ };
+ case Module['AtomicRMWId']:
+ return {
+ 'id': id,
+ 'type': type,
+ 'op': Module['_BinaryenAtomicRMWGetOp'](expr),
+ 'bytes': Module['_BinaryenAtomicRMWGetBytes'](expr),
+ 'offset': Module['_BinaryenAtomicRMWGetOffset'](expr),
+ 'ptr': Module['_BinaryenAtomicRMWGetPtr'](expr),
+ 'value': Module['_BinaryenAtomicRMWGetValue'](expr)
+ };
+ case Module['AtomicCmpxchgId']:
+ return {
+ 'id': id,
+ 'type': type,
+ 'bytes': Module['_BinaryenAtomicCmpxchgGetBytes'](expr),
+ 'offset': Module['_BinaryenAtomicCmpxchgGetOffset'](expr),
+ 'ptr': Module['_BinaryenAtomicCmpxchgGetPtr'](expr),
+ 'expected': Module['_BinaryenAtomicCmpxchgGetExpected'](expr),
+ 'replacement': Module['_BinaryenAtomicCmpxchgGetReplacement'](expr)
+ };
+ case Module['AtomicWaitId']:
+ return {
+ 'id': id,
+ 'type': type,
+ 'ptr': Module['_BinaryenAtomicWaitGetPtr'](expr),
+ 'expected': Module['_BinaryenAtomicWaitGetExpected'](expr),
+ 'timeout': Module['_BinaryenAtomicWaitGetTimeout'](expr),
+ 'expectedType': Module['_BinaryenAtomicWaitGetExpectedType'](expr)
+ };
+ case Module['AtomicWakeId']:
+ return {
+ 'id': id,
+ 'type': type,
+ 'ptr': Module['_BinaryenAtomicWakeGetPtr'](expr),
+ 'wakeCount': Module['_BinaryenAtomicWakeGetWakeCount'](expr)
+ };
+ default:
+ throw Error('unexpected id: ' + id);
+ }
};
- Module['getConstValueI64'] = function(expr) {
+ // Obtains information about a 'FunctionType'
+ Module['getFunctionTypeInfo'] = function(func) {
return {
- 'low': Module['_BinaryenConstGetValueI64Low'](expr),
- 'high': Module['_BinaryenConstGetValueI64High'](expr)
+ 'name': Module['_BinaryenFunctionTypeGetName'](func),
+ 'params': getAllNested(func, Module['_BinaryenFunctionTypeGetNumParams'], Module['_BinaryenFunctionTypeGetParam']),
+ 'result': Module['_BinaryenFunctionTypeGetResult'](func)
};
};
- Module['getConstValueF32'] = function(expr) {
- return Module['_BinaryenConstGetValueF32'](expr);
+ // Obtains information about a 'Function'
+ Module['getFunctionInfo'] = function(func) {
+ return {
+ 'name': Module['_BinaryenFunctionGetName'](func),
+ 'type': Module['_BinaryenFunctionGetType'](func),
+ 'params': getAllNested(func, Module['_BinaryenFunctionGetNumParams'], Module['_BinaryenFunctionGetParam']),
+ 'result': Module['_BinaryenFunctionGetResult'](func),
+ 'vars': getAllNested(func, Module['_BinaryenFunctionGetNumVars'], Module['_BinaryenFunctionGetVar']),
+ 'body': Module['_BinaryenFunctionGetBody'](func)
+ };
};
- Module['getConstValueF64'] = function(expr) {
- return Module['_BinaryenConstGetValueF64'](expr);
+ // Obtains information about an 'Import'
+ Module['getImportInfo'] = function(import_) {
+ return {
+ 'kind': Module['_BinaryenImportGetKind'](import_),
+ 'module': Pointer_stringify(Module['_BinaryenImportGetModule'](import_)),
+ 'base': Pointer_stringify(Module['_BinaryenImportGetBase'](import_)),
+ 'name': Pointer_stringify(Module['_BinaryenImportGetName'](import_)),
+ 'globalType': Module['_BinaryenImportGetGlobalType'](import_),
+ 'functionType': Pointer_stringify(Module['_BinaryenImportGetFunctionType'](import_))
+ };
};
- Module['getFunctionBody'] = function(func) {
- return Module['_BinaryenFunctionGetBody'](func);
+ // Obtains information about an 'Export'
+ Module['getExportInfo'] = function(export_) {
+ return {
+ 'kind': Module['_BinaryenExportGetKind'](export_),
+ 'name': Pointer_stringify(Module['_BinaryenExportGetName'](export_)),
+ 'value': Pointer_stringify(Module['_BinaryenExportGetValue'](export_))
+ };
};
- // emit text of an expression or a module
+ // Emits text format of an expression or a module
Module['emitText'] = function(expr) {
if (typeof expr === 'object') {
return expr.emitText();
@@ -1248,6 +1512,7 @@
return ret;
};
+ // Parses a binary to a module
Module['readBinary'] = function(data) {
var buffer = allocate(data, 'i8', ALLOC_NORMAL);
var ptr = Module['_BinaryenModuleRead'](buffer, data.length);
@@ -1255,6 +1520,7 @@
return new Module['Module'](ptr);
};
+ // Parses text format to a module
Module['parseText'] = function(text) {
var buffer = _malloc(text.length + 1);
writeAsciiToMemory(text, buffer);
@@ -1263,23 +1529,24 @@
return new Module['Module'](ptr);
};
+ // Enables or disables C-API tracing
Module['setAPITracing'] = function(on) {
return Module['_BinaryenSetAPITracing'](on);
};
// Support AMD-compatible loaders by defining a factory function that returns 'Module'
- if (typeof define === "function" && define["amd"])
+ if (typeof define === 'function' && define['amd'])
define(function() { return Module; });
// Support CommonJS-compatible loaders by checking for 'require' and 'module.exports'
- else if (typeof require === "function" && typeof module !== "undefined" && module && module.exports)
+ else if (typeof require === 'function' && typeof module !== 'undefined' && module && module.exports)
module.exports = Module;
// Otherwise expose as 'Binaryen' globally checking for common names of the global object
// first (namely 'global' and 'window') and fall back to 'this' (i.e. within web workers).
else
- (typeof global !== "undefined" && global ||
- typeof window !== "undefined" && window ||
- this)["Binaryen"] = Module;
+ (typeof global !== 'undefined' && global ||
+ typeof window !== 'undefined' && window ||
+ this)['Binaryen'] = Module;
})();