summaryrefslogtreecommitdiff
path: root/src/binaryen-c.cpp
diff options
context:
space:
mode:
authorHeejin Ahn <aheejin@gmail.com>2021-01-15 18:48:00 +0900
committerGitHub <noreply@github.com>2021-01-15 18:48:00 +0900
commitbeccdf70258cd99ea25f10af13103e14dc243ffa (patch)
tree1081d7d350fbab7f901b917f2f082c8d351c3157 /src/binaryen-c.cpp
parentf18c18e01d03d6d293fe3d701408855bbcea58bd (diff)
downloadbinaryen-beccdf70258cd99ea25f10af13103e14dc243ffa.tar.gz
binaryen-beccdf70258cd99ea25f10af13103e14dc243ffa.tar.bz2
binaryen-beccdf70258cd99ea25f10af13103e14dc243ffa.zip
Basic EH instrucion support for the new spec (#3487)
This updates `try`-`catch`-`catch_all` and `rethrow` instructions to match the new spec. `delegate` is not included. Now `Try` contains not a single `catchBody` expression but a vector of catch bodies and events. This updates most existing routines, optimizations, and tests modulo the interpreter and the CFG traversal. Because the interpreter has not been updated yet, the EH spec test is temporarily disabled in check.py. Also, because the CFG traversal for EH is not yet updated, several EH tests in `rse_all-features.wast`, which uses CFG traversal, are temporarily commented out. Also added a few more tests in existing EH test functions in test/passes. In the previous spec, `catch` was catching all exceptions so it was assumed that anything `try` body throws is caught by its `catch`, but now we can assume the same only if there is a `catch_all`. Newly added tests test cases when there is a `catch_all` and cases there are only `catch`es separately.
Diffstat (limited to 'src/binaryen-c.cpp')
-rw-r--r--src/binaryen-c.cpp127
1 files changed, 109 insertions, 18 deletions
diff --git a/src/binaryen-c.cpp b/src/binaryen-c.cpp
index 59ca3aebc..f08b5f777 100644
--- a/src/binaryen-c.cpp
+++ b/src/binaryen-c.cpp
@@ -1200,10 +1200,20 @@ BinaryenExpressionRef BinaryenRefEq(BinaryenModuleRef module,
BinaryenExpressionRef BinaryenTry(BinaryenModuleRef module,
BinaryenExpressionRef body,
- BinaryenExpressionRef catchBody) {
+ const char** catchEvents_,
+ BinaryenIndex numCatchEvents,
+ BinaryenExpressionRef* catchBodies_,
+ BinaryenIndex numCatchBodies) {
+ std::vector<Name> catchEvents;
+ std::vector<Expression*> catchBodies;
+ for (BinaryenIndex i = 0; i < numCatchEvents; i++) {
+ catchEvents.push_back(catchEvents_[i]);
+ }
+ for (BinaryenIndex i = 0; i < numCatchBodies; i++) {
+ catchBodies.push_back((Expression*)catchBodies_[i]);
+ }
return static_cast<Expression*>(
- Builder(*(Module*)module)
- .makeTry((Expression*)body, (Expression*)catchBody));
+ Builder(*(Module*)module).makeTry(body, catchEvents, catchBodies));
}
BinaryenExpressionRef BinaryenThrow(BinaryenModuleRef module,
@@ -1219,9 +1229,8 @@ BinaryenExpressionRef BinaryenThrow(BinaryenModuleRef module,
}
BinaryenExpressionRef BinaryenRethrow(BinaryenModuleRef module,
- BinaryenExpressionRef exnref) {
- return static_cast<Expression*>(
- Builder(*(Module*)module).makeRethrow((Expression*)exnref));
+ BinaryenIndex depth) {
+ return static_cast<Expression*>(Builder(*(Module*)module).makeRethrow(depth));
}
BinaryenExpressionRef BinaryenBrOnExn(BinaryenModuleRef module,
@@ -2755,17 +2764,101 @@ void BinaryenTrySetBody(BinaryenExpressionRef expr,
assert(bodyExpr);
static_cast<Try*>(expression)->body = (Expression*)bodyExpr;
}
-BinaryenExpressionRef BinaryenTryGetCatchBody(BinaryenExpressionRef expr) {
+BinaryenIndex BinaryenTryGetNumCatchEvents(BinaryenExpressionRef expr) {
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Try>());
+ return static_cast<Try*>(expression)->catchEvents.size();
+}
+BinaryenIndex BinaryenTryGetNumCatchBodies(BinaryenExpressionRef expr) {
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Try>());
+ return static_cast<Try*>(expression)->catchBodies.size();
+}
+const char* BinaryenTryGetCatchEventAt(BinaryenExpressionRef expr,
+ BinaryenIndex index) {
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Try>());
+ assert(index < static_cast<Try*>(expression)->catchEvents.size());
+ return static_cast<Try*>(expression)->catchEvents[index].c_str();
+}
+void BinaryenTrySetCatchEventAt(BinaryenExpressionRef expr,
+ BinaryenIndex index,
+ const char* catchEvent) {
auto* expression = (Expression*)expr;
assert(expression->is<Try>());
- return static_cast<Try*>(expression)->catchBody;
+ assert(index < static_cast<Try*>(expression)->catchEvents.size());
+ assert(catchEvent);
+ static_cast<Try*>(expression)->catchEvents[index] = catchEvent;
}
-void BinaryenTrySetCatchBody(BinaryenExpressionRef expr,
- BinaryenExpressionRef catchBodyExpr) {
+BinaryenIndex BinaryenTryAppendCatchEvent(BinaryenExpressionRef expr,
+ const char* catchEvent) {
auto* expression = (Expression*)expr;
assert(expression->is<Try>());
- assert(catchBodyExpr);
- static_cast<Try*>(expression)->catchBody = (Expression*)catchBodyExpr;
+ assert(catchEvent);
+ auto& list = static_cast<Try*>(expression)->catchEvents;
+ auto index = list.size();
+ list.push_back(catchEvent);
+ return index;
+}
+void BinaryenTryInsertCatchEventAt(BinaryenExpressionRef expr,
+ BinaryenIndex index,
+ const char* catchEvent) {
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Try>());
+ assert(catchEvent);
+ static_cast<Try*>(expression)->catchEvents.insertAt(index, catchEvent);
+}
+const char* BinaryenTryRemoveCatchEventAt(BinaryenExpressionRef expr,
+ BinaryenIndex index) {
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Try>());
+ return static_cast<Try*>(expression)->catchEvents.removeAt(index).c_str();
+}
+BinaryenExpressionRef BinaryenTryGetCatchBodyAt(BinaryenExpressionRef expr,
+ BinaryenIndex index) {
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Try>());
+ assert(index < static_cast<Try*>(expression)->catchBodies.size());
+ return static_cast<Try*>(expression)->catchBodies[index];
+}
+void BinaryenTrySetCatchBodyAt(BinaryenExpressionRef expr,
+ BinaryenIndex index,
+ BinaryenExpressionRef catchExpr) {
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Try>());
+ assert(index < static_cast<Try*>(expression)->catchBodies.size());
+ assert(catchExpr);
+ static_cast<Try*>(expression)->catchBodies[index] = (Expression*)catchExpr;
+}
+BinaryenIndex BinaryenTryAppendCatchBody(BinaryenExpressionRef expr,
+ BinaryenExpressionRef catchExpr) {
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Try>());
+ assert(catchExpr);
+ auto& list = static_cast<Try*>(expression)->catchBodies;
+ auto index = list.size();
+ list.push_back((Expression*)catchExpr);
+ return index;
+}
+void BinaryenTryInsertCatchBodyAt(BinaryenExpressionRef expr,
+ BinaryenIndex index,
+ BinaryenExpressionRef catchExpr) {
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Try>());
+ assert(catchExpr);
+ static_cast<Try*>(expression)
+ ->catchBodies.insertAt(index, (Expression*)catchExpr);
+}
+BinaryenExpressionRef BinaryenTryRemoveCatchBodyAt(BinaryenExpressionRef expr,
+ BinaryenIndex index) {
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Try>());
+ return static_cast<Try*>(expression)->catchBodies.removeAt(index);
+}
+int BinaryenTryHasCatchAll(BinaryenExpressionRef expr) {
+ auto* expression = (Expression*)expr;
+ assert(expression->is<Try>());
+ return static_cast<Try*>(expression)->hasCatchAll();
}
// Throw
const char* BinaryenThrowGetEvent(BinaryenExpressionRef expr) {
@@ -2825,17 +2918,15 @@ BinaryenExpressionRef BinaryenThrowRemoveOperandAt(BinaryenExpressionRef expr,
return static_cast<Throw*>(expression)->operands.removeAt(index);
}
// Rethrow
-BinaryenExpressionRef BinaryenRethrowGetExnref(BinaryenExpressionRef expr) {
+BinaryenIndex BinaryenRethrowGetDepth(BinaryenExpressionRef expr) {
auto* expression = (Expression*)expr;
assert(expression->is<Rethrow>());
- return static_cast<Rethrow*>(expression)->exnref;
+ return static_cast<Rethrow*>(expression)->depth;
}
-void BinaryenRethrowSetExnref(BinaryenExpressionRef expr,
- BinaryenExpressionRef exnrefExpr) {
+void BinaryenRethrowSetDepth(BinaryenExpressionRef expr, BinaryenIndex depth) {
auto* expression = (Expression*)expr;
assert(expression->is<Rethrow>());
- assert(exnrefExpr);
- static_cast<Rethrow*>(expression)->exnref = (Expression*)exnrefExpr;
+ static_cast<Rethrow*>(expression)->depth = depth;
}
// BrOnExn
const char* BinaryenBrOnExnGetEvent(BinaryenExpressionRef expr) {