-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrame.java
More file actions
171 lines (144 loc) · 3.62 KB
/
Frame.java
File metadata and controls
171 lines (144 loc) · 3.62 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Map;
import absyn.*;
public class Frame
{
static class FrameScope
{
public HashMap<String, Integer> localList = new HashMap<>();
public int locals = 0;
public int cumulativeOffset = 0;
FrameScope(int offSetSoFar)
{
cumulativeOffset = offSetSoFar;
}
public void addLocal(Exp e)
{
locals++;
cumulativeOffset++;
localList.put(getName(e), cumulativeOffset);
}
public Integer getOffset(Exp e)
{
return localList.get(getName(e));
}
}
public FunDec function; //the function this Frame pertains to
public int codeStart; //always 1 less than the actual memory address (pc++)
public Integer params = 0;
public Integer locals = 0;
public HashMap<String, Integer> paramMap = new HashMap<>();
private ArrayList<FrameScope> localList = new ArrayList<>();
private int totalScope = 0;
private int stack = 0;
Frame (FunDec func, int codeStart)
{
function = func;
this.codeStart = codeStart;
localList.add(new FrameScope(totalScope));
}
public void addParam(Exp e)
{
//adds variable to the parameter block
paramMap.put(getName(e), ++params);
}
public void addLocal(Exp e)
{
//adds a local to the current scope and increases total stack to manage offsets
localList.get(localList.size() - 1).addLocal(e);
totalScope++;
}
public void addScope()
{
//adds new scope
localList.add(new FrameScope(totalScope));
}
public void popScope()
{
int lastScope = localList.size() - 1;
FrameScope f = localList.get(lastScope);
localList.remove(lastScope);
totalScope = localList.get(lastScope - 1).cumulativeOffset;
stack = 0;
}
public void resetStack()
{
stack = 0;
}
public void incrementStack()
{
stack++;
}
public void decrementStack()
{
stack--;
}
public void incrementStack(int i)
{
stack += i;
}
public void decrementStack(int i)
{
stack -= i;;
}
public int getStackOffset()
{
//NOTE: always returns the last spot with something in it
return totalScope + stack;
}
public int getFrameOffset(Frame f)
{
System.out.println(f.function.name);
System.out.println("totalScope: " + totalScope +" stack: " + stack + " f.params: " + f.params);
System.out.println("frameOffSet: " + (totalScope + stack + 1));
return totalScope + stack + 1;
}
public int getVarOffset(Exp e)
{
ListIterator<FrameScope> li = localList.listIterator(localList.size());
Integer offset = null;
//Search of variable e in the local scopes
while(li.hasPrevious())
{
offset = li.previous().getOffset(e);
if (offset != null)
return offset;
}
//if it is not there it must be in params
return paramMap.get(getName(e)) - paramMap.size() - 1;
}
public void printFrame()
{
System.out.println("Code Start :" + codeStart);
System.out.println("Params Count :" + params);
System.out.println("Scopes :" + localList.size());
System.out.println("Params:");
paramMap.forEach((key, value) -> {
System.out.println(key + " : " + (value - paramMap.size() - 1));
});
int i = 0;
ListIterator<FrameScope> li = localList.listIterator();
while(li.hasNext())
{
System.out.println("Scope "+(i++)+":");
li.next().localList.forEach((key, value) -> {System.out.println(key + " : " + value);});
}
}
private static String getName(Exp e)
{
if( e instanceof VarExp )
return ((VarExp)e).name;
else if( e instanceof ArrExp )
return ((ArrExp)e).name;
if( e instanceof VarDec )
return ((VarDec)e).name;
else if( e instanceof ArrDec )
return ((ArrDec)e).name;
else
System.out.println("fuck "+ e);
return "Fuck";
}
}