package vaporize.library; import cs132.vapor.ast.*; import graphviz.*; import misc.*; import java.io.File; import java.util.*; public class CFGSimp extends VInstr.VisitorPR { private boolean use_graphviz = true; // if true, generates svg files of the edges in each function private VaporProgram vp; private Kettle kettle; private ArrayList cfgs; private CFGNode curr; private String dot_format; // a list of edges to be processed by graphviz public CFGSimp(VaporProgram vp, ArrayList vapor) { this.vp = vp; this.kettle = new Kettle(vapor); this.cfgs = new ArrayList(); this.curr = null; for (VFunction f : this.vp.functions) { ControlFlowGraph cfg = new ControlFlowGraph(); this.dot_format = ""; // first visit may not find edges; cfg.addEdges will handle this.curr = new CFGNode(f.body[0]); cfg.setStart(curr); MinimalLogger.info(String.format("CFGSimp is collecting nodes for %s", this.kettle.parseFuncName(f))); for (VCodeLabel s : f.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) { MinimalLogger.info(String.format(" Body %d \"%s\"", s.sourcePos.line, this.kettle.get(s).trim())); cfg.addNode(new CFGNode(s)); } MinimalLogger.info(String.format("CFGSimp is collecting edges for %s", this.kettle.parseFuncName(f))); for (VInstr s : f.body) s.accept(cfg, this); if (this.use_graphviz) this.createDotGraph(this.kettle.parseFuncName(f)); this.cfgs.add(cfg); } } public ArrayList getCFGs() { 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(); 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(), this.kettle.get(n).trim(), n.sourcePos.toString())); /////////////////////////////////////////////////////////////// CFGNode curr = cfg.getNode(n); this.dot_format += cfg.addEdge(this.curr, curr); this.curr = curr; /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<-%s (\"%s\":%s)", n.getClass().getSimpleName(), this.kettle.get(n).trim(), n.sourcePos.toString())); return null; } public String visit(ControlFlowGraph cfg, VMemWrite n) throws RuntimeException { MinimalLogger.info(String.format("->%s (\"%s\":%s)", n.getClass().getSimpleName(), this.kettle.get(n).trim(), n.sourcePos.toString())); /////////////////////////////////////////////////////////////// CFGNode curr = cfg.getNode(n); this.dot_format += cfg.addEdge(this.curr, curr); this.curr = curr; /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<-%s (\"%s\":%s)", n.getClass().getSimpleName(), this.kettle.get(n).trim(), n.sourcePos.toString())); return null; } public String visit(ControlFlowGraph cfg, VAssign n) throws RuntimeException { MinimalLogger.info(String.format("->%s (\"%s\":%s)", n.getClass().getSimpleName(), this.kettle.get(n).trim(), n.sourcePos.toString())); /////////////////////////////////////////////////////////////// CFGNode curr = cfg.getNode(n); this.dot_format += cfg.addEdge(this.curr, curr); this.curr = curr; /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<-%s (\"%s\":%s)", n.getClass().getSimpleName(), this.kettle.get(n).trim(), n.sourcePos.toString())); return null; } public String visit(ControlFlowGraph cfg, VBranch n) throws RuntimeException { MinimalLogger.info(String.format("->%s (\"%s\":%s)", n.getClass().getSimpleName(), this.kettle.get(n).trim(), n.sourcePos.toString())); /////////////////////////////////////////////////////////////// CFGNode curr = cfg.getNode(n); this.dot_format += cfg.addEdge(this.curr, curr); this.dot_format += cfg.addEdge(curr, cfg.getNode(new Integer(this.kettle .findLabelIndex(n.target.toString())))); this.curr = curr; /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<-%s (\"%s\":%s)", n.getClass().getSimpleName(), this.kettle.get(n).trim(), n.sourcePos.toString())); return null; } public String visit(ControlFlowGraph cfg, VGoto n) throws RuntimeException { MinimalLogger.info(String.format("->%s (\"%s\":%s)", n.getClass().getSimpleName(), this.kettle.get(n).trim(), n.sourcePos.toString())); /////////////////////////////////////////////////////////////// CFGNode curr = cfg.getNode(n); this.dot_format += cfg.addEdge(this.curr, curr); this.dot_format += cfg.addEdge(curr, cfg.getNode(new Integer(this.kettle .findLabelIndex(n.target.toString())))); MinimalLogger.info(String.format("curr is now %s", this.kettle.get(n).trim())); this.curr = cfg.getNextNode(curr); /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<-%s (\"%s\":%s)", n.getClass().getSimpleName(), this.kettle.get(n).trim(), n.sourcePos.toString())); return null; } public String visit(ControlFlowGraph cfg, VCall n) throws RuntimeException { MinimalLogger.info(String.format("->%s (\"%s\":%s)", n.getClass().getSimpleName(), this.kettle.get(n).trim(), n.sourcePos.toString())); /////////////////////////////////////////////////////////////// CFGNode curr = cfg.getNode(n); this.dot_format += cfg.addEdge(this.curr, curr); this.curr = curr; /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<-%s (\"%s\":%s)", n.getClass().getSimpleName(), this.kettle.get(n).trim(), n.sourcePos.toString())); return null; } public String visit(ControlFlowGraph cfg, VBuiltIn n) throws RuntimeException { MinimalLogger.info(String.format("->%s (\"%s\":%s)", n.getClass().getSimpleName(), this.kettle.get(n).trim(), n.sourcePos.toString())); /////////////////////////////////////////////////////////////// CFGNode curr = cfg.getNode(n); this.dot_format += cfg.addEdge(this.curr, curr); this.curr = curr; /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<-%s (\"%s\":%s)", n.getClass().getSimpleName(), this.kettle.get(n).trim(), n.sourcePos.toString())); return null; } public String visit(ControlFlowGraph cfg, VReturn n) throws RuntimeException { MinimalLogger.info(String.format("->%s (\"%s\":%s)", n.getClass().getSimpleName(), this.kettle.get(n).trim(), n.sourcePos.toString())); /////////////////////////////////////////////////////////////// CFGNode curr = cfg.getNode(n); this.dot_format += cfg.addEdge(this.curr, curr); this.curr = curr; /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<-%s (\"%s\":%s)", n.getClass().getSimpleName(), this.kettle.get(n).trim(), n.sourcePos.toString())); return null; } }