diff options
Diffstat (limited to 'vaporize')
-rw-r--r-- | vaporize/library/CFGSimp.java | 52 | ||||
-rw-r--r-- | vaporize/library/Kettle.java | 52 | ||||
-rw-r--r-- | vaporize/library/SpillEverywhere.java | 107 |
3 files changed, 134 insertions, 77 deletions
diff --git a/vaporize/library/CFGSimp.java b/vaporize/library/CFGSimp.java index 3a612bd..79eb4c2 100644 --- a/vaporize/library/CFGSimp.java +++ b/vaporize/library/CFGSimp.java @@ -1,5 +1,8 @@ package vaporize.library; +import java.util.logging.Logger; +import java.util.logging.ConsoleHandler; + import cs132.vapor.ast.*; import st.*; import misc.*; @@ -7,26 +10,47 @@ import java.util.*; public class CFGSimp extends VInstr.VisitorPR<String, String, RuntimeException> { - private ControlFlowGraph cfg; + private static Logger log = Logger.getLogger(CFGSimp.class.getName()); + private static ConsoleHandler consoleHandler = new ConsoleHandler(); + static { + consoleHandler.setFormatter(new MinimalSimpleFormatter()); + log.addHandler(consoleHandler); + } + + private VaporProgram vp; + private Kettle kettle; + private ArrayList<ControlFlowGraph> cfgs; + + public CFGSimp(VaporProgram vp, ArrayList<String> vapor) { + this.vp = vp; + this.kettle = new Kettle(vapor); + this.cfgs = new ArrayList<ControlFlowGraph>(); + + + for (VFunction f : this.vp.functions) { + ControlFlowGraph cfg = new ControlFlowGraph(); + CFGNode start = new CFGNode(f.body[0]); + cfg.setStart(start); - public CFGSimp(VFunction n) { - this.cfg = new ControlFlowGraph(); - CFGNode start = new CFGNode(n.body[0]); - this.cfg.setStart(start); + // nodes + this.log.info(String.format("CFGSimp is collecting nodes for %s", + this.kettle.parseFuncName(f))); + for (VInstr s : f.body) + cfg.addNode(new CFGNode(s)); - // nodes - for (VInstr s : n.body) - this.cfg.addNode(new CFGNode(s)); - // edges - for (VInstr s : n.body) - s.accept("", this); + // edges + this.log.info(String.format("CFGSimp is collecting edges for %s", + this.kettle.parseFuncName(f))); + for (VInstr s : f.body) + s.accept("", this); - System.out.println(this.cfg.getNodes().toString()); + this.cfgs.add(cfg); + } } - public ControlFlowGraph getCFG() { - return this.cfg; + public ArrayList<ControlFlowGraph> getCFGs() { + return this.cfgs; } public String visit(String p, VMemRead n) throws RuntimeException { diff --git a/vaporize/library/Kettle.java b/vaporize/library/Kettle.java index 340d53e..fb9ddc4 100644 --- a/vaporize/library/Kettle.java +++ b/vaporize/library/Kettle.java @@ -1,6 +1,11 @@ package vaporize.library; +import java.util.logging.Logger; +import java.util.logging.ConsoleHandler; + import cs132.vapor.ast.*; +import misc.*; + import java.util.ArrayList; /** @@ -12,9 +17,17 @@ import java.util.ArrayList; */ class Kettle { + private static Logger log = Logger.getLogger(Kettle.class.getName()); + private static ConsoleHandler consoleHandler = new ConsoleHandler(); + static { + consoleHandler.setFormatter(new MinimalSimpleFormatter()); + log.addHandler(consoleHandler); + } + ArrayList<String> vapor; protected Kettle(ArrayList<String> vapor) { + log.info("Pouring program into kettle..."); this.vapor = vapor; } @@ -27,12 +40,31 @@ class Kettle { protected void replaceFunctionDeclare(VFunction prev, int in, int out, int local) { - this.set(prev, - String.format("func %s [in %d, out %d, local %d]", - this.parseFuncName(this.get(prev)), - in, - out, - local)); + /** + * Replaces a function declaraction in the program + * with the vapor equivalent. + */ + String next = String.format("func %s [in %d, out %d, local %d]", + this.parseFuncName(prev), + in, + out, + local); + + log.info(String.format("Replacing function declaraction\n%s\nwith\n%s", + this.get(prev), + next)); + + this.set(prev, next); + } + + protected String parseFuncName(VFunction func) { + /** + * Helper for replaceFunctionDeclare + * "func A_foo(this t.1)" -> "A_foo" + */ + String str = this.get(func); + int openParenIndex = str.indexOf('('); + return str.substring(5, openParenIndex).trim(); } protected String spill() { @@ -59,12 +91,4 @@ class Kettle { this.vapor.set(this.indexOf(n), s); } - private static String parseFuncName(String func) { - /** - * "func A_foo(this t.1)" -> "A_foo" - */ - int openParenIndex = func.indexOf('('); - return func.substring(5, openParenIndex); - } - } diff --git a/vaporize/library/SpillEverywhere.java b/vaporize/library/SpillEverywhere.java index a1b0968..d9e9f3f 100644 --- a/vaporize/library/SpillEverywhere.java +++ b/vaporize/library/SpillEverywhere.java @@ -1,5 +1,8 @@ package vaporize.library; +import java.util.logging.Logger; +import java.util.logging.ConsoleHandler; + import cs132.vapor.ast.*; import st.*; import misc.*; @@ -7,9 +10,16 @@ import java.util.*; public class SpillEverywhere extends VInstr.VisitorPR<String, String, RuntimeException> { - VaporProgram vp; - Kettle kettle; - String vaporm; + private static Logger log = Logger.getLogger(SpillEverywhere.class.getName()); + private static ConsoleHandler consoleHandler = new ConsoleHandler(); + static { + consoleHandler.setFormatter(new MinimalSimpleFormatter()); + log.addHandler(consoleHandler); + } + + private VaporProgram vp; + private Kettle kettle; + private String vaporm; // public CFGSimp(VaporProgram v) { @@ -25,82 +35,81 @@ public class SpillEverywhere extends VInstr.VisitorPR<String, String, RuntimeExc // } public SpillEverywhere(VaporProgram vp, ArrayList<String> vapor) { - this.vp = vp; + this.vp = vp; this.kettle = new Kettle(vapor); this.vaporm = ""; - for (VFunction f : vp.functions) { - for (VInstr s : f.body) { - // s.accept("", this); - } - PrintFilter.print(this.kettle.get(f), true); - this.kettle.replaceFunctionDeclare(f, 0, 0, 0); - } - PrintFilter.print(kettle.dump(), true); + for (VFunction f : vp.functions) { + for (VInstr s : f.body) { + // s.accept("", this); + } + this.kettle.replaceFunctionDeclare(f, 0, 0, 0); + } + this.log.info(kettle.dump()); } public String visit(String p, VMemRead n) throws RuntimeException { - PrintFilter.print(String.format("%s (%s:%s)", - n.getClass().getSimpleName(), - this.kettle.get(n), - n.sourcePos.toString()), true); + log.info(String.format("%s (%s:%s)", + n.getClass().getSimpleName(), + this.kettle.get(n), + n.sourcePos.toString())); return null; } public String visit(String p, VMemWrite n) throws RuntimeException { - PrintFilter.print(String.format("%s (%s:%s)", - n.getClass().getSimpleName(), - this.kettle.get(n), - n.sourcePos.toString()), true); + log.info(String.format("%s (%s:%s)", + n.getClass().getSimpleName(), + this.kettle.get(n), + n.sourcePos.toString())); return null; } public String visit(String p, VAssign n) throws RuntimeException { - PrintFilter.print(String.format("%s (%s:%s)", - n.getClass().getSimpleName(), - this.kettle.get(n), - n.sourcePos.toString()), true); + log.info(String.format("%s (%s:%s)", + n.getClass().getSimpleName(), + this.kettle.get(n), + n.sourcePos.toString())); return null; } public String visit(String p, VBranch n) throws RuntimeException { - PrintFilter.print(String.format("%s (%s:%s)", - n.getClass().getSimpleName(), - this.kettle.get(n), - n.sourcePos.toString()), true); - return null; + log.info(String.format("%s (%s:%s)", + n.getClass().getSimpleName(), + this.kettle.get(n), + n.sourcePos.toString())); + return null; } public String visit(String p, VGoto n) throws RuntimeException { - PrintFilter.print(String.format("%s (%s:%s)", - n.getClass().getSimpleName(), - this.kettle.get(n), - n.sourcePos.toString()), true); - return null; + log.info(String.format("%s (%s:%s)", + n.getClass().getSimpleName(), + this.kettle.get(n), + n.sourcePos.toString())); + return null; } public String visit(String p, VCall n) throws RuntimeException { - PrintFilter.print(String.format("%s (%s:%s)", - n.getClass().getSimpleName(), - this.kettle.get(n), - n.sourcePos.toString()), true); - return null; + log.info(String.format("%s (%s:%s)", + n.getClass().getSimpleName(), + this.kettle.get(n), + n.sourcePos.toString())); + return null; } public String visit(String p, VBuiltIn n) throws RuntimeException { - PrintFilter.print(String.format("%s (%s:%s)", - n.op.name, - this.kettle.get(n), - n.sourcePos.toString()), true); - return null; + log.info(String.format("%s (%s:%s)", + n.op.name, + this.kettle.get(n), + n.sourcePos.toString())); + return null; } public String visit(String p, VReturn n) throws RuntimeException { - PrintFilter.print(String.format("%s (%s:%s)", - n.getClass().getSimpleName(), - this.kettle.get(n), - n.sourcePos.toString()), true); - return null; + log.info(String.format("%s (%s:%s)", + n.getClass().getSimpleName(), + this.kettle.get(n), + n.sourcePos.toString())); + return null; } } |