diff options
author | bd-912 <bdunahu@colostate.edu> | 2024-04-17 01:33:05 -0600 |
---|---|---|
committer | bd-912 <bdunahu@colostate.edu> | 2024-04-17 01:33:05 -0600 |
commit | 62178e370f21ddf80766b8e1075c55e0d3945493 (patch) | |
tree | 13fcfc0d7177e578020366e905b71ca48e0857f0 | |
parent | f08708ee07931db592ffb76c073bd80bcf6cf060 (diff) |
CFG Skeleton Files
-rw-r--r-- | V2VM.java | 54 | ||||
-rw-r--r-- | vaporize/library/CFGSimp.java | 48 | ||||
-rw-r--r-- | vaporize/library/ControlFlowGraph.java | 69 | ||||
-rw-r--r-- | vaporize/library/Node.java | 38 | ||||
-rw-r--r-- | vaporize/library/SpillEverywhere.java | 84 |
5 files changed, 233 insertions, 60 deletions
@@ -1,9 +1,12 @@ import java.io.*; +import java.util.ArrayList; import cs132.util.ProblemException; import cs132.vapor.parser.VaporParser; import cs132.vapor.ast.VaporProgram; import cs132.vapor.ast.VBuiltIn.Op; +import cs132.vapor.ast.VDataSegment; import cs132.vapor.ast.VFunction; +import cs132.vapor.ast.VInstr; import java.io.InputStreamReader; import java.io.IOException; @@ -17,13 +20,25 @@ public class V2VM { public static void main(String[] args) { try { - VFunction[] funts = parseVapor(System.in, System.out).functions; + System.in.mark(-1); + InputStream systemInCopy = createCopyOfSystemIn(); + System.in.reset(); + ArrayList<String> strProg = inputStreamToArrayList(systemInCopy); - ControlFlowGraph<String, Void> cfg = new ControlFlowGraph<String, Void>(); + VaporProgram prog = parseVapor(System.in, System.out); - for (VFunction f : funts) { - f.body[0].accept("", cfg); - } + ControlFlowGraph cfg = new ControlFlowGraph(); + CFGSimp<String, Void> cfg_vis = new CFGSimp<String, Void>(cfg); + + for (VFunction f : prog.functions) + for (VInstr s : f.body) + s.accept("", cfg_vis); + + SpillEverywhere<String, Void> se = new SpillEverywhere<String, Void>(strProg); + + for (VFunction f : prog.functions) + for (VInstr s : f.body) + s.accept("", se); } catch (IOException e) { System.out.println(e.toString()); @@ -52,4 +67,33 @@ public class V2VM { return program; } + + public static ArrayList<String> inputStreamToArrayList(InputStream inputStream) { + ArrayList<String> lines = new ArrayList<>(); + try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { + String line; + while ((line = reader.readLine()) != null) { + lines.add(line); + } + } catch (IOException e) { + e.printStackTrace(); + } + return lines; + } + + private static InputStream createCopyOfSystemIn() { + byte[] buffer = new byte[1024]; + int bytesRead; + ByteArrayInputStream bais = new ByteArrayInputStream(buffer); + + try { + bytesRead = System.in.read(buffer); + bais = new ByteArrayInputStream(buffer, 0, bytesRead); + } catch (IOException e) { + e.printStackTrace(); + } + + return bais; + } + } diff --git a/vaporize/library/CFGSimp.java b/vaporize/library/CFGSimp.java new file mode 100644 index 0000000..372f926 --- /dev/null +++ b/vaporize/library/CFGSimp.java @@ -0,0 +1,48 @@ +package vaporize.library; + +import cs132.vapor.ast.*; +import st.*; +import misc.*; +import java.util.*; + +public class CFGSimp<P, R> extends VInstr.VisitorPR<P, R, RuntimeException> { + + ControlFlowGraph cfg; + + public CFGSimp(ControlFlowGraph cfg) { + this.cfg = cfg; + } + + public R visit(P p, VMemRead n) throws RuntimeException { + return null; + } + + public R visit(P p, VMemWrite n) throws RuntimeException { + return null; + } + + public R visit(P p, VAssign n) throws RuntimeException { + return null; + } + + public R visit(P p, VBranch n) throws RuntimeException { + return null; + } + + public R visit(P p, VGoto n) throws RuntimeException { + return null; + } + + public R visit(P p, VCall n) throws RuntimeException { + return null; + } + + public R visit(P p, VBuiltIn n) throws RuntimeException { + return null; + } + + public R visit(P p, VReturn n) throws RuntimeException { + return null; + } + +} diff --git a/vaporize/library/ControlFlowGraph.java b/vaporize/library/ControlFlowGraph.java index 787ef8f..f181d36 100644 --- a/vaporize/library/ControlFlowGraph.java +++ b/vaporize/library/ControlFlowGraph.java @@ -1,68 +1,27 @@ package vaporize.library; import cs132.vapor.ast.*; -import st.*; -import misc.*; -import java.util.*; +import java.util.ArrayList; -public class ControlFlowGraph<P, R> extends VInstr.VisitorPR<P, R, RuntimeException> { +public class ControlFlowGraph { + private ArrayList<Node> nodes; + private Node start; + private Node end; - public R visit(P p, VMemRead n) throws RuntimeException { - PrintFilter.print(String.format("%s (%s)", - n.getClass().getSimpleName(), - n.sourcePos.toString()), true); - return null; + public ControlFlowGraph() { + this.nodes = new ArrayList<>(); + this.start = null; + this.end = null; } - public R visit(P p, VMemWrite n) throws RuntimeException { - PrintFilter.print(String.format("%s (%s)", - n.getClass().getSimpleName(), - n.sourcePos.toString()), true); - return null; + protected void addNode(Node node) { + this.nodes.add(node); } - public R visit(P p, VAssign n) throws RuntimeException { - PrintFilter.print(String.format("%s (%s)", - n.getClass().getSimpleName(), - n.sourcePos.toString()), true); - return null; - } - - public R visit(P p, VBranch n) throws RuntimeException { - PrintFilter.print(String.format("%s (%s)", - n.getClass().getSimpleName(), - n.sourcePos.toString()), true); - return null; - } - - public R visit(P p, VGoto n) throws RuntimeException { - PrintFilter.print(String.format("%s (%s)", - n.getClass().getSimpleName(), - n.sourcePos.toString()), true); - return null; - } - - public R visit(P p, VCall n) throws RuntimeException { - PrintFilter.print(String.format("%s (%s)", - n.getClass().getSimpleName(), - n.sourcePos.toString()), true); - return null; - } - - public R visit(P p, VBuiltIn n) throws RuntimeException { - PrintFilter.print(String.format("%s (%s:%s)", - n.getClass().getSimpleName(), - n.op.name, - n.sourcePos.toString()), true); - return null; - } - - public R visit(P p, VReturn n) throws RuntimeException { - PrintFilter.print(String.format("%s (%s)", - n.getClass().getSimpleName(), - n.sourcePos.toString()), true); - return null; + protected void addEdge(Node source, Node dest) { + source.addDest(dest); + dest.addSource(source); } } diff --git a/vaporize/library/Node.java b/vaporize/library/Node.java new file mode 100644 index 0000000..203f931 --- /dev/null +++ b/vaporize/library/Node.java @@ -0,0 +1,38 @@ +package vaporize.library; + +import cs132.vapor.ast.*; +import java.util.ArrayList; + +class Node { + + private VInstr instruction; + private ArrayList<Node> sources; + private ArrayList<Node> dests; + + protected Node(VInstr instruction) { + this.instruction = instruction; + this.sources = new ArrayList<>(); + this.dests = new ArrayList<>(); + } + + protected void addSource(Node node) { + this.sources.add(node); + } + + protected void addDest(Node node) { + this.dests.add(node); + } + + protected VInstr getInstruction() { + return this.instruction; + } + + protected ArrayList<Node> getSources() { + return this.sources; + } + + protected ArrayList<Node> getDests() { + return this.dests; + } + +} diff --git a/vaporize/library/SpillEverywhere.java b/vaporize/library/SpillEverywhere.java new file mode 100644 index 0000000..aa8368f --- /dev/null +++ b/vaporize/library/SpillEverywhere.java @@ -0,0 +1,84 @@ +package vaporize.library; + +import cs132.vapor.ast.*; +import st.*; +import misc.*; +import java.util.*; + +public class SpillEverywhere<P, R> extends VInstr.VisitorPR<P, R, RuntimeException> { + + ArrayList<String> vapor; + String vaporm; + + + public SpillEverywhere(ArrayList<String> vapor) { + this.vapor = vapor; + this.vaporm = ""; + } + + public R visit(P p, VMemRead n) throws RuntimeException { + PrintFilter.print(String.format("%s (%s:%s)", + n.getClass().getSimpleName(), + this.vapor.get(n.sourcePos.line-1), + n.sourcePos.toString()), true); + return null; + } + + public R visit(P p, VMemWrite n) throws RuntimeException { + PrintFilter.print(String.format("%s (%s:%s)", + n.getClass().getSimpleName(), + this.vapor.get(n.sourcePos.line-1), + n.sourcePos.toString()), true); + return null; + } + + public R visit(P p, VAssign n) throws RuntimeException { + PrintFilter.print(String.format("%s (%s:%s)", + n.getClass().getSimpleName(), + this.vapor.get(n.sourcePos.line-1), + n.sourcePos.toString()), true); + return null; + } + + public R visit(P p, VBranch n) throws RuntimeException { + PrintFilter.print(String.format("%s (%s:%s)", + n.getClass().getSimpleName(), + this.vapor.get(n.sourcePos.line-1), + n.sourcePos.toString()), true); + System.out.println("NEXT: " + + return null; + } + + public R visit(P p, VGoto n) throws RuntimeException { + PrintFilter.print(String.format("%s (%s:%s)", + n.getClass().getSimpleName(), + this.vapor.get(n.sourcePos.line-1), + n.sourcePos.toString()), true); + return null; + } + + public R visit(P p, VCall n) throws RuntimeException { + PrintFilter.print(String.format("%s (%s:%s)", + n.getClass().getSimpleName(), + this.vapor.get(n.sourcePos.line-1), + n.sourcePos.toString()), true); + return null; + } + + public R visit(P p, VBuiltIn n) throws RuntimeException { + PrintFilter.print(String.format("%s (%s:%s)", + n.op.name, + this.vapor.get(n.sourcePos.line-1), + n.sourcePos.toString()), true); + return null; + } + + public R visit(P p, VReturn n) throws RuntimeException { + PrintFilter.print(String.format("%s (%s:%s)", + n.getClass().getSimpleName(), + this.vapor.get(n.sourcePos.line-1), + n.sourcePos.toString()), true); + return null; + } + + } |