-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTables.java
289 lines (239 loc) · 8.54 KB
/
Tables.java
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import java.util.Map;
import java.util.HashMap;
import java.util.LinkedHashMap;
class SymbolTableEntry {
public Type type;
public Integer offset; // stack pointer offset
public String register;
public boolean isRegisterAllocated() {
return register != null;
}
public boolean isStackAllocated() {
return offset != null;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(type.toString());
if (isStackAllocated()) sb.append(" | stack offset = " + offset);
if (isRegisterAllocated()) sb.append(" | register = %" + register);
return sb.toString();
}
}
class SymbolTable {
public LinkedHashMap<String, SymbolTableEntry> locals = new LinkedHashMap<>();
public LinkedHashMap<String, SymbolTableEntry> params = new LinkedHashMap<>();
public Integer size = 0; // depends on whether there are other things on the stack before local variables
public SymbolTableEntry getEntry(String name) {
return locals.containsKey(name) ? locals.get(name) : params.get(name); // local vars shadow params
}
public void setLocal(String name, Type type) {
SymbolTableEntry entry = new SymbolTableEntry();
entry.type = type;
locals.put(name, entry);
}
public void removeLocal(String name) {
locals.remove(name);
}
public void setParam(String name, Type type) {
SymbolTableEntry entry = new SymbolTableEntry();
entry.type = type;
params.put(name, entry);
}
public void generateOffsetsAndRegisters() {
this.generateParameterOffsetsAndRegisters();
this.generateLocalVariableOffsetsAndRegisters();
}
private void generateParameterOffsetsAndRegisters() {
int count = 0;
for (Map.Entry<String, SymbolTableEntry> p : this.params.entrySet()) {
SymbolTableEntry entry = p.getValue();
if (count++ < 4) {
entry.register = "a" + count;
} else {
break; // TODO: SUPPORT MORE THAN 4 PARAMETERS!
// entry.offset = size;
// size += entry.type.width();
}
}
}
private void generateLocalVariableOffsetsAndRegisters() {
for (Map.Entry<String, SymbolTableEntry> p : this.locals.entrySet()) {
SymbolTableEntry entry = p.getValue();
entry.offset = size;
size += entry.type.width();
}
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(" Parameters\n");
for (Map.Entry<String, SymbolTableEntry> p : this.params.entrySet()) {
String name = p.getKey();
SymbolTableEntry entry = p.getValue();
sb.append(" ");
sb.append(name + " : " + entry.toString());
sb.append("\n");
}
sb.append(" Local Variables\n");
for (Map.Entry<String, SymbolTableEntry> p : this.locals.entrySet()) {
String name = p.getKey();
SymbolTableEntry entry = p.getValue();
sb.append(" ");
sb.append(name + " : " + entry.toString());
sb.append("\n");
}
return sb.toString();
}
}
class SymbolTables {
public static HashMap<String, SymbolTable> tables = new HashMap<>();
public static SymbolTable currentTable;
public static void create() {
currentTable = new SymbolTable();
}
public static void flush(String functionName) {
currentTable.generateOffsetsAndRegisters();
tables.put(functionName, currentTable);
currentTable = null;
}
public static SymbolTable get(String functionName) {
return tables.get(functionName);
}
public static void print() {
StringBuilder sb = new StringBuilder();
sb.append("===== Symbol Tables BEGIN =====\n");
for (HashMap.Entry<String, SymbolTable> p : tables.entrySet()) {
sb.append("size = " + p.getValue().size + "\n");
sb.append("function " + p.getKey() + "\n");
sb.append(p.getValue().toString());
sb.append("\n");
}
sb.append("===== Symbol Tables END =====\n");
System.out.println(sb.toString());
}
}
class ClassTableEntry {
Type type;
Integer offset; // offset from object start address in heap
public ClassTableEntry(Type type, Integer offset) {
this.type = type;
this.offset = offset;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(type.toString());
sb.append(" | heap offset = " + offset);
return sb.toString();
}
}
class ClassTable {
public String name;
public HashMap<String, ClassTableEntry> fields = new HashMap<>();
public Integer size = 0;
public ClassTable(ClassDescriptor cdesc) {
this.name = cdesc.name;
for (Map.Entry<String, String> field : cdesc.fields.entrySet()) {
Type type = Type.fromTypeString(field.getValue());
Integer offset = size;
size += type.width();
ClassTableEntry entry = new ClassTableEntry(type, offset);
this.fields.put(field.getKey(), entry);
}
}
public boolean hasField(String name) {
return fields.containsKey(name);
}
public Type getFieldType(String name) {
return fields.get(name).type;
}
public Integer getFieldOffset(String name) {
return fields.get(name).offset;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, ClassTableEntry> entry : fields.entrySet()) {
sb.append(" " + entry.getKey() + " : ");
sb.append(entry.getValue().toString() + "\n");
}
return sb.toString();
}
}
class ClassTables {
public static HashMap<String, ClassTable> tables = new HashMap<>();
public static String nameOfMainClass = "Main";
public static void generateFromClassDescriptors(ClassDescriptors cdescs) {
for (Map.Entry<String, ClassDescriptor> entry : cdescs.classes.entrySet()) {
if (entry.getValue().hasMethod("main")) nameOfMainClass = entry.getKey();
tables.put(entry.getKey(), new ClassTable(entry.getValue()));
}
}
public static ClassTable get(String className) {
return tables.get(className);
}
public static ClassTable get(Type classNameType) {
return tables.get(classNameType.toString());
}
public static void print() {
StringBuilder sb = new StringBuilder();
sb.append("===== Class Table BEGIN =====\n");
for (Map.Entry<String, ClassTable> p : tables.entrySet()) {
sb.append(p.getKey() + " size = " + p.getValue().size + " {\n");
sb.append(p.getValue().toString());
sb.append("}\n");
}
sb.append("===== Class Table END =====\n");
System.out.println(sb.toString());
}
}
class DataTableEntry {
String directive;
String item;
public DataTableEntry(String directive, String item) {
this.directive = directive;
this.item = item;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (char c : item.toCharArray()) {
if (c == '\n') {
sb.append("\\n");
} else {
sb.append(c);
}
}
return directive + " \"" + sb.toString() + "\\n\"";
}
}
class DataTable {
public static HashMap<Integer, DataTableEntry> data = new HashMap<>();
public static void create(Integer label, DataTableEntry entry) {
data.put(label, entry);
}
public static boolean has(Integer label) {
return data.containsKey(label);
}
public static DataTableEntry get(Integer label) {
return data.get(label);
}
public static void print() {
System.out.println(getTableString());
}
public static String getTableString() {
StringBuilder sb = new StringBuilder();
for (Map.Entry<Integer, DataTableEntry> p : data.entrySet()) {
sb.append("L" + p.getKey() + ":\n ");
sb.append(p.getValue().toString());
sb.append("\n");
}
return sb.toString();
}
public static Integer PRINT_INT_FORMAT_STR_LABEL;
public static void init() {
PRINT_INT_FORMAT_STR_LABEL = IR3.mkLabel();
DataTable.create(PRINT_INT_FORMAT_STR_LABEL, new DataTableEntry(".asciz", "%i"));
}
}