-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate.c
608 lines (585 loc) · 17.6 KB
/
translate.c
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
Type* doExp(Node*);
#include "intercode.h"
int calcTypeSize(Type* vartype);
InterCodes* translate_Args(Node* p);
InterCodes* translate_Exp(Node*, Operand*);
InterCodes* translate_Cond(Node*, Operand*, Operand*);
extern Type* TypeNodeInt;
extern Type* TypeNodeFloat;
extern Operand* OperandNodeZero;
extern Operand* OperandNodeOne;
extern Operand* OperandNodeFour;
extern int translate_error;
SNode* fnode = NULL;
int temp_var_no = 0, label_address_no = 0, st_var_no = 0;
int new_var(){
st_var_no ++ ;
return st_var_no;
}
Operand* new_temp(){
temp_var_no ++;
return opInit(TEMPVAR, temp_var_no);
}
Operand* new_label(){
label_address_no ++;
return opInit(LABELADDRESS, label_address_no);
}
int calcTypeSize(Type* vartype){
int size = 0;
if(vartype -> kind == BASIC){
size = 4;
}
else if(vartype -> kind == ARRAY){
size = vartype -> u.array.size * calcTypeSize(vartype -> u.array.elem);
}
else if(vartype -> kind == STRUCTURE){
FieldList* field = vartype -> u.structure;
while(field != NULL){
size = size + calcTypeSize(field -> type);
field = field -> tail;
}
}
return size;
}
InterCodes* translate_Args(Node* p){
/* The first child must be Exp */
assert(p != NULL);
int childno = p -> childno;
Node* child[4];
int i;
for(i = 0; i < childno; i++){
child[i] = p -> child[i];
}
InterCodes* code1 = NULL;
InterCodes* code2 = NULL;
Operand* var = new_temp();
code1 = translate_Exp(child[0], var);
code2 = codesInit(ARG, 1, var);
if(childno == 1){
return codesJoin(code1, code2);
}
else {
/* Exp COMMA Args */
InterCodes* code3 = translate_Args(child[2]);
code1 = codesJoin(code1, code3);
return codesJoin(code1, code2);
}
}
InterCodes* translate_Exp(Node* p, Operand* place){
/* if Exp is BASIC, return value;
* else if ARRAY or STRUCTURE, return ADDRESS. */
assert(p != NULL);
int childno = p -> childno;
Node* child[4];
int i;
for(i = 0; i < childno; i++){
child[i] = p -> child[i];
}
/* LP Exp RP */
if(child[0] -> type == LP){
Operand* t1 = new_temp();
InterCodes* code1 = translate_Exp(child[1], t1);
InterCodes* code2 = NULL;
if(place != NULL)code2 = codesInit(ASSIGN_ORIGIN, 2, place, t1);
return codesJoin(code1, code2);
}
/* INT */
else if(child[0] -> type == INT){
if(place == NULL)return NULL;
Operand* v = opInit(CONSTANT, child[0] -> value.Int);
return codesInit(ASSIGN_ORIGIN, 2, place, v);
}
/* ID */
else if(childno == 1 && child[0] -> type == ID){
if(place == NULL)return NULL;
SNode* vnode = stFind(child[0] -> String);
assert(vnode -> visitedTag == VARIABLE);
Type* vartype = vnode -> Message.var;
Operand* v = opInit(VARIABLE_3, vnode -> op_var_no);
if(vartype -> kind == BASIC){
return codesInit(ASSIGN_ORIGIN, 2, place, v);
}
else {
if(vnode -> isParam != 0){
return codesInit(ASSIGN_ORIGIN, 2, place, v);
}
else return codesInit(ASSIGN_ADDRESS_TO, 2, place, v);
}
}
/* FLOAT */
else if(child[0] -> type == FLOAT){
if(place == NULL)return NULL;
/* Hypothesis:
* No FLOAT
* */
translate_error ++;
printf("Cannot translate: Code contains variables or parameters of Float.\n");
}
/* MINUS Exp */
else if(child[0] -> type == MINUS){
Operand* t1 = new_temp();
InterCodes* code1 = translate_Exp(child[1], t1);
InterCodes* code2 = NULL;
if(place != NULL)code2 = codesInit(SUB_3, 3, place, OperandNodeZero, t1);
return codesJoin(code1, code2);
}
/* NOT Exp */
/* Exp RELOP Exp */
/* Exp AND Exp */
/* Exp OR Exp */
else if(child[0] -> type == NOT || (childno == 3 && (child[1] -> type == RELOP || child[1] -> type == AND || child[1] -> type == OR))){
assert(place != NULL);
Operand* label1 = new_label();
Operand* label2 = new_label();
InterCodes* code0 = codesInit(ASSIGN_ORIGIN, 2, place, OperandNodeZero);
InterCodes* code1 = translate_Cond(p, label1, label2);
InterCodes* code2 = codesJoin(codesInit(LABEL, 1, label1), codesInit(ASSIGN_ORIGIN, 2, place, OperandNodeOne));
code0 = codesJoin(code0 ,code1);
code2 = codesJoin(code2, codesInit(LABEL, 1, label2));
return codesJoin(code0 ,code2);
}
/* Exp PLUS Exp */
/* Exp MINUS Exp */
/* Exp STAR Exp */
/* Exp DIV Exp */
else if(childno == 3 && (child[1] -> type == PLUS || child[1] -> type == MINUS || child[1] -> type == STAR || child[1] -> type == DIV)){
Operand* t1 = new_temp();
Operand* t2 = new_temp();
InterCodes* code1 = translate_Exp(child[0], t1);
InterCodes* code2 = translate_Exp(child[2], t2);
InterCodes* code3 = NULL;
if(place != NULL){
if(child[1] -> type == PLUS){
code3 = codesInit(ADD_3, 3, place, t1, t2);
}
else if(child[1] -> type == MINUS){
code3 = codesInit(SUB_3, 3, place, t1, t2);
}
else if(child[1] -> type == STAR){
code3 = codesInit(MUL_3, 3, place, t1, t2);
}
else if(child[1] -> type == DIV){
code3 = codesInit(DIV_3, 3, place, t1, t2);
}
}
code1 = codesJoin(code1, code2);
return codesJoin(code1, code3);
}
/* Exp ASSIGNOP Exp */
else if(childno == 3 && child[1] -> type == ASSIGNOP){
Type* lefttype = doExp(child[0]);
/* Exp DOT ID
* ID
* Exp LB Exp RB */
int leftchildno = child[0] -> childno;
Operand* right = new_temp();
InterCodes* calc_right = translate_Exp(child[2], right);
Operand* left = NULL; // value or address
InterCodes* assign = NULL;
if(leftchildno == 1){ // ID
SNode* vnode = stFind(child[0] -> child[0] -> String);
left = opInit(VARIABLE_3, vnode -> op_var_no);
assign = codesInit(ASSIGN_ORIGIN, 2, left, right);
}
else if(leftchildno == 4){ // Exp LB Exp RB
InterCodes* code[5];
Operand* num = new_temp();
code[0] = translate_Exp(child[0] -> child[2], num);
Operand* base = new_temp();
code[1] = translate_Exp(child[0] -> child[0], base);
int typesize = calcTypeSize(lefttype);
Operand* size = opInit(CONSTANT, typesize);
code[2] = codesInit(MUL_3, 3, num, num, size);
left = new_temp();
code[3] = codesInit(ADD_3, 3, left, base, num);
code[4] = codesInit(ASSIGN_TO_ADDRESS, 2, left, right);
int i;
for(i = 0; i < 5; i++){
assign = codesJoin(assign, code[i]);
}
}
else if(leftchildno == 3){ // Exp DOT ID
InterCodes* code[3];
Operand* base = new_temp();
code[0] = translate_Exp(child[0] -> child[0], base);
Type* stype = doExp(child[0] -> child[0]);
int size = calcTypeSize(stype);
FieldList* f = stype -> u.structure;
while(f != NULL){
size -= calcTypeSize(f -> type);
if(strcmp(child[0] -> child[2] -> String, f -> name) == 0)break;
f = f -> tail;
}
Operand* rel = opInit(CONSTANT, size);
left = new_temp();
code[1] = codesInit(ADD_3, 3, left, base, rel);
code[2] = codesInit(ASSIGN_TO_ADDRESS, 2, left, right);
int i;
for(i = 0; i < 3; i++){
assign = codesJoin(assign, code[i]);
}
}
if(place != NULL)assign = codesJoin(assign, codesInit(ASSIGN_ORIGIN, 2, place, right));
return codesJoin(calc_right, assign);
}
/* ID LP Args RP */
else if(childno == 4 && child[0] -> type == ID){
SNode* stnode = stFind(child[0] -> String);
if(strcmp(stnode -> name, "write") == 0){
Operand* t1 = new_temp();
InterCodes* code1 = translate_Exp(child[2] -> child[0], t1);
return codesJoin(code1, codesInit(WRITE, 1 ,t1));
}
else {
InterCodes* code1 = translate_Args(child[2]);
Operand* drop = new_temp();
InterCodes* code2 = codesInit(CALLFUNC, 2, drop, stnode);
if(place != NULL)code2 = codesJoin(code2, codesInit(ASSIGN_ORIGIN, 2, place, drop));
return codesJoin(code1, code2);
}
}
/* ID LP RP */
else if(childno == 3 && child[0] -> type == ID){
SNode* fnode = stFind(child[0] -> String);
if(strcmp(fnode -> name, "read") == 0){
if(place != NULL) return codesInit(READ, 1, place);
else {
Operand* drop = new_temp();
return codesInit(READ, 1, drop);
}
}
else {
if(place != NULL)return codesInit(CALLFUNC, 2, place, fnode);
else{
Operand* drop = new_temp();
return codesInit(CALLFUNC, 2, drop, fnode);
}
}
}
/* Exp LB Exp RB */
else if(childno == 4 && child[1] -> type == LB){
InterCodes* code[6];
Type* ptype = doExp(p); // this is for "place"
Operand* base = new_temp();
code[0] = translate_Exp(child[0], base);
Operand* num = new_temp();
code[1] = translate_Exp(child[2], num);
int typesize = calcTypeSize(ptype);
Operand* size = opInit(CONSTANT, typesize);
code[2] = codesInit(MUL_3, 3, num, size, num);
code[3] = codesInit(ADD_3, 3, base, base, num);
Operand* v = new_temp();
if(ptype -> kind != BASIC){
code[4] = codesInit(ASSIGN_ORIGIN, 2, v, base);
}
else {
code[4] = codesInit(ASSIGN_VALUE_FROM, 2, v, base);
}
code[5] = NULL;
if(place != NULL){
code[5] = codesInit(ASSIGN_ORIGIN, 2, place, v);
}
int i;
for(i = 1; i < 6; i++){
code[0] = codesJoin(code[0], code[i]);
}
return code[0];
}
/* Exp DOT ID */
else if(childno == 3 && child[1] -> type == DOT){
Type* stype = doExp(child[0]);
assert(stype -> kind == STRUCTURE);
Operand* v = new_temp();
Operand* base = new_temp();
InterCodes* code[4];
code[0] = translate_Exp(child[0], base);
/* base must be an adddress */
int size = calcTypeSize(stype);
FieldList* f = stype -> u.structure;
while(f != NULL){
size -= calcTypeSize(f -> type);
if(strcmp(child[2] -> String, f -> name) == 0){
Operand* rel = opInit(CONSTANT, size);
code[1] = codesInit(ADD_3, 3, base, base, rel);
if(f -> type -> kind != BASIC){
code[2] = codesInit(ASSIGN_ORIGIN, 2, v, base);
}
else {
code[2] = codesInit(ASSIGN_VALUE_FROM, 2, v, base);
}
break;
}
f = f -> tail;
}
code[3] = NULL;
if(place != NULL){
code[3] = codesInit(ASSIGN_ORIGIN, 2, place, v);
}
int i;
for(i = 1; i < 4; i++){
code[0] = codesJoin(code[0], code[i]);
}
return code[0];
}
}
InterCodes* translate_Cond(Node* p, Operand* label_true, Operand* label_false){
/* p's type is Exp */
assert(p != NULL);
int childno = p -> childno;
Node* child[4];
int i;
for(i = 0; i < childno; i++){
child[i] = p -> child[i];
}
/* Exp RELOP Exp */
if(child[1] -> type == RELOP){
Operand* t1 = new_temp();
Operand* t2 = new_temp();
InterCodes* code1 = translate_Exp(child[0], t1);
InterCodes* code2 = translate_Exp(child[2], t2);
InterCodes* code3 = codesInit(IFGOTO, 4, t1, child[1] -> value.Int, t2, label_true);
code1 = codesJoin(code1, code2);
code3 = codesJoin(code3, codesInit(GOTO, 1, label_false));
return codesJoin(code1, code3);
}
/* NOT Exp */
else if(child[0] -> type == NOT){
return translate_Cond(child[1], label_false, label_true);
}
/* Exp AND Exp */
else if(child[1] -> type == AND){
Operand* label1 = new_label();
InterCodes* code1 = translate_Cond(child[0], label1, label_false);
InterCodes* code2 = translate_Cond(child[2], label_true, label_false);
code1 = codesJoin(code1, codesInit(LABEL, 1, label1));
return codesJoin(code1, code2);
}
/* Exp OR Exp */
else if(child[1] -> type == OR){
Operand* label1 = new_label();
InterCodes* code1 = translate_Cond(child[0], label_true, label1);
InterCodes* code2 = translate_Cond(child[2], label_true, label_false);
code1 = codesJoin(code1, codesInit(LABEL, 1, label1));
return codesJoin(code1, code2);
}
else {
Operand* t1 = new_temp();
InterCodes* code1 = translate_Exp(p, t1);
InterCodes* code2 = codesInit(IFGOTO, 4, t1, NE, OperandNodeZero, label_true);
code2 = codesJoin(code2, codesInit(GOTO, 1, label_false));
return codesJoin(code1, code2);
}
}
InterCodes* translate(Node* p){
/* Hypothesis:
* There are no global variables or function declarations.
* */
if(p == NULL || p -> childno == 0)return NULL;
int childno = p -> childno;
Node* child[8];
int i;
for(i = 0; i < childno; i++){
child[i] = p -> child[i];
}
switch(p -> type){
case Program:
/* ExtDefList */
return translate(child[0]);
case ExtDefList:
if(childno == 2){
/* ExtDef ExtDefList */
InterCodes* code1 = translate(child[0]);
InterCodes* code2 = translate(child[1]);
return codesJoin(code1, code2);
}
/* */
else return NULL;
case ExtDef:
if(childno == 3 && child[2] -> type == CompSt){
/* Specifier FunDec CompSt */
SNode* stnode = stFind(child[1] -> child[0] -> String);
assert(stnode != NULL);
InterCodes* code1 = codesInit(FUNCTION_3, 1, stnode);
InterCodes* code2 = translate(child[1]);
/* set currently handled function */
fnode = stnode;
InterCodes* code3 = translate(child[2]);
InterCodes* code4 = codesInit(BLANKLINE, 0);
code1 = codesJoin(code1, code2);
code3 = codesJoin(code3, code4);
return codesJoin(code1, code3);
}
else if(childno == 2){
/* Specifier SEMI */
return NULL;
}
else{
/* Specifier ExtDecList SEMI
* Because of the Hypothesis 4:
* No global variants */
return NULL;
}
break;
case FunDec:
if(childno == 3){ /* ID LP RP */
return NULL;
}
else { /* ID LP VarList RP */
SNode* stnode = stFind(child[0] -> String);
Funcmsg* pfunc = stnode -> Message.func;
int no = pfunc -> para.no;
assert(no != 0);
SNode* paranode = pfunc -> para.head;
InterCodes* code = NULL;
while(no--){
assert(paranode != NULL);
/* set a new no for each parameter */
paranode -> op_var_no = new_var();
/* Generate 'PARAM x's */
Operand* v = opInit(VARIABLE_3, paranode -> op_var_no);
paranode -> isParam = 1;
/* v represents the value when parameter is BASIC,
* and the address when ARRAY or STRUCTURE */
code = codesJoin(codesInit(PARAM, 1, v), code);
paranode = paranode -> fnext;
}
return code;
}
case CompSt: {
/* LC DefList StmtList RC */
InterCodes* code1 = translate(child[1]);
InterCodes* code2 = translate(child[2]);
return codesJoin(code1, code2);
}
case DefList:
if(childno == 2){
/* Def DefList */
InterCodes* code1 = translate(child[0]);
InterCodes* code2 = translate(child[1]);
return codesJoin(code1, code2);
}
else return NULL;
case Def:
/* Specifier DecList SEMI */
return translate(child[1]);
case DecList: {
InterCodes* code1 = translate(child[0]);
if(childno == 1){
/* Dec */
return code1;
}
else if(childno == 3){
/* Dec COMMA DecList */
return codesJoin(code1, translate(child[2]));
}
}
case Dec:{
/* 1. Find id
* 2. New a place for it if ARRAY or STRUCTURE
* 3. (Optional)Assign value for BASIC
* Hypothesis:
* no direct assignment for non-basic variants */
Node* varp = child[0];
while(varp != NULL && varp -> type != ID){
varp = varp -> child[0];
}
assert(varp != NULL);
SNode* vnode = stFind(varp -> String);
assert(vnode != NULL);
/* set a new no for each parameter */
vnode -> op_var_no = new_var();
Type* vartype = vnode -> Message.var;
if(vartype -> kind == ARRAY || vartype -> kind == STRUCTURE){
/* apply for memory space */
int size = calcTypeSize(vartype);
Operand* v = opInit(VARIABLE_3, vnode -> op_var_no);
InterCodes* code = codesInit(DEC, 2, v, size);
return code;
}
/* if basic type has initialization */
else if(vartype -> kind == BASIC && childno == 3){
/* VarDec ASSIGNOP Exp */
Operand* t1 = new_temp();
Type* type = doExp(child[2]);
InterCodes* code1 = translate_Exp(child[2], t1);
Operand* v1 = opInit(VARIABLE_3, vnode -> op_var_no);
InterCodes* code2 = NULL;
if(type -> kind == BASIC){
code2 = codesInit(ASSIGN_ORIGIN, 2, v1, t1);
}
else {
assert(0);
//code2 = codesInit(ASSIGN_VALUE_FROM, 2, v1, t1);
//TODO
}
return codesJoin(code1, code2);
}
}
case StmtList:
if(childno == 2){
/* Stmt StmtList */
InterCodes* code1 = translate(child[0]);
InterCodes* code2 = translate(child[1]);
return codesJoin(code1, code2);
}
else return NULL;
case Stmt:
/* CompSt */
if(childno == 1)return translate(child[0]);
/* Exp SEMI */
else if(childno == 2)return translate_Exp(child[0], NULL);
else if(childno == 3){
/* RETURN Exp SEMI
* Hypothesis 5:
* Only return BASIC type */
Operand* t1 = new_temp();
InterCodes* code1 = translate_Exp(child[1], t1);
InterCodes* code2 = codesInit(RETURN_3, 1, t1);
return codesJoin(code1, code2);
}
/* IF LP Exp RP Stmt ELSE Stmt */
else if(childno == 7){
Operand* label1 = new_label();
Operand* label2 = new_label();
Operand* label3 = new_label();
InterCodes* code1 = translate_Cond(child[2], label1, label2);
InterCodes* code2 = translate(child[4]);
InterCodes* code3 = translate(child[6]);
code1 = codesJoin(code1, codesInit(LABEL, 1, label1));
code2 = codesJoin(code2, codesInit(GOTO, 1, label3));
code2 = codesJoin(code2, codesInit(LABEL, 1, label2));
code3 = codesJoin(code3, codesInit(LABEL, 1, label3));
code1 = codesJoin(code1, code2);
return codesJoin(code1, code3);
}
/* IF LP Exp RP Stmt */
else if(child[0] -> type == IF){
Operand* label1 = new_label();
Operand* label2 = new_label();
InterCodes* code1 = translate_Cond(child[2], label1, label2);
InterCodes* code2 = translate(child[4]);
code1 = codesJoin(code1, codesInit(LABEL, 1, label1));
code2 = codesJoin(code2, codesInit(LABEL, 1, label2));
return codesJoin(code1, code2);
}
/* WHILE LP Exp RP Stmt */
else if(child[0] -> type == WHILE){
Operand* label1 = new_label();
Operand* label2 = new_label();
Operand* label3 = new_label();
InterCodes* code[6];
code[0] = codesInit(LABEL, 1, label1);
code[1] = translate_Cond(child[2], label2, label3);
code[2] = codesInit(LABEL, 1, label2);
code[3] = translate(child[4]);
code[4] = codesInit(GOTO, 1, label1);
code[5] = codesInit(LABEL, 1, label3);
int i;
for(i = 1; i < 6; i++){
code[0] = codesJoin(code[0], code[i]);
}
return code[0];
}
break;
}
}