-
Notifications
You must be signed in to change notification settings - Fork 0
/
IRCodeGenerator.java
444 lines (382 loc) · 14.9 KB
/
IRCodeGenerator.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
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
import org.antlr.v4.runtime.tree.ParseTree;
import java.util.*;
public class IRCodeGenerator {
final Visitor visitor;
final ParseTree tree;
StringBuilder result; // The intermediate code is stored here.
IRCodeGenerator(ParseTree tree) {
this.tree = tree;
visitor = new Visitor();
result = new StringBuilder();
}
public String generate() {
visitor.visit(tree);
return result.toString();
}
private class Visitor extends CCALBaseVisitor<String> {
int labelIndex, condIndex, tempVarIndex, funcCount;
Stack<String> globalConstants, functionParams;
Stack<HashMap<String, String>> funcVariables;
ArrayList<String> args;
boolean funcCtx;
Visitor() {
labelIndex = condIndex = tempVarIndex = funcCount = 1;
globalConstants = new Stack<>();
functionParams = new Stack<>();
funcVariables = new Stack<>();
args = new ArrayList<>();
funcCtx = false;
}
// Make an id unique in a function.
String makeUnique(String id) {
String result = id;
for (int i = 0; i < funcCount; i++) { result += "_"; }
return result;
}
// Make a label unique.
String makeUniqueLabel(int i) {
String label = "L";
for (int und = labelIndex; und > 0; und--) { label += "_"; und--; }
return label + i;
}
// Convert an arithmetic expression to TAC.
String convertArithOpExpr(final String expr) {
String[] split = expr
.replaceAll("\\(|\\)", "")
.replaceAll("\\+", " \\+ ")
.replaceAll("-", " - ")
.split(" ");
// For some reasons this case must be handled.
if (split.length == 1) {
String a1 = split[0];
if (funcCtx && funcVariables.peek().containsKey(split[0])) {
a1 = funcVariables.peek().get(split[0]);
}
return a1;
}
// Expression is in the form "A op B".
if (split.length == 3) {
String a1 = split[0], a2 = split[2];
if (funcCtx) {
if (funcVariables.peek().containsKey(split[0])) {
a1 = funcVariables.peek().get(split[0]);
}
if (funcVariables.peek().containsKey(split[2])) {
a2 = funcVariables.peek().get(split[2]);
}
}
return a1 + " " + split[1] + " " + a2;
}
Stack<String> s = new Stack<>();
for (int i = split.length-1; i >= 0; i--) {
if (funcCtx && funcVariables.peek().containsKey(split[i])) {
s.push(funcVariables.peek().get(split[i]));
} else { s.push(split[i]); }
}
String a1 = s.pop();
String op = s.pop();
String a2 = s.pop();
String ti = "t" + tempVarIndex;
result.append(ti + " = " + a1 + " " + op + " " + a2 + "\n");
s.push(ti);
while (s.size() > 2) {
tempVarIndex++;
ti = "t" + tempVarIndex;
a1 = s.pop();
op = s.pop();
a2 = s.pop();
result.append(ti + " = " + a1 + " " + op + " " + a2 + "\n");
s.push(ti);
}
tempVarIndex++;
return s.peek();
}
@Override
public String visitProgram(CCALParser.ProgramContext ctx) {
funcCtx = true;
visit(ctx.func_list());
/* We want our global variables to be after the main label,
so we visit the function declarations first. */
result.append("\nmain:\n");
funcCtx = false;
visit(ctx.decl_list());
funcCtx = true;
return visit(ctx.main());
}
@Override
public String visitDecl_list(CCALParser.Decl_listContext ctx) {
if (ctx.decl(0) != null) {
String decl = visit(ctx.decl(0));
if (decl != null) { globalConstants.add(decl); }
}
if (ctx.decl_list(0) != null) { visit(ctx.decl_list(0)); }
return null;
}
@Override
public String visitVar_decl(CCALParser.Var_declContext ctx) {
/* If in func context, make its label unique and save it
else, do nothing, the used TAC interpreter is dynamically typed. */
if (funcCtx) {
String id = ctx.ID().getText();
String og = id;
id = makeUnique(id);
funcVariables.peek().put(og, id);
}
return null;
}
@Override
public String visitConst_decl(CCALParser.Const_declContext ctx) {
String id = ctx.ID().getText();
// If in func context, must be made unique.
if (funcCtx) {
String og = id;
id = makeUnique(id);
funcVariables.peek().put(og, id);
}
String value = convertArithOpExpr(ctx.expr().getText());
return id + " = " + value;
}
@Override
public String visitFunc_list(CCALParser.Func_listContext ctx) {
if (ctx.func(0) != null) { visit(ctx.func(0)); }
if (ctx.func_list(0) != null) { visit(ctx.func_list(0)); }
return null;
}
@Override
public String visitFunc(CCALParser.FuncContext ctx) {
String funcID = ctx.ID().getText();
result.append("\n" + funcID + ":\n");
funcVariables.push(new HashMap<String, String>());
visit(ctx.param_list());
funcCtx = true;
visit(ctx.decl_list());
int i = 1;
while (!functionParams.isEmpty()) {
String param = functionParams.pop();
result.append(param + " = getparam " + i + "\n");
i++;
}
visit(ctx.stmt_block());
String ret = visit(ctx.expr());
result.append("return " + ret + "\n");
funcVariables.pop();
funcCount++;
return null;
}
@Override
public String visitStmt_block(CCALParser.Stmt_blockContext ctx) {
if (ctx.stmt(0) != null) { visit(ctx.stmt(0)); }
if (ctx.stmt_block(0) != null) { visit(ctx.stmt_block(0)); }
return null;
}
@Override
public String visitAssignmentStmt(CCALParser.AssignmentStmtContext ctx) {
return visit(ctx.assignment());
}
@Override
public String visitAssignment(CCALParser.AssignmentContext ctx) {
String expr = visit(ctx.expr());
if (expr != null) {
String id = ctx.ID().getText();
if (funcCtx && funcVariables.peek().containsKey(id)) {
String _id = funcVariables.peek().get(id);
result.append(_id + " = " + expr + "\n");
} else { result.append(id + " = " + expr + "\n"); }
}
return null;
}
@Override
public String visitParam_list(CCALParser.Param_listContext ctx) {
if (ctx.parameter_list(0) != null) { visit(ctx.parameter_list(0)); }
return null;
}
@Override
public String visitSingleParam(CCALParser.SingleParamContext ctx) {
return visit(ctx.param());
}
@Override
public String visitLoopStmt(CCALParser.LoopStmtContext ctx) {
return visit(ctx.loop());
}
@Override
public String visitLoop(CCALParser.LoopContext ctx) {
String label1 = makeUniqueLabel(1);
String label2 = makeUniqueLabel(2);
result.append(label1 + ": \n");
labelIndex += 2;
String condition = getCondition(ctx.condition());
result.append("ifz " + condition + " goto " + label2 + "\n");
labelIndex += 2;
visit(ctx.stmt_block());
result.append("goto " + label1 + "\n");
result.append(label2 + ":\n");
return null;
}
@Override
public String visitIfElseStmt(CCALParser.IfElseStmtContext ctx) {
return visit(ctx.if_else());
}
@Override
public String visitArithOpExpr(CCALParser.ArithOpExprContext ctx) {
return convertArithOpExpr(ctx.getText());
}
@Override
public String visitFragExpr(CCALParser.FragExprContext ctx) {
return visit(ctx.frag());
}
@Override
public String visitNotIdFrag(CCALParser.NotIdFragContext ctx) {
return "!" + ctx.ID().getText();
}
@Override
public String visitZeroFrag(CCALParser.ZeroFragContext ctx) {
return "0";
}
@Override
public String visitIdNumFrag(CCALParser.IdNumFragContext ctx) {
if (ctx.NUM() != null) {
if (ctx.MINUS() != null) { return "0 - " + ctx.NUM().getText(); }
return ctx.NUM().getText();
}
String id = ctx.ID().getText();
if (funcCtx) { id = funcVariables.peek().get(id); }
if (ctx.MINUS() != null) { return "0 - " + id; }
return id;
}
String replaceConditionVars(String cond) {
String condition = cond.replaceAll("[_a-zA-Z0-9\\(\\)]*", "");
String[] split = cond.split(condition);
String res = "";
if (funcVariables.peek().containsKey(split[0])) {
res += funcVariables.peek().get(split[0]);
} else { res += split[0]; }
res += condition;
if (funcVariables.peek().containsKey(split[1])) {
res += funcVariables.peek().get(split[1]);
} else { res += split[1]; }
return res;
}
String getCondition(CCALParser.ConditionContext ctx) {
labelIndex++;
String[] split = ctx.getText()
.replaceAll("\\(", "")
.replaceAll("\\)", "")
.replaceAll("&&", " && ")
.replaceAll("\\|\\|", " \\|\\| ")
.split(" ")
;
Stack<String> conditions = new Stack<>();
Stack<String> newConditions = new Stack<>();
Stack<String> operators = new Stack<>();
for (String s : split) {
if (s.equals("&&") || s.equals("||")) { operators.push(s); }
else { conditions.push(s); }
}
int i = 1;
while (!conditions.empty()) {
String label = makeUniqueLabel(i);
String cond = conditions.pop();
cond = replaceConditionVars(cond);
String c = "c" + condIndex;
newConditions.push(c);
result.append(c + " = false\n");
result.append("ifz " + cond + " goto " + label + "\n");
result.append(c + " = true\n");
result.append(label + ":\n");
condIndex++;
i++;
}
String c = "c" + condIndex;
while (newConditions.size() > 1) {
c = "c" + condIndex;
String op = operators.pop();
String cA = newConditions.pop();
String cB = newConditions.pop();
replaceConditionVars(cA);
result.append(c + " = " + cA + " " + op + " " + cB + "\n");
newConditions.push(c);
condIndex++;
}
return newConditions.peek() + " == true";
}
@Override
public String visitIf_else(CCALParser.If_elseContext ctx) {
String label1 = makeUniqueLabel(1);
String label2 = makeUniqueLabel(2);
labelIndex += 2;
String condition = getCondition(ctx.condition());
result.append("ifz " + condition + " goto " + label1 + "\n");
labelIndex += 2;
visit(ctx.stmt_block(0));
result.append("goto " + label2 + "\n");
result.append(label1 + ":\n");
labelIndex += 2;
visit(ctx.stmt_block(1));
result.append(label2 + ":\n");
labelIndex += 2;
return null;
}
@Override
public String visitNotSingleParam(CCALParser.NotSingleParamContext ctx) {
visit(ctx.param());
return visit(ctx.parameter_list());
}
@Override
public String visitArg_list(CCALParser.Arg_listContext ctx) {
if (ctx.argument_list(0) != null) { visit(ctx.argument_list(0)); }
return null;
}
@Override
public String visitBoolValueFrag(CCALParser.BoolValueFragContext ctx) {
return ctx.bool_value().getText();
}
@Override
public String visitArgument_list(CCALParser.Argument_listContext ctx) {
if (ctx.ID() != null) {
String id = ctx.ID().getText();
if (funcCtx && funcVariables.peek().containsKey(id)) {
args.add(funcVariables.peek().get(id));
} else { args.add(id); }
}
if (ctx.NUM() != null) { args.add(ctx.NUM().getText()); }
if (ctx.argument_list() != null) { return visit(ctx.argument_list()); }
return null;
}
@Override
public String visitFunc_call(CCALParser.Func_callContext ctx) {
String id = ctx.ID().getText();
visit(ctx.arg_list());
for (String arg : args) {
result.append("param " + arg + "\n");
}
String call = "call " + id + ", " + args.size();
if (ctx.parent.parent.getClass() != CCALParser.AssignmentContext.class) {
result.append(call + "\n");
}
args.clear();
return call;
}
@Override
public String visitParam(CCALParser.ParamContext ctx) {
String id = ctx.ID().getText();
String og = id;
id = makeUnique(id);
functionParams.push(id);
funcVariables.peek().put(og, id);
return id;
}
@Override
public String visitMain(CCALParser.MainContext ctx) {
funcVariables.push(new HashMap<>());
funcCtx = true;
visit(ctx.decl_list());
while (!globalConstants.empty()) {
String constant = globalConstants.pop();
result.append(constant + "\n");
}
visit(ctx.stmt_block());
result.append("call _exit, 0\n");
return null;
}
}
}