diff options
author | Alon Zakai <alonzakai@gmail.com> | 2016-05-02 16:00:55 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2016-05-02 21:41:46 -0700 |
commit | 5d2c7a86ba47ef0a0cb27508d81669c4a923a857 (patch) | |
tree | 18facfb7732102adb2c2b42ed5243d00f8f6c3b5 /test/example | |
parent | e888c841832895f292e8582c0043c8bf232e5d96 (diff) | |
download | binaryen-5d2c7a86ba47ef0a0cb27508d81669c4a923a857.tar.gz binaryen-5d2c7a86ba47ef0a0cb27508d81669c4a923a857.tar.bz2 binaryen-5d2c7a86ba47ef0a0cb27508d81669c4a923a857.zip |
C API plus test
Diffstat (limited to 'test/example')
-rw-r--r-- | test/example/c-api-hello-world.c | 29 | ||||
-rw-r--r-- | test/example/c-api-hello-world.txt | 10 |
2 files changed, 39 insertions, 0 deletions
diff --git a/test/example/c-api-hello-world.c b/test/example/c-api-hello-world.c new file mode 100644 index 000000000..016c2404b --- /dev/null +++ b/test/example/c-api-hello-world.c @@ -0,0 +1,29 @@ + +#include <binaryen-c.h> + +// "hello world" type example: create a function that adds two i32s and returns the result + +int main() { + BinaryenModuleRef module = BinaryenModuleCreate(); + + // Creation a function type for i32 (i32, i32) + BinaryenType params[2] = { BinaryenInt32(), BinaryenInt32() }; + BinaryenFunctionTypeRef iii = BinaryenAddFunctionType(module, "iii", BinaryenInt32(), params, 2); + + // Get the 0 and 1 arguments, and add them + BinaryenExpressionRef x = BinaryenGetLocal(module, 0, BinaryenInt32()), + y = BinaryenGetLocal(module, 1, BinaryenInt32()); + BinaryenExpressionRef add = BinaryenBinary(module, BinaryenAdd(), x, y); + + // Create the add function + // Note: no additional local variables + // Note: no basic blocks here, we are an AST. The function body is just an expression node. + BinaryenFunctionRef adder = BinaryenAddFunction(module, "adder", iii, NULL, 0, add); + + // Print it out + BinaryenModulePrint(module); + + // Clean up the module, which owns all the objects we created above + BinaryenModuleDispose(module); +} + diff --git a/test/example/c-api-hello-world.txt b/test/example/c-api-hello-world.txt new file mode 100644 index 000000000..4360107d3 --- /dev/null +++ b/test/example/c-api-hello-world.txt @@ -0,0 +1,10 @@ +(module + (memory 0) + (type $iii (func (param i32 i32) (result i32))) + (func $adder (type $iii) (param $0 i32) (param $1 i32) (result i32) + (i32.add + (get_local $0) + (get_local $1) + ) + ) +) |