diff options
author | bd-912 <bdunahu@colostate.edu> | 2024-05-06 00:59:26 -0600 |
---|---|---|
committer | bd-912 <bdunahu@colostate.edu> | 2024-05-06 00:59:26 -0600 |
commit | 8e33e2828ffc62238afc32bb2593b8619f586077 (patch) | |
tree | 60158e69db97739d5e7512210f53c9d2f99fc62d /condense/CondenseVisitor.java | |
parent | 925372248bfb45e0027cf71d8d40c26e48261ee5 (diff) |
Partial implementation for starter Condense
Diffstat (limited to 'condense/CondenseVisitor.java')
-rw-r--r-- | condense/CondenseVisitor.java | 165 |
1 files changed, 155 insertions, 10 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())); } +} |