-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTMGenerator.java
More file actions
340 lines (270 loc) · 9.74 KB
/
Copy pathTMGenerator.java
File metadata and controls
340 lines (270 loc) · 9.74 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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import absyn.*;
import java.lang.String;
import java.util.*;
public class TMGenerator implements AbsynVisitor
{
static int sFlag = 0;
private int depth;
private TMDecEntry currFunction = null;
private TMUtils asm = null;
private TMSymbolTable table = null;
private String fileName = "";
public TMGenerator(String fileName) {
depth = 0;
asm = new TMUtils(fileName);
table = new TMSymbolTable(sFlag);
this.fileName = fileName;
}
//Constant definitions
final static int SPACES = 4;
private void indent(int level) {
if (sFlag == 1) {
for (int i = 0; i < level * SPACES; i++)
System.out.print(" ");
}
}
public void visit(DecList decList, int level) {
asm.prelude(fileName);
while (decList != null && decList.head != null) {
decList.head.accept(this, level);
decList = decList.tail;
}
asm.end();
}
public void visit(VarDecList varDecList, int level) {
while (varDecList != null && varDecList.head != null) {
varDecList.head.accept(this, level);
varDecList = varDecList.tail;
}
}
public void visit(ExpList expList, int level) {
while (expList != null && expList.head != null) {
expList.head.accept(this, level);
//genCode(expList.head);
expList = expList.tail;
}
}
public void visit(AssignExp exp, int level) {
level++;
asm.emitComment("-> op");
exp.lhs.isAddr = true;
exp.lhs.isAssign = true;
exp.lhs.accept(this, level);
int tmpVar = asm.newTemp();
exp.rhs.isAddr = false;
exp.rhs.accept(this, level);
//get result expression into temp
asm.processResultAssignExp(tmpVar);
asm.releaseTempVar();
asm.emitComment("<- op");
}
public void visit(IfExp exp, int level) {
level++;
asm.emitComment("-> if");
asm.emitComment("-> test");
//This will build the asm expression but we'll still have to check the result
exp.test.accept(this, level);
int testLoc = 0;
if (exp.test instanceof OpExp)
testLoc = asm.processIfJump((OpExp)exp.test);
else
asm.emitComment("Error with if test case, not OpExp");
asm.emitComment("<- test");
exp.thenpart.accept(this, level);
int finishThenLoc = asm.emitSkip("Jump to end of if block");
//If the statement resulted in false we need to jump to the else block
asm.emitJumpToCurrentINS("JEQ", 0, testLoc, "Jump over then block");
if (exp.elsepart != null)
exp.elsepart.accept(this, level);
asm.emitJumpToCurrentINS(finishThenLoc, "Leave then block");
asm.emitComment("<- if");
//indent(depth);
//System.out.println("Leaving if block");
}
public void visit(IntExp exp, int level) {
exp.type = NameTy.INT;
asm.processConstant(exp);
}
public void visit(OpExp exp, int level) {
asm.emitComment("-> op");
exp.left.isAddr = false;
exp.left.accept(this, level);
int tmpVar = asm.newTemp();
exp.right.isAddr = false;
exp.right.accept(this, level);
//get result expression into temp
asm.processResultTmpOpExp(exp, tmpVar);
asm.releaseTempVar();
asm.emitComment("<- op");
}
public void visit(RepeatExp exp, int level) {
exp.exps.accept(this, level);
exp.test.accept(this, level);
}
public void visit(VarExp exp, int level) {
exp.value.accept(this, level);
exp.type = table.getVarType(exp.value.name);
//TODO
if (exp.value instanceof IndexVar) {
IndexVar var = (IndexVar)exp.value;
TMDecEntry dec = table.getVar(var.name);
boolean loadAddr = true;
if (dec.dec instanceof ArrayDec)
if (((ArrayDec)dec.dec).isParam)
loadAddr = false;
asm.loadVar(exp.value, dec, loadAddr);
//Store addr in tmp
int tmp = asm.newTemp();
var.index.accept(this, ++level);
asm.verifyArrayAccess(0);
asm.loadArray(var, dec, exp.isAssign, tmp);
//need to do work
} else if (exp.value instanceof SimpleVar) {
boolean isAddr = exp.isAddr;
TMDecEntry dec = table.getVar(exp.value.name);
if (dec.dec instanceof ArrayDec && !((ArrayDec)dec.dec).isParam)
isAddr = true;
asm.loadVar(exp.value, dec, isAddr);
}
}
public void visit(WriteExp exp, int level) {
exp.output.accept(this, ++level);
}
public void visit(NameTy t, int level) {
// Get the string of the represented int
String typeString = "";
if (t.type == 0) {
typeString = "int";
} else {
typeString = "void";
}
}
public void visit(ArrayDec arr, int level) {
arr.type.accept(this, ++level);
//Add var to table
table.addEntryToTable(arr, arr.name, arr.type.type, depth);
if (arr.size == null) {
arr.isParam = true;
asm.processArrayDec(arr, 1, depth);
} else {
arr.isParam = false;
asm.processArrayDec(arr, arr.size.value, depth);
}
//if (arr.size != null)
//arr.size.accept(this, ++level);
}
public void visit(SimpleDec dec, int level) {
//Add var to table
table.addEntryToTable(dec, dec.name, dec.type.type, depth);
asm.processSimpleDec(dec, depth);
dec.type.accept(this, ++level);
}
public void visit(FunctionDec dec, int level) {
dec.type.accept(this, ++level);
//Add function to table
if (sFlag == 1)
System.out.println("");
currFunction = table.addEntryToTable(dec, dec.func, dec.type.type, depth);
currFunction.params = new ArrayList<>();
int loc = asm.buildFunction(dec, dec.func);
dec.address = loc+1;
//Add parameters to the new block depth
indent(++depth);
if (sFlag == 1)
System.out.println("Params: ");
dec.params.accept(this, ++level);
indent(depth);
if (sFlag == 1)
System.out.println("");
//Store parameter information related to function dec
VarDecList list = dec.params;
while (list != null && list.head != null) {
currFunction.params.add(list.head);
list = list.tail;
}
//leave param depth
depth--;
dec.body.accept(this, ++level);
currFunction = null;
asm.finishFunction(loc, dec.func);
}
public void visit(CompoundExp exp, int level) {
//Enter a compound block
if (depth>0 && sFlag == 1) {
indent(depth);
System.out.println("Entering a new block: ");
}
depth++;
if (exp.decs != null)
exp.decs.accept(this, ++level);
if (exp.exps != null)
exp.exps.accept(this, ++level);
//leave compound block, clear variables defined in scope
table.clearSymTable(depth);
depth--;
if (depth > 0 && sFlag == 1) {
indent(depth);
System.out.println("Leaving the block");
}
}
public void visit(ReturnExp e, int level) {
asm.emitComment("-> return");
e.exp.accept(this, level);
asm.returnToCaller();
asm.emitComment("<- return");
}
public void visit(WhileExp exp, int level) {
level++;
asm.emitComment("-> While");
//Record the starting position of the while loop, we'll need to jump back here each iteration
int topOfWhile = asm.getCurrINS();
//Build the looping expression
exp.test.accept(this, level);
//Check the result of the expression
int testLoc = 0;
if (exp.test instanceof OpExp)
testLoc = asm.processIfJump((OpExp)exp.test);
else
asm.emitComment("Error with if test case, not OpExp");
exp.body.accept(this, level);
//At the end of the loop body jump back
asm.emitJump(asm.getCurrINS(), topOfWhile, "Jump back to test condition");
//Backpatch a jump over the loop body if the expression resulted in false
asm.emitJumpToCurrentINS("JEQ", 0, testLoc, "Jump over body");
asm.emitComment("<- While");;
}
public void visit(CallExp exp, int level) {
//Assume no errors
TMDecEntry asmDec = table.getVar(exp.func);
if (!(asmDec.dec instanceof FunctionDec)) {
System.out.println("Error needed function, got something else");
return;
}
int frameStart = asm.startCallExp();
//exp.args.accept(this, ++level);
ExpList args = exp.args;
while (args != null && args.head != null) {
//args.head.isAddr = false;
//Passing array value in to function
/*if (args.head instanceof VarExp)
if (((VarExp)args.head).value instanceof IndexVar)
args.head.isAddr = true;*/
args.head.accept(this, level);
//Push arg on stack
asm.pushArgOnStack(1);
args = args.tail;
}
exp.type = table.getVarType(exp.func);
asm.processCallExp(exp, (FunctionDec)asmDec.dec, depth, frameStart);
}
public void visit(NilExp exp, int level) {
}
public void visit(ErrorExp exp, int level) {
}
public void visit(IndexVar var, int level) {
//lda flag = false;
//var.index.accept(this, ++level);
}
public void visit(SimpleVar var, int level) {
}
}