summaryrefslogtreecommitdiff
path: root/condense
diff options
context:
space:
mode:
Diffstat (limited to 'condense')
-rw-r--r--condense/CondenseVisitor.java165
-rw-r--r--condense/StackHelper.java70
-rw-r--r--condense/Utilities.java40
3 files changed, 225 insertions, 50 deletions
diff --git a/condense/CondenseVisitor.java b/condense/CondenseVisitor.java
index d494845..4ca987c 100644
--- a/condense/CondenseVisitor.java
+++ b/condense/CondenseVisitor.java
@@ -1,23 +1,168 @@
package condense;
+import cs132.vapor.ast.*;
import misc.*;
+
import java.util.*;
+import java.util.regex.*;
-public class CondenseVisitor {
+public class CondenseVisitor extends VInstr.Visitor<RuntimeException>{
ArrayList<String> vaporm;
ArrayList<String> mips;
- public CondenseVisitor(ArrayList<String> vaporm) {
+ StackHelper curr;
+
+ public CondenseVisitor(VaporProgram vp, ArrayList<String> vaporm) {
this.vaporm = vaporm;
this.mips = new ArrayList<String>();
- for (instr : this.vaporm) {
- instr = instr.trim();
- if (instr.startsWith("local")) {
- // store
- this.store(instr);
- } else if (instr
- }
- }
+ for (int i = 0; i < vp.functions.length; ++i) {
+ this.curr = new StackHelper(vp.functions[i].stack);
+ this.addMIPS(vp.functions[i].ident+":");
+
+ // prologue
+ this.addMIPS(" sw $fp -8($sp)");
+ this.addMIPS(" move $fp $sp");
+ this.addMIPS(String.format(" subu $sp $sp %d",
+ this.curr.getFrameSize()));
+ this.addMIPS(" sw $ra -4($fp)");
+
+ TreeSet<Node> f = this.sortFunction(vp.functions[i]);
+ MinimalLogger.info(String.format("Starting loop with function:\n %s",
+ f.toString()));
+
+ for (Node n : f) {
+ if (n instanceof VInstr) {
+ ((VInstr) n).accept(this);
+ }
+ else
+ this.addMIPS(((VCodeLabel) n).ident + ":");
+ }
+
+ }
+ }
+
+ public ArrayList<String> getMIPS() {
+ return this.mips;
+ }
+
+ public void addMIPS(String str) {
+ MinimalLogger.info(String.format("Adding string:\n%s",
+ str));
+
+ this.mips.add(str);
+ }
+
+ public TreeSet<Node> sortFunction(VFunction f) {
+ TreeSet<Node> sort = new TreeSet<Node>((v1, v2) -> {
+ return Integer.compare(v1.sourcePos.line, v2.sourcePos.line);
+ });
+
+ for (VInstr s : f.body) {
+ sort.add(s);
+ }
+ for (VCodeLabel l : f.labels) {
+ sort.add(l);
+ }
+ return sort;
+ }
+
+ public void visit(VAssign n) throws RuntimeException {
+ MinimalLogger.info(String.format("->%s (%s)",
+ n.getClass().getSimpleName(),
+ n.sourcePos.toString()));
+ ///////////////////////////////////////////////////////////////
+
+ ///////////////////////////////////////////////////////////////
+ MinimalLogger.info(String.format("<-%s (%s)",
+ n.getClass().getSimpleName(),
+ n.sourcePos.toString()));
+ }
+
+ public void visit(VCall n) throws RuntimeException {
+ MinimalLogger.info(String.format("->%s (%s)",
+ n.getClass().getSimpleName(),
+ n.sourcePos.toString()));
+ ///////////////////////////////////////////////////////////////
+
+ ///////////////////////////////////////////////////////////////
+ MinimalLogger.info(String.format("<-%s (%s)",
+ n.getClass().getSimpleName(),
+ n.sourcePos.toString()));
+ }
+
+ public void visit(VBuiltIn n) throws RuntimeException {
+ MinimalLogger.info(String.format("->%s (%s)",
+ n.getClass().getSimpleName(),
+ n.sourcePos.toString()));
+ ///////////////////////////////////////////////////////////////
+
+ ///////////////////////////////////////////////////////////////
+ MinimalLogger.info(String.format("<-%s (%s)",
+ n.getClass().getSimpleName(),
+ n.sourcePos.toString()));
+ }
+
+ public void visit(VMemWrite n) throws RuntimeException {
+ MinimalLogger.info(String.format("->%s (%s)",
+ n.getClass().getSimpleName(),
+ n.sourcePos.toString()));
+ ///////////////////////////////////////////////////////////////
+ this.addMIPS(String.format(" sw %s %s",
+ n.source.toString(),
+ this.curr.get(((VMemRef.Stack) n.dest))));
+ ///////////////////////////////////////////////////////////////
+ MinimalLogger.info(String.format("<-%s (%s)",
+ n.getClass().getSimpleName(),
+ n.sourcePos.toString()));
+ }
+
+ public void visit(VMemRead n) throws RuntimeException {
+ MinimalLogger.info(String.format("->%s (%s)",
+ n.getClass().getSimpleName(),
+ n.sourcePos.toString()));
+ ///////////////////////////////////////////////////////////////
+
+ ///////////////////////////////////////////////////////////////
+ MinimalLogger.info(String.format("<-%s (%s)",
+ n.getClass().getSimpleName(),
+ n.sourcePos.toString()));
+ }
+
+ public void visit(VBranch n) throws RuntimeException {
+ MinimalLogger.info(String.format("->%s (%s)",
+ n.getClass().getSimpleName(),
+ n.sourcePos.toString()));
+ ///////////////////////////////////////////////////////////////
+
+ ///////////////////////////////////////////////////////////////
+ MinimalLogger.info(String.format("<-%s (%s)",
+ n.getClass().getSimpleName(),
+ n.sourcePos.toString()));
+ }
+
+ public void visit(VGoto n) throws RuntimeException {
+ MinimalLogger.info(String.format("->%s (%s)",
+ n.getClass().getSimpleName(),
+ n.sourcePos.toString()));
+ ///////////////////////////////////////////////////////////////
+
+ ///////////////////////////////////////////////////////////////
+ MinimalLogger.info(String.format("<-%s (%s)",
+ n.getClass().getSimpleName(),
+ n.sourcePos.toString()));
+ }
+
+ public void visit(VReturn n) throws RuntimeException {
+ MinimalLogger.info(String.format("->%s (%s)",
+ n.getClass().getSimpleName(),
+ n.sourcePos.toString()));
+ ///////////////////////////////////////////////////////////////
+
+ ///////////////////////////////////////////////////////////////
+ MinimalLogger.info(String.format("<-%s (%s)",
+ n.getClass().getSimpleName(),
+ n.sourcePos.toString()));
}
+}
diff --git a/condense/StackHelper.java b/condense/StackHelper.java
new file mode 100644
index 0000000..a0a6e2d
--- /dev/null
+++ b/condense/StackHelper.java
@@ -0,0 +1,70 @@
+package condense;
+
+import misc.*;
+import cs132.vapor.ast.VFunction;
+import cs132.vapor.ast.VMemRef;
+
+import java.util.*;
+
+public class StackHelper {
+
+ private int in;
+ private int out;
+ private int local;
+ private int frame_size;
+ private HashMap<String, String> idx_to_addr;
+
+ public StackHelper(VFunction.Stack s) {
+ this.in = s.in;
+ this.out = s.out;
+ this.local = s.local;
+ this.frame_size = this.stackFrameFormula();
+ this.idx_to_addr = new HashMap<String,String>();
+ MinimalLogger.info(String.format("Found in:%d out:%d local%d framesize:%d...",
+ this.in,
+ this.out,
+ this.local,
+ this.frame_size));
+
+ String key;
+ String value;
+ int loc;
+ for (int i = 0; i < this.local; ++i) { // locals
+ loc = (( -i - 1 ) * 4) - 8;
+ key = String.format("Local[%d]", i);
+ value = String.format("%d($fp)", loc);
+ this.idx_to_addr.put(key, value);
+ }
+ for (int i = 0; i < this.out; ++i) { // out
+ loc = ((this.out + this.local + 2) - i) * -4;
+ key = String.format("Out[%d]", i);
+ value = String.format("%d($fp)", loc);
+ this.idx_to_addr.put(key, value);
+ }
+ for (int i = 0; i < this.in; ++i) { // in
+ loc = i * 4;
+ key = String.format("In[%d]", i);
+ value = String.format("%d($fp)", loc);
+ this.idx_to_addr.put(key, value);
+ }
+
+ MinimalLogger.info(String.format("Mapping: %s", this.idx_to_addr.toString()));
+ }
+
+ public String get(VMemRef.Stack m) {
+ String key = String.format("%s[%d]",
+ m.region,
+ m.index);
+ MinimalLogger.severe(key);
+ return this.idx_to_addr.get(key);
+ }
+
+ public int getFrameSize() {
+ return this.frame_size;
+ }
+
+ private int stackFrameFormula() {
+ return (this.out + this.local + 2) * 4;
+ }
+
+}
diff --git a/condense/Utilities.java b/condense/Utilities.java
deleted file mode 100644
index 9c052fc..0000000
--- a/condense/Utilities.java
+++ /dev/null
@@ -1,40 +0,0 @@
-package condense;
-
-import java.util.*;
-
-public class Utilities {
-
-
- public static int computeStackFrameSize(String declare) {
- MinimalLogger.info(String.format("Computing frame stack size for %s...",
- declare));
- declare = extractNumeric(declare);
- // assume that the LAST three digits are
- // in, out, local
- String[] sizes = declare.split(SPACE)[:-3];
- int ret = this.stackFrameFormula(Integer.parseInt(sizes[1]),
- Integer.parseInt(sizes[2]));
- MinimalLogger.info(String.format("Stack size is (%s + %s + 2) * 4 = %d",
- sizes[1],
- sizes[2],
- ret));
- return ret;
- }
-
- private static int stackFrameFormula(int out, int local) {
- return (out + local + 2) * 4;
- }
-
- private static int extractNumeric(String str) {
- str = str.replaceAll("[^\\d] & quot;, "
- ");
- str = str.trim();
- str = str.replaceAll(" + ", "
- ");
- if (str.equals(" "))
- return "
- -1 & quot;
- ;
- return str;
- }
-}