summaryrefslogtreecommitdiff
path: root/vaporize/library/CFGSimp.java
diff options
context:
space:
mode:
Diffstat (limited to 'vaporize/library/CFGSimp.java')
-rw-r--r--vaporize/library/CFGSimp.java72
1 files changed, 22 insertions, 50 deletions
diff --git a/vaporize/library/CFGSimp.java b/vaporize/library/CFGSimp.java
index 54ec61c..30ddc7d 100644
--- a/vaporize/library/CFGSimp.java
+++ b/vaporize/library/CFGSimp.java
@@ -2,6 +2,7 @@ package vaporize.library;
import cs132.vapor.ast.*;
import graphviz.*;
+import cfg.*;
import misc.*;
import java.io.File;
@@ -13,7 +14,6 @@ public class CFGSimp extends VInstr.VisitorPR<ControlFlowGraph, String, RuntimeE
private Kettle kettle;
private ArrayList<ControlFlowGraph> cfgs;
- private VFunction func; // the current function being processed
private CFGNode curr; // the current node being processed
private String dot_format; // a list of edges to be processed by graphviz
@@ -24,22 +24,15 @@ public class CFGSimp extends VInstr.VisitorPR<ControlFlowGraph, String, RuntimeE
for (VFunction f : vp.functions) {
- this.func = f;
- ControlFlowGraph cfg = new ControlFlowGraph();
+ ControlFlowGraph cfg = new ControlFlowGraph(f);
this.dot_format = "";
MinimalLogger.info(String.format("CFGSimp is collecting nodes for %s",
- this.kettle.parseFuncName(this.func)));
- for (VCodeLabel s : this.func.labels) {
- MinimalLogger.info(String.format("Label %d \"%s\"",
- s.sourcePos.line,
- this.kettle.get(s).trim()));
+ this.kettle.parseFuncName(f)));
+ for (VCodeLabel s : f.labels) {
cfg.addNode(new CFGNode(s));
}
- for (VInstr s : this.func.body) {
- MinimalLogger.info(String.format(" Body %d \"%s\"",
- s.sourcePos.line,
- this.kettle.get(s).trim()));
+ for (VInstr s : f.body) {
cfg.addNode(new CFGNode(s));
}
@@ -50,22 +43,21 @@ public class CFGSimp extends VInstr.VisitorPR<ControlFlowGraph, String, RuntimeE
// inital setup
// first visit may not find edges; cfg.addEdges will handle
this.curr = new CFGNode(f.body[0]);
- cfg.setStart(curr);
// cascades downwards --- cfg.addEdges
for (VVarRef.Local l : f.params)
- this.curr.addReaching(l.ident.toString());
- for (VInstr s : this.func.body)
+ cfg.addReaching(this.curr, l.ident.toString());
+ for (VInstr s : f.body)
s.accept(cfg, this);
MinimalLogger.info(String.format("Spitting out reaching/liveness..."));
for (CFGNode n : cfg.getNodes())
- MinimalLogger.info(String.format("%d ::: %s ::: %s",
- n.getLine(),
+ MinimalLogger.info(String.format("%s ::: %s ::: %s",
+ n.toString(),
n.getReaching(),
n.getLiveness()));
if (this.use_graphviz)
- this.createDotGraph(this.kettle.parseFuncName(this.func));
+ this.createDotGraph(this.kettle.parseFuncName(f));
this.cfgs.add(cfg);
}
@@ -92,15 +84,6 @@ public class CFGSimp extends VInstr.VisitorPR<ControlFlowGraph, String, RuntimeE
gv.writeGraphToFile( gv.getGraph( gv.getDotSource(), type ), out );
}
- protected boolean isKnownVar(String str) {
- /**
- * Returns true if the variable belongs to the current function.
- * Required when matching liveness in certain instructions.
- */
- return Arrays.asList(this.func.vars).contains(str) ||
- Arrays.asList(this.func.params).contains(str);
- }
-
public String visit(ControlFlowGraph cfg, VMemRead n) throws RuntimeException {
MinimalLogger.info(String.format("->%s (\"%s\":%s)",
n.getClass().getSimpleName(),
@@ -111,10 +94,8 @@ public class CFGSimp extends VInstr.VisitorPR<ControlFlowGraph, String, RuntimeE
this.dot_format += cfg.addEdge(this.curr, curr);
this.curr = curr;
- curr.addReaching(n.dest.toString());
- String source = ((VMemRef.Global) n.source).base.toString();
- if (isKnownVar(source))
- curr.addLiveness(source);
+ cfg.addReaching(curr, n.dest.toString());
+ cfg.addLiveness(curr, ((VMemRef.Global) n.source).base.toString());
///////////////////////////////////////////////////////////////
MinimalLogger.info(String.format("<-%s (\"%s\":%s)",
n.getClass().getSimpleName(),
@@ -133,9 +114,7 @@ public class CFGSimp extends VInstr.VisitorPR<ControlFlowGraph, String, RuntimeE
this.dot_format += cfg.addEdge(this.curr, curr);
this.curr = curr;
- String source = n.source.toString();
- if (isKnownVar(source))
- curr.addLiveness(source);
+ cfg.addLiveness(curr, n.source.toString());
///////////////////////////////////////////////////////////////
MinimalLogger.info(String.format("<-%s (\"%s\":%s)",
n.getClass().getSimpleName(),
@@ -154,10 +133,8 @@ public class CFGSimp extends VInstr.VisitorPR<ControlFlowGraph, String, RuntimeE
this.dot_format += cfg.addEdge(this.curr, curr);
this.curr = curr;
- curr.addReaching(n.dest.toString());
- String source = n.source.toString();
- if (isKnownVar(source))
- curr.addLiveness(source);
+ cfg.addReaching(curr, n.dest.toString());
+ cfg.addLiveness(curr, n.source.toString());
///////////////////////////////////////////////////////////////
MinimalLogger.info(String.format("<-%s (\"%s\":%s)",
n.getClass().getSimpleName(),
@@ -177,8 +154,7 @@ public class CFGSimp extends VInstr.VisitorPR<ControlFlowGraph, String, RuntimeE
this.dot_format += cfg.addEdge(curr, cfg.getNode(new Integer(this.kettle
.findLabelIndex(n.target.toString()))));
- this.curr = curr;
- curr.addLiveness(n.value.toString());
+ cfg.addLiveness(curr, n.value.toString());
///////////////////////////////////////////////////////////////
MinimalLogger.info(String.format("<-%s (\"%s\":%s)",
n.getClass().getSimpleName(),
@@ -219,11 +195,9 @@ public class CFGSimp extends VInstr.VisitorPR<ControlFlowGraph, String, RuntimeE
this.curr = curr;
if (n.dest != null)
- curr.addReaching(n.dest.toString());
+ cfg.addReaching(curr, n.dest.toString());
for (VOperand a : n.args) {
- String source = a.toString();
- if (isKnownVar(source))
- curr.addLiveness(source);
+ cfg.addLiveness(curr, a.toString());
}
///////////////////////////////////////////////////////////////
MinimalLogger.info(String.format("<-%s (\"%s\":%s)",
@@ -244,11 +218,9 @@ public class CFGSimp extends VInstr.VisitorPR<ControlFlowGraph, String, RuntimeE
this.curr = curr;
if (n.dest != null)
- curr.addReaching(n.dest.toString());
+ cfg.addReaching(curr, n.dest.toString());
for (VOperand a : n.args) {
- String source = a.toString();
- if (isKnownVar(source))
- curr.addLiveness(source);
+ cfg.addLiveness(curr, a.toString());
}
///////////////////////////////////////////////////////////////
MinimalLogger.info(String.format("<-%s (\"%s\":%s)",
@@ -268,8 +240,8 @@ public class CFGSimp extends VInstr.VisitorPR<ControlFlowGraph, String, RuntimeE
this.dot_format += cfg.addEdge(this.curr, curr);
this.curr = curr;
- if (n.value != null && isKnownVar(n.value.toString()))
- curr.addLiveness(n.value.toString());
+ if (n.value != null)
+ cfg.addLiveness(curr, n.value.toString());
///////////////////////////////////////////////////////////////
MinimalLogger.info(String.format("<-%s (\"%s\":%s)",
n.getClass().getSimpleName(),