diff options
author | bd-912 <bdunahu@colostate.edu> | 2024-04-24 21:05:15 -0600 |
---|---|---|
committer | bd-912 <bdunahu@colostate.edu> | 2024-04-24 21:05:15 -0600 |
commit | 62091005231095abbf8e2cebbfce708815cb63f0 (patch) | |
tree | 5186bf7db88b20704a48ab14413349291675ea82 | |
parent | 0bbc4cb511bd90cebf94ccfcfce5ff8983ebb918 (diff) |
Added VaporizeVisitor, with VAssign, VMemRead, and VMemWrite
-rw-r--r-- | V2VM.java | 13 | ||||
-rw-r--r-- | vaporize/library/LIRDict.java | 8 | ||||
-rw-r--r-- | vaporize/library/RegisterAlloc.java | 1 | ||||
-rw-r--r-- | vaporize/library/VaporizeVisitor.java | 145 |
4 files changed, 166 insertions, 1 deletions
@@ -38,9 +38,20 @@ public class V2VM { for (LIRDict interval : lvs) { MinimalLogger.info(String.format("Starting Linear Allocation for %s...", interval.getFunction())); - new RegisterAlloc(interval, Arrays.copyOfRange(prog.registers, 6, 22)); + new RegisterAlloc(interval, Arrays.copyOfRange(prog.registers, 9, 22)); // spill registers: t0, t1, t2 } + MinimalLogger.info("Removing extraneous lines from the program representation..."); + for (String line : new ArrayList<String>(strProg)) { + // delete all lines not a function table! + if (!line.trim().startsWith("const") && !line.trim().startsWith(":")) + strProg.remove(line); + } + MinimalLogger.info(String.format("New program: %s", + strProg.toString())); + VaporizeVisitor vv = new VaporizeVisitor(prog, strProg, lvs); + System.out.println(String.join("\n", vv.getVaporm())); + } catch (IOException e) { System.out.println(e.toString()); System.exit(1); diff --git a/vaporize/library/LIRDict.java b/vaporize/library/LIRDict.java index 2095254..d924d5e 100644 --- a/vaporize/library/LIRDict.java +++ b/vaporize/library/LIRDict.java @@ -10,6 +10,7 @@ import java.util.*; public class LIRDict { private TreeSet<LIRVar> intervals; + private int spilled_num; // the number of spilled registers private ControlFlowGraph cfg; public LIRDict(VFunction f, ControlFlowGraph cfg) { @@ -74,5 +75,12 @@ public class LIRDict { return this.cfg.getFunction(); } + public void addSpilledNum() { + ++this.spilled_num; + } + + public int getSpilledNum() { + return this.spilled_num; + } } diff --git a/vaporize/library/RegisterAlloc.java b/vaporize/library/RegisterAlloc.java index 4032de3..b316b96 100644 --- a/vaporize/library/RegisterAlloc.java +++ b/vaporize/library/RegisterAlloc.java @@ -61,6 +61,7 @@ public class RegisterAlloc { private void spillAtInterval(LIRVar interval) { MinimalLogger.severe(String.format("Ran out of free registers, but a spill for %s was not performed!", interval.toString())); + this.intervals.addSpilledNum(); // LIRVar spill = this.active.get(this.active.length()-1); // if (spill.getLastUse() > interval.getLastUse()) { // ; diff --git a/vaporize/library/VaporizeVisitor.java b/vaporize/library/VaporizeVisitor.java new file mode 100644 index 0000000..11f0bc5 --- /dev/null +++ b/vaporize/library/VaporizeVisitor.java @@ -0,0 +1,145 @@ +package vaporize.library; + +import cs132.vapor.ast.*; +import misc.*; + +import java.util.*; + +public class VaporizeVisitor extends VInstr.VisitorP<LIRDict, RuntimeException> { + + private ArrayList<String> vaporm; + + public VaporizeVisitor(VaporProgram vp, ArrayList<String> vaporm, ArrayList<LIRDict> interval_list) { + this.vaporm = vaporm; + + // is it possible for interval_list and the + // function in vp to be out of order? + for (int i = 0; i < vp.functions.length; ++i) { + this.addVaporm(String.format("func %s [in %d, out %d, local %d]", + vp.functions[i].ident, 0, 0, interval_list.get(i).getSpilledNum())); + for (VInstr s : vp.functions[i].body) + s.accept(interval_list.get(i), this); + } + } + + public ArrayList<String> getVaporm() { + return this.vaporm; + } + + public void addVaporm(String str) { + MinimalLogger.info(String.format("Adding string:\n%s", + str)); + this.vaporm.add(str); + } + + public void visit(LIRDict d, VAssign n) throws RuntimeException { + MinimalLogger.info(String.format("->%s (%s)", + n.getClass().getSimpleName(), + n.sourcePos.toString())); + /////////////////////////////////////////////////////////////// + String dest = d.getInterval(((VVarRef.Local) n.dest).ident).getAssignedRegister(); + String source = (n.source instanceof VVarRef.Local) ? + d.getInterval(n.source.toString()).getAssignedRegister() + : n.source.toString(); + this.addVaporm(String.format(" %s = %s", + dest, source)); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<-%s (%s)", + n.getClass().getSimpleName(), + n.sourcePos.toString())); + } + + public void visit(LIRDict d, 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(LIRDict d, 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(LIRDict d, VMemWrite n) throws RuntimeException { + MinimalLogger.info(String.format("->%s (%s)", + n.getClass().getSimpleName(), + n.sourcePos.toString())); + /////////////////////////////////////////////////////////////// + String dest = d.getInterval(((VMemRef.Global) n.dest).base.toString()) + .getAssignedRegister(); + String source = (n.source instanceof VVarRef.Local) ? + d.getInterval(n.source.toString()).getAssignedRegister() : + n.source.toString(); + int byteOffset = ((VMemRef.Global) n.dest).byteOffset; + this.addVaporm(String.format(" [%s+%d] = %s", + dest, byteOffset, source)); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<-%s (%s)", + n.getClass().getSimpleName(), + n.sourcePos.toString())); + } + + public void visit(LIRDict d, VMemRead n) throws RuntimeException { + MinimalLogger.info(String.format("->%s (%s)", + n.getClass().getSimpleName(), + n.sourcePos.toString())); + /////////////////////////////////////////////////////////////// + String dest = (n.dest instanceof VVarRef.Local) ? + d.getInterval(n.dest.toString()).getAssignedRegister() : + n.dest.toString(); + String source = d.getInterval(((VMemRef.Global) n.source).base.toString()) + .getAssignedRegister(); + int byteOffset = ((VMemRef.Global) n.source).byteOffset; + this.addVaporm(String.format(" %s = [%s+%d]", + dest, source, byteOffset)); + /////////////////////////////////////////////////////////////// + MinimalLogger.info(String.format("<-%s (%s)", + n.getClass().getSimpleName(), + n.sourcePos.toString())); + } + + public void visit(LIRDict d, 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(LIRDict d, 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(LIRDict d, 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())); + } + +} |