-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslate.c
746 lines (740 loc) · 16.7 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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
#include "translate.h"
#include "dictionary.h"
//成对出现,保护两个量。
#define save() do{type_d*__current_struct=current_struct;int __current_size=current_size;
#define load() current_struct=__current_struct;current_size=__current_size;}while(0);
//当前的struct定义处,用于嵌套定义struct的访问。
static type_d* current_struct=NULL;
//当前数组单元大小。
static int current_size=4;
//当前函数参数表,用来确定某一个变量是结构体的引用还是结构体本身。
static func_d* current_func=NULL;
/*
* 翻译exp,其中的option表示place的主动、被动。
* 0:被动:place上层仅仅提供了位置,可以没有左值。你填写任何,上层会直接使用,比如填写立即数也可以。
* 1:主动:place是由上层给定的,确定的变量,请将你的结果放入place
* 如果place==NULL,则本层不要生成变量返回该exp的结果。
*/
void translate_exp(Node* h,char* place,int option);
//翻译COND,由上层提供跳转label
void translate_cond(Node*h,char* label_true,char* label_false);
//得到这个exp的地址。
void get_location(Node*h,char* place);
//翻译参数列表。需要当前函数定义中的参数表,当前是第几个参数。
void translate_args(Node* h,val_d** args,int count);
void translate(Node* h)
{
if(h==NULL)return;
switch(h->type)
{
case Specifier:return;
case FunDec:
{
func_d* temp=find_function(h->child[0]->name);
add_code(3,"FUNCTION",h->child[0]->name,":");
for(int i=0;i<temp->parameter_count;i++){
struct nlist *dic = lookup(temp->parameter_list[i]->name);
if(dic == NULL)
dic = install(temp->parameter_list[i]->name, dic_temp());
add_code(2,"PARAM",dic->defn);
}
current_func=temp;
break;
}
case Dec:
{
Node* p=h->child[0]->child[0];
while(p->type!=_ID)
{
p=p->child[0];
}
val_d* temp=find_value(p->name);
if(temp->kind==USER_DEFINED)
{
char length[8];
itoa(struct_get_size(temp->val_type),length,10);
struct nlist *dic = lookup(temp->name);
if(dic == NULL)
dic = install(temp->name, dic_temp());
add_code(3,"DEC",dic->defn,length);
}
if(h->child_count==3)
{
struct nlist *dic = lookup(temp->name);
if(dic == NULL)
dic = install(temp->name, dic_temp());
translate_exp(h->child[2],dic->defn,1);
}
break;
}
case Stmt:
{
//根据第一个子结点的不同,分别作不同的处理
switch(h->child[0]->type)
{
case Exp:
{
translate_exp(h->child[0],NULL,0);
break;
}
case CompSt:
{
translate(h->child[0]);
break;
}
case _RETURN:
{
char temp[32];
translate_exp(h->child[1],temp,0);
add_code(2,"RETURN",temp);
break;
}
case _IF:
{
char label1[32],label2[32],label3[32];
new_label(label1);
new_label(label2);
if(h->child_count==7)
{
new_label(label3);
}
translate_cond(h->child[2],label1,label2);
add_code(3,"LABEL",label1,":");
translate(h->child[4]);
if(h->child_count==7)
add_code(2,"GOTO",label3);
add_code(3,"LABEL",label2,":");
if(h->child_count==7)
{
translate(h->child[6]);
add_code(3,"LABEL",label3,":");
}
break;
}
case _WHILE:
{
char label1[32],label2[32],label3[32];
new_label(label1);
new_label(label2);
new_label(label3);
add_code(3,"LABEL",label1,":");
translate_cond(h->child[2],label2,label3);
add_code(3,"LABEL",label2,":");
translate(h->child[4]);
add_code(2,"GOTO",label1);
add_code(3,"LABEL",label3,":");
break;
}
}
break;
}
default:
{
for(int i=0;i<h->child_count;i++)
translate(h->child[i]);
break;
}
}
}
void translate_exp(Node* h,char* place,int option)
{
if(h->child[0]->type==_LP)
{
return translate_exp(h->child[1],place,option);
}
else if(h->child_count==3 && h->child[1]->type==_ASSIGNOP)
{
char temp[32];
translate_exp(h->child[0],temp,0);
translate_exp(h->child[2],temp,1);
if(place!=NULL)
{
if(option==0)
{
strcpy(place,temp);
}
else
{
add_code(3,place,":=",temp);
}
}
}
else if(h->child_count==3 && h->child[1]->type==_LP)
{
if(place==NULL)
{
if(strcmp(h->child[0]->name,"read")==0)
return;
char temp[32];
new_temp(temp);
add_code(4,temp,":=","CALL",h->child[0]->name);
}
else
{
if(option==0)
new_temp(place);
if(strcmp(h->child[0]->name,"read")==0)
add_code(2,"READ",place);
else
add_code(4,place,":=","CALL",h->child[0]->name);
}
}
else if(h->child_count==4 && h->child[1]->type==_LP)
{
if(place==NULL)
{
if(strcmp(h->child[0]->name,"write")==0)
{
char temp[32];
translate_exp(h->child[2]->child[0],temp,0);
add_code(2,"WRITE",temp);
}
else
{
func_d* f=find_function(h->child[0]->name);
translate_args(h->child[2],f->parameter_list,0);
char temp[32];
new_temp(temp);
add_code(4,temp,":=","CALL",h->child[0]->name);
}
}
else
{
if(strcmp(h->child[0]->name,"write")==0)
{
char temp[32];
translate_exp(h->child[2]->child[0],temp,0);
add_code(2,"WRITE",temp);
if(option==0)
{
place[0]='#';
place[1]='0';
place[2]='\0';
}
else
{
add_code(3,place,":=","#0");
}
}
else
{
func_d* f=find_function(h->child[0]->name);
translate_args(h->child[2],f->parameter_list,0);
if(option==0)
new_temp(place);
add_code(4,place,":=","CALL",h->child[0]->name);
}
}
}
if(place==NULL)return;
if(h->child[0]->type==_INT)
{
if(option==0)
{
sprintf(place,"#%d",h->child[0]->value_i);
}
else
{
char temp[32];
sprintf(temp,"#%d",h->child[0]->value_i);
add_code(3,place,":=",temp);
}
}
else if(h->child[0]->type==_ID && h->child_count==1)
{
struct nlist *dic=lookup(h->child[0]->name);
if(dic == NULL)
dic = install(h->child[0]->name, dic_temp());
if(option==0)
{
strcpy(place,dic->defn);
}
else
{
add_code(3,place,":=",dic->defn);
}
}
else if(h->child_count==3 && (h->child[1]->type==_PLUS || h->child[1]->type==_MINUS || h->child[1]->type==_STAR || h->child[1]->type==_DIV))
{
char temp1[32],temp2[32];
translate_exp(h->child[0],temp1,0);
if((temp1[0]=='#' && temp1[1]=='0') && (h->child[1]->type==_STAR || h->child[1]->type==_DIV))
{
if(option==0)
{
place[0]='#';
place[1]='0';
place[2]='\0';
}
else
{
add_code(3,place,":=","#0");
}
}
translate_exp(h->child[2],temp2,0);
if((temp2[0]=='#' && temp2[1]=='0') && (h->child[1]->type==_STAR || h->child[1]->type==_DIV))
{
if(option==0)
{
place[0]='#';
place[1]='0';
place[2]='\0';
}
else
{
add_code(3,place,":=","#0");
}
}
else if(temp1[0]=='#' && temp2[0]=='#')
{
int i1=strtol(&temp1[1],NULL,10);
int i2=strtol(&temp2[1],NULL,10);
if(h->child[1]->type==_PLUS)
{
if(option==0)
{
sprintf(place,"#%d",i1+i2);
}
else
{
char temp[32];
sprintf(temp,"#%d",i1+i2);
add_code(3,place,":=",temp);
}
}
else if(h->child[1]->type==_MINUS)
{
if(option==0)
{
sprintf(place,"#%d",i1-i2);
}
else
{
char temp[32];
sprintf(temp,"#%d",i1-i2);
add_code(3,place,":=",temp);
}
}
else if(h->child[1]->type==_STAR)
{
if(option==0)
{
sprintf(place,"#%d",i1*i2);
}
else
{
char temp[32];
sprintf(temp,"#%d",i1*i2);
add_code(3,place,":=",temp);
}
}
else
{
if(option==0)
{
sprintf(place,"#%d",i1/i2);
}
else
{
char temp[32];
sprintf(temp,"#%d",i1/i2);
add_code(3,place,":=",temp);
}
}
}
else if(temp2[0]=='#' && temp2[1]=='0')
{
if(option==0)
strcpy(place,temp1);
else
add_code(3,place,":=",temp1);
}
else if(temp1[0]=='#' && temp1[1]=='0' && h->child[1]->type==_PLUS)
{
if(option==0)
strcpy(place,temp2);
else
add_code(3,place,":=",temp2);
}
else
{
char op[2];
op[1]='\0';
if(h->child[1]->type==_PLUS) op[0]='+';
else if(h->child[1]->type==_MINUS) op[0]='-';
else if(h->child[1]->type==_STAR) op[0]='*';
else op[0]='/';
if(option==0)
{
new_temp(place);
}
add_code(5,place,":=",temp1,op,temp2);
}
}
else if(h->child[0]->type==_MINUS)
{
char temp[32];
translate_exp(h->child[1],temp,0);
if(temp[0]=='#')
{
int i=strtol(&temp[1],NULL,10);
if(option==0)
{
sprintf(place,"#%d",-i);
}
else
{
char temp[32];
sprintf(temp,"#%d",-i);
add_code(3,place,":=",temp);
}
}
else
{
if(option==0)
{
new_temp(place);
}
add_code(5,place,":=","#0","-",temp);
}
}
else if(h->child[0]->type==_NOT || h->child[1]->type==_RELOP || h->child[1]->type==_AND || h->child[1]->type==_OR)
{
char label1[32],label2[32];
new_label(label1);
new_label(label2);
if(option==0)
new_temp(place);
add_code(3,place,":=","#0");
translate_cond(h,label1,label2);
add_code(3,"LABEL",label1,":");
add_code(3,place,":=","#1");
add_code(3,"LABEL",label2,":");
}
else if(h->child_count==4 && h->child[1]->type==_LB)
{
char temp1[32], temp2[32];
char *pre_temp;
char *temp3[32];
int temp_count = 0, arr_count = 0;
int arr_dem[20];
Node* out_temp = h;
int count_sum = 0, number_hav = 0;
while(out_temp->child_count != 1){
temp3[temp_count] = (char *)malloc(sizeof(char)*32);
translate_exp(out_temp->child[2],temp3[temp_count],0);
printf("process %d %s children_count %d\n", temp_count, temp3[temp_count], out_temp->child[2]->child_count);
temp_count++;
out_temp = out_temp->child[0];
}
array_def_list* arr_list=find_value(out_temp->child[0]->name)->val_type->def.a;
while(arr_list != NULL){
arr_dem[arr_count++] = arr_list->number;
arr_list = arr_list->next;
}
for(int i_c = 0; i_c < temp_count; i_c++){
if(temp3[i_c][0] == '#'){
int tt = strtol(&temp3[i_c][1],NULL,10);
for(int j_c = i_c - 1; j_c > -1; j_c--){
tt *= arr_dem[j_c];
}
count_sum += tt;
}else{
int tt = 1;
for(int j_c = i_c - 1; j_c > -1; j_c--){
tt *= arr_dem[j_c];
}
if(tt != 1){
char temp[32], tempi[32];
sprintf(tempi, "#%d", tt);
new_temp(temp);
add_code(5,temp,":=",temp3[i_c],"*",tempi);
temp3[i_c] = temp;
}
if(number_hav == 1){
char tempj[32];
new_temp(tempj);
add_code(5,tempj,":=", pre_temp,"*",temp3[i_c]);
pre_temp = tempj;
}else{
number_hav = 1;
pre_temp = temp3[i_c];
}
}
}
if(number_hav == 1 && count_sum != 0){
char temp[32];
new_temp(temp);
add_code(5,temp,":=", pre_temp,"*",temp2);
strcpy(temp2, temp);
}else if(number_hav == 1 && count_sum == 0){
sprintf(temp2, "%s", pre_temp);
}else{
sprintf(temp2, "#%d", count_sum);
}
printf("process successful %s %s\n", out_temp->child[0]->name, temp2);
get_location(out_temp,temp1);
printf("process successful2\n");
if(temp2[0]=='#'&& temp2[1]=='0')
{
char temp[32];
if(temp1[0]=='&')
strcpy(temp,&temp1[1]);
else
sprintf(temp,"*%s",temp1);
if(option==0)
{
strcpy(place,temp);
}
else
{
add_code(3,place,":=",temp);
}
}
else if(temp2[0]=='#')
{
int i=strtol(&temp2[1],NULL,10);
char temp[32],tempi[32];
sprintf(tempi,"#%d",i*current_size);
new_temp(temp);
add_code(5,temp,":=",temp1,"+",tempi);
char result[32];
sprintf(result,"*%s",temp);
if(option==0)
strcpy(place,result);
else
add_code(3,place,":=",result);
}
else
{
char temp[32],tempi[32],result[32],num[32];
sprintf(num,"#%d",current_size);
new_temp(temp);
new_temp(tempi);
add_code(5,tempi,":=",temp2,"*",num);
add_code(5,temp,":=",temp1,"+",tempi);
sprintf(result,"*%s",temp);
if(option==0)
strcpy(place,result);
else
add_code(3,place,":=",result);
}
printf("process successful2\n");
}
else if(h->child_count==3 && h->child[1]->type==_DOT)
{
close_opt();
char left[32],temp[32],result[32];
get_location(h->child[0],left);
int offseti=struct_get_offset(current_struct,h->child[2]->name);
char offset[32];
sprintf(offset,"#%d",offseti);
new_temp(temp);
add_code(5,temp,":=",left,"+",offset);
sprintf(result,"*%s",temp);
if(option==0)
strcpy(place,result);
else
add_code(3,place,":=",result);
}
}
void get_location(Node*h,char* place)
{
if(h->child_count==1)
{
val_d* va=find_value(h->child[0]->name);
if(va->val_type->kind==_struct)
current_struct=va->val_type;
else{
array_def_list* temp = va->val_type->def.a;
while(temp->next != NULL){
temp = temp->next;
}
current_size = temp->size_of_each;
}
for(int i=0;i<current_func->parameter_count;i++)
if(current_func->parameter_list[i]==va)
{
struct nlist *dic = lookup(h->child[0]->name);
if(dic == NULL)
dic = install(h->child[0]->name, dic_temp());
strcpy(place,dic->defn);
return;
}
struct nlist *dic = lookup(h->child[0]->name);
if(dic == NULL)
dic = install(h->child[0]->name, dic_temp());
sprintf(place,"&%s",dic->defn);
}
else if(h->child_count==4 && h->child[1]->type==_LB)
{
char temp1[32],temp2[32];
get_location(h->child[0],temp1);
save();
translate_exp(h->child[2],temp2,0);
load();
if(temp2[0]=='#'&& temp2[1]=='0')
{
strcpy(place,temp1);
}
else if(temp2[0]=='#')
{
int i=strtol(&temp2[1],NULL,10);
char temp[32],tempi[32];
new_temp(temp);
sprintf(tempi,"#%d",i*current_size);
add_code(5,temp,":=",temp1,"+",tempi);
strcpy(place,temp);
}
else
{
char temp[32],tempi[32],num[32];
sprintf(num,"#%d",current_size);
new_temp(temp);
new_temp(tempi);
add_code(5,tempi,":=",temp2,"*",num);
add_code(5,temp,":=",temp1,"+",tempi);
strcpy(place,temp);
}
}
else
{
close_opt();
char left[32];
get_location(h->child[0],left);
int offseti=struct_get_offset(current_struct,h->child[2]->name);
char offset[32];
sprintf(offset,"#%d",offseti);
new_temp(place);
add_code(5,place,":=",left,"+",offset);
val_d* va=find_value(h->child[2]->name);
if(va->kind==USER_DEFINED)
{
if(va->val_type->kind==_array)
current_size=va->val_type->def.a->size_of_each;
if(va->val_type->kind==_struct)
current_struct=va->val_type;
else if(va->val_type->def.a->kind==USER_DEFINED)
current_struct=va->val_type->def.a->val_type;
}
}
}
void translate_cond(Node*h,char* label_true,char* label_false)
{
if(h->child[0]->type==_NOT)
return translate_cond(h->child[1],label_false,label_true);
if(h->child_count==3 && h->child[1]->type==_RELOP)
{
char temp1[32],temp2[32];
translate_exp(h->child[0],temp1,0);
translate_exp(h->child[2],temp2,0);
if(temp1[0]=='#' && temp2[0]=='#')
{
int i1=strtol(&temp1[1],NULL,10);
int i2=strtol(&temp2[1],NULL,10);
if(strcmp(h->child[1]->name,">")==0)
{
if(i1>i2)
add_code(2,"GOTO",label_true);
else
add_code(2,"GOTO",label_false);
}
if(strcmp(h->child[1]->name,"<")==0)
{
if(i1<i2)
add_code(2,"GOTO",label_true);
else
add_code(2,"GOTO",label_false);
}
if(strcmp(h->child[1]->name,">=")==0)
{
if(i1>=i2)
add_code(2,"GOTO",label_true);
else
add_code(2,"GOTO",label_false);
}
if(strcmp(h->child[1]->name,"<=")==0)
{
if(i1<=i2)
add_code(2,"GOTO",label_true);
else
add_code(2,"GOTO",label_false);
}
if(strcmp(h->child[1]->name,"==")==0)
{
if(i1==i2)
add_code(2,"GOTO",label_true);
else
add_code(2,"GOTO",label_false);
}
if(strcmp(h->child[1]->name,"!=")==0)
{
if(i1!=i2)
add_code(2,"GOTO",label_true);
else
add_code(2,"GOTO",label_false);
}
}
else
{
add_code(6,"IF",temp1,h->child[1]->name,temp2,"GOTO",label_true);
add_code(2,"GOTO",label_false);
}
}
else if(h->child_count==3 && h->child[1]->type==_AND)
{
char label[32];
new_label(label);
translate_cond(h->child[0],label,label_false);
add_code(3,"LABEL",label,":");
translate_cond(h->child[2],label_true,label_false);
}
else if(h->child_count==3 && h->child[1]->type==_OR)
{
char label[32];
new_label(label);
translate_cond(h->child[0],label_true,label);
add_code(3,"LABEL",label,":");
translate_cond(h->child[2],label_true,label_false);
}
else
{
char temp[32];
translate_exp(h,temp,0);
if(temp[0]=='#')
{
if(temp[1]=='0')
add_code(2,"GOTO",label_false);
else
add_code(2,"GOTO",label_true);
}
else
{
add_code(6,"IF",temp,"!=","#0","GOTO",label_true);
add_code(2,"GOTO",label_false);
}
}
}
void translate_args(Node* h,val_d** args,int count)
{
if(h->child_count==3)
translate_args(h->child[2],args,count+1);
if(args[count]->kind!=USER_DEFINED)
{
char temp[32];
translate_exp(h->child[0],temp,0);
add_code(2,"ARG",temp);
}
else if(args[count]->val_type->kind==_struct)
{
char temp[32];
get_location(h->child[0],temp);
add_code(2,"ARG",temp);
}
else
{
char temp[32];
translate_exp(h->child[0],temp,0);
add_code(2,"ARG",temp);
}
}