forked from sam5Lin/MiniC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCgen.cpp
More file actions
550 lines (477 loc) · 14.4 KB
/
Cgen.cpp
File metadata and controls
550 lines (477 loc) · 14.4 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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/*
* @Copyright: minic
* @Author: David.Huangjunlang
* @Description: 中间代码生成:实现中间代码的生成函数
* @LastEditors: linmaosen
* @LastEditTime: 2020-06-14
* @FilePath: /minic/cGen.cpp
*/
#include "Cgen.h"
#include "Code.h"
#include "symtab.h"
// 记录输出
int TraceCode = 1;
static int main_locals = 0;
/*
1 表示存储值
0 表示存储地址
*/
static int getValue = 1;
/* prototype for internal recursive code generator */
static void cGen(node *tree);
static int tmp;
/*
1 表示会递归
0 表示不递归
*/
static int isRecursive = 1;
node *paramStack[10];
int top = 0;
/**
* @description: 函数方法入栈
* @param {node*}
* @return: int
*/
int pushParam(node *param)
{
if (top == SIZE)
return 1;
paramStack[top++] = param;
return 0;
}
/**
* @description: 栈顶出栈,并返回栈顶
* @param {}
* @return: node*
*/
node *popParam()
{
if (top == 0)
return NULL;
return paramStack[--top];
}
/**
* @description: 获取变量的地址
* @param {node*}
* @return: void
*/
void emitGetAddr(node *var)
{
BucketList v = var_lookup(var->name, var->scope);
switch (var->scope)
{
case 0: // 全局的变量
if (v->isArray)
{
emitRM("LDA", bx, -(st_lookup(var->name, 0)), gp, "get global array address");
}
else
{
emitRM("LDA", bx, -1 - (st_lookup(var->name, 0)), gp, "get global address");
}
break;
case 1:
case 2:
case 3:
case 4:
case 5:
if (var_lookup(var->name, var->scope)->isParam)
{
if (v->isArray)
{
emitRM("LD", bx, 2 + (st_lookup(var->name, var->scope)), bp, "get param array address");
}
else
{
emitRM("LDA", bx, 2 + (st_lookup(var->name, var->scope)), bp, "get param variable address");
}
}
else
{
if (v->isArray)
{
emitRM("LDA", bx, -(st_lookup(var->name, var->scope)), bp, "get local array address");
}
else
{
emitRM("LDA", bx, -1 - (st_lookup(var->name, var->scope)), bp, "get local address");
}
}
break;
}
}
/**
* @description: 对stmt结点进行中间代码生成
* @param {node*}
* @return: void
*/
static void genStmt(node *tree)
{
BucketList fun;
node *p1, *p2, *p3;
int savedLoc1, savedLoc2, currentLoc;
switch (tree->kind.stmt)
{
case CallK:
if (TraceCode)
emitComment("-> call");
p1 = tree->nodeChild[0]; // 参数
if (p1 != NULL)
{
p1 = p1->next;
}
while (p1 != NULL)
{
pushParam(p1);
p1 = p1->next;
}
isRecursive = 0;
while ((p1 = popParam()) != NULL)
{
cGen(p1);
emitRM("LDA", sp, -1, sp, "push prepare");
emitRM("ST", ax, 0, sp, "push parameters");
}
isRecursive = 1;
fun = fun_lookup(tree->name, 0);
emitRM("LDA", ax, 3, pc, "store returned PC");
emitRM("LDA", sp, -1, sp, "push prepare");
emitRM("ST", ax, 0, sp, "push returned PC");
emitRM("LDC", pc, fun->fun_start, 0, "jump to function");
emitRM("LDA", sp, (tree->nodeChild[0]) != NULL ? (tree->nodeChild[0])->param_size : 0, sp, "release parameters");
if (TraceCode)
emitComment("<- call");
break;
case FunK:
if (tree->name == "main")
main_locals = tree->nodeChild[1]->param_size;
if (TraceCode)
emitComment("-> function:");
p1 = tree->nodeChild[0]; // 参数
p2 = tree->nodeChild[1]; // 方法体
fun = fun_lookup(tree->name, 0);
fun->fun_start = emitSkip(0);
emitRM("LDA", sp, -1, sp, "push prepare");
emitRM("ST", bp, 0, sp, "push old bp");
emitRM("LDA", bp, 0, sp, "let bp == sp");
emitRM("LDA", sp, -tree->local_size, sp, "allocate for local variables");
cGen(p2);
if (tree->type == 0)
{
// return
emitRM("LDA", sp, 0, bp, "let sp == bp");
emitRM("LDA", sp, 2, sp, "pop prepare");
emitRM("LD", bp, -2, sp, "pop old bp");
emitRM("LD", pc, -1, sp, "pop return addr");
}
if (TraceCode)
emitComment("<- function");
break;
case ArrayK:
if (tree->isGlobal != 1)
{
if (TraceCode)
emitComment("-> array element");
p1 = tree->nodeChild[0];
emitGetAddr(tree);
emitRM("LDA", sp, -1, sp, "push prepare");
emitRM("ST", bx, 0, sp, "protect array address");
tmp = getValue;
getValue = 1;
cGen(p1);
getValue = tmp;
emitRM("LDA", sp, 1, sp, "pop prepare");
emitRM("LD", bx, -1, sp, "recover array address");
emitRO("SUB", bx, bx, ax, "get address of array element");
if (getValue)
emitRM("LD", ax, 0, bx, "get value of array element");
if (TraceCode)
emitComment("<- array element");
}
break;
case SelectK:
if (TraceCode)
emitComment("-> if");
p1 = tree->nodeChild[0];
p2 = tree->nodeChild[1];
p3 = tree->nodeChild[2];
cGen(p1);
savedLoc1 = emitSkip(1);
emitComment("if: jump to else belongs here");
cGen(p2);
savedLoc2 = emitSkip(1);
emitComment("if: jump to end belongs here");
currentLoc = emitSkip(0);
emitBackup(savedLoc1);
emitRM("JEQ", ax, currentLoc, zero,"if: jmp to else");
emitRestore();
// else
cGen(p3);
currentLoc = emitSkip(0);
emitBackup(savedLoc2);
emitRM("LDA", pc, currentLoc, zero,"jmp to end");
emitRestore();
if (TraceCode)
emitComment("<- if");
break;
case IteraK:
if (TraceCode)
emitComment("-> while");
p1 = tree->nodeChild[0];
p2 = tree->nodeChild[1];
savedLoc1 = emitSkip(0);
emitComment("jump here after body");
cGen(p1);
savedLoc2 = emitSkip(1);
emitComment("jump to end if test fails");
cGen(p2);
emitRM("LDA", pc, savedLoc1, zero, "jump to test");
currentLoc = emitSkip(0);
emitBackup(savedLoc2);
emitRM("JEQ", ax, currentLoc, zero, "jump to end");
emitRestore();
if (TraceCode)
emitComment("<- while");
break;
case ReturnK:
if (TraceCode)
emitComment("-> return");
p1 = tree->nodeChild[0];
if (p1 != NULL)
cGen(p1);
// return
emitRM("LDA", sp, 0, bp, "let sp == bp");
emitRM("LDA", sp, 2, sp, "pop prepare");
emitRM("LD", bp, -2, sp, "pop old bp");
emitRM("LD", pc, -1, sp, "pop return addr");
if (TraceCode)
emitComment("<- return");
break;
case CompK:
if (TraceCode)
emitComment("-> compound");
p1 = tree->nodeChild[1];
cGen(p1);
if (TraceCode)
emitComment("<- compound");
break;
default:
break;
}
}
/**
* @description: 对exp结点进行中间代码生成
* @param {node*}
* @return: void
*/
static void genExp(node *tree)
{
node *p1, *p2;
BucketList fun;
switch (tree->kind.exp)
{
case AssignK:
if (TraceCode)
emitComment("-> assign");
p1 = tree->nodeChild[0];
p2 = tree->nodeChild[1];
getValue = 0;
cGen(p1);
emitRM("LDA", sp, -1, sp, "push prepare");
emitRM("ST", bx, 0, sp, "protect bx");
getValue = 1;
cGen(p2);
emitRM("LDA", sp, 1, sp, "pop prepare");
emitRM("LD", bx, -1, sp, "recover bx");
// 赋值
emitRM("ST", ax, 0, bx, "assign: store");
if (TraceCode)
emitComment("<- assign");
break;
case ConstK:
if (TraceCode)
emitComment("-> Const");
emitRM("LDC", ax, tree->val, 0, "load const");
if (TraceCode)
emitComment("<- Const");
break;
case IdK:
{
if (TraceCode)
emitComment("-> Id");
emitGetAddr(tree);
BucketList v = var_lookup(tree->name, tree->scope);
if (getValue)
{
if (v->isArray)
emitRM("LDA", ax, 0, bx, "get array variable value");
else
emitRM("LD", ax, 0, bx, "get variable value");
}
if (TraceCode)
emitComment("<- Id");
}
break;
case OpK:
if (TraceCode)
emitComment("-> Op");
p1 = tree->nodeChild[0];
p2 = tree->nodeChild[1];
cGen(p1);
emitRM("LDA", sp, -1, sp, "push prepare");
emitRM("ST", ax, 0, sp, "op: protect left");
cGen(p2);
emitRM("LDA", sp, 1, sp, "pop prepare");
emitRM("LD", bx, -1, sp, "op: recover left");
switch (tree->type)
{
case 2:
emitRO("ADD", ax, bx, ax, "op +");
break;
case 3:
emitRO("SUB", ax, bx, ax, "op -");
break;
case 4:
emitRO("MUL", ax, bx, ax, "op *");
break;
case 5:
emitRO("DIV", ax, bx, ax, "op /");
break;
case 6:
emitRO("SUB", ax, bx, ax, "op <");
emitRM("JLT", ax, 2, pc, "br if true");
emitRM("LDC", ax, 0, 0, "false case");
emitRM("LDA", pc, 1, pc, "unconditional jmp");
emitRM("LDC", ax, 1, 0, "true case");
break;
case 7:
emitRO("SUB", ax, bx, ax, "op ==");
emitRM("JEQ", ax, 2, pc, "br if true");
emitRM("LDC", ax, 0, 0, "false case");
emitRM("LDA", pc, 1, pc, "unconditional jmp");
emitRM("LDC", ax, 1, 0, "true case");
break;
case 8:
emitRO("SUB", ax, bx, ax, "op <=");
emitRM("JLE", ax, 2, pc, "br if true");
emitRM("LDC", ax, 0, 0, "false case");
emitRM("LDA", pc, 1, pc, "unconditional jmp");
emitRM("LDC", ax, 1, 0, "true case");
break;
case 9:
emitRO("SUB", ax, bx, ax, "op >");
emitRM("JGT", ax, 2, pc, "br if true");
emitRM("LDC", ax, 0, 0, "false case");
emitRM("LDA", pc, 1, pc, "unconditional jmp");
emitRM("LDC", ax, 1, 0, "true case");
break;
case 10:
emitRO("SUB", ax, bx, ax, "op >=");
emitRM("JGE", ax, 2, pc, "br if true");
emitRM("LDC", ax, 0, 0, "false case");
emitRM("LDA", pc, 1, pc, "unconditional jmp");
emitRM("LDC", ax, 1, 0, "true case");
break;
case 11:
emitRO("SUB", ax, bx, ax, "op !=");
emitRM("JNE", ax, 2, pc, "br if true");
emitRM("LDC", ax, 0, 0, "false case");
emitRM("LDA", pc, 1, pc, "unconditional jmp");
emitRM("LDC", ax, 1, 0, "true case");
break;
default:
emitComment("BUG: Unknown operator");
break;
}
if (TraceCode)
emitComment("<- Op");
break;
default:
break;
}
}
/**
* @description: 分别对stmt和exp结点进行中间代码生成
* @param {node*}
* @return: void
*/
static void cGen(node *tree)
{
if (tree != NULL)
{
switch (tree->nodekind)
{
case StmtK:
genStmt(tree);
break;
case ExpK:
genExp(tree);
break;
// case DecK:
// genDec(tree);
default:
break;
}
if (isRecursive)
cGen(tree->next);
}
}
/**
* @description: 对语法树结点生成中间代码
* @param {node*}
* @return: void
*/
void codeGen(node *syntaxTree)
{
BucketList fun;
emitComment("miniC- Compilation to MC Code");
emitComment("Standard prelude:");
emitRM("LD", gp, 0, zero, "load maxaddress from location 0");
emitRM("ST", zero, 0, zero, "clear location 0");
emitRM("LDA", sp, syntaxTree->kind.stmt != FunK ? -(syntaxTree->local_size) : 0, gp, "copy gp to sp & allocating global variables(if any)");
emitComment("End of standard prelude.");
// 找main方法
if (TraceCode)
emitComment("Jump to main()");
int loc = emitSkip(6);
// input output方法
fun = fun_lookup("input", 0);
if (fun != NULL)
{
if (TraceCode)
emitComment("Begin input()");
fun->fun_start = emitSkip(0);
emitRO("IN", ax, 0, 0, "read input into ax");
emitRM("LDA", sp, 1, sp, "pop prepare");
emitRM("LD", pc, -1, sp, "pop return addr");
if (TraceCode)
emitComment("End input()");
}
fun = fun_lookup("output", 0);
if (fun != NULL)
{
if (TraceCode)
emitComment("Begin output()");
fun->fun_start = emitSkip(0);
emitRM("LD", ax, 1, sp, "load param into ax");
emitRO("OUT", ax, 0, 0, "output using ax");
emitRM("LDA", sp, 1, sp, "pop prepare");
emitRM("LD", pc, -1, sp, "pop return addr");
if (TraceCode)
emitComment("End output()");
}
cGen(syntaxTree);
emitBackup(loc);
fun = fun_lookup("main", 0);
if (fun == NULL)
{
fprintf(stderr, "main not found\n");
}
emitRM("LDA", ax, 3, pc, "store returned PC");
emitRM("LDA", sp, -1, sp, "push prepare");
emitRM("ST", ax, 0, sp, "push returned PC");
emitRM("LDC", pc, fun->fun_start, 0, "jump to function");
emitRM("LDA", sp, main_locals, sp, "release local var");
emitComment("End of execution.");
emitRO("HALT", 0, 0, 0, "");
}