Skip to content

Commit

Permalink
improvement(semantic/cfg): better control flow for switch statements.
Browse files Browse the repository at this point in the history
  • Loading branch information
rzvxa committed Jun 5, 2024
1 parent b92c5f0 commit 93ce1a6
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 19 deletions.
9 changes: 5 additions & 4 deletions crates/oxc_semantic/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1068,12 +1068,13 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
);
}

if let Some(last) = switch_case_conditions.last() {
self.cfg.add_edge(*last, self.cfg.current_node_ix, EdgeType::Normal);
let end_of_switch_case_statement = self.cfg.new_basic_block_normal();

if let Some(last) = ends_of_switch_cases.last() {
self.cfg.add_edge(*last, end_of_switch_case_statement, EdgeType::Normal);
}

let current_node_ix = self.cfg.current_node_ix;
self.cfg.ctx(None).mark_break(current_node_ix).resolve();
self.cfg.ctx(None).mark_break(end_of_switch_case_statement).resolve();
/* cfg */

self.leave_scope();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ digraph {
7 [ label = "" ]
8 [ label = "ExpressionStatement" ]
9 [ label = "" ]
10 [ label = "" ]
1 -> 0 [ label = "Error(Implicit)" ]
2 -> 0 [ label = "Error(Implicit)" ]
3 -> 0 [ label = "Error(Implicit)" ]
Expand All @@ -29,12 +30,13 @@ digraph {
6 -> 7 [ label = "Normal" ]
3 -> 4 [ label = "Normal" ]
3 -> 7 [ label = "Normal" ]
7 -> 8 [ label = "Normal" ]
9 -> 0 [ label = "Error(Implicit)" ]
8 -> 9 [ label = "Normal" ]
10 -> 0 [ label = "Error(Implicit)" ]
1 -> 2 [ label = "Normal" ]
2 -> 3 [ label = "Normal" ]
8 -> 2 [ label = "Backedge" ]
8 -> 9 [ label = "Normal" ]
2 -> 9 [ label = "Normal" ]
9 -> 2 [ label = "Backedge" ]
9 -> 10 [ label = "Normal" ]
2 -> 10 [ label = "Normal" ]
5 -> 2 [ label = "Jump" ]
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,7 @@ bb8: {
bb9: {

}

bb10: {

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ digraph {
16 [ label = "ExpressionStatement\nExpressionStatement\nExpressionStatement" ]
17 [ label = "" ]
18 [ label = "ExpressionStatement\nreturn <value>" ]
19 [ label = "unreachable\nExpressionStatement\nreturn <value>" ]
20 [ label = "unreachable" ]
21 [ label = "" ]
19 [ label = "unreachable" ]
20 [ label = "ExpressionStatement\nreturn <value>" ]
21 [ label = "unreachable" ]
22 [ label = "" ]
1 -> 0 [ label = "Error(Implicit)" ]
3 -> 2 [ label = "Error(Implicit)" ]
1 -> 3 [ label = "NewFunction" ]
Expand Down Expand Up @@ -81,11 +82,12 @@ digraph {
16 -> 17 [ label = "Normal" ]
3 -> 15 [ label = "Normal" ]
3 -> 17 [ label = "Normal" ]
17 -> 19 [ label = "Normal" ]
5 -> 19 [ label = "Jump" ]
10 -> 19 [ label = "Jump" ]
20 -> 2 [ label = "Error(Implicit)" ]
19 -> 20 [ label = "Unreachable" , style = "dotted" ]
21 -> 0 [ label = "Error(Implicit)" ]
1 -> 21 [ label = "Normal" ]
19 -> 20 [ label = "Normal" ]
5 -> 20 [ label = "Jump" ]
10 -> 20 [ label = "Jump" ]
21 -> 2 [ label = "Error(Implicit)" ]
20 -> 21 [ label = "Unreachable" , style = "dotted" ]
22 -> 0 [ label = "Error(Implicit)" ]
1 -> 22 [ label = "Normal" ]
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,17 @@ bb18: {

bb19: {
unreachable
}

bb20: {
statement
return <value>
}

bb20: {
bb21: {
unreachable
}

bb21: {
bb22: {

}

0 comments on commit 93ce1a6

Please sign in to comment.