From e4d6be7f1a92bd8809777e59583e2d7544b065a0 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Fri, 6 May 2016 10:28:40 -0700 Subject: comments in c api header --- src/binaryen-c.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/binaryen-c.h b/src/binaryen-c.h index da445dfb2..5a22169d9 100644 --- a/src/binaryen-c.h +++ b/src/binaryen-c.h @@ -214,7 +214,9 @@ BinaryenExpressionRef BinaryenCallIndirect(BinaryenModuleRef module, BinaryenExp // begins.) BinaryenExpressionRef BinaryenGetLocal(BinaryenModuleRef module, BinaryenIndex index, BinaryenType type); BinaryenExpressionRef BinaryenSetLocal(BinaryenModuleRef module, BinaryenIndex index, BinaryenExpressionRef value); +// Load: align can be 0, in which case it will be the natural alignment (equal to bytes) BinaryenExpressionRef BinaryenLoad(BinaryenModuleRef module, uint32_t bytes, int8_t signed_, uint32_t offset, uint32_t align, BinaryenType type, BinaryenExpressionRef ptr); +// Store: align can be 0, in which case it will be the natural alignment (equal to bytes) BinaryenExpressionRef BinaryenStore(BinaryenModuleRef module, uint32_t bytes, uint32_t offset, uint32_t align, BinaryenExpressionRef ptr, BinaryenExpressionRef value); BinaryenExpressionRef BinaryenConst(BinaryenModuleRef module, struct BinaryenLiteral value); BinaryenExpressionRef BinaryenUnary(BinaryenModuleRef module, BinaryenOp op, BinaryenExpressionRef value); @@ -302,10 +304,10 @@ RelooperBlockRef RelooperAddBlock(RelooperRef relooper, BinaryenExpressionRef co void RelooperAddBranch(RelooperBlockRef from, RelooperBlockRef to, BinaryenExpressionRef condition, BinaryenExpressionRef code); // Create a basic block that ends a switch on a condition -RelooperBlockRef RelooperAddBlockWithSwitch(RelooperRef relooper, BinaryenExpressionRef code, BinaryenExpressionRef condition); +// TODO RelooperBlockRef RelooperAddBlockWithSwitch(RelooperRef relooper, BinaryenExpressionRef code, BinaryenExpressionRef condition); // Create a switch-style branch to another basic block. The block's switch table will have an index for this branch -void RelooperAddBranchForSwitch(RelooperBlockRef from, RelooperBlockRef to, BinaryenIndex index, BinaryenExpressionRef code); +// TODO void RelooperAddBranchForSwitch(RelooperBlockRef from, RelooperBlockRef to, BinaryenIndex index, BinaryenExpressionRef code); // Generate structed wasm control flow from the CFG of blocks and branches that were created // on this relooper instance. This returns the rendered output, and also disposes of the -- cgit v1.2.3 From ffb9e4b9cfac5091dfe19b9b5215c43bd0f98e72 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Fri, 6 May 2016 15:31:47 -0700 Subject: add a return type parameter to call and call_import in the C API. we need it since we don't know the type while building functions --- src/binaryen-c.cpp | 6 ++++-- src/binaryen-c.h | 8 +++++-- src/wasm.h | 4 ++++ test/example/c-api-kitchen-sink.c | 16 +++++++++----- test/example/c-api-kitchen-sink.txt | 42 +++++++++++++++++++++---------------- 5 files changed, 49 insertions(+), 27 deletions(-) (limited to 'src') diff --git a/src/binaryen-c.cpp b/src/binaryen-c.cpp index d46125bb3..eca4ce5f1 100644 --- a/src/binaryen-c.cpp +++ b/src/binaryen-c.cpp @@ -209,21 +209,23 @@ BinaryenExpressionRef BinaryenSwitch(BinaryenModuleRef module, const char **name ret->finalize(); return ret; } -BinaryenExpressionRef BinaryenCall(BinaryenModuleRef module, const char *target, BinaryenExpressionRef* operands, BinaryenIndex numOperands) { +BinaryenExpressionRef BinaryenCall(BinaryenModuleRef module, const char *target, BinaryenExpressionRef* operands, BinaryenIndex numOperands, BinaryenType returnType) { auto* ret = ((Module*)module)->allocator.alloc(); ret->target = target; for (BinaryenIndex i = 0; i < numOperands; i++) { ret->operands.push_back((Expression*)operands[i]); } + ret->type = WasmType(returnType); ret->finalize(); return ret; } -BinaryenExpressionRef BinaryenCallImport(BinaryenModuleRef module, const char *target, BinaryenExpressionRef* operands, BinaryenIndex numOperands) { +BinaryenExpressionRef BinaryenCallImport(BinaryenModuleRef module, const char *target, BinaryenExpressionRef* operands, BinaryenIndex numOperands, BinaryenType returnType) { auto* ret = ((Module*)module)->allocator.alloc(); ret->target = target; for (BinaryenIndex i = 0; i < numOperands; i++) { ret->operands.push_back((Expression*)operands[i]); } + ret->type = WasmType(returnType); ret->finalize(); return ret; } diff --git a/src/binaryen-c.h b/src/binaryen-c.h index 5a22169d9..583454a9f 100644 --- a/src/binaryen-c.h +++ b/src/binaryen-c.h @@ -199,8 +199,12 @@ BinaryenExpressionRef BinaryenLoop(BinaryenModuleRef module, const char* out, co BinaryenExpressionRef BinaryenBreak(BinaryenModuleRef module, const char* name, BinaryenExpressionRef condition, BinaryenExpressionRef value); // Switch: value can be NULL BinaryenExpressionRef BinaryenSwitch(BinaryenModuleRef module, const char **names, BinaryenIndex numNames, const char* defaultName, BinaryenExpressionRef condition, BinaryenExpressionRef value); -BinaryenExpressionRef BinaryenCall(BinaryenModuleRef module, const char *target, BinaryenExpressionRef* operands, BinaryenIndex numOperands); -BinaryenExpressionRef BinaryenCallImport(BinaryenModuleRef module, const char *target, BinaryenExpressionRef* operands, BinaryenIndex numOperands); +// Call, CallImport: Note the 'returnType' parameter. You must declare the +// type returned by the function being called, as that +// function might not have been created yet, so we don't +// know what it is. +BinaryenExpressionRef BinaryenCall(BinaryenModuleRef module, const char *target, BinaryenExpressionRef* operands, BinaryenIndex numOperands, BinaryenType returnType); +BinaryenExpressionRef BinaryenCallImport(BinaryenModuleRef module, const char *target, BinaryenExpressionRef* operands, BinaryenIndex numOperands, BinaryenType returnType); BinaryenExpressionRef BinaryenCallIndirect(BinaryenModuleRef module, BinaryenExpressionRef target, BinaryenExpressionRef* operands, BinaryenIndex numOperands, BinaryenFunctionTypeRef type); // GetLocal: Note the 'type' parameter. It might seem redundant, since the // local at that index must have a type. However, this API lets you diff --git a/src/wasm.h b/src/wasm.h index dff0bcd5a..049dc2e30 100644 --- a/src/wasm.h +++ b/src/wasm.h @@ -1022,6 +1022,10 @@ public: ExpressionList operands; FunctionType *fullType; Expression *target; + + void finalize() { + type = fullType->result; + } }; class GetLocal : public SpecificExpression { diff --git a/test/example/c-api-kitchen-sink.c b/test/example/c-api-kitchen-sink.c index 4a63e931d..db1903cfd 100644 --- a/test/example/c-api-kitchen-sink.c +++ b/test/example/c-api-kitchen-sink.c @@ -165,9 +165,15 @@ void test_core() { BinaryenBreak(module, "the-body", NULL, NULL), BinaryenSwitch(module, switchValueNames, 1, "the-value", makeInt32(module, 0), makeInt32(module, 1)), BinaryenSwitch(module, switchBodyNames, 1, "the-body", makeInt32(module, 2), NULL), - BinaryenCall(module, "kitchen-sinker", callOperands4, 4), - BinaryenCallImport(module, "an-imported", callOperands2, 2), - BinaryenCallIndirect(module, makeInt32(module, 2449), callOperands4, 4, iiIfF), + BinaryenUnary(module, BinaryenEqZ(), // check the output type of the call node + BinaryenCall(module, "kitchen-sinker", callOperands4, 4, BinaryenInt32()) + ), + BinaryenUnary(module, BinaryenEqZ(), // check the output type of the call node + BinaryenCallImport(module, "an-imported", callOperands2, 2, BinaryenFloat32()) + ), + BinaryenUnary(module, BinaryenEqZ(), // check the output type of the call node + BinaryenCallIndirect(module, makeInt32(module, 2449), callOperands4, 4, iiIfF) + ), BinaryenGetLocal(module, 0, BinaryenInt32()), BinaryenSetLocal(module, 0, makeInt32(module, 101)), BinaryenLoad(module, 4, 0, 0, 0, BinaryenInt32(), makeInt32(module, 1)), @@ -195,8 +201,8 @@ void test_core() { // Imports BinaryenType iparams[2] = { BinaryenInt32(), BinaryenFloat64() }; - BinaryenFunctionTypeRef viF = BinaryenAddFunctionType(module, "viF", BinaryenNone(), iparams, 2); - BinaryenAddImport(module, "an-imported", "module", "base", viF); + BinaryenFunctionTypeRef fiF = BinaryenAddFunctionType(module, "fiF", BinaryenFloat32(), iparams, 2); + BinaryenAddImport(module, "an-imported", "module", "base", fiF); // Exports diff --git a/test/example/c-api-kitchen-sink.txt b/test/example/c-api-kitchen-sink.txt index cb037d80c..7a1366888 100644 --- a/test/example/c-api-kitchen-sink.txt +++ b/test/example/c-api-kitchen-sink.txt @@ -10,9 +10,9 @@ BinaryenFloat64: 4 (export "mem" memory) (start $starter) (type $iiIfF (func (param i32 i64 f32 f64) (result i32))) - (type $viF (func (param i32 f64))) + (type $fiF (func (param i32 f64) (result f32))) (type $v (func)) - (import $an-imported "module" "base" (param i32 f64)) + (import $an-imported "module" "base" (param i32 f64) (result f32)) (export "kitchen_sinker" $kitchen-sinker) (table $kitchen-sinker) (func $kitchen-sinker (type $iiIfF) (param $0 i32) (param $1 i64) (param $2 f32) (param $3 f64) (result i32) @@ -293,22 +293,28 @@ BinaryenFloat64: 4 (br_table $the-body $the-body (i32.const 2) ) - (call $kitchen-sinker - (i32.const 13) - (i64.const 37) - (f32.const 1.2999999523162842) - (f64.const 3.7) - ) - (call_import $an-imported - (i32.const 13) - (f64.const 3.7) - ) - (call_indirect $iiIfF - (i32.const 2449) - (i32.const 13) - (i64.const 37) - (f32.const 1.2999999523162842) - (f64.const 3.7) + (i32.eqz + (call $kitchen-sinker + (i32.const 13) + (i64.const 37) + (f32.const 1.2999999523162842) + (f64.const 3.7) + ) + ) + (f32.eqz + (call_import $an-imported + (i32.const 13) + (f64.const 3.7) + ) + ) + (i32.eqz + (call_indirect $iiIfF + (i32.const 2449) + (i32.const 13) + (i64.const 37) + (f32.const 1.2999999523162842) + (f64.const 3.7) + ) ) (get_local $0) (set_local $0 -- cgit v1.2.3