Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 159 additions & 0 deletions src/binaryen-c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,12 @@ BinaryenType BinaryenTypeNullExternref(void) {
BinaryenType BinaryenTypeNullFuncref(void) {
return Type(HeapType::nofunc, Nullable).getID();
}
BinaryenType BinaryenTypeExnref(void) {
return Type(HeapType::exn, Nullable).getID();
}
BinaryenType BinaryenTypeNullExnref(void) {
return Type(HeapType::noexn, Nullable).getID();
}
BinaryenType BinaryenTypeUnreachable(void) { return Type::unreachable; }
BinaryenType BinaryenTypeAuto(void) { return uintptr_t(-1); }

Expand Down Expand Up @@ -302,6 +308,12 @@ BinaryenHeapType BinaryenHeapTypeNoext() {
BinaryenHeapType BinaryenHeapTypeNofunc() {
return static_cast<BinaryenHeapType>(HeapType::BasicHeapType::nofunc);
}
BinaryenHeapType BinaryenHeapTypeExn() {
return static_cast<BinaryenHeapType>(HeapType::BasicHeapType::exn);
}
BinaryenHeapType BinaryenHeapTypeNoexn() {
return static_cast<BinaryenHeapType>(HeapType::BasicHeapType::noexn);
}

bool BinaryenHeapTypeIsBasic(BinaryenHeapType heapType) {
return HeapType(heapType).isBasic();
Expand Down Expand Up @@ -1780,6 +1792,28 @@ BinaryenExpressionRef BinaryenTry(BinaryenModuleRef module,
return static_cast<Expression*>(ret);
}

BinaryenExpressionRef BinaryenTryTable(BinaryenModuleRef module,
BinaryenExpressionRef body,
const char** catchTags,
const char** catchDests,
const bool* catchRefs,
BinaryenIndex numCatches) {
std::vector<Name> tags;
std::vector<Name> dests;
std::vector<bool> refs;
tags.reserve(numCatches);
dests.reserve(numCatches);
refs.reserve(numCatches);
for (BinaryenIndex i = 0; i < numCatches; i++) {
tags.push_back(catchTags[i] ? Name(catchTags[i]) : Name());
dests.push_back(catchDests[i]);
refs.push_back(catchRefs[i]);
}
return static_cast<Expression*>(
Builder(*(Module*)module)
.makeTryTable((Expression*)body, tags, dests, refs));
}

BinaryenExpressionRef BinaryenThrow(BinaryenModuleRef module,
const char* tag,
BinaryenExpressionRef* operands,
Expand All @@ -1798,6 +1832,12 @@ BinaryenExpressionRef BinaryenRethrow(BinaryenModuleRef module,
Builder(*(Module*)module).makeRethrow(target));
}

BinaryenExpressionRef BinaryenThrowRef(BinaryenModuleRef module,
BinaryenExpressionRef exnref) {
return static_cast<Expression*>(
Builder(*(Module*)module).makeThrowRef((Expression*)exnref));
}

BinaryenExpressionRef BinaryenRefI31(BinaryenModuleRef module,
BinaryenExpressionRef value) {
return static_cast<Expression*>(
Expand Down Expand Up @@ -4086,6 +4126,112 @@ bool BinaryenTryIsDelegate(BinaryenExpressionRef expr) {
assert(expression->is<Try>());
return static_cast<Try*>(expression)->isDelegate();
}
// TryTable
BinaryenExpressionRef BinaryenTryTableGetBody(BinaryenExpressionRef expr) {
auto* expression = (Expression*)expr;
assert(expression->is<TryTable>());
return static_cast<TryTable*>(expression)->body;
}
void BinaryenTryTableSetBody(BinaryenExpressionRef expr,
BinaryenExpressionRef bodyExpr) {
auto* expression = (Expression*)expr;
assert(expression->is<TryTable>());
assert(bodyExpr);
static_cast<TryTable*>(expression)->body = (Expression*)bodyExpr;
}
BinaryenIndex BinaryenTryTableGetNumCatches(BinaryenExpressionRef expr) {
auto* expression = (Expression*)expr;
assert(expression->is<TryTable>());
return static_cast<TryTable*>(expression)->catchTags.size();
}
const char* BinaryenTryTableGetCatchTagAt(BinaryenExpressionRef expr,
BinaryenIndex index) {
auto* expression = (Expression*)expr;
assert(expression->is<TryTable>());
assert(index < static_cast<TryTable*>(expression)->catchTags.size());
auto name = static_cast<TryTable*>(expression)->catchTags[index];
return name.is() ? name.str.data() : nullptr;
}
void BinaryenTryTableSetCatchTagAt(BinaryenExpressionRef expr,
BinaryenIndex index,
const char* catchTag) {
auto* expression = (Expression*)expr;
assert(expression->is<TryTable>());
assert(index < static_cast<TryTable*>(expression)->catchTags.size());
static_cast<TryTable*>(expression)->catchTags[index] =
catchTag ? Name(catchTag) : Name();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aheejin do we need to call refinalize() here so that sentTypes is repopulated?

}
const char* BinaryenTryTableGetCatchDestAt(BinaryenExpressionRef expr,
BinaryenIndex index) {
auto* expression = (Expression*)expr;
assert(expression->is<TryTable>());
assert(index < static_cast<TryTable*>(expression)->catchDests.size());
return static_cast<TryTable*>(expression)->catchDests[index].str.data();
}
void BinaryenTryTableSetCatchDestAt(BinaryenExpressionRef expr,
BinaryenIndex index,
const char* catchDest) {
auto* expression = (Expression*)expr;
assert(expression->is<TryTable>());
assert(index < static_cast<TryTable*>(expression)->catchDests.size());
static_cast<TryTable*>(expression)->catchDests[index] = catchDest;
}
bool BinaryenTryTableIsCatchRefAt(BinaryenExpressionRef expr,
BinaryenIndex index) {
auto* expression = (Expression*)expr;
assert(expression->is<TryTable>());
assert(index < static_cast<TryTable*>(expression)->catchRefs.size());
return static_cast<TryTable*>(expression)->catchRefs[index];
}
void BinaryenTryTableSetCatchRefAt(BinaryenExpressionRef expr,
BinaryenIndex index,
bool catchRef) {
auto* expression = (Expression*)expr;
assert(expression->is<TryTable>());
assert(index < static_cast<TryTable*>(expression)->catchRefs.size());
static_cast<TryTable*>(expression)->catchRefs[index] = catchRef;
}
BinaryenIndex BinaryenTryTableAppendCatch(BinaryenExpressionRef expr,
const char* catchTag,
const char* catchDest,
bool catchRef) {
auto* expression = (Expression*)expr;
assert(expression->is<TryTable>());
assert(catchDest);
auto* tryTable = static_cast<TryTable*>(expression);
auto index = tryTable->catchTags.size();
tryTable->catchTags.push_back(catchTag ? Name(catchTag) : Name());
tryTable->catchDests.push_back(Name(catchDest));
tryTable->catchRefs.push_back(catchRef);
return index;
}
void BinaryenTryTableInsertCatchAt(BinaryenExpressionRef expr,
BinaryenIndex index,
const char* catchTag,
const char* catchDest,
bool catchRef) {
auto* expression = (Expression*)expr;
assert(expression->is<TryTable>());
assert(catchDest);
auto* tryTable = static_cast<TryTable*>(expression);
tryTable->catchTags.insertAt(index, catchTag ? Name(catchTag) : Name());
tryTable->catchDests.insertAt(index, Name(catchDest));
tryTable->catchRefs.insertAt(index, catchRef);
}
const char* BinaryenTryTableRemoveCatchAt(BinaryenExpressionRef expr,
BinaryenIndex index) {
auto* expression = (Expression*)expr;
assert(expression->is<TryTable>());
auto* tryTable = static_cast<TryTable*>(expression);
tryTable->catchTags.removeAt(index);
tryTable->catchRefs.removeAt(index);
return tryTable->catchDests.removeAt(index).str.data();
}
bool BinaryenTryTableHasCatchAll(BinaryenExpressionRef expr) {
auto* expression = (Expression*)expr;
assert(expression->is<TryTable>());
return static_cast<TryTable*>(expression)->hasCatchAll();
}
// Throw
const char* BinaryenThrowGetTag(BinaryenExpressionRef expr) {
auto* expression = (Expression*)expr;
Expand Down Expand Up @@ -4154,6 +4300,19 @@ void BinaryenRethrowSetTarget(BinaryenExpressionRef expr, const char* target) {
assert(expression->is<Rethrow>());
static_cast<Rethrow*>(expression)->target = target;
}
// ThrowRef
BinaryenExpressionRef BinaryenThrowRefGetExnref(BinaryenExpressionRef expr) {
auto* expression = (Expression*)expr;
assert(expression->is<ThrowRef>());
return static_cast<ThrowRef*>(expression)->exnref;
}
void BinaryenThrowRefSetExnref(BinaryenExpressionRef expr,
BinaryenExpressionRef exnrefExpr) {
auto* expression = (Expression*)expr;
assert(expression->is<ThrowRef>());
assert(exnrefExpr);
static_cast<ThrowRef*>(expression)->exnref = (Expression*)exnrefExpr;
}
// TupleMake
BinaryenIndex BinaryenTupleMakeGetNumOperands(BinaryenExpressionRef expr) {
auto* expression = (Expression*)expr;
Expand Down
84 changes: 84 additions & 0 deletions src/binaryen-c.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ BINARYEN_API BinaryenType BinaryenTypeStringref(void);
BINARYEN_API BinaryenType BinaryenTypeNullref(void);
BINARYEN_API BinaryenType BinaryenTypeNullExternref(void);
BINARYEN_API BinaryenType BinaryenTypeNullFuncref(void);
BINARYEN_API BinaryenType BinaryenTypeExnref(void);
BINARYEN_API BinaryenType BinaryenTypeNullExnref(void);
BINARYEN_API BinaryenType BinaryenTypeUnreachable(void);
// Not a real type. Used as the last parameter to BinaryenBlock to let
// the API figure out the type instead of providing one.
Expand Down Expand Up @@ -151,6 +153,8 @@ BINARYEN_API BinaryenHeapType BinaryenHeapTypeString(void);
BINARYEN_API BinaryenHeapType BinaryenHeapTypeNone(void);
BINARYEN_API BinaryenHeapType BinaryenHeapTypeNoext(void);
BINARYEN_API BinaryenHeapType BinaryenHeapTypeNofunc(void);
BINARYEN_API BinaryenHeapType BinaryenHeapTypeExn(void);
BINARYEN_API BinaryenHeapType BinaryenHeapTypeNoexn(void);

BINARYEN_API bool BinaryenHeapTypeIsBasic(BinaryenHeapType heapType);
BINARYEN_API bool BinaryenHeapTypeIsSignature(BinaryenHeapType heapType);
Expand Down Expand Up @@ -1037,6 +1041,14 @@ BinaryenTry(BinaryenModuleRef module,
BinaryenExpressionRef* catchBodies,
BinaryenIndex numCatchBodies,
const char* delegateTarget);
// TryTable: catch tag names may be NULL to denote catch_all or catch_all_ref.
// catchRefs[i] is true if the i-th catch is catch_ref or catch_all_ref.
BINARYEN_API BinaryenExpressionRef BinaryenTryTable(BinaryenModuleRef module,
BinaryenExpressionRef body,
const char** catchTags,
const char** catchDests,
const bool* catchRefs,
BinaryenIndex numCatches);
BINARYEN_API BinaryenExpressionRef
BinaryenThrow(BinaryenModuleRef module,
const char* tag,
Expand All @@ -1045,6 +1057,8 @@ BinaryenThrow(BinaryenModuleRef module,
BINARYEN_API BinaryenExpressionRef BinaryenRethrow(BinaryenModuleRef module,
const char* target);
BINARYEN_API BinaryenExpressionRef
BinaryenThrowRef(BinaryenModuleRef module, BinaryenExpressionRef exnref);
BINARYEN_API BinaryenExpressionRef
BinaryenTupleMake(BinaryenModuleRef module,
BinaryenExpressionRef* operands,
BinaryenIndex numOperands);
Expand Down Expand Up @@ -2362,6 +2376,67 @@ BINARYEN_API void BinaryenTrySetDelegateTarget(BinaryenExpressionRef expr,
// Gets whether a `try` expression is a try-delegate.
BINARYEN_API bool BinaryenTryIsDelegate(BinaryenExpressionRef expr);

// TryTable

// Gets the body expression of a `try_table` expression.
BINARYEN_API BinaryenExpressionRef
BinaryenTryTableGetBody(BinaryenExpressionRef expr);
// Sets the body expression of a `try_table` expression.
BINARYEN_API void BinaryenTryTableSetBody(BinaryenExpressionRef expr,
BinaryenExpressionRef bodyExpr);
// Gets the number of catch clauses of a `try_table` expression.
BINARYEN_API BinaryenIndex
BinaryenTryTableGetNumCatches(BinaryenExpressionRef expr);
// Gets the catch tag at the specified index of a `try_table` expression. Empty
// (NULL) for catch_all and catch_all_ref clauses.
BINARYEN_API const char*
BinaryenTryTableGetCatchTagAt(BinaryenExpressionRef expr, BinaryenIndex index);
// Sets the catch tag at the specified index of a `try_table` expression. Pass
// NULL for catch_all/catch_all_ref clauses.
BINARYEN_API void BinaryenTryTableSetCatchTagAt(BinaryenExpressionRef expr,
BinaryenIndex index,
const char* catchTag);
// Gets the catch destination label at the specified index of a `try_table`
// expression.
BINARYEN_API const char*
BinaryenTryTableGetCatchDestAt(BinaryenExpressionRef expr, BinaryenIndex index);
// Sets the catch destination label at the specified index of a `try_table`
// expression.
BINARYEN_API void BinaryenTryTableSetCatchDestAt(BinaryenExpressionRef expr,
BinaryenIndex index,
const char* catchDest);
// Gets whether the catch clause at the specified index of a `try_table`
// expression is a `catch_ref` or `catch_all_ref` clause (passes the exnref).
BINARYEN_API bool BinaryenTryTableIsCatchRefAt(BinaryenExpressionRef expr,
BinaryenIndex index);
// Sets whether the catch clause at the specified index of a `try_table`
// expression is a `catch_ref`/`catch_all_ref` clause.
BINARYEN_API void BinaryenTryTableSetCatchRefAt(BinaryenExpressionRef expr,
BinaryenIndex index,
bool catchRef);
// Appends a catch clause to a `try_table` expression, returning its insertion
// index. Pass NULL for `catchTag` for catch_all/catch_all_ref.
BINARYEN_API BinaryenIndex
BinaryenTryTableAppendCatch(BinaryenExpressionRef expr,
const char* catchTag,
const char* catchDest,
bool catchRef);
// Inserts a catch clause at the specified index of a `try_table` expression,
// moving existing clauses including the one previously at that index one
// index up.
BINARYEN_API void BinaryenTryTableInsertCatchAt(BinaryenExpressionRef expr,
BinaryenIndex index,
const char* catchTag,
const char* catchDest,
bool catchRef);
// Removes the catch clause at the specified index of a `try_table` expression,
// moving all subsequent clauses one index down. Returns the removed clause's
// destination label.
BINARYEN_API const char*
BinaryenTryTableRemoveCatchAt(BinaryenExpressionRef expr, BinaryenIndex index);
// Gets whether a `try_table` expression has a catch_all/catch_all_ref clause.
BINARYEN_API bool BinaryenTryTableHasCatchAll(BinaryenExpressionRef expr);

// Throw

// Gets the name of the tag being thrown by a `throw` expression.
Expand Down Expand Up @@ -2404,6 +2479,15 @@ BINARYEN_API const char* BinaryenRethrowGetTarget(BinaryenExpressionRef expr);
BINARYEN_API void BinaryenRethrowSetTarget(BinaryenExpressionRef expr,
const char* target);

// ThrowRef

// Gets the exnref operand of a `throw_ref` expression.
BINARYEN_API BinaryenExpressionRef
BinaryenThrowRefGetExnref(BinaryenExpressionRef expr);
// Sets the exnref operand of a `throw_ref` expression.
BINARYEN_API void BinaryenThrowRefSetExnref(BinaryenExpressionRef expr,
BinaryenExpressionRef exnrefExpr);

// TupleMake

// Gets the number of operands of a `tuple.make` expression.
Expand Down
Loading
Loading