diff options
Diffstat (limited to 'vaporize/library')
-rw-r--r-- | vaporize/library/Kettle.java | 116 | ||||
-rw-r--r-- | vaporize/library/LIRDict.java | 86 | ||||
-rw-r--r-- | vaporize/library/LIRVar.java | 81 | ||||
-rw-r--r-- | vaporize/library/LIRVisitor.java | 258 | ||||
-rw-r--r-- | vaporize/library/RegisterAlloc.java | 71 | ||||
-rw-r--r-- | vaporize/library/SpillEverywhere.java | 92 | ||||
-rw-r--r-- | vaporize/library/TransientInterval.java | 21 | ||||
-rw-r--r-- | vaporize/library/VaporizeVisitor.java | 269 |
8 files changed, 0 insertions, 994 deletions
diff --git a/vaporize/library/Kettle.java b/vaporize/library/Kettle.java deleted file mode 100644 index ffbf2dd..0000000 --- a/vaporize/library/Kettle.java +++ /dev/null @@ -1,116 +0,0 @@ -package vaporize.library; - -import cs132.vapor.ast.*; -import misc.*; - -import java.util.ArrayList; - -/** - * This class contains various generic methods for - * assembling common-use vaporm strings and managing - * the original input program. - * - * Robert Martin cries - */ -class Kettle { - - ArrayList<String> vapor; - ArrayList<String> vaporm; - - protected Kettle(ArrayList<String> vapor) { - MinimalLogger.info("Pouring program into kettle..."); - this.vapor = vapor; - this.vaporm = new ArrayList<String>(); - } - - protected String get(Node n) { - /** - * Given the source position of a Node, returns the original line. - */ - return this.vapor.get(this.indexOf(n)); - } - - protected void replaceFunctionDeclare(VFunction prev, int in, - int out, int 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); - - MinimalLogger.info(String.format("Replacing function declaraction %s with %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() { - return null; - } - - protected String backup() { - return null; - } - - protected String dump() { - return String.join("\n", this.vapor); - } - - private void set(Node n, String s) { - /** - * Sets the position of Node n in the original - * file to String s. - */ - this.vapor.set(this.indexOf(n), s); - } - - protected int indexOf(Node n) { - return n.sourcePos.line-1; - } - - // METHODS INTENDED TO BE USED WITH CFG - - /** - * Needed because VBranch doesn't seem to - * keep track of this... - */ - protected int findLabelIndex(String str) { - int index = -3; - // is this guarenteed? - String search = str.substring(1); - String comp; - for (int i = 0; i < this.vapor.size(); ++i) { - if (!this.vapor.get(i).isEmpty()) { - comp = this.vapor.get(i).trim(); - if (comp.substring(0, comp.length()-1) - .equals(search)) { - index = i; - break; - } - } - } - - if (index == -3) { - MinimalLogger.severe(String.format("findLabelIndex could not compute label for %s!", - str.trim())); - } - // go to the section AFTER the label, and account for starting at zero - return index+2; - } - -} diff --git a/vaporize/library/LIRDict.java b/vaporize/library/LIRDict.java deleted file mode 100644 index d924d5e..0000000 --- a/vaporize/library/LIRDict.java +++ /dev/null @@ -1,86 +0,0 @@ -package vaporize.library; - -import cs132.vapor.ast.VFunction; -import cs132.vapor.ast.VInstr; - -import misc.*; -import cfg.*; -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) { - - this.intervals = new TreeSet<LIRVar>((v1, v2) -> { - return (v1.compareTo(v2) != 0) ? v1.compareTo(v2) : v1.equals(v2) ? 0 : 1; - }); - this.cfg = cfg; - - for (VInstr s : f.body) { - CFGNode n = cfg.getNode(s); - int line = n.getInstruction().sourcePos.line; - - String info = "L" + line; - for (String var : n.getReaching()) - if (!this.contains(var)) { - this.intervals.add(new LIRVar(var, line, line)); - MinimalLogger.info(String.format("Reaching on %s --- New var %s", - info, - var)); - } else { - this.getInterval(var).trySetLastUse(line); - MinimalLogger.info(String.format("Reaching on %s --- Updating var %s", - info, - var)); - } - for (String var : n.getLiveness()) { - if (!this.contains(var)) - MinimalLogger.severe(String.format("%s was used before defined!", - var)); - MinimalLogger.info(String.format("Liveness on %s --- Updating var %s", - info, - var)); - this.getInterval(var).trySetLastUse(line); - } - - } - } - - public LIRVar getInterval(String s) { - LIRVar ret = null; - for (LIRVar v : this.intervals) { - if (v.equals(s)) { - ret = v; - break; - } - } - - return ret; - } - - public boolean contains(String s) { - return this.getInterval(s) != null; - } - - public SortedSet<LIRVar> getIntervals() { - // TODO Make this class iterable instead - return Collections.unmodifiableSortedSet(this.intervals); - } - - public String getFunction() { - return this.cfg.getFunction(); - } - - public void addSpilledNum() { - ++this.spilled_num; - } - - public int getSpilledNum() { - return this.spilled_num; - } - -} diff --git a/vaporize/library/LIRVar.java b/vaporize/library/LIRVar.java deleted file mode 100644 index d388797..0000000 --- a/vaporize/library/LIRVar.java +++ /dev/null @@ -1,81 +0,0 @@ -package vaporize.library; - -import misc.*; - -public class LIRVar implements Comparable<LIRVar> { - - private String alias; - private TransientInterval interval; - - private String register; - - public LIRVar(String alias, int fd, int lu) { - this.alias = alias; - this.interval = new TransientInterval(fd, lu); - this.register = null; - } - - @Override public String toString() { - return String.format("%s: %d -> %d", - this.alias, - this.interval.first_def, - this.interval.last_use); - } - - @Override public boolean equals(Object other) { - return (other instanceof LIRVar && - ((LIRVar) other).alias.equals(this.alias) && - ((LIRVar) other).interval.equals(this.interval)) || - (other instanceof String && - this.alias.equals((String) other)); - } - - @Override public int hashCode() { - return alias.hashCode(); - } - - @Override public int compareTo(LIRVar other) { - int ret; - if (this.interval.first_def > other.interval.first_def) - ret = 1; - else if (this.interval.first_def < other.interval.first_def) - ret = -1; - else - ret = 0; - return ret; - } - - protected boolean trySetLastUse(int last_use) { - /** - * If the passed last_use is greater than - * the this.last_use, override this.last_use. - */ - boolean ret = last_use > this.interval.last_use; - - if (ret) - this.interval.last_use = last_use; - else - MinimalLogger.info(String.format("Bad order! %s %d >= %d", - this.alias, - this.interval.last_use, - last_use)); - return ret; - } - - public void assignRegister(String register) { - this.register = register; - } - - public int getFirstDef() { - return this.interval.first_def; - } - - public int getLastUse() { - return this.interval.last_use; - } - - public String getAssignedRegister() { - return this.register; - } -} - diff --git a/vaporize/library/LIRVisitor.java b/vaporize/library/LIRVisitor.java deleted file mode 100644 index a962775..0000000 --- a/vaporize/library/LIRVisitor.java +++ /dev/null @@ -1,258 +0,0 @@ -package vaporize.library; - -import cs132.vapor.ast.*; -import graphviz.*; -import cfg.*; -import misc.*; - -import java.io.File; -import java.util.*; - -public class LIRVisitor extends VInstr.VisitorPR<ControlFlowGraph, String, RuntimeException> { - - private boolean use_graphviz = true; // if true, generates svg files of the edges in each function - - private Kettle kettle; - private ArrayList<LIRDict> lirs; - private CFGNode curr; // the current node being processed - private String dot_format; // a list of edges to be processed by graphviz - - public LIRVisitor(VaporProgram vp, ArrayList<String> vapor) { - this.kettle = new Kettle(vapor); - this.lirs = new ArrayList<LIRDict>(); - this.curr = null; - - - for (VFunction f : vp.functions) { - ControlFlowGraph cfg = new ControlFlowGraph(f); - this.dot_format = ""; - - MinimalLogger.info(String.format("LIRVisitor is collecting nodes for %s", - this.kettle.parseFuncName(f))); - for (VCodeLabel s : f.labels) { - cfg.addNode(new CFGNode(s)); - } - for (VInstr s : f.body) { - cfg.addNode(new CFGNode(s)); - } - - - MinimalLogger.info(String.format("LIRVisitor is collecting edges for %s", - this.kettle.parseFuncName(f))); - - // inital setup - // first visit may not find edges; cfg.addEdges will handle - this.curr = cfg.getNode(f.body[0]); - // cascades downwards --- cfg.addEdges - for (VVarRef.Local l : f.params) - cfg.addReaching(this.curr, l.ident.toString()); - for (VInstr s : f.body) - s.accept(cfg, this); - - MinimalLogger.info(String.format("Spitting out reaching/liveness...")); - for (CFGNode n : cfg.getNodes()) - MinimalLogger.info(String.format("%s ::: %s ::: %s", - n.toString(), - n.getReaching(), - n.getLiveness())); - - if (this.use_graphviz) - this.createDotGraph(this.kettle.parseFuncName(f)); - - MinimalLogger.info(String.format("Gathering intervals for %s", - this.kettle.parseFuncName(f))); - LIRDict lir = new LIRDict(f, cfg); - this.lirs.add(lir); - MinimalLogger.info(String.format("Found intervals: %s", - lir.getIntervals().toString())); - } - } - - public ArrayList<LIRDict> getLIRs() { - return this.lirs; - } - - protected void createDotGraph(String file_name) { - MinimalLogger.info(String.format("Outputting %s to %s", - this.dot_format, - file_name)); - GraphViz gv = new GraphViz(); - gv.addln(gv.start_graph()); - gv.add(this.dot_format); - gv.addln(gv.end_graph()); - String type = "svg"; - gv.decreaseDpi(); - gv.decreaseDpi(); - gv.decreaseDpi(); - gv.decreaseDpi(); - File out = new File(file_name+"."+ type); - gv.writeGraphToFile( gv.getGraph( gv.getDotSource(), type ), out ); - } - - public String visit(ControlFlowGraph cfg, VMemRead n) throws RuntimeException { - MinimalLogger.info(String.format("->%s (\"%s\":%s)", - n.getClass().getSimpleName(), - this.kettle.get(n).trim(), - n.sourcePos.toString())); - /////////////////////////////////////////////////////////////// - CFGNode curr = cfg.getNode(n); - this.dot_format += cfg.addEdge(this.curr, curr); - this.curr = curr; - - cfg.addReaching(curr, n.dest.toString()); - cfg.addLiveness(curr, ((VMemRef.Global) n.source).base.toString()); - /////////////////////////////////////////////////////////////// - MinimalLogger.info(String.format("<-%s (\"%s\":%s)", - n.getClass().getSimpleName(), - this.kettle.get(n).trim(), - n.sourcePos.toString())); - return null; - } - - public String visit(ControlFlowGraph cfg, VMemWrite n) throws RuntimeException { - MinimalLogger.info(String.format("->%s (\"%s\":%s)", - n.getClass().getSimpleName(), - this.kettle.get(n).trim(), - n.sourcePos.toString())); - /////////////////////////////////////////////////////////////// - CFGNode curr = cfg.getNode(n); - this.dot_format += cfg.addEdge(this.curr, curr); - this.curr = curr; - - cfg.addLiveness(curr, n.source.toString()); - /////////////////////////////////////////////////////////////// - MinimalLogger.info(String.format("<-%s (\"%s\":%s)", - n.getClass().getSimpleName(), - this.kettle.get(n).trim(), - n.sourcePos.toString())); - return null; - } - - public String visit(ControlFlowGraph cfg, VAssign n) throws RuntimeException { - MinimalLogger.info(String.format("->%s (\"%s\":%s)", - n.getClass().getSimpleName(), - this.kettle.get(n).trim(), - n.sourcePos.toString())); - /////////////////////////////////////////////////////////////// - CFGNode curr = cfg.getNode(n); - this.dot_format += cfg.addEdge(this.curr, curr); - this.curr = curr; - - cfg.addReaching(curr, n.dest.toString()); - cfg.addLiveness(curr, n.source.toString()); - /////////////////////////////////////////////////////////////// - MinimalLogger.info(String.format("<-%s (\"%s\":%s)", - n.getClass().getSimpleName(), - this.kettle.get(n).trim(), - n.sourcePos.toString())); - return null; - } - - public String visit(ControlFlowGraph cfg, VBranch n) throws RuntimeException { - MinimalLogger.info(String.format("->%s (\"%s\":%s)", - n.getClass().getSimpleName(), - this.kettle.get(n).trim(), - n.sourcePos.toString())); - /////////////////////////////////////////////////////////////// - CFGNode curr = cfg.getNode(n); - this.dot_format += cfg.addEdge(this.curr, curr); - this.dot_format += cfg.addEdge(curr, cfg.getNode(new Integer(this.kettle - .findLabelIndex(n.target.toString())))); - - cfg.addLiveness(curr, n.value.toString()); - /////////////////////////////////////////////////////////////// - MinimalLogger.info(String.format("<-%s (\"%s\":%s)", - n.getClass().getSimpleName(), - this.kettle.get(n).trim(), - n.sourcePos.toString())); - return null; - } - - public String visit(ControlFlowGraph cfg, VGoto n) throws RuntimeException { - MinimalLogger.info(String.format("->%s (\"%s\":%s)", - n.getClass().getSimpleName(), - this.kettle.get(n).trim(), - n.sourcePos.toString())); - /////////////////////////////////////////////////////////////// - CFGNode curr = cfg.getNode(n); - this.dot_format += cfg.addEdge(this.curr, curr); - this.dot_format += cfg.addEdge(curr, - cfg.getNode(new Integer(this.kettle - .findLabelIndex(n.target.toString())))); - - this.curr = cfg.getNextNode(curr); - /////////////////////////////////////////////////////////////// - MinimalLogger.info(String.format("<-%s (\"%s\":%s)", - n.getClass().getSimpleName(), - this.kettle.get(n).trim(), - n.sourcePos.toString())); - return null; - } - - public String visit(ControlFlowGraph cfg, VCall n) throws RuntimeException { - MinimalLogger.info(String.format("->%s (\"%s\":%s)", - n.getClass().getSimpleName(), - this.kettle.get(n).trim(), - n.sourcePos.toString())); - /////////////////////////////////////////////////////////////// - CFGNode curr = cfg.getNode(n); - this.dot_format += cfg.addEdge(this.curr, curr); - this.curr = curr; - - if (n.dest != null) - cfg.addReaching(curr, n.dest.toString()); - for (VOperand a : n.args) { - cfg.addLiveness(curr, a.toString()); - } - /////////////////////////////////////////////////////////////// - MinimalLogger.info(String.format("<-%s (\"%s\":%s)", - n.getClass().getSimpleName(), - this.kettle.get(n).trim(), - n.sourcePos.toString())); - return null; - } - - public String visit(ControlFlowGraph cfg, VBuiltIn n) throws RuntimeException { - MinimalLogger.info(String.format("->%s (\"%s\":%s)", - n.getClass().getSimpleName(), - this.kettle.get(n).trim(), - n.sourcePos.toString())); - /////////////////////////////////////////////////////////////// - CFGNode curr = cfg.getNode(n); - this.dot_format += cfg.addEdge(this.curr, curr); - this.curr = curr; - - if (n.dest != null) - cfg.addReaching(curr, n.dest.toString()); - for (VOperand a : n.args) { - cfg.addLiveness(curr, a.toString()); - } - /////////////////////////////////////////////////////////////// - MinimalLogger.info(String.format("<-%s (\"%s\":%s)", - n.getClass().getSimpleName(), - this.kettle.get(n).trim(), - n.sourcePos.toString())); - return null; - } - - public String visit(ControlFlowGraph cfg, VReturn n) throws RuntimeException { - MinimalLogger.info(String.format("->%s (\"%s\":%s)", - n.getClass().getSimpleName(), - this.kettle.get(n).trim(), - n.sourcePos.toString())); - /////////////////////////////////////////////////////////////// - CFGNode curr = cfg.getNode(n); - this.dot_format += cfg.addEdge(this.curr, curr); - this.curr = curr; - - if (n.value != null) - cfg.addLiveness(curr, n.value.toString()); - /////////////////////////////////////////////////////////////// - MinimalLogger.info(String.format("<-%s (\"%s\":%s)", - n.getClass().getSimpleName(), - this.kettle.get(n).trim(), - n.sourcePos.toString())); - return null; - } - -} diff --git a/vaporize/library/RegisterAlloc.java b/vaporize/library/RegisterAlloc.java deleted file mode 100644 index b316b96..0000000 --- a/vaporize/library/RegisterAlloc.java +++ /dev/null @@ -1,71 +0,0 @@ -package vaporize.library; - -import java.util.*; -import misc.*; - -public class RegisterAlloc { - - private LIRDict intervals; - private String[] all_registers; - private Stack<String> free_registers; - private TreeSet<LIRVar> active; - - public RegisterAlloc(LIRDict intervals, String[] all_registers) { - this.intervals = intervals; - this.all_registers = all_registers; - this.free_registers = new Stack<String>(); - this.free_registers.addAll(Arrays.asList(this.all_registers)); - this.active = new TreeSet<LIRVar>((v1, v2) -> { - int ret; - if (v1.getLastUse() > v2.getLastUse()) - ret = 1; - else if (v1.getLastUse() < v2.getLastUse()) - ret = -1; - else if (v1.equals(v2)) - ret = 0; - else - ret = 1; - return ret; - }); - - MinimalLogger.info(String.format("Starting allocation with registers %s", - this.free_registers.toString())); - - String register; - for (LIRVar interval : this.intervals.getIntervals()) { - this.expireOldIntervals(interval); - if (this.active.size() >= this.all_registers.length) - this.spillAtInterval(interval); - else { - register = this.free_registers.pop(); - interval.assignRegister(register); - this.active.add(interval); - MinimalLogger.info(String.format("Assigning register %s to %s.", - register, - interval.toString())); - - } - } - } - - private void expireOldIntervals(LIRVar interval) { - for (LIRVar active : new TreeSet<LIRVar>(this.active)) { - if (active.getLastUse() >= interval.getFirstDef()) - return; - MinimalLogger.info("Register " + active.getAssignedRegister() + " expired!"); - this.active.remove(active); - this.free_registers.push(active.getAssignedRegister()); - } - } - - 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/SpillEverywhere.java b/vaporize/library/SpillEverywhere.java deleted file mode 100644 index 993bfa6..0000000 --- a/vaporize/library/SpillEverywhere.java +++ /dev/null @@ -1,92 +0,0 @@ -package vaporize.library; - -import cs132.vapor.ast.*; -import st.*; -import misc.*; -import java.util.*; - -public class SpillEverywhere extends VInstr.VisitorPR<String, String, RuntimeException> { - - private VaporProgram vp; - private Kettle kettle; - - public SpillEverywhere(VaporProgram vp, ArrayList<String> vapor) { - this.vp = vp; - this.kettle = new Kettle(vapor); - - for (VFunction f : this.vp.functions) { - MinimalLogger.severe("Num : " + Arrays.toString(f.vars)); - this.kettle.replaceFunctionDeclare(f, 0, 0, 3); // use three registers for "spill everywhere" - for (VInstr s : f.body) { - s.accept("", this); - } - } - MinimalLogger.info(kettle.dump()); - } - - - public String visit(String p, VMemRead n) throws RuntimeException { - MinimalLogger.info(String.format("->%s (%s:%s)", - n.getClass().getSimpleName(), - this.kettle.get(n).trim(), - n.sourcePos.toString())); - return null; - } - - public String visit(String p, VMemWrite n) throws RuntimeException { - MinimalLogger.info(String.format("->%s (%s:%s)", - n.getClass().getSimpleName(), - this.kettle.get(n).trim(), - n.sourcePos.toString())); - return null; - } - - public String visit(String p, VAssign n) throws RuntimeException { - MinimalLogger.info(String.format("->%s (%s:%s)", - n.getClass().getSimpleName(), - this.kettle.get(n).trim(), - n.sourcePos.toString())); - return null; - } - - public String visit(String p, VBranch n) throws RuntimeException { - MinimalLogger.info(String.format("->%s (%s:%s)", - n.getClass().getSimpleName(), - this.kettle.get(n).trim(), - n.sourcePos.toString())); - return null; - } - - public String visit(String p, VGoto n) throws RuntimeException { - MinimalLogger.info(String.format("->%s (%s:%s)", - n.getClass().getSimpleName(), - this.kettle.get(n).trim(), - n.sourcePos.toString())); - return null; - } - - public String visit(String p, VCall n) throws RuntimeException { - MinimalLogger.info(String.format("->%s (%s:%s)", - n.getClass().getSimpleName(), - this.kettle.get(n).trim(), - n.sourcePos.toString())); - return null; - } - - public String visit(String p, VBuiltIn n) throws RuntimeException { - MinimalLogger.info(String.format("->%s (%s:%s)", - n.op.name, - this.kettle.get(n).trim(), - n.sourcePos.toString())); - return null; - } - - public String visit(String p, VReturn n) throws RuntimeException { - MinimalLogger.info(String.format("->%s (%s:%s)", - n.getClass().getSimpleName(), - this.kettle.get(n).trim(), - n.sourcePos.toString())); - return null; - } - -} diff --git a/vaporize/library/TransientInterval.java b/vaporize/library/TransientInterval.java deleted file mode 100644 index faf7637..0000000 --- a/vaporize/library/TransientInterval.java +++ /dev/null @@ -1,21 +0,0 @@ -package vaporize.library; - -import misc.*; - -class TransientInterval { - - protected int first_def; - protected int last_use; - - protected TransientInterval(int first_def, int last_use) { - this.first_def = first_def; - this.last_use = last_use; - } - - @Override public boolean equals(Object other) { - TransientInterval o; - return (other instanceof TransientInterval) && - (((o = (TransientInterval) other)).first_def == this.first_def); - } - -} diff --git a/vaporize/library/VaporizeVisitor.java b/vaporize/library/VaporizeVisitor.java deleted file mode 100644 index 0dfca02..0000000 --- a/vaporize/library/VaporizeVisitor.java +++ /dev/null @@ -1,269 +0,0 @@ -package vaporize.library; - -import cs132.vapor.ast.*; -import misc.*; - -import java.util.*; - -public class VaporizeVisitor extends VInstr.VisitorP<LIRDict, RuntimeException> { - - private ArrayList<String> vaporm; - - 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[] arg_pass = new String[] { "$a0", "$a1", "$a2", "$a3" }; - String[] ret_pass = new String[] { "$v0" }; - - 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() + 17)); - for (int j = 0; j < this.callee_save.length; ++j) { - this.addVaporm(String.format(" local[%s] = %s", - j, - this.callee_save[j])); - } - - for (int j = 0; j < vp.functions[i].params.length; ++j) - this.addVaporm(String.format(" %s = %s", - interval_list.get(i).getInterval(vp.functions[i].params[j].toString()) - .getAssignedRegister(), - arg_pass[j])); - - - 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(interval_list.get(i), this); - else - this.addVaporm(((VCodeLabel) n).ident + ":"); - } - } - } - - 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 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())); - /////////////////////////////////////////////////////////////// - - for (int i = 0; i < this.caller_save.length; ++i) { - this.addVaporm(String.format(" local[%s] = %s", - i+8, - this.caller_save[i])); - } - - // 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() - : n.args[i].toString(); - this.addVaporm(String.format(" %s = %s", - this.arg_pass[i], - reg)); - } - - this.addVaporm(String.format(" call %s", - d.getInterval(n.addr.toString()) - .getAssignedRegister())); - // get dest - if (n.dest != null) { - this.addVaporm(String.format(" %s = $v0", - d.getInterval(((VVarRef.Local) n.dest).ident) - .getAssignedRegister())); - } - - for (int i = 0; i < this.caller_save.length; ++i) { - this.addVaporm(String.format(" %s = local[%s]", - this.caller_save[i], - i+8)); - } - /////////////////////////////////////////////////////////////// - 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())); - /////////////////////////////////////////////////////////////// - String ret = ""; - if (n.dest != null) { - ret += String.format("%s = ", - d.getInterval(((VVarRef.Local) n.dest).ident) - .getAssignedRegister()); - } - ret += String.format("%s(", - ((VBuiltIn.Op) n.op).name); - - String par; - for (VOperand a : n.args) { - par = (a instanceof VVarRef.Local) ? - d.getInterval(a.toString()).getAssignedRegister() - : a.toString(); - ret += String.format("%s ", - par); - } - - ret += ")"; - - this.addVaporm(String.format(" %s", - ret)); - /////////////////////////////////////////////////////////////// - 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())); - /////////////////////////////////////////////////////////////// - String ret = " "; - if (n.positive) - ret += "if "; - else - ret += "if0 "; - - ret += d.getInterval(n.value.toString()) - .getAssignedRegister() + " goto :"; - - ret += n.target.ident; - - this.addVaporm(String.format("%s", - ret)); - /////////////////////////////////////////////////////////////// - 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())); - /////////////////////////////////////////////////////////////// - this.addVaporm(String.format(" goto :%s", - (((VAddr.Label) n.target).label).ident)); - /////////////////////////////////////////////////////////////// - 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())); - /////////////////////////////////////////////////////////////// - // get ret - if (n.value != null) { - String reg = (n.value instanceof VVarRef.Local) ? - d.getInterval(n.value.toString()).getAssignedRegister() : - n.value.toString(); - this.addVaporm(String.format(" $v0 = %s", - d.getInterval(((VVarRef.Local) n.value).ident) - .getAssignedRegister())); - } - for (int j = 0; j < this.callee_save.length; ++j) { - this.addVaporm(String.format(" %s = local[%s]", - this.callee_save[j], - j)); - } - this.addVaporm(" ret"); - /////////////////////////////////////////////////////////////// - MinimalLogger.info(String.format("<-%s (%s)", - n.getClass().getSimpleName(), - n.sourcePos.toString())); - } - -} |