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; import java.io.PrintStream; import st.*; import misc.*; import vaporize.library.*; public class V2VM { public static void main(String[] args) { try { System.in.mark(-1); InputStream systemInCopy = createCopyOfSystemIn(); System.in.reset(); ArrayList strProg = inputStreamToArrayList(systemInCopy); VaporProgram prog = parseVapor(System.in, System.out); 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()); System.exit(1); } } public static VaporProgram parseVapor(InputStream in, PrintStream err) throws IOException { Op[] ops = { Op.Add, Op.Sub, Op.MulS, Op.Eq, Op.Lt, Op.LtS, Op.PrintIntS, Op.HeapAllocZ, Op.Error, }; boolean allowLocals = true; String[] registers = null; boolean allowStack = false; VaporProgram program; try { program = VaporParser.run(new InputStreamReader(in), 1, 1, java.util.Arrays.asList(ops), allowLocals, registers, allowStack); } catch (ProblemException ex) { err.println(ex.getMessage()); return null; } 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; } }