summaryrefslogtreecommitdiff
path: root/vaporize/LIRDict.java
blob: 30bfed3a576a58ad2e065312267cb4cb5e3729fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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<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;

            // 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<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;
    }

}