Skip to content

Commit

Permalink
Refactor CFG classes
Browse files Browse the repository at this point in the history
- Extract CFG building logic to CFGBuilder
- Add helper methods in CFG for graph algorithms
- Add data members in BasicBlock for dataflow analysis
  • Loading branch information
whtoo committed Nov 16, 2023
1 parent 62aafd4 commit f86770f
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package org.teachfx.antlr4.ep20.pass.cfg;

import org.jetbrains.annotations.NotNull;

import org.teachfx.antlr4.ep20.ir.IRNode;
import org.teachfx.antlr4.ep20.ir.expr.Operand;
import org.teachfx.antlr4.ep20.ir.stmt.Label;
import org.teachfx.antlr4.ep20.utils.Kind;

import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.Set;

public class BasicBlock<I extends IRNode> implements Iterable<Loc<I>> {

Expand Down Expand Up @@ -57,6 +60,23 @@ public int getId() {
public boolean isEmpty() {
return codes.isEmpty();
}
public List<Loc<I>> allSeq() {
if (kind.equals(Kind.CONTINUOUS)){
return codes;
}
return codes.subList(0, codes.size() - 1);
}

public I getLastInstr() {
return codes.get(codes.size() - 1).instr;
}

// For data flow analysis
public Set<Operand> def;

public Set<Operand> liveUse;

public Set<Operand> liveIn;

public Set<Operand> liveOut;
}
38 changes: 31 additions & 7 deletions ep20/src/main/java/org/teachfx/antlr4/ep20/pass/cfg/CFG.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package org.teachfx.antlr4.ep20.pass.cfg;

import org.apache.commons.lang3.tuple.Pair;
import org.jetbrains.annotations.NotNull;
import org.teachfx.antlr4.ep20.ir.IRNode;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.*;

public class CFG {
public final List<IRNode> nodes;
public class CFG<I extends IRNode> implements Iterable<BasicBlock<I>> {
public final List<BasicBlock<I>> nodes;

public final List<Pair<Integer,Integer>> edges;

private List<Pair<Set<Integer>, Set<Integer>>> links;

public CFG(List<IRNode> nodes, List<Pair<Integer, Integer>> edges) {
public CFG(List<BasicBlock<I>> nodes, List<Pair<Integer, Integer>> edges) {
// Generate init
this.nodes = nodes;
this.edges = edges;
Expand All @@ -33,4 +31,30 @@ public CFG(List<IRNode> nodes, List<Pair<Integer, Integer>> edges) {

}
}

public BasicBlock<I> getBlock(int id) {
return nodes.get(id);
}

public Set<Integer> getPrev(int id) {
return links.get(id).getLeft();
}

public Set<Integer> getSucceed(int id) {
return links.get(id).getRight();
}

public int getInDegree(int id) {
return links.get(id).getLeft().size();
}

public int getOutDegree(int id) {
return links.get(id).getRight().size();
}

@NotNull
@Override
public Iterator<BasicBlock<I>> iterator() {
return nodes.iterator();
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
package org.teachfx.antlr4.ep20.pass.cfg;public class CFGBuilder {
package org.teachfx.antlr4.ep20.pass.cfg;

import org.teachfx.antlr4.ep20.ir.IRNode;

import java.util.List;

public class CFGBuilder<I extends IRNode> {
public CFGBuilder(List<I> instrs){

}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package org.teachfx.antlr4.ep20.pass.cfg;

import org.teachfx.antlr4.ep20.ir.IRNode;
import org.teachfx.antlr4.ep20.ir.stmt.CJMP;
import org.teachfx.antlr4.ep20.ir.stmt.JMP;
import org.teachfx.antlr4.ep20.ir.stmt.ReturnVal;
import org.teachfx.antlr4.ep20.ir.stmt.Stmt;
import org.teachfx.antlr4.ep20.ir.stmt.*;
import org.teachfx.antlr4.ep20.symtab.scope.Scope;
import org.teachfx.antlr4.ep20.utils.Kind;

Expand Down Expand Up @@ -99,6 +96,12 @@ public void setScope(Scope scope) {

@Override
public String toString() {
var firstInstr = stmts.get(0);

if (firstInstr instanceof Label){
return firstInstr.toString();
}

return "L"+ord;
}

Expand Down

0 comments on commit f86770f

Please sign in to comment.