-
Notifications
You must be signed in to change notification settings - Fork 828
Description
Hey developers, I try to rewrite Wasm modules using the Binaryen C API, and the rewriting process may introduce new local variables to the function, however I could not find a way to append newly-introduced locals to the function via C API. Either there is one C API that serves this purpose and I do not recognize that (in this case could anyone point that out for me?), or it doesn't exist yet.
In the latter case, I manually add one such API called BinaryenFunctionAppendVar to binaryen-c.h/cpp, whose source code can be inspected in pull request #6213 . A sample code using such API may look like this:
#include <stdio.h>
#include <binaryen-c.h>
int main(void) {
BinaryenType par = BinaryenTypeNone();
BinaryenType res = BinaryenTypeInt32();
BinaryenModuleRef mod = BinaryenModuleCreate();
BinaryenExpressionRef local_get = BinaryenLocalGet(mod, 0, BinaryenTypeInt32());
BinaryenFunctionRef new_func = BinaryenAddFunction(mod, "test", par, res, NULL, 0, local_get);
printf("new local idx: %u\n", BinaryenFunctionAppendVar(new_func, BinaryenTypeInt32()));
printf("%d\n", BinaryenModuleValidate(mod));
BinaryenModulePrint(mod);
BinaryenModuleDispose(mod);
return 0;
}Compiling above code with following command:
clang test.c -o test -L/usr/local/lib -lbinaryen -Wl,-rpath,/usr/local/libThe terminal output looks like this, here the local index is counted from the function's local variables instead of parameters:
new local idx: 0
1
(module
(type $0 (func (result i32)))
(func $test (result i32)
(local $0 i32)
(local.get $0)
)If we could not find an off-the-shelf C API, maybe above API can be a possible solution.