Skip to content

Commit 3babe5e

Browse files
authored
Order control flow loop and branch antecedents the same as Strada (#1231)
1 parent 5d559e3 commit 3babe5e

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

internal/binder/binder.go

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -550,21 +550,24 @@ func setFlowNodeReferenced(flow *ast.FlowNode) {
550550
}
551551
}
552552

553-
func hasAntecedent(list *ast.FlowList, antecedent *ast.FlowNode) bool {
554-
for list != nil {
553+
func (b *Binder) addAntecedent(label *ast.FlowLabel, antecedent *ast.FlowNode) {
554+
if antecedent.Flags&ast.FlowFlagsUnreachable != 0 {
555+
return
556+
}
557+
// If antecedent isn't already on the Antecedents list, add it to the end of the list
558+
var last *ast.FlowList
559+
for list := label.Antecedents; list != nil; list = list.Next {
555560
if list.Flow == antecedent {
556-
return true
561+
return
557562
}
558-
list = list.Next
563+
last = list
559564
}
560-
return false
561-
}
562-
563-
func (b *Binder) addAntecedent(label *ast.FlowLabel, antecedent *ast.FlowNode) {
564-
if antecedent.Flags&ast.FlowFlagsUnreachable == 0 && !hasAntecedent(label.Antecedents, antecedent) {
565-
label.Antecedents = b.newFlowList(antecedent, label.Antecedents)
566-
setFlowNodeReferenced(antecedent)
565+
if last == nil {
566+
label.Antecedents = b.newFlowList(antecedent, nil)
567+
} else {
568+
last.Next = b.newFlowList(antecedent, nil)
567569
}
570+
setFlowNodeReferenced(antecedent)
568571
}
569572

570573
func (b *Binder) finishFlowLabel(label *ast.FlowLabel) *ast.FlowNode {
@@ -1897,13 +1900,12 @@ func (b *Binder) bindWhileStatement(node *ast.Node) {
18971900
preWhileLabel := b.setContinueTarget(node, b.createLoopLabel())
18981901
preBodyLabel := b.createBranchLabel()
18991902
postWhileLabel := b.createBranchLabel()
1900-
topFlow := b.currentFlow
1903+
b.addAntecedent(preWhileLabel, b.currentFlow)
19011904
b.currentFlow = preWhileLabel
19021905
b.bindCondition(stmt.Expression, preBodyLabel, postWhileLabel)
19031906
b.currentFlow = b.finishFlowLabel(preBodyLabel)
19041907
b.bindIterativeStatement(stmt.Statement, postWhileLabel, preWhileLabel)
19051908
b.addAntecedent(preWhileLabel, b.currentFlow)
1906-
b.addAntecedent(preWhileLabel, topFlow)
19071909
b.currentFlow = b.finishFlowLabel(postWhileLabel)
19081910
}
19091911

@@ -1912,13 +1914,12 @@ func (b *Binder) bindDoStatement(node *ast.Node) {
19121914
preDoLabel := b.createLoopLabel()
19131915
preConditionLabel := b.setContinueTarget(node, b.createBranchLabel())
19141916
postDoLabel := b.createBranchLabel()
1915-
topFlow := b.currentFlow
1917+
b.addAntecedent(preDoLabel, b.currentFlow)
19161918
b.currentFlow = preDoLabel
19171919
b.bindIterativeStatement(stmt.Statement, postDoLabel, preConditionLabel)
19181920
b.addAntecedent(preConditionLabel, b.currentFlow)
19191921
b.currentFlow = b.finishFlowLabel(preConditionLabel)
19201922
b.bindCondition(stmt.Expression, preDoLabel, postDoLabel)
1921-
b.addAntecedent(preDoLabel, topFlow)
19221923
b.currentFlow = b.finishFlowLabel(postDoLabel)
19231924
}
19241925

@@ -1929,7 +1930,7 @@ func (b *Binder) bindForStatement(node *ast.Node) {
19291930
preIncrementorLabel := b.createBranchLabel()
19301931
postLoopLabel := b.createBranchLabel()
19311932
b.bind(stmt.Initializer)
1932-
topFlow := b.currentFlow
1933+
b.addAntecedent(preLoopLabel, b.currentFlow)
19331934
b.currentFlow = preLoopLabel
19341935
b.bindCondition(stmt.Condition, preBodyLabel, postLoopLabel)
19351936
b.currentFlow = b.finishFlowLabel(preBodyLabel)
@@ -1938,7 +1939,6 @@ func (b *Binder) bindForStatement(node *ast.Node) {
19381939
b.currentFlow = b.finishFlowLabel(preIncrementorLabel)
19391940
b.bind(stmt.Incrementor)
19401941
b.addAntecedent(preLoopLabel, b.currentFlow)
1941-
b.addAntecedent(preLoopLabel, topFlow)
19421942
b.currentFlow = b.finishFlowLabel(postLoopLabel)
19431943
}
19441944

@@ -1947,7 +1947,7 @@ func (b *Binder) bindForInOrForOfStatement(node *ast.Node) {
19471947
preLoopLabel := b.setContinueTarget(node, b.createLoopLabel())
19481948
postLoopLabel := b.createBranchLabel()
19491949
b.bind(stmt.Expression)
1950-
topFlow := b.currentFlow
1950+
b.addAntecedent(preLoopLabel, b.currentFlow)
19511951
b.currentFlow = preLoopLabel
19521952
if node.Kind == ast.KindForOfStatement {
19531953
b.bind(stmt.AwaitModifier)
@@ -1959,7 +1959,6 @@ func (b *Binder) bindForInOrForOfStatement(node *ast.Node) {
19591959
}
19601960
b.bindIterativeStatement(stmt.Statement, postLoopLabel, preLoopLabel)
19611961
b.addAntecedent(preLoopLabel, b.currentFlow)
1962-
b.addAntecedent(preLoopLabel, topFlow)
19631962
b.currentFlow = b.finishFlowLabel(postLoopLabel)
19641963
}
19651964

0 commit comments

Comments
 (0)