summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2016-05-06 15:31:47 -0700
committerAlon Zakai <alonzakai@gmail.com>2016-05-06 16:51:32 -0700
commitffb9e4b9cfac5091dfe19b9b5215c43bd0f98e72 (patch)
tree6a6320d42db20730620c6212846bef53e2c22143
parente4d6be7f1a92bd8809777e59583e2d7544b065a0 (diff)
downloadbinaryen-ffb9e4b9cfac5091dfe19b9b5215c43bd0f98e72.tar.gz
binaryen-ffb9e4b9cfac5091dfe19b9b5215c43bd0f98e72.tar.bz2
binaryen-ffb9e4b9cfac5091dfe19b9b5215c43bd0f98e72.zip
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
-rw-r--r--src/binaryen-c.cpp6
-rw-r--r--src/binaryen-c.h8
-rw-r--r--src/wasm.h4
-rw-r--r--test/example/c-api-kitchen-sink.c16
-rw-r--r--test/example/c-api-kitchen-sink.txt42
5 files changed, 49 insertions, 27 deletions
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<Call>();
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<CallImport>();
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<Expression::GetLocalId> {
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