-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathBBHelper.java
More file actions
27 lines (24 loc) · 839 Bytes
/
BBHelper.java
File metadata and controls
27 lines (24 loc) · 839 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.compilerprogramming.ezlang.compiler;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.function.Consumer;
public class BBHelper {
/**
* Utility to locate all the basic blocks, order does not matter.
*/
public static List<BasicBlock> findAllBlocks(BasicBlock root) {
List<BasicBlock> nodes = new ArrayList<>();
postOrderWalk(root, (n) -> nodes.add(n), new HashSet<>());
return nodes;
}
static void postOrderWalk(BasicBlock n, Consumer<BasicBlock> consumer, HashSet<BasicBlock> visited) {
visited.add(n);
/* For each successor node */
for (BasicBlock s : n.successors) {
if (!visited.contains(s))
postOrderWalk(s, consumer, visited);
}
consumer.accept(n);
}
}