From 62178e370f21ddf80766b8e1075c55e0d3945493 Mon Sep 17 00:00:00 2001 From: bd-912 Date: Wed, 17 Apr 2024 01:33:05 -0600 Subject: CFG Skeleton Files --- V2VM.java | 54 +++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 49 insertions(+), 5 deletions(-) (limited to 'V2VM.java') diff --git a/V2VM.java b/V2VM.java index 4e90a32..6a0645b 100644 --- a/V2VM.java +++ b/V2VM.java @@ -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 strProg = inputStreamToArrayList(systemInCopy); - ControlFlowGraph cfg = new ControlFlowGraph(); + VaporProgram prog = parseVapor(System.in, System.out); - for (VFunction f : funts) { - f.body[0].accept("", cfg); - } + ControlFlowGraph cfg = new ControlFlowGraph(); + CFGSimp cfg_vis = new CFGSimp(cfg); + + for (VFunction f : prog.functions) + for (VInstr s : f.body) + s.accept("", cfg_vis); + + SpillEverywhere se = new SpillEverywhere(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 inputStreamToArrayList(InputStream inputStream) { + ArrayList 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; + } + } -- cgit v1.2.3