-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Extract BasicBlock to LinearIRBlock and BasicBlock classes - LinearIRBlock represents the original BasicBlock - BasicBlock is now a generic class for holding IRNode locations - Add Loc class to represent a location in BasicBlock - Update usages of BasicBlock to LinearIRBlock or BasicBlock - Remove unused methods and fields from LinearIRBlock - Add CFG class to hold graph data - Add commons-lang3 dependency
- Loading branch information
Showing
12 changed files
with
281 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 39 additions & 117 deletions
156
ep20/src/main/java/org/teachfx/antlr4/ep20/pass/cfg/BasicBlock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,140 +1,62 @@ | ||
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.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.symtab.scope.Scope; | ||
import org.teachfx.antlr4.ep20.ir.stmt.Label; | ||
import org.teachfx.antlr4.ep20.utils.Kind; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public class BasicBlock { | ||
public class BasicBlock<I extends IRNode> implements Iterable<Loc<I>> { | ||
|
||
public enum Kind { | ||
/// No jump instruction | ||
CONTINUOUS, | ||
/// Conditional instruction | ||
END_BY_CJMP, | ||
/// Unconditional jump instruction | ||
END_BY_JMP, | ||
/// Return instruction | ||
END_BY_RETURN | ||
} | ||
|
||
public Kind kind = Kind.CONTINUOUS; | ||
private static int LABEL_SEQ = 1; | ||
private ArrayList<IRNode> stmts; | ||
|
||
private List<BasicBlock> successors; | ||
|
||
private List<BasicBlock> predecessors; | ||
|
||
protected Scope scope = null; | ||
|
||
private List<IRNode> jmpRefMap = new ArrayList<>(); | ||
|
||
private int ord = 0; | ||
|
||
public BasicBlock() { | ||
stmts = new ArrayList<>(); | ||
successors = new ArrayList<>(); | ||
predecessors = new ArrayList<>(); | ||
ord = LABEL_SEQ++; | ||
} | ||
// Generate codes | ||
public List<Loc<I>> codes; | ||
|
||
public void addStmt(IRNode stmt) { | ||
stmts.add(stmt); | ||
updateKindByLastInstr(stmt); | ||
} | ||
public int id; | ||
|
||
private void updateKindByLastInstr(IRNode stmt) { | ||
if (stmt instanceof CJMP) { | ||
kind = Kind.END_BY_CJMP; | ||
} else if (stmt instanceof JMP) { | ||
kind = Kind.END_BY_JMP; | ||
} else if (stmt instanceof ReturnVal) { | ||
kind = Kind.END_BY_RETURN; | ||
} else { | ||
kind = Kind.CONTINUOUS; | ||
} | ||
} | ||
public static boolean isBasicBlock(Stmt stmt) { | ||
return !(stmt instanceof CJMP) && !(stmt instanceof JMP); | ||
} | ||
protected Optional<Label> label; | ||
|
||
public List<IRNode> getStmts() { | ||
return stmts; | ||
} | ||
|
||
public void setStmts(ArrayList<IRNode> stmts) { | ||
this.stmts = stmts; | ||
var lastInstr = stmts.get(stmts.size() - 1); | ||
updateKindByLastInstr(lastInstr); | ||
} | ||
|
||
public List<BasicBlock> getSuccessors() { | ||
return successors; | ||
} | ||
|
||
public void setSuccessors(List<BasicBlock> successors) { | ||
this.successors = successors; | ||
} | ||
|
||
public List<BasicBlock> getPredecessors() { | ||
return predecessors; | ||
} | ||
|
||
public void setPredecessors(List<BasicBlock> predecessors) { | ||
this.predecessors = predecessors; | ||
} | ||
|
||
public static void setLink(BasicBlock current,BasicBlock next) { | ||
current.successors.add(next); | ||
next.predecessors.add(current); | ||
} | ||
|
||
public void setLink(BasicBlock next) { | ||
BasicBlock.setLink(this,next); | ||
} | ||
|
||
public Scope getScope() { | ||
return scope; | ||
} | ||
|
||
public void setScope(Scope scope) { | ||
this.scope = scope; | ||
} | ||
public Kind kind; | ||
|
||
@NotNull | ||
@Override | ||
public String toString() { | ||
return "L"+ord; | ||
public Iterator<Loc<I>> iterator() { | ||
return codes.iterator(); | ||
} | ||
public Iterator<Loc<I>> backwardIterator() { | ||
return new Iterator<Loc<I>>() { | ||
private int index = codes.size() - 1; | ||
@Override | ||
public boolean hasNext() { | ||
return index != -1; | ||
} | ||
|
||
public List<IRNode> getJmpRefMap() { | ||
return jmpRefMap; | ||
@Override | ||
public Loc<I> next() { | ||
var loc = codes.get(index); | ||
index--; | ||
return loc; | ||
} | ||
}; | ||
} | ||
|
||
public void setJmpRefMap(List<IRNode> jmpRefMap) { | ||
this.jmpRefMap = jmpRefMap; | ||
public BasicBlock(Kind kind, int id, List<Loc<I>> codes, Optional<Label> label) { | ||
this.codes = codes; | ||
this.label = label; | ||
this.id = id; | ||
this.kind = kind; | ||
} | ||
|
||
public int getOrd() { | ||
return ord; | ||
public int getId() { | ||
return id; | ||
} | ||
|
||
|
||
public void refJMP(IRNode node) { | ||
jmpRefMap.add(node); | ||
// Generate isEmpty | ||
public boolean isEmpty() { | ||
return codes.isEmpty(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
return toString().equalsIgnoreCase(obj.toString()); | ||
} | ||
|
||
public IRNode getLastInstr() { | ||
return stmts.get(stmts.size() - 1); | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
ep20/src/main/java/org/teachfx/antlr4/ep20/pass/cfg/CFG.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,36 @@ | ||
package org.teachfx.antlr4.ep20.pass.cfg; | ||
|
||
import org.apache.commons.lang3.tuple.Pair; | ||
import org.teachfx.antlr4.ep20.ir.IRNode; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.TreeSet; | ||
|
||
public class CFG { | ||
public final List<IRNode> 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) { | ||
// Generate init | ||
this.nodes = nodes; | ||
this.edges = edges; | ||
|
||
links = new ArrayList<>(); | ||
for (var i = 0;i < nodes.size();i++) { | ||
links.add(Pair.of(new TreeSet<>(), new TreeSet<>())); | ||
} | ||
|
||
for (var edge : edges) { | ||
var u = edge.getLeft(); | ||
var v = edge.getRight(); | ||
links.get(u).getRight().add(v); | ||
links.get(v).getLeft().add(u); | ||
|
||
} | ||
} | ||
} |
Oops, something went wrong.