summaryrefslogtreecommitdiff
path: root/src/wasm
diff options
context:
space:
mode:
authorThomas Lively <tlively@google.com>2024-01-04 14:50:29 -0800
committerGitHub <noreply@github.com>2024-01-04 14:50:29 -0800
commitd312604d46ff3e1e14228394a2a4536b71483520 (patch)
tree060129b6d166495c89169aef152413357660da59 /src/wasm
parenta58281ca114359cd6e65f5daaf086636aa18b0b0 (diff)
downloadbinaryen-d312604d46ff3e1e14228394a2a4536b71483520.tar.gz
binaryen-d312604d46ff3e1e14228394a2a4536b71483520.tar.bz2
binaryen-d312604d46ff3e1e14228394a2a4536b71483520.zip
[Parser] Parse br_if correctly (#6202)
The new text parser and IRBuilder were previously not differentiating between `br` and `br_if`. Handle `br_if` correctly by popping and assigning a condition.
Diffstat (limited to 'src/wasm')
-rw-r--r--src/wasm/wasm-ir-builder.cpp11
-rw-r--r--src/wasm/wasm-s-parser.cpp4
2 files changed, 11 insertions, 4 deletions
diff --git a/src/wasm/wasm-ir-builder.cpp b/src/wasm/wasm-ir-builder.cpp
index 43a989c6e..e8a2a2386 100644
--- a/src/wasm/wasm-ir-builder.cpp
+++ b/src/wasm/wasm-ir-builder.cpp
@@ -430,6 +430,11 @@ Result<Expression*> IRBuilder::getBranchValue(Name labelName,
}
Result<> IRBuilder::visitBreak(Break* curr, std::optional<Index> label) {
+ if (curr->condition) {
+ auto cond = pop();
+ CHECK_ERR(cond);
+ curr->condition = *cond;
+ }
auto value = getBranchValue(curr->name, label);
CHECK_ERR(value);
curr->value = *value;
@@ -961,13 +966,15 @@ Result<> IRBuilder::makeLoop(Name label, Type type) {
return visitLoopStart(loop);
}
-Result<> IRBuilder::makeBreak(Index label) {
+Result<> IRBuilder::makeBreak(Index label, bool isConditional) {
auto name = getLabelName(label);
CHECK_ERR(name);
Break curr;
curr.name = *name;
+ // Use a dummy condition value if we need to pop a condition.
+ curr.condition = isConditional ? &curr : nullptr;
CHECK_ERR(visitBreak(&curr, label));
- push(builder.makeBreak(curr.name, curr.value));
+ push(builder.makeBreak(curr.name, curr.value, curr.condition));
return Ok{};
}
diff --git a/src/wasm/wasm-s-parser.cpp b/src/wasm/wasm-s-parser.cpp
index 9270a19b0..9dbfc88c2 100644
--- a/src/wasm/wasm-s-parser.cpp
+++ b/src/wasm/wasm-s-parser.cpp
@@ -2580,7 +2580,7 @@ Name SExpressionWasmBuilder::getLabel(Element& s, LabelType labelType) {
}
}
-Expression* SExpressionWasmBuilder::makeBreak(Element& s) {
+Expression* SExpressionWasmBuilder::makeBreak(Element& s, bool isConditional) {
auto ret = allocator.alloc<Break>();
size_t i = 1;
ret->name = getLabel(*s[i]);
@@ -2588,7 +2588,7 @@ Expression* SExpressionWasmBuilder::makeBreak(Element& s) {
if (i == s.size()) {
return ret;
}
- if (elementStartsWith(s, BR_IF)) {
+ if (isConditional) {
if (i + 1 < s.size()) {
ret->value = parseExpression(s[i]);
i++;