summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorThomas Lively <7121787+tlively@users.noreply.github.com>2019-07-15 17:12:10 -0700
committerGitHub <noreply@github.com>2019-07-15 17:12:10 -0700
commitf6ca3828aca8e7da966ba158dd9dab16205206db (patch)
tree899995b7072e31039b435d679a66d11b56fb8730 /src
parentc7e927179b4ce67bbf4f4c10a62e0fb0f7960750 (diff)
downloadbinaryen-f6ca3828aca8e7da966ba158dd9dab16205206db.tar.gz
binaryen-f6ca3828aca8e7da966ba158dd9dab16205206db.tar.bz2
binaryen-f6ca3828aca8e7da966ba158dd9dab16205206db.zip
Tail call C/JS API (#2223)
Diffstat (limited to 'src')
-rw-r--r--src/binaryen-c.cpp73
-rw-r--r--src/binaryen-c.h12
-rw-r--r--src/js/binaryen.js-post.js10
3 files changed, 78 insertions, 17 deletions
diff --git a/src/binaryen-c.cpp b/src/binaryen-c.cpp
index c22ab4411..fd0ac889a 100644
--- a/src/binaryen-c.cpp
+++ b/src/binaryen-c.cpp
@@ -977,11 +977,12 @@ BinaryenExpressionRef BinaryenSwitch(BinaryenModuleRef module,
ret->finalize();
return static_cast<Expression*>(ret);
}
-BinaryenExpressionRef BinaryenCall(BinaryenModuleRef module,
- const char* target,
- BinaryenExpressionRef* operands,
- BinaryenIndex numOperands,
- BinaryenType returnType) {
+static BinaryenExpressionRef makeBinaryenCall(BinaryenModuleRef module,
+ const char* target,
+ BinaryenExpressionRef* operands,
+ BinaryenIndex numOperands,
+ BinaryenType returnType,
+ bool isReturn) {
auto* ret = ((Module*)module)->allocator.alloc<Call>();
if (tracing) {
@@ -999,7 +1000,7 @@ BinaryenExpressionRef BinaryenCall(BinaryenModuleRef module,
}
std::cout << " };\n ";
traceExpression(ret,
- "BinaryenCall",
+ (isReturn ? "BinaryenReturnCall" : "BinaryenCall"),
StringLit(target),
"operands",
numOperands,
@@ -1012,14 +1013,33 @@ BinaryenExpressionRef BinaryenCall(BinaryenModuleRef module,
ret->operands.push_back((Expression*)operands[i]);
}
ret->type = Type(returnType);
+ ret->isReturn = isReturn;
ret->finalize();
return static_cast<Expression*>(ret);
}
-BinaryenExpressionRef BinaryenCallIndirect(BinaryenModuleRef module,
- BinaryenExpressionRef target,
- BinaryenExpressionRef* operands,
- BinaryenIndex numOperands,
- const char* type) {
+BinaryenExpressionRef BinaryenCall(BinaryenModuleRef module,
+ const char* target,
+ BinaryenExpressionRef* operands,
+ BinaryenIndex numOperands,
+ BinaryenType returnType) {
+ return makeBinaryenCall(
+ module, target, operands, numOperands, returnType, false);
+}
+BinaryenExpressionRef BinaryenReturnCall(BinaryenModuleRef module,
+ const char* target,
+ BinaryenExpressionRef* operands,
+ BinaryenIndex numOperands,
+ BinaryenType returnType) {
+ return makeBinaryenCall(
+ module, target, operands, numOperands, returnType, true);
+}
+static BinaryenExpressionRef
+makeBinaryenCallIndirect(BinaryenModuleRef module,
+ BinaryenExpressionRef target,
+ BinaryenExpressionRef* operands,
+ BinaryenIndex numOperands,
+ const char* type,
+ bool isReturn) {
auto* wasm = (Module*)module;
auto* ret = wasm->allocator.alloc<CallIndirect>();
@@ -1037,12 +1057,13 @@ BinaryenExpressionRef BinaryenCallIndirect(BinaryenModuleRef module,
std::cout << "0";
}
std::cout << " };\n ";
- traceExpression(ret,
- "BinaryenCallIndirect",
- target,
- "operands",
- numOperands,
- StringLit(type));
+ traceExpression(
+ ret,
+ (isReturn ? "BinaryenReturnCallIndirect" : "BinaryenCallIndirect"),
+ target,
+ "operands",
+ numOperands,
+ StringLit(type));
std::cout << " }\n";
}
@@ -1052,9 +1073,27 @@ BinaryenExpressionRef BinaryenCallIndirect(BinaryenModuleRef module,
}
ret->fullType = type;
ret->type = wasm->getFunctionType(ret->fullType)->result;
+ ret->isReturn = isReturn;
ret->finalize();
return static_cast<Expression*>(ret);
}
+BinaryenExpressionRef BinaryenCallIndirect(BinaryenModuleRef module,
+ BinaryenExpressionRef target,
+ BinaryenExpressionRef* operands,
+ BinaryenIndex numOperands,
+ const char* type) {
+ return makeBinaryenCallIndirect(
+ module, target, operands, numOperands, type, false);
+}
+BinaryenExpressionRef
+BinaryenReturnCallIndirect(BinaryenModuleRef module,
+ BinaryenExpressionRef target,
+ BinaryenExpressionRef* operands,
+ BinaryenIndex numOperands,
+ const char* type) {
+ return makeBinaryenCallIndirect(
+ module, target, operands, numOperands, type, true);
+}
BinaryenExpressionRef BinaryenLocalGet(BinaryenModuleRef module,
BinaryenIndex index,
BinaryenType type) {
diff --git a/src/binaryen-c.h b/src/binaryen-c.h
index b186032f5..db49c45c9 100644
--- a/src/binaryen-c.h
+++ b/src/binaryen-c.h
@@ -545,6 +545,18 @@ BinaryenExpressionRef BinaryenCallIndirect(BinaryenModuleRef module,
BinaryenExpressionRef* operands,
BinaryenIndex numOperands,
const char* type);
+BinaryenExpressionRef BinaryenReturnCall(BinaryenModuleRef module,
+ const char* target,
+ BinaryenExpressionRef* operands,
+ BinaryenIndex numOperands,
+ BinaryenType returnType);
+BinaryenExpressionRef
+BinaryenReturnCallIndirect(BinaryenModuleRef module,
+ BinaryenExpressionRef target,
+ BinaryenExpressionRef* operands,
+ BinaryenIndex numOperands,
+ const char* type);
+
// LocalGet: Note the 'type' parameter. It might seem redundant, since the
// local at that index must have a type. However, this API lets you
// build code "top-down": create a node, then its parents, and so
diff --git a/src/js/binaryen.js-post.js b/src/js/binaryen.js-post.js
index 1760f6359..5b24f3a8f 100644
--- a/src/js/binaryen.js-post.js
+++ b/src/js/binaryen.js-post.js
@@ -450,6 +450,16 @@ function wrapModule(module, self) {
return Module['_BinaryenCallIndirect'](module, target, i32sToStack(operands), operands.length, strToStack(type));
});
};
+ self['returnCall'] = function(name, operands, type) {
+ return preserveStack(function() {
+ return Module['_BinaryenReturnCall'](module, strToStack(name), i32sToStack(operands), operands.length, type);
+ });
+ };
+ self['returnCallIndirect'] = function(target, operands, type) {
+ return preserveStack(function() {
+ return Module['_BinaryenReturnCallIndirect'](module, target, i32sToStack(operands), operands.length, strToStack(type));
+ });
+ };
self['local'] = {
'get': function(index, type) {