package vaporize.library; import cs132.vapor.ast.*; import misc.*; import java.util.*; public class VaporizeVisitor extends VInstr.VisitorP { private ArrayList 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 vaporm, ArrayList 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 (VInstr s : vp.functions[i].body) s.accept(interval_list.get(i), this); } } public ArrayList 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])); } String ret = " "; // get dest if (n.dest != null) { ret += String.format("%s = ", d.getInterval(((VVarRef.Local) n.dest).ident).getAssignedRegister()); } ret += d.getInterval(n.addr.toString()).getAssignedRegister() + "("; for (VOperand a : n.args) { ret += (a instanceof VVarRef.Local) ? d.getInterval(a.toString()).getAssignedRegister() : a.toString(); ret += " "; } this.addVaporm(ret + ")"); //FIXME 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())); /////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////// 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())); /////////////////////////////////////////////////////////////// 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"); //FIXME /////////////////////////////////////////////////////////////// MinimalLogger.info(String.format("<-%s (%s)", n.getClass().getSimpleName(), n.sourcePos.toString())); } }