diff options
author | bd-912 <bdunahu@colostate.edu> | 2024-04-20 17:09:21 -0600 |
---|---|---|
committer | bd-912 <bdunahu@colostate.edu> | 2024-04-20 17:09:21 -0600 |
commit | 38ef4ec52804876ba0a3daef3a2d1817f17bc1e5 (patch) | |
tree | 97bee3fde903059fd72866487e0e09d11fac7e36 | |
parent | 64710a5a9dd258bccf39ab1fa4ce607e62507c04 (diff) |
Fix minor bugs in Liveness computation
-rw-r--r-- | vaporize/library/CFGSimp.java | 63 |
1 files changed, 39 insertions, 24 deletions
diff --git a/vaporize/library/CFGSimp.java b/vaporize/library/CFGSimp.java index 76d5acd..54ec61c 100644 --- a/vaporize/library/CFGSimp.java +++ b/vaporize/library/CFGSimp.java @@ -11,32 +11,32 @@ public class CFGSimp extends VInstr.VisitorPR<ControlFlowGraph, String, RuntimeE private boolean use_graphviz = true; // if true, generates svg files of the edges in each function - private VaporProgram vp; private Kettle kettle; private ArrayList<ControlFlowGraph> cfgs; - private CFGNode curr; + 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 public CFGSimp(VaporProgram vp, ArrayList<String> vapor) { - this.vp = vp; this.kettle = new Kettle(vapor); this.cfgs = new ArrayList<ControlFlowGraph>(); this.curr = null; - for (VFunction f : this.vp.functions) { + for (VFunction f : vp.functions) { + this.func = f; ControlFlowGraph cfg = new ControlFlowGraph(); this.dot_format = ""; MinimalLogger.info(String.format("CFGSimp is collecting nodes for %s", - this.kettle.parseFuncName(f))); - for (VCodeLabel s : f.labels) { + 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())); cfg.addNode(new CFGNode(s)); } - for (VInstr s : f.body) { + for (VInstr s : this.func.body) { MinimalLogger.info(String.format(" Body %d \"%s\"", s.sourcePos.line, this.kettle.get(s).trim())); @@ -54,7 +54,7 @@ public class CFGSimp extends VInstr.VisitorPR<ControlFlowGraph, String, RuntimeE // cascades downwards --- cfg.addEdges for (VVarRef.Local l : f.params) this.curr.addReaching(l.ident.toString()); - for (VInstr s : f.body) + for (VInstr s : this.func.body) s.accept(cfg, this); MinimalLogger.info(String.format("Spitting out reaching/liveness...")); @@ -65,7 +65,7 @@ public class CFGSimp extends VInstr.VisitorPR<ControlFlowGraph, String, RuntimeE n.getLiveness())); if (this.use_graphviz) - this.createDotGraph(this.kettle.parseFuncName(f)); + this.createDotGraph(this.kettle.parseFuncName(this.func)); this.cfgs.add(cfg); } @@ -75,7 +75,7 @@ public class CFGSimp extends VInstr.VisitorPR<ControlFlowGraph, String, RuntimeE return this.cfgs; } - public void createDotGraph(String file_name) { + protected void createDotGraph(String file_name) { MinimalLogger.info(String.format("Outputting %s to %s", this.dot_format, file_name)); @@ -92,6 +92,15 @@ 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(), @@ -103,8 +112,9 @@ public class CFGSimp extends VInstr.VisitorPR<ControlFlowGraph, String, RuntimeE this.curr = curr; curr.addReaching(n.dest.toString()); - if (!n.source.toString().matches("-?(0|[1-9]\\d*)")) - curr.addLiveness(((VMemRef.Global) n.source).base.toString()); + String source = ((VMemRef.Global) n.source).base.toString(); + if (isKnownVar(source)) + curr.addLiveness(source); /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<-%s (\"%s\":%s)", n.getClass().getSimpleName(), @@ -123,9 +133,9 @@ public class CFGSimp extends VInstr.VisitorPR<ControlFlowGraph, String, RuntimeE this.dot_format += cfg.addEdge(this.curr, curr); this.curr = curr; - if (!n.source.toString().matches("-?(0|[1-9]\\d*)")) - curr.addLiveness(n.source.toString()); - + String source = n.source.toString(); + if (isKnownVar(source)) + curr.addLiveness(source); /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<-%s (\"%s\":%s)", n.getClass().getSimpleName(), @@ -145,8 +155,9 @@ public class CFGSimp extends VInstr.VisitorPR<ControlFlowGraph, String, RuntimeE this.curr = curr; curr.addReaching(n.dest.toString()); - if (!n.source.toString().matches("-?(0|[1-9]\\d*)")) - curr.addLiveness(n.source.toString()); + String source = n.source.toString(); + if (isKnownVar(source)) + curr.addLiveness(source); /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<-%s (\"%s\":%s)", n.getClass().getSimpleName(), @@ -209,9 +220,11 @@ public class CFGSimp extends VInstr.VisitorPR<ControlFlowGraph, String, RuntimeE if (n.dest != null) curr.addReaching(n.dest.toString()); - for (VOperand a : n.args) - if (!a.toString().matches("-?(0|[1-9]\\d*)")) - curr.addLiveness(a.toString()); + for (VOperand a : n.args) { + String source = a.toString(); + if (isKnownVar(source)) + curr.addLiveness(source); + } /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<-%s (\"%s\":%s)", n.getClass().getSimpleName(), @@ -232,9 +245,11 @@ public class CFGSimp extends VInstr.VisitorPR<ControlFlowGraph, String, RuntimeE if (n.dest != null) curr.addReaching(n.dest.toString()); - for (VOperand a : n.args) - if (!a.toString().matches("-?(0|[1-9]\\d*)")) - curr.addLiveness(a.toString()); + for (VOperand a : n.args) { + String source = a.toString(); + if (isKnownVar(source)) + curr.addLiveness(source); + } /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<-%s (\"%s\":%s)", n.getClass().getSimpleName(), @@ -253,7 +268,7 @@ public class CFGSimp extends VInstr.VisitorPR<ControlFlowGraph, String, RuntimeE this.dot_format += cfg.addEdge(this.curr, curr); this.curr = curr; - if (n.value != null) + if (n.value != null && isKnownVar(n.value.toString())) curr.addLiveness(n.value.toString()); /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<-%s (\"%s\":%s)", |