summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbd-912 <bdunahu@colostate.edu>2024-04-19 19:04:01 -0600
committerbd-912 <bdunahu@colostate.edu>2024-04-19 19:04:01 -0600
commit66ffeaf968bd332f1e18cb27aad11b50e4dd0eab (patch)
treec0d52e8e82590c0656f915610b5a7027a6abd057
parente98ca14483fb531c41d51677cc7075a0b8e6bd55 (diff)
Switched to use of Logger Module
-rw-r--r--V2VM.java21
-rw-r--r--misc/MinimalSimpleFormatter.java12
-rw-r--r--vaporize/library/CFGSimp.java52
-rw-r--r--vaporize/library/Kettle.java52
-rw-r--r--vaporize/library/SpillEverywhere.java107
5 files changed, 160 insertions, 84 deletions
diff --git a/V2VM.java b/V2VM.java
index 3b17c3a..9fdfc73 100644
--- a/V2VM.java
+++ b/V2VM.java
@@ -1,3 +1,6 @@
+import java.util.logging.Logger;
+import java.util.logging.ConsoleHandler;
+
import java.io.*;
import java.util.ArrayList;
import cs132.util.ProblemException;
@@ -18,7 +21,13 @@ import vaporize.library.*;
public class V2VM {
+
public static void main(String[] args) {
+ Logger log = Logger.getLogger(V2VM.class.getName());
+ ConsoleHandler consoleHandler = new ConsoleHandler();
+ consoleHandler.setFormatter(new MinimalSimpleFormatter());
+ log.addHandler(consoleHandler);
+
try {
System.in.mark(-1);
InputStream systemInCopy = createCopyOfSystemIn();
@@ -27,13 +36,11 @@ public class V2VM {
VaporProgram prog = parseVapor(System.in, System.out);
- SpillEverywhere spill = new SpillEverywhere(prog, strProg);
-
- ArrayList<ControlFlowGraph> cfgs = new ArrayList<ControlFlowGraph>();
- for (VFunction f : prog.functions) {
- CFGSimp cfg = new CFGSimp(f);
- cfgs.add(cfg.getCFG());
- }
+ log.info(String.format("Generating CFGs..."));
+ CFGSimp cfv = new CFGSimp(prog, strProg);
+ ArrayList<ControlFlowGraph> cfgs = cfv.getCFGs();
+ log.info(String.format("Spilling Everywhere..."));
+ SpillEverywhere spill = new SpillEverywhere(prog, strProg);
} catch (IOException e) {
System.out.println(e.toString());
diff --git a/misc/MinimalSimpleFormatter.java b/misc/MinimalSimpleFormatter.java
new file mode 100644
index 0000000..3a42398
--- /dev/null
+++ b/misc/MinimalSimpleFormatter.java
@@ -0,0 +1,12 @@
+package misc;
+
+import java.util.logging.ConsoleHandler;
+import java.util.logging.LogRecord;
+import java.util.logging.SimpleFormatter;
+
+public class MinimalSimpleFormatter extends SimpleFormatter {
+ @Override
+ public String format(LogRecord record) {
+ return record.getLevel() + ": " + formatMessage(record) + "\n";
+ }
+}
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;
}
}