-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIRVisitor.java
More file actions
430 lines (375 loc) · 13.5 KB
/
IRVisitor.java
File metadata and controls
430 lines (375 loc) · 13.5 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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
import java.util.List;
import java.util.ArrayList;
public class IRVisitor implements Visitor {
private Environment<String, TempVar> variableEnv = new Environment<>();
private Environment<String, Function> functionEnv = new Environment<>();
TempAllocator allocator;
List<IRInstruction> instList;
IRProgram program;
Integer labelCount = 0;
String programName = "";
Integer cmp_labelCount = 0;
@Override
public TempVar visit(Program p) throws SemanticException {
program = new IRProgram(p, programName);
int funcCount = p.functions.size();
for (int i = 0; i < funcCount; i++) {
Function f = p.getFunction(i);
functionEnv.add(f.decl.id.name, f);
}
for (int i = 0; i < funcCount; i++)
{
Function f = p.getFunction(i);
f.accept(this);
}
return null;
}
@Override
public TempVar visit(Function f) throws SemanticException {
allocator = new TempAllocator();
instList = new ArrayList<IRInstruction>();
labelCount = 0;
variableEnv.beginScope();
f.decl.accept(this);
f.body.accept(this);
variableEnv.endScope();
IRFunction func = new IRFunction(f, instList, allocator);
program.addFunction(func);
return null;
}
@Override
public TempVar visit(FunctionDeclaration d) throws SemanticException {
if (d.params != null) {
d.params.accept(this);
}
return null;
}
@Override
public TempVar visit(FormalParameterList l) throws SemanticException {
int listSize = l.getFormalParameterCount();
for (int i=0; i < listSize; i++)
{
FormalParameter p = l.getFormalParameter(i);
p.accept(this);
}
return null;
}
@Override
public TempVar visit(FormalParameter p) throws SemanticException {
TempVar t = allocator.allocate(p.type, p.id.name, null);
variableEnv.add(p.id.name, t);
return t;
}
@Override
public TempVar visit(FunctionBody f) throws SemanticException {
for (int i=0; i< f.getVariableDeclarationCount() ; i++) {
VariableDeclaration v = f.getVariableDeclaration(i);
v.accept(this);
}
for (int i=0; i< f.getStatementCount() ; i++)
{
Statement s = f.getStatement(i);
s.accept(this);
}
return null;
}
@Override
public TempVar visit(VariableDeclaration v) throws SemanticException {
TempVar t = allocator.allocate(v.type, v.id.name, null);
variableEnv.add(v.id.name, t);
if (t.type instanceof ArrayType) {
IRArrayCreation ir = new IRArrayCreation(t);
instList.add(ir);
}
return null;
}
@Override
public TempVar visit(Block b) throws SemanticException {
for (int i=0; i< b.getStatementCount(); i++)
{
Statement s = b.getStatement(i);
s.accept(this);
}
if (b.jumpLbNumb != -1) {
instList.add(new IRJump(b.jumpLbNumb));
}
return null;
}
@Override
public TempVar visit(WhileStatement w) throws SemanticException {
// instList.add(new IRDirective("while start"));
int whileBlockLbNumb = labelCount++;
IRLabel lb = new IRLabel(whileBlockLbNumb);
instList.add(lb);
TempVar tmp = (TempVar)w.expr.accept(this);
TempVar condTemp = new TempVar(tmp.type, tmp.name, tmp.number);
if (variableEnv.lookupInScope(tmp.name) != null) {
condTemp = allocator.allocate(new BooleanType(), "LOCAL", null);
}
IRNegation neg = new IRNegation(condTemp, tmp);
instList.add(neg);
Integer endScopeLb = labelCount++;
IRIfInstruction ifIr = new IRIfInstruction(condTemp, endScopeLb);
instList.add(ifIr);
w.block.accept(this);
IRJump jump = new IRJump(whileBlockLbNumb);
instList.add(jump);
IRLabel endLb = new IRLabel(endScopeLb);
instList.add(endLb);
// instList.add(new IRDirective("while end0"));
return tmp;
}
@Override
public TempVar visit(IfStatement i) throws SemanticException {
// instList.add(new IRDirective("if start"));
TempVar tmp = (TempVar)i.e.accept(this);
TempVar condTemp = new TempVar(tmp.type, tmp.name, tmp.number);
if (variableEnv.lookupInScope(tmp.name) != null) {
condTemp = allocator.allocate(new BooleanType(), "LOCAL", null);
}
IRNegation neg = new IRNegation(condTemp, tmp);
instList.add(neg);
if (i.elseBlock != null) {
// instList.add(new IRDirective("else start"));
int elseBlkLbNumber = labelCount++;
int outScopeLbNumber = labelCount++;
IRIfInstruction ifIr = new IRIfInstruction(condTemp, elseBlkLbNumber);
instList.add(ifIr);
i.ifBlock.accept(this);
instList.add(new IRJump(outScopeLbNumber));
instList.add(new IRLabel(elseBlkLbNumber));
i.elseBlock.accept(this);
instList.add(new IRLabel(outScopeLbNumber));
// instList.add(new IRDirective("else end"));
} else {
int lbNumb = labelCount++;
IRIfInstruction ifIr = new IRIfInstruction(condTemp, lbNumb);
instList.add(ifIr);
i.ifBlock.accept(this);
instList.add(new IRLabel(lbNumb));
}
// instList.add(new IRDirective("if end"));
return tmp;
}
@Override
public TempVar visit(PrintStatement p) throws SemanticException {
TempVar t = (TempVar)p.e.accept(this);
IRPrintInstruction ir = new IRPrintInstruction(t, false);
instList.add(ir);
return null;
}
@Override
public TempVar visit(PrintLnStatement p) throws SemanticException {
TempVar t = (TempVar)p.e.accept(this);
IRPrintInstruction ir = new IRPrintInstruction(t, true);
instList.add(ir);
return null;
}
@Override
public TempVar visit(AssignmentStatement a) throws SemanticException {
TempVar var = (TempVar)a.id.accept(this);
TempVar exprType = (TempVar)a.e.accept(this);
IRAssignInstruction ir = create_assignment_IR(var, exprType);
instList.add(ir);
return null;
}
@Override
public TempVar visit(ArrayAssignment a) throws SemanticException {
TempVar lhs = (TempVar)a.id.accept(this);
TempVar index = (TempVar)a.e1.accept(this);
TempVar rhs = (TempVar)a.e2.accept(this);
IRArrayAssignment ir = new IRArrayAssignment(lhs, rhs, index);
instList.add(ir);
return null;
}
@Override
public TempVar visit(ExpressionStatement e) throws SemanticException {
return (TempVar)e.e.accept(this);
}
@Override
public TempVar visit(ReturnStatement r) throws SemanticException {
if (r.e != null) {
TempVar t = (TempVar)r.e.accept(this);
IRReturnInstruction ir = new IRReturnInstruction(t);
instList.add(ir);
} else {
instList.add(new IRReturnInstruction(null));
}
return null;
}
@Override
public TempVar visit(EmptyStatement s) throws SemanticException {
return null;
}
@Override
public TempVar visit(PlusExpression p) throws SemanticException {
TempVar lhs = (TempVar)p.e1.accept(this);
TempVar rhs = (TempVar)p.e2.accept(this);
TempVar t = allocator.allocate(lhs.type, "LOCAL", null);
IRBinaryOperation ir = new IRBinaryOperation(t, lhs, rhs, "add");
instList.add(ir);
return t;
}
@Override
public TempVar visit(SubtractExpression p) throws SemanticException {
TempVar lhs = (TempVar)p.e1.accept(this);
TempVar rhs = (TempVar)p.e2.accept(this);
TempVar t = allocator.allocate(lhs.type, "LOCAL", null);
IRBinaryOperation ir = new IRBinaryOperation(t, lhs, rhs, "sub");
instList.add(ir);
return t;
}
@Override
public TempVar visit(MultExpression m) throws SemanticException {
TempVar lhs = (TempVar)m.e1.accept(this);
TempVar rhs = (TempVar)m.e2.accept(this);
TempVar t = allocator.allocate(lhs.type, "LOCAL", null);
IRBinaryOperation ir = new IRBinaryOperation(t, lhs, rhs, "mul");
instList.add(ir);
return t;
}
@Override
public TempVar visit(EqualityExpression e) throws SemanticException {
TempVar lhs = (TempVar)e.e1.accept(this);
TempVar rhs = (TempVar)e.e2.accept(this);
TempVar t = allocator.allocate(new BooleanType(), "LOCAL", null);
IREqualityOperation ir = new IREqualityOperation(t, lhs, rhs, cmp_labelCount++);
instList.add(ir);
return t;
}
@Override
public TempVar visit(LessThanExpression l) throws SemanticException {
TempVar lhs = (TempVar)l.e1.accept(this);
TempVar rhs = (TempVar)l.e2.accept(this);
TempVar t = allocator.allocate(new BooleanType(), "LOCAL", null);
IRLessthanOperation ir = new IRLessthanOperation(t, lhs, rhs, cmp_labelCount++);
instList.add(ir);
return t;
}
@Override
public TempVar visit(ArrayReference a) throws SemanticException {
TempVar var = (TempVar)a.id.accept(this);
TempVar index = (TempVar)a.e.accept(this);
ArrayType aType = (ArrayType)var.type;
TempVar t = allocator.allocate(aType.type, "LOCAL", null);
IRArrayReference ir = new IRArrayReference(t, var, index);
instList.add(ir);
return t;
}
@Override
public TempVar visit(FunctionCall f) throws SemanticException {
TempVar[] params = null;
if (f.exprList != null) {
params = (TempVar[])f.exprList.accept(this);
}
Function func = functionEnv.lookup(f.id.id.name);
FunctionDeclaration decl = func.decl;
if (func.decl == null) {
return null;
}
TempVar t = null;
if (!(decl.type instanceof VoidType)) {
t = allocator.allocate(decl.type, "LOCAL", null);
}
IRFunctionCall ir = new IRFunctionCall(t, func, params, programName);
instList.add(ir);
return t;
}
@Override
public TempVar visit(IdentifierValue iv) throws SemanticException {
TempVar tmp = variableEnv.lookupInScope(iv.id.name);
return tmp;
}
@Override
public TempVar visit(IntegerLiteral il) throws SemanticException {
TempVar t = allocator.allocate(new IntegerType(), "LOCAL", null);
IRConstantAssign ir = create_assignment_IR(t, new Value(il.val));
instList.add(ir);
return t;
}
@Override
public TempVar visit(FloatLiteral fl) throws SemanticException {
TempVar t = allocator.allocate(new FloatType(), "LOCAL", null);
IRConstantAssign ir = create_assignment_IR(t, new Value(fl.val));
instList.add(ir);
return t;
}
@Override
public TempVar visit(StringLiteral sl) throws SemanticException {
TempVar t = allocator.allocate(new StringType(), "LOCAL", null);
IRConstantAssign ir = create_assignment_IR(t, new Value(sl.val));
instList.add(ir);
return t;
}
@Override
public TempVar visit(CharacterLiteral cl) throws SemanticException {
TempVar t = allocator.allocate(new CharType(), "LOCAL", null);
IRConstantAssign ir = create_assignment_IR(t, new Value(cl.val));
instList.add(ir);
return t;
}
@Override
public TempVar visit(BooleanLiteral bl) throws SemanticException {
TempVar t = allocator.allocate(new BooleanType(), "LOCAL", null);
IRConstantAssign ir = create_assignment_IR(t, new Value(bl.val));
instList.add(ir);
return t;
}
@Override
public TempVar visit(ParenExpression p) throws SemanticException {
return (TempVar)p.e.accept(this);
}
@Override
public TempVar[] visit(ExpressionList l) throws SemanticException {
int listSize = l.getExpressionCount();
if (listSize == 0) {
return null;
}
TempVar[] temps = new TempVar[listSize];
for (int i=0; i < listSize; i++)
{
Expression e = l.getExpression(i);
TempVar t = (TempVar)e.accept(this);
temps[i] = t;
}
return temps;
}
@Override
public TempVar visit(IntegerType type) throws SemanticException {
return null;
}
@Override
public TempVar visit(FloatType type) throws SemanticException {
return null;
}
@Override
public TempVar visit(StringType type) throws SemanticException {
return null;
}
@Override
public TempVar visit(CharType type) throws SemanticException {
return null;
}
@Override
public TempVar visit(VoidType type) throws SemanticException {
return null;
}
@Override
public TempVar visit(ArrayType type) throws SemanticException {
return null;
}
@Override
public TempVar visit(BooleanType type) throws SemanticException {
return null;
}
@Override
public TempVar visit(Identifier id) throws SemanticException {
return null;
}
private IRConstantAssign create_assignment_IR(TempVar t, Value value) {
return new IRConstantAssign(t, value);
}
private IRAssignInstruction create_assignment_IR(TempVar lhs, TempVar rhs) {
return new IRAssignInstruction(lhs, rhs);
}
}