summaryrefslogtreecommitdiff
path: root/condense/StackHelper.java
blob: a0a6e2d6bcf9877532861fc6703378a3447c27f8 (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
package condense;

import misc.*;
import cs132.vapor.ast.VFunction;
import cs132.vapor.ast.VMemRef;

import java.util.*;

public class StackHelper {

    private int in;
    private int out;
    private int local;
    private int frame_size;
    private HashMap<String, String> idx_to_addr;

    public StackHelper(VFunction.Stack s) {
        this.in = s.in;
        this.out = s.out;
        this.local = s.local;
        this.frame_size = this.stackFrameFormula();
        this.idx_to_addr = new HashMap<String,String>();
        MinimalLogger.info(String.format("Found in:%d out:%d local%d framesize:%d...",
                                         this.in,
                                         this.out,
                                         this.local,
                                         this.frame_size));

        String key;
        String value;
        int loc;
        for (int i = 0; i < this.local; ++i) { // locals
            loc = (( -i - 1 ) * 4) - 8;
            key = String.format("Local[%d]", i);
            value = String.format("%d($fp)", loc);
            this.idx_to_addr.put(key, value);
        }
        for (int i = 0; i < this.out; ++i) { // out
            loc = ((this.out + this.local + 2) - i) * -4;
            key = String.format("Out[%d]", i);
            value = String.format("%d($fp)", loc);
            this.idx_to_addr.put(key, value);
        }
        for (int i = 0; i < this.in; ++i) { // in
            loc = i * 4;
            key = String.format("In[%d]", i);
            value = String.format("%d($fp)", loc);
            this.idx_to_addr.put(key, value);
        }

        MinimalLogger.info(String.format("Mapping: %s", this.idx_to_addr.toString()));
    }

    public String get(VMemRef.Stack m) {
        String key = String.format("%s[%d]",
                                   m.region,
                                   m.index);
        MinimalLogger.severe(key);
        return this.idx_to_addr.get(key);
    }

    public int getFrameSize() {
        return this.frame_size;
    }

    private int stackFrameFormula() {
        return (this.out + this.local + 2) * 4;
    }

}