diff options
Diffstat (limited to 'vaporize/library')
| -rw-r--r-- | vaporize/library/LIRDict.java | 8 | ||||
| -rw-r--r-- | vaporize/library/RegisterAlloc.java | 1 | ||||
| -rw-r--r-- | vaporize/library/VaporizeVisitor.java | 145 | 
3 files changed, 154 insertions, 0 deletions
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())); +    } + +}  | 
