diff options
author | bd-912 <bdunahu@colostate.edu> | 2024-04-19 21:50:26 -0600 |
---|---|---|
committer | bd-912 <bdunahu@colostate.edu> | 2024-04-19 21:50:51 -0600 |
commit | b733f594c4ab0697aff9afdcc45e0421107dec85 (patch) | |
tree | eb4f5a2d3330523a7c962726f7576c26dc4fc98e | |
parent | e188aa3f962df621fc49097418959c7d00ce9969 (diff) |
Vaporize.CFG Add incomplete edge creation
-rw-r--r-- | V2VM.java | 8 | ||||
-rw-r--r-- | vaporize/library/CFGNode.java | 5 | ||||
-rw-r--r-- | vaporize/library/CFGSimp.java | 148 | ||||
-rw-r--r-- | vaporize/library/ControlFlowGraph.java | 28 | ||||
-rw-r--r-- | vaporize/library/Kettle.java | 2 |
5 files changed, 166 insertions, 25 deletions
@@ -48,7 +48,13 @@ public class V2VM { Op.PrintIntS, Op.HeapAllocZ, Op.Error, }; boolean allowLocals = true; - String[] registers = null; + String[] registers = { + "v0", "v1", + "a0", "a1", "a2", "a3", + "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", + "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7", + "t8", + }; boolean allowStack = false; VaporProgram program; diff --git a/vaporize/library/CFGNode.java b/vaporize/library/CFGNode.java index ca60856..a01bfb0 100644 --- a/vaporize/library/CFGNode.java +++ b/vaporize/library/CFGNode.java @@ -25,6 +25,11 @@ class CFGNode { return this.line; } + public boolean equals(Node other) { + return other.sourcePos == + this.instruction.sourcePos; + } + protected void addSource(CFGNode node) { this.sources.add(node); } diff --git a/vaporize/library/CFGSimp.java b/vaporize/library/CFGSimp.java index b7ca7bd..3e39576 100644 --- a/vaporize/library/CFGSimp.java +++ b/vaporize/library/CFGSimp.java @@ -5,35 +5,46 @@ import st.*; import misc.*; import java.util.*; -public class CFGSimp extends VInstr.VisitorPR<String, String, RuntimeException> { +public class CFGSimp extends VInstr.VisitorPR<ControlFlowGraph, String, RuntimeException> { private VaporProgram vp; private Kettle kettle; private ArrayList<ControlFlowGraph> cfgs; + private CFGNode curr; 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) { ControlFlowGraph cfg = new ControlFlowGraph(); - CFGNode start = new CFGNode(f.body[0]); - cfg.setStart(start); + curr = new CFGNode(f.body[0]); + cfg.setStart(curr); - // nodes MinimalLogger.info(String.format("CFGSimp is collecting nodes for %s", - this.kettle.parseFuncName(f))); - for (VInstr s : f.body) + 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)); + } // edges MinimalLogger.info(String.format("CFGSimp is collecting edges for %s", - this.kettle.parseFuncName(f))); + this.kettle.parseFuncName(f))); for (VInstr s : f.body) - s.accept("", this); + s.accept(cfg, this); this.cfgs.add(cfg); } @@ -43,36 +54,139 @@ public class CFGSimp extends VInstr.VisitorPR<String, String, RuntimeException> return this.cfgs; } - public String visit(String p, VMemRead n) throws RuntimeException { + 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); + cfg.addEdge(this.curr, cfg.getNode(n)); + 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(String p, VMemWrite n) throws RuntimeException { + 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); + cfg.addEdge(this.curr, cfg.getNode(n)); + 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(String p, VAssign n) throws RuntimeException { + 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); + cfg.addEdge(this.curr, cfg.getNode(n)); + 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(String p, VBranch n) throws RuntimeException { - // two edges + 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); + cfg.addEdge(this.curr, cfg.getNode(n)); + 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(String p, VGoto n) throws RuntimeException { + 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); + cfg.addEdge(this.curr, cfg.getNode(n)); + 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(String p, VCall n) throws RuntimeException { + 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); + cfg.addEdge(this.curr, cfg.getNode(n)); + 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(String p, VBuiltIn n) throws RuntimeException { + 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); + cfg.addEdge(this.curr, cfg.getNode(n)); + 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(String p, VReturn n) throws RuntimeException { + 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); + cfg.addEdge(this.curr, cfg.getNode(n)); + this.curr = curr; + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<-%s (\"%s\":%s)", + n.getClass().getSimpleName(), + this.kettle.get(n).trim(), + n.sourcePos.toString())); return null; } diff --git a/vaporize/library/ControlFlowGraph.java b/vaporize/library/ControlFlowGraph.java index 773c4cd..e7057ac 100644 --- a/vaporize/library/ControlFlowGraph.java +++ b/vaporize/library/ControlFlowGraph.java @@ -1,18 +1,33 @@ package vaporize.library; import cs132.vapor.ast.*; +import misc.*; + import java.util.ArrayList; public class ControlFlowGraph { private ArrayList<CFGNode> nodes; private CFGNode start; - private CFGNode end; - public ControlFlowGraph() { + protected ControlFlowGraph() { this.nodes = new ArrayList<>(); this.start = null; - this.end = null; + } + + protected CFGNode getNode(Node a) { + CFGNode ret = null; + for (CFGNode n : this.nodes) { + if (n.equals(a)) { + ret = n; + break; + } + } + + if (ret == null) + MinimalLogger.severe(String.format("Could not find a node matching %s", + a.sourcePos.toString())); + return ret; } protected void addNode(CFGNode node) { @@ -20,6 +35,9 @@ public class ControlFlowGraph { } protected void addEdge(CFGNode source, CFGNode dest) { + MinimalLogger.info(String.format("Edge %s -> %s", + source.getInstruction().sourcePos.line, + dest.getInstruction().sourcePos.line)); source.addDest(dest); dest.addSource(source); } @@ -28,10 +46,6 @@ public class ControlFlowGraph { this.start = start; } - /** - * The following methods are for testing - * only! - */ protected ArrayList<CFGNode> getNodes() { return this.nodes; } diff --git a/vaporize/library/Kettle.java b/vaporize/library/Kettle.java index 561ae32..5baf69e 100644 --- a/vaporize/library/Kettle.java +++ b/vaporize/library/Kettle.java @@ -15,10 +15,12 @@ import java.util.ArrayList; class Kettle { ArrayList<String> vapor; + ArrayList<String> vaporm; protected Kettle(ArrayList<String> vapor) { MinimalLogger.info("Pouring program into kettle..."); this.vapor = vapor; + this.vaporm = new ArrayList<String>(); } protected String get(Node n) { |