diff options
author | Alon Zakai <alonzakai@gmail.com> | 2016-05-03 17:14:07 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2016-05-03 17:14:07 -0700 |
commit | 5b2adeb4b2a66dfcda7667ce7a4c27ec49c62b1b (patch) | |
tree | 01dc41e6217c6026e4c97f0fe5bbc28066f79479 /test/example/c-api-hello-world.c | |
parent | d435670997e524df9707354d468a22432a181411 (diff) | |
parent | fd14d9cfc4cd744a8d143bb6a2622c68f5d33cfd (diff) | |
download | binaryen-5b2adeb4b2a66dfcda7667ce7a4c27ec49c62b1b.tar.gz binaryen-5b2adeb4b2a66dfcda7667ce7a4c27ec49c62b1b.tar.bz2 binaryen-5b2adeb4b2a66dfcda7667ce7a4c27ec49c62b1b.zip |
Merge pull request #427 from WebAssembly/c-api-nice
C API
Diffstat (limited to 'test/example/c-api-hello-world.c')
-rw-r--r-- | test/example/c-api-hello-world.c | 29 |
1 files changed, 29 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); +} + |