package vaporize; import cs132.vapor.ast.VCodeLabel; import cs132.vapor.ast.VFunction; import cs132.vapor.ast.VInstr; import misc.*; import cfg.*; import java.util.*; public class LIRDict { private TreeSet intervals; private int spilled_num; // the number of spilled registers private int in_num; // the number of arguments passed in through the stack private int out_num; // the size of the out stack private ControlFlowGraph cfg; public LIRDict(VFunction f, ControlFlowGraph cfg, int out_num) { this.intervals = new TreeSet((v1, v2) -> { return (v1.compareTo(v2) != 0) ? v1.compareTo(v2) : v1.equals(v2) ? 0 : 1; }); this.cfg = cfg; this.out_num = out_num; for (VInstr s : f.body) { CFGNode n = cfg.getNode(s); int line = n.getInstruction().sourcePos.line; // reaching String info = "L" + line; for (String var : n.getDefinitions()) if (!this.contains(var)) { this.intervals.add(new LIRVar(var, line, line)); MinimalLogger.info(String.format("Found def of %s at line %s", var, line)); } for (String var : n.getReaching()) if (n.getLiveness().contains(var)) { if (this.contains(var)) { this.getInterval(var).trySetLastUse(line); MinimalLogger.info(String.format("Var %s still live on %s", var, line)); } else { this.intervals.add(new LIRVar(var, line, line)); MinimalLogger.info(String.format("Var %s still live on %s", var, 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 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; } public int getOutNum() { return this.out_num; } }