Skip to content

Commit 9eb535b

Browse files
committed
Fix try-delegate targetted by try-delegate case
1 parent 046ebef commit 9eb535b

File tree

2 files changed

+29
-24
lines changed

2 files changed

+29
-24
lines changed

src/passes/TranslateEH.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -313,11 +313,9 @@ struct TranslateEHOldToNew
313313
curr->body, delegateBrTarget, brToOuter, Type(HeapType::exn, Nullable));
314314
}
315315
curr->body = builder.makeThrowRef(innerBody);
316-
assert(outerBlock->list.empty());
317-
outerBlock->list.push_back(curr->body);
318316
}
319317

320-
void processDelegate(Try* curr) {
318+
void processDelegate(Try* curr, Block* outerBlock) {
321319
Builder builder(*getModule());
322320
// Convert
323321
// (try
@@ -337,11 +335,20 @@ struct TranslateEHOldToNew
337335
// processDelegateTarget(), when we process the 'try' that is the target of
338336
// this try~delegate. See processDelegateTarget() for how the rest of the
339337
// conversion is completed.
340-
replaceCurrent(
338+
auto* tryTable =
341339
builder.makeTryTable(curr->body,
342340
{Name()},
343341
{delegateTargetToBrTarget[curr->delegateTarget]},
344-
{true}));
342+
{true});
343+
// If we need an outer block for other reasons (if this is a target of a
344+
// delegate), we insert the new try_table into it. If not we just replace
345+
// the current try with the new try_table.
346+
if (outerBlock) {
347+
outerBlock->list.push_back(tryTable);
348+
replaceCurrent(outerBlock);
349+
} else {
350+
replaceCurrent(tryTable);
351+
}
345352
}
346353

347354
void processCatches(Try* curr, Block* outerBlock) {
@@ -667,12 +674,10 @@ struct TranslateEHOldToNew
667674
items.swap(nextItems);
668675
}
669676

670-
// In case this was already popuated in processDelegateTarget(), we empty
671-
// the block to repopulate with the newly created structures here
672-
outerBlock->list.clear();
673677
for (auto* item : items) {
674678
outerBlock->list.push_back(item);
675679
}
680+
replaceCurrent(outerBlock);
676681
}
677682

678683
void visitTry(Try* curr) {
@@ -689,14 +694,10 @@ struct TranslateEHOldToNew
689694
processDelegateTarget(curr, outerBlock);
690695
}
691696
if (curr->isDelegate()) {
692-
processDelegate(curr);
697+
processDelegate(curr, outerBlock);
693698
} else { // try-catch or catch-less try
694699
processCatches(curr, outerBlock);
695700
}
696-
697-
if (outerBlock) {
698-
replaceCurrent(outerBlock);
699-
}
700701
}
701702

702703
void visitPop(Pop* curr) {

test/lit/passes/translate-eh-old-to-new.wast

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,7 +1201,7 @@
12011201
)
12021202

12031203
;; CHECK: (func $deletate-target-outer-try-unreachable (type $1)
1204-
;; CHECK-NEXT: (block $outer1
1204+
;; CHECK-NEXT: (try_table
12051205
;; CHECK-NEXT: (throw_ref
12061206
;; CHECK-NEXT: (block $l00 (result exnref)
12071207
;; CHECK-NEXT: (try_table (catch_all_ref $l00)
@@ -1357,12 +1357,14 @@
13571357
;; CHECK-NEXT: (throw_ref
13581358
;; CHECK-NEXT: (block $__binaryen_delegate_caller_target0 (result exnref)
13591359
;; CHECK-NEXT: (block $outer2
1360-
;; CHECK-NEXT: (throw_ref
1361-
;; CHECK-NEXT: (block $l01 (result exnref)
1362-
;; CHECK-NEXT: (try_table (catch_all_ref $l01)
1363-
;; CHECK-NEXT: (nop)
1360+
;; CHECK-NEXT: (try_table (catch_all_ref $__binaryen_delegate_caller_target0)
1361+
;; CHECK-NEXT: (throw_ref
1362+
;; CHECK-NEXT: (block $l01 (result exnref)
1363+
;; CHECK-NEXT: (try_table (catch_all_ref $l01)
1364+
;; CHECK-NEXT: (nop)
1365+
;; CHECK-NEXT: )
1366+
;; CHECK-NEXT: (br $outer2)
13641367
;; CHECK-NEXT: )
1365-
;; CHECK-NEXT: (br $outer2)
13661368
;; CHECK-NEXT: )
13671369
;; CHECK-NEXT: )
13681370
;; CHECK-NEXT: )
@@ -1389,11 +1391,13 @@
13891391
;; CHECK-NEXT: (block $__binaryen_delegate_caller_target0 (result exnref)
13901392
;; CHECK-NEXT: (return
13911393
;; CHECK-NEXT: (block $outer2 (result i32)
1392-
;; CHECK-NEXT: (throw_ref
1393-
;; CHECK-NEXT: (block $l01 (result exnref)
1394-
;; CHECK-NEXT: (br $outer2
1395-
;; CHECK-NEXT: (try_table (result i32) (catch_all_ref $l01)
1396-
;; CHECK-NEXT: (i32.const 0)
1394+
;; CHECK-NEXT: (try_table (catch_all_ref $__binaryen_delegate_caller_target0)
1395+
;; CHECK-NEXT: (throw_ref
1396+
;; CHECK-NEXT: (block $l01 (result exnref)
1397+
;; CHECK-NEXT: (br $outer2
1398+
;; CHECK-NEXT: (try_table (result i32) (catch_all_ref $l01)
1399+
;; CHECK-NEXT: (i32.const 0)
1400+
;; CHECK-NEXT: )
13971401
;; CHECK-NEXT: )
13981402
;; CHECK-NEXT: )
13991403
;; CHECK-NEXT: )

0 commit comments

Comments
 (0)