summaryrefslogtreecommitdiff
path: root/vaporize/LIRDict.java
diff options
context:
space:
mode:
Diffstat (limited to 'vaporize/LIRDict.java')
-rw-r--r--vaporize/LIRDict.java86
1 files changed, 86 insertions, 0 deletions
diff --git a/vaporize/LIRDict.java b/vaporize/LIRDict.java
new file mode 100644
index 0000000..4f3d3c1
--- /dev/null
+++ b/vaporize/LIRDict.java
@@ -0,0 +1,86 @@
+package vaporize;
+
+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;
+ }
+
+}