diff options
Diffstat (limited to 'vaporize')
-rw-r--r-- | vaporize/library/CFGSimp.java | 45 | ||||
-rw-r--r-- | vaporize/library/ControlFlowGraph.java | 13 |
2 files changed, 42 insertions, 16 deletions
diff --git a/vaporize/library/CFGSimp.java b/vaporize/library/CFGSimp.java index 9f2556d..f4d63cf 100644 --- a/vaporize/library/CFGSimp.java +++ b/vaporize/library/CFGSimp.java @@ -1,8 +1,10 @@ package vaporize.library; import cs132.vapor.ast.*; -import st.*; +import graphviz.*; import misc.*; + +import java.io.File; import java.util.*; public class CFGSimp extends VInstr.VisitorPR<ControlFlowGraph, String, RuntimeException> { @@ -11,6 +13,7 @@ public class CFGSimp extends VInstr.VisitorPR<ControlFlowGraph, String, RuntimeE private Kettle kettle; private ArrayList<ControlFlowGraph> cfgs; private CFGNode curr; + private String dot_format; // a list of edges to be processed by graphviz public CFGSimp(VaporProgram vp, ArrayList<String> vapor) { this.vp = vp; @@ -21,6 +24,7 @@ public class CFGSimp extends VInstr.VisitorPR<ControlFlowGraph, String, RuntimeE for (VFunction f : this.vp.functions) { ControlFlowGraph cfg = new ControlFlowGraph(); + this.dot_format = ""; curr = new CFGNode(f.body[0]); cfg.setStart(curr); @@ -46,6 +50,8 @@ public class CFGSimp extends VInstr.VisitorPR<ControlFlowGraph, String, RuntimeE for (VInstr s : f.body) s.accept(cfg, this); + this.createDotGraph(this.kettle.parseFuncName(f)); + this.cfgs.add(cfg); } } @@ -54,6 +60,21 @@ public class CFGSimp extends VInstr.VisitorPR<ControlFlowGraph, String, RuntimeE return this.cfgs; } + public void createDotGraph(String file_name) { + MinimalLogger.info(String.format("Outputting %s to %s", + this.dot_format, + file_name)); + GraphViz gv = new GraphViz(); + gv.addln(gv.start_graph()); + gv.add(this.dot_format); + gv.addln(gv.end_graph()); + String type = "svg"; + gv.decreaseDpi(); + gv.decreaseDpi(); + File out = new File(file_name+"."+ type); + gv.writeGraphToFile( gv.getGraph( gv.getDotSource(), type ), out ); + } + public String visit(ControlFlowGraph cfg, VMemRead n) throws RuntimeException { MinimalLogger.info(String.format("->%s (\"%s\":%s)", n.getClass().getSimpleName(), @@ -61,7 +82,7 @@ public class CFGSimp extends VInstr.VisitorPR<ControlFlowGraph, String, RuntimeE n.sourcePos.toString())); /////////////////////////////////////////////////////////////// CFGNode curr = cfg.getNode(n); - cfg.addEdge(this.curr, cfg.getNode(n)); + this.dot_format += cfg.addEdge(this.curr, cfg.getNode(n)); this.curr = curr; /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<-%s (\"%s\":%s)", @@ -78,7 +99,7 @@ public class CFGSimp extends VInstr.VisitorPR<ControlFlowGraph, String, RuntimeE n.sourcePos.toString())); /////////////////////////////////////////////////////////////// CFGNode curr = cfg.getNode(n); - cfg.addEdge(this.curr, cfg.getNode(n)); + this.dot_format += cfg.addEdge(this.curr, cfg.getNode(n)); this.curr = curr; /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<-%s (\"%s\":%s)", @@ -95,7 +116,7 @@ public class CFGSimp extends VInstr.VisitorPR<ControlFlowGraph, String, RuntimeE n.sourcePos.toString())); /////////////////////////////////////////////////////////////// CFGNode curr = cfg.getNode(n); - cfg.addEdge(this.curr, cfg.getNode(n)); + this.dot_format += cfg.addEdge(this.curr, cfg.getNode(n)); this.curr = curr; /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<-%s (\"%s\":%s)", @@ -112,9 +133,9 @@ public class CFGSimp extends VInstr.VisitorPR<ControlFlowGraph, String, RuntimeE n.sourcePos.toString())); /////////////////////////////////////////////////////////////// CFGNode curr = cfg.getNode(n); - cfg.addEdge(this.curr, cfg.getNode(n)); - cfg.addEdge(this.curr, cfg.getNode(new Integer(this.kettle - .findLabelIndex(n.target.toString())))); + this.dot_format += cfg.addEdge(this.curr, cfg.getNode(n)); + this.dot_format += cfg.addEdge(this.curr, cfg.getNode(new Integer(this.kettle + .findLabelIndex(n.target.toString())))); this.curr = curr; /////////////////////////////////////////////////////////////// @@ -132,8 +153,8 @@ public class CFGSimp extends VInstr.VisitorPR<ControlFlowGraph, String, RuntimeE n.sourcePos.toString())); /////////////////////////////////////////////////////////////// CFGNode curr = cfg.getNode(n); - cfg.addEdge(this.curr, cfg.getNode(new Integer(this.kettle - .findLabelIndex(n.target.toString())))); + this.dot_format += cfg.addEdge(this.curr, cfg.getNode(new Integer(this.kettle + .findLabelIndex(n.target.toString())))); this.curr = curr; /////////////////////////////////////////////////////////////// @@ -151,7 +172,7 @@ public class CFGSimp extends VInstr.VisitorPR<ControlFlowGraph, String, RuntimeE n.sourcePos.toString())); /////////////////////////////////////////////////////////////// CFGNode curr = cfg.getNode(n); - cfg.addEdge(this.curr, cfg.getNode(n)); + this.dot_format += cfg.addEdge(this.curr, cfg.getNode(n)); this.curr = curr; /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<-%s (\"%s\":%s)", @@ -168,7 +189,7 @@ public class CFGSimp extends VInstr.VisitorPR<ControlFlowGraph, String, RuntimeE n.sourcePos.toString())); /////////////////////////////////////////////////////////////// CFGNode curr = cfg.getNode(n); - cfg.addEdge(this.curr, cfg.getNode(n)); + this.dot_format += cfg.addEdge(this.curr, cfg.getNode(n)); this.curr = curr; /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<-%s (\"%s\":%s)", @@ -185,7 +206,7 @@ public class CFGSimp extends VInstr.VisitorPR<ControlFlowGraph, String, RuntimeE n.sourcePos.toString())); /////////////////////////////////////////////////////////////// CFGNode curr = cfg.getNode(n); - cfg.addEdge(this.curr, cfg.getNode(n)); + this.dot_format += cfg.addEdge(this.curr, cfg.getNode(n)); this.curr = curr; /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<-%s (\"%s\":%s)", diff --git a/vaporize/library/ControlFlowGraph.java b/vaporize/library/ControlFlowGraph.java index c0eff10..364cec1 100644 --- a/vaporize/library/ControlFlowGraph.java +++ b/vaporize/library/ControlFlowGraph.java @@ -37,12 +37,17 @@ public class ControlFlowGraph { this.nodes.add(node); } - protected void addEdge(CFGNode source, CFGNode dest) { - MinimalLogger.info(String.format("Edge %s -> %s", - source.getInstruction().sourcePos.line, - dest.getInstruction().sourcePos.line)); + protected String addEdge(CFGNode source, CFGNode dest) { + String ret = String.format("%d -> %d", + source.getInstruction().sourcePos.line, + dest.getInstruction().sourcePos.line); + MinimalLogger.info(String.format("Edge %s", + ret)); + source.addDest(dest); dest.addSource(source); + + return ret +";"; } protected void setStart(CFGNode start) { |