diff options
Diffstat (limited to 'vaporize')
-rw-r--r-- | vaporize/LIRDict.java | 10 | ||||
-rw-r--r-- | vaporize/RegisterAlloc.java | 10 | ||||
-rw-r--r-- | vaporize/VaporizeVisitor.java | 80 |
3 files changed, 60 insertions, 40 deletions
diff --git a/vaporize/LIRDict.java b/vaporize/LIRDict.java index 50c79b0..30bfed3 100644 --- a/vaporize/LIRDict.java +++ b/vaporize/LIRDict.java @@ -84,14 +84,4 @@ public class LIRDict { return this.spilled_num; } - private int subOneLine(int i) { - int ret = i - 1; - - CFGNode n = cfg.getNode(new Integer(ret)); - if (n != null && n.getInstruction() instanceof VCodeLabel) - --ret; - - return ret; - } - } diff --git a/vaporize/RegisterAlloc.java b/vaporize/RegisterAlloc.java index ae5826b..100d6c4 100644 --- a/vaporize/RegisterAlloc.java +++ b/vaporize/RegisterAlloc.java @@ -9,8 +9,10 @@ public class RegisterAlloc { private String[] all_registers; private Stack<String> free_registers; private TreeSet<LIRVar> active; + private int spill_start; public RegisterAlloc(LIRDict intervals, String[] all_registers) { + this.spill_start = 14; this.intervals = intervals; this.all_registers = all_registers; this.free_registers = new Stack<String>(); @@ -68,13 +70,11 @@ 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!", + // You can make this spill optimally (the sarkar linearscan algorithm) + MinimalLogger.severe(String.format("Ran out of free registers, had to spill %s!", interval.toString())); this.intervals.addSpilledNum(); - // LIRVar spill = this.active.get(this.active.length()-1); - // if (spill.getLastUse() > interval.getLastUse()) { - // ; - // } + interval.assignRegister(String.format("local[%d]", this.spill_start++)); } } diff --git a/vaporize/VaporizeVisitor.java b/vaporize/VaporizeVisitor.java index 17e9981..ba319cb 100644 --- a/vaporize/VaporizeVisitor.java +++ b/vaporize/VaporizeVisitor.java @@ -8,20 +8,25 @@ import java.util.*; public class VaporizeVisitor extends VInstr.VisitorP<LIRDict, RuntimeException> { private ArrayList<String> vaporm; + private Stack<String> spill_stack; + private LIRDict curr; String[] callee_save = new String[] { "$s0", "$s1", "$s2", "$s3", "$s4", "$s5", "$s6", "$s7" }; - String[] caller_save = new String[] { "$t0", "$t1", "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t8" }; + String[] caller_save = new String[] { "$t3", "$t4", "$t5", "$t6", "$t7", "$t8" }; String[] arg_pass = new String[] { "$a0", "$a1", "$a2", "$a3" }; String[] ret_pass = new String[] { "$v0" }; + String[] spillers = new String[] {"$t0", "$t1", "$t2" }; + public VaporizeVisitor(VaporProgram vp, ArrayList<String> vaporm, ArrayList<LIRDict> interval_list) { this.vaporm = vaporm; + this.spill_stack = new Stack<String>(); // 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() + 17)); + vp.functions[i].ident, 0, 0, interval_list.get(i).getSpilledNum() + 14)); for (int j = 0; j < this.callee_save.length; ++j) { this.addVaporm(String.format(" local[%s] = %s", j, @@ -40,8 +45,11 @@ public class VaporizeVisitor extends VInstr.VisitorP<LIRDict, RuntimeException> f.toString())); for (Node n : f) { - if (n instanceof VInstr) + if (n instanceof VInstr) { + this.curr = interval_list.get(i); ((VInstr) n).accept(interval_list.get(i), this); + this.emptySpillStack(); + } else this.addVaporm(((VCodeLabel) n).ident + ":"); } @@ -62,6 +70,29 @@ public class VaporizeVisitor extends VInstr.VisitorP<LIRDict, RuntimeException> return sort; } + public String getRegister(String s) { + if (this.curr.getInterval(s) == null) + return null; + String r = this.curr.getInterval(s).getAssignedRegister(); + if (r.contains("local")) {// SPILL + int i = this.spill_stack.size(); + String reg = this.spillers[i]; + this.addVaporm(String.format(" %s = %s", + reg, + r)); + this.spill_stack.push(String.format(" %s = %s", + r, + reg)); + r = reg; + } + return r; + } + + public void emptySpillStack() { + for (int i = 0; i < this.spill_stack.size(); ++i) + this.addVaporm(this.spill_stack.pop()); + } + public ArrayList<String> getVaporm() { return this.vaporm; } @@ -77,9 +108,9 @@ public class VaporizeVisitor extends VInstr.VisitorP<LIRDict, RuntimeException> n.getClass().getSimpleName(), n.sourcePos.toString())); /////////////////////////////////////////////////////////////// - String dest = d.getInterval(((VVarRef.Local) n.dest).ident).getAssignedRegister(); + String dest = this.getRegister(((VVarRef.Local) n.dest).ident); String source = (n.source instanceof VVarRef.Local) ? - d.getInterval(n.source.toString()).getAssignedRegister() + this.getRegister(n.source.toString()) : n.source.toString(); this.addVaporm(String.format(" %s = %s", dest, source)); @@ -104,7 +135,7 @@ public class VaporizeVisitor extends VInstr.VisitorP<LIRDict, RuntimeException> // FIXME for arg num > 4! for (int i = 0; i < n.args.length; ++i) { String reg = (n.args[i] instanceof VVarRef.Local) ? - d.getInterval(n.args[i].toString()).getAssignedRegister() + this.getRegister(n.args[i].toString()) : n.args[i].toString(); MinimalLogger.info(String.format("Adding argument for %s", n.args[i].toString())); @@ -116,16 +147,15 @@ public class VaporizeVisitor extends VInstr.VisitorP<LIRDict, RuntimeException> MinimalLogger.severe(String.format("n addr: %s", n.addr.toString())); MinimalLogger.severe(String.format("Interval: %s", - d.getInterval(n.addr.toString()))); - LIRVar interval = d.getInterval(n.addr.toString()); - if (interval == null) + this.getRegister(n.addr.toString()))); + String r = this.getRegister(n.addr.toString()); + if (r == null) // a label this.addVaporm(String.format(" call %s", n.addr.toString())); else this.addVaporm(String.format(" call %s", - interval - .getAssignedRegister())); + r)); for (int i = 0; i < this.caller_save.length; ++i) { this.addVaporm(String.format(" %s = local[%s]", this.caller_save[i], @@ -135,8 +165,8 @@ public class VaporizeVisitor extends VInstr.VisitorP<LIRDict, RuntimeException> // get dest if (n.dest != null) { this.addVaporm(String.format(" %s = $v0", - d.getInterval(((VVarRef.Local) n.dest).ident) - .getAssignedRegister())); + this.getRegister(((VVarRef.Local) n.dest).ident) + )); } /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<-%s (%s)", @@ -152,8 +182,8 @@ public class VaporizeVisitor extends VInstr.VisitorP<LIRDict, RuntimeException> String ret = ""; if (n.dest != null) { ret += String.format("%s = ", - d.getInterval(((VVarRef.Local) n.dest).ident) - .getAssignedRegister()); + this.getRegister(((VVarRef.Local) n.dest).ident) + ); } ret += String.format("%s(", ((VBuiltIn.Op) n.op).name); @@ -161,7 +191,7 @@ public class VaporizeVisitor extends VInstr.VisitorP<LIRDict, RuntimeException> String par; for (VOperand a : n.args) { par = (a instanceof VVarRef.Local) ? - d.getInterval(a.toString()).getAssignedRegister() + this.getRegister(a.toString()) : a.toString(); ret += String.format("%s ", par); @@ -182,10 +212,10 @@ public class VaporizeVisitor extends VInstr.VisitorP<LIRDict, RuntimeException> n.getClass().getSimpleName(), n.sourcePos.toString())); /////////////////////////////////////////////////////////////// - String dest = d.getInterval(((VMemRef.Global) n.dest).base.toString()) - .getAssignedRegister(); + String dest = this.getRegister(((VMemRef.Global) n.dest).base.toString()) + ; String source = (n.source instanceof VVarRef.Local) ? - d.getInterval(n.source.toString()).getAssignedRegister() : + this.getRegister(n.source.toString()) : n.source.toString(); int byteOffset = ((VMemRef.Global) n.dest).byteOffset; this.addVaporm(String.format(" [%s+%d] = %s", @@ -202,10 +232,10 @@ public class VaporizeVisitor extends VInstr.VisitorP<LIRDict, RuntimeException> n.sourcePos.toString())); /////////////////////////////////////////////////////////////// String dest = (n.dest instanceof VVarRef.Local) ? - d.getInterval(n.dest.toString()).getAssignedRegister() : + this.getRegister(n.dest.toString()) : n.dest.toString(); - String source = d.getInterval(((VMemRef.Global) n.source).base.toString()) - .getAssignedRegister(); + String source = this.getRegister(((VMemRef.Global) n.source).base.toString()) + ; int byteOffset = ((VMemRef.Global) n.source).byteOffset; this.addVaporm(String.format(" %s = [%s+%d]", dest, source, byteOffset)); @@ -226,8 +256,8 @@ public class VaporizeVisitor extends VInstr.VisitorP<LIRDict, RuntimeException> else ret += "if0 "; - ret += d.getInterval(n.value.toString()) - .getAssignedRegister() + " goto :"; + ret += this.getRegister(n.value.toString()) + + " goto :"; ret += n.target.ident; @@ -260,7 +290,7 @@ public class VaporizeVisitor extends VInstr.VisitorP<LIRDict, RuntimeException> // get ret if (n.value != null) { String reg = (n.value instanceof VVarRef.Local) ? - d.getInterval(n.value.toString()).getAssignedRegister() : + this.getRegister(n.value.toString()) : n.value.toString(); this.addVaporm(String.format(" $v0 = %s", reg)); |