/
func (2).c
1233 lines (1070 loc) · 38.5 KB
/
func (2).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
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "stdio.h"
#include "syntax.tab.h"
#include <stdlib.h>
#include <string.h>
typedef struct Type_* Type;
typedef struct FieldList_* FieldList;
extern FILE* yyin;
extern int tag;
extern int flag;
struct TreeNode{
char name[20];
int lineno;
int depth;
int value_int;//int型数的值
char value_str[20];//id的名字或者type类型
int type;//判断是否为空
/*一个节点的操作源,-1代表不关注,0代表基本变量定义,1代表结构体定义;2代表结构体成员定义,3代表函数参数定义,4代表函数体定义,5代表函数体声明,6代表使用变量,7代表函数调用,
*/
int srcop;
Type real_type;//用来数组确定真实属性
Type return_type;//综合属性,记录return返回类型
Type syn_type;//综合属性,记录总体类型
struct TreeNode* neighbours;
struct TreeNode* children;
};
struct Type_
{
enum { basic, array, structure, function, variable } kind;
union
{
// 基本类型
int basic;
// 数组类型信息包括元素类型与数组大小构成
struct { Type elem; int size; } array;
// 结构体类型信息是一个链表
FieldList structure;
FieldList function;
FieldList variable;
} u;
};
struct FieldList_
{
char* name; // 域的名字
Type type; // 域的类型
int func_dec; //函数是否被定义 0未定义,1定义
int func_lineno;
char inter_name[20];
FieldList tail; // 下一个域
};
FieldList pFunc;
typedef struct Operand_* Operand;
struct Operand_ {
enum { VARIABLE, CONSTANT, CONSTANT_FLOAT, ADDRESS, REFERENCE, GOTOOP, LABELOP, RETURNOP} kind;
union {
char ID[20];//这个有待考虑
int value;
//if操作数
//其他
} u;
};
struct ArgList{
Operand op;
struct ArgList* next;
};
struct InterCode
{
enum { ASSIGN_, ADD_, SUB_, MUL_, DIV_, MINUS_, IF_, GOTO_, LABEL_, RETURN_, READ_, WRITE_, ARG_, CALL_, DEC_, FUNCTION_, PARAM_, NONE_} kind;
union {
struct { Operand right, left; } assign;
struct { Operand result, op1, op2; } binop;
struct { Operand result, op1; } sinop;
struct { Operand op1, op2, label; char op_type[20]; } ifcode;
struct { Operand label; } gotocode;//GOTO label
struct { Operand label; } labelcode;//LABEL label
struct { Operand r; } returncode;
struct { Operand rd; } read;
struct { Operand wr; } write;
struct { Operand argument; } arg;
struct { Operand left, function; } call;
struct { Operand space; int size; } dec;
struct { char func[20]; } function;
struct { Operand op; } param;
//其他
} u;
} ;
struct InterCodes {
struct InterCode code;
struct InterCodes *prev, *next;
};
struct InterCodes* translate_Exp(struct TreeNode* Exp, Operand place);
struct InterCodes* translate_Stmt(struct TreeNode* Stmt);
struct InterCodes* translate_Cond(struct TreeNode* Exp, Operand label_true, Operand label_false);
struct InterCodes* translate_Args(struct TreeNode* Args, struct ArgList* arg_list);
struct InterCodes* translate_ExtDecList(struct TreeNode* ExtDecList);
struct InterCodes* translate_VarDec(struct TreeNode* VarDec);
struct InterCodes* translate_Dec(struct TreeNode* Dec);
struct InterCodes* translate_FunDec(struct TreeNode* FunDec);
struct InterCodes* translate_CompSt(struct TreeNode* CompSt);
struct InterCodes* translate_StmtList(struct TreeNode* StmtList);
struct InterCodes* translate_DefList(struct TreeNode* DefList);
struct InterCodes* translate_Def(struct TreeNode* Def);
struct InterCodes* translate_Defstruct (struct TreeNode* Def);
struct InterCodes* translate_DecList(struct TreeNode* DecList);
struct InterCodes* translate_ExtDefList(struct TreeNode* ExtDefList);
struct InterCodes* translate_ExtDef(struct TreeNode* ExtDef);
Operand new_temp(){
static int used_no = 0;
Operand newop = (Operand)malloc(sizeof(struct Operand_));
memset(newop,0,sizeof(struct Operand_));
sprintf(newop->u.ID,"t%d",used_no);
used_no++;
return newop;
}
Operand new_label(){
static int used_label = 1;
Operand newLabel = (Operand)malloc(sizeof(struct Operand_));
memset(newLabel,0,sizeof(struct Operand_));
sprintf(newLabel->u.ID,"t%d",used_label);
used_label++;
return newLabel;
}
struct InterCodes* GenerateInterCode(struct TreeNode* root){
return translate_ExtDefList(root->children);
}
//符号表是全局变量
struct InterCodes* translate_Exp(struct TreeNode* Exp, Operand place){
if (strcmp(Exp->children->name, "INT")){
printf("Exp - INT\n");
if(place != NULL){
FieldList item = (FieldList)malloc(sizeof(struct FieldList_));
lookupTable(Exp->children->value_str,&item,0);
place->kind = CONSTANT;
place->u.value = Exp->children->value_int;
struct InterCodes* temp = (struct InterCodes*)malloc(sizeof(struct InterCodes));
temp->code.kind = NONE_;
return temp;
}
else{
struct InterCodes* temp = (struct InterCodes*)malloc(sizeof(struct InterCodes));
temp->code.kind = NONE_;
return temp;
}
}
if (strcmp(Exp->children->name, "ID")){
if(Exp->children->neighbours == NULL){
printf("Exp - ID\n");
if(place != NULL){
FieldList item = (FieldList)malloc(sizeof(struct FieldList_));
lookupTable(Exp->children->value_str,&item,0);
place->kind = VARIABLE;
strcpy(place->u.ID, item->inter_name);
struct InterCodes* temp = (struct InterCodes*)malloc(sizeof(struct InterCodes));
temp->code.kind = NONE_;
return temp;
}
else{
struct InterCodes* temp = (struct InterCodes*)malloc(sizeof(struct InterCodes));
temp->code.kind = NONE_;
return temp;
}
}
if(strcmp(Exp->children->neighbours->name, "DOT")){
printf("Exp - Exp DOT ID\n");
//Operand t1 = new_temp();
//struct InterCodes* code1 = translate_Exp(Exp->children->neighbours->neighbours, t1);
FieldList item = (FieldList)malloc(sizeof(struct FieldList_));
int i = lookupTable(Exp->children->neighbours->neighbours->value_str,&item,0);
switch(i){
case 0:{
Operand t1 = new_temp();
struct InterCodes* code1 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
code1->code.kind = ADD_;
code1->code.u.binop.result = t1;
code1->code.u.binop.op1 = (Operand)malloc(sizeof(struct Operand_));
code1->code.u.binop.op1->kind = VARIABLE;
strcpy(code1->code.u.binop.op1->u.ID, item->inter_name);
code1->code.u.binop.op2 = (Operand)malloc(sizeof(struct Operand_));
code1->code.u.binop.op2->kind = CONSTANT;
code1->code.u.binop.op2->u.value = index_size(item, Exp->children->value_str);
place->kind = REFERENCE;
strcpy(place->u.ID, t1->u.ID);
return code1;
//break;
}
case 1:{
Operand t1 = new_temp();
Operand t2 = new_temp();
struct InterCodes* code1 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
code1->code.kind = ASSIGN_;
code1->code.u.assign.left = t1;
code1->code.u.assign.right = (Operand)malloc(sizeof(struct Operand_));
code1->code.u.assign.right->kind = ADDRESS;
strcpy(code1->code.u.assign.right->u.ID, item->inter_name);
struct InterCodes* code2 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
code2->code.kind = ADD_;
code2->code.u.binop.result = t2;
code2->code.u.binop.op1 = t1;
code2->code.u.binop.op2 = (Operand)malloc(sizeof(struct Operand_));
code2->code.u.binop.op2->kind = CONSTANT;
code2->code.u.binop.op2->u.value = index_size(item, Exp->children->value_str);
place->kind = REFERENCE;
strcpy(place->u.ID, t2->u.ID);
code1->next = code2;
code2->prev = code1;
return code1;
}
}
}
}
if (strcmp(Exp->children->name, "Exp")){
if (strcmp(Exp->children->neighbours->name, "ASSIGNOP")){
if (strcmp(Exp->children->neighbours->neighbours->children->name, "ID")){
printf("Exp - Exp ASSIGNOP Exp\n");
if(place != NULL){
Operand t1 = new_temp();
FieldList item = (FieldList)malloc(sizeof(struct FieldList_));
lookupTable(Exp->children->neighbours->neighbours->children->value_str,&item,0);
struct InterCodes* code1 = translate_Exp(Exp->children, t1);
struct InterCodes* code2 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
code2->code.kind = ASSIGN_;
code2->code.u.assign.left = (Operand)malloc(sizeof(struct Operand_));
code2->code.u.assign.left->kind = VARIABLE;
strcpy(code2->code.u.assign.left->u.ID, item->inter_name);
code2->code.u.assign.right = t1;
/*struct InterCodes* code3 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
code3->code.kind = ASSIGN;
code3->code.u.assign.left = (Operand)malloc(sizeof(struct Operand_));
code3->code.u.assign.left->kind = VARIABLE;
strcpy(code3->code.u.assign.left->u.ID, place->u.ID);
code3->code.u.assign.right = (Operand)malloc(sizeof(struct Operand_));
code3->code.u.assign.right->kind = VARIABLE;
strcpy(code3->code.u.assign.right->u.ID, Exp->children->neighbours->neighbours->children->inter_name);*/
place->kind = VARIABLE;
strcpy(place->u.ID, item->inter_name);
struct InterCodes* p = code1;
while(p->next != NULL)
p = p->next;
p->next = code2;
code2->prev = p;
code2->next = NULL;
return code1;
}
else{
Operand t1 = new_temp();
FieldList item = (FieldList)malloc(sizeof(struct FieldList_));
lookupTable(Exp->children->neighbours->neighbours->children->value_str,&item,0);
struct InterCodes* code1 = translate_Exp(Exp->children, t1);
struct InterCodes* code2 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
code2->code.kind = ASSIGN_;
code2->code.u.assign.left = (Operand)malloc(sizeof(struct Operand_));
code2->code.u.assign.left->kind = VARIABLE;
strcpy(code2->code.u.assign.left->u.ID, item->inter_name);
code2->code.u.assign.right = t1;
struct InterCodes* p = code1;
while(p->next != NULL)
p = p->next;
p->next = code2;
code2->prev = p;
code2->next = NULL;
return code1;
}
}
}
if(strcmp(Exp->children->neighbours->name, "PLUS")){
printf("Exp - Exp PLUS Exp\n");
if(place != NULL){
Operand t1 = new_temp();
Operand t2 = new_temp();
struct InterCodes* code1 = translate_Exp(Exp->children->neighbours->neighbours, t1);
struct InterCodes* code2 = translate_Exp(Exp->children, t2);
struct InterCodes* code3 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
code3->code.kind = ADD_;
code3->code.u.binop.result = (Operand)malloc(sizeof(struct Operand_));
code3->code.u.binop.result->kind = VARIABLE;
strcpy(code3->code.u.binop.result->u.ID, place->u.ID);
code3->code.u.binop.op1 = t1;
code3->code.u.binop.op2 = t2;
struct InterCodes* p = code1;
while(p->next != NULL)
p = p->next;
p->next = code2;
code2->prev = p;
p = code2;
while(p->next != NULL)
p = p->next;
p->next = code3;
code3->prev = p;
code3->next = NULL;
return code1;
}
else{
Operand t1 = new_temp();
Operand t2 = new_temp();
struct InterCodes* code1 = translate_Exp(Exp->children->neighbours->neighbours, t1);
struct InterCodes* code2 = translate_Exp(Exp->children, t2);
struct InterCodes* p = code1;
while(p->next != NULL)
p = p->next;
p->next = code2;
code2->prev = p;
code2->next = NULL;
return code1;
}
}
if(strcmp(Exp->children->neighbours->name, "MINUS")){
printf("Exp - Exp MINUS Exp\n");
if(place != NULL){
if(Exp->children->neighbours->neighbours != NULL){
Operand t1 = new_temp();
Operand t2 = new_temp();
struct InterCodes* code1 = translate_Exp(Exp->children->neighbours->neighbours, t1);
struct InterCodes* code2 = translate_Exp(Exp->children, t2);
struct InterCodes* code3 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
code3->code.kind = SUB_;
code3->code.u.binop.result = (Operand)malloc(sizeof(struct Operand_));
code3->code.u.binop.result->kind = VARIABLE;
strcpy(code3->code.u.binop.result->u.ID, place->u.ID);
code3->code.u.binop.op1 = t1;
code3->code.u.binop.op2 = t2;
struct InterCodes* p = code1;
while(p->next != NULL)
p = p->next;
p->next = code2;
code2->prev = p;
p = code2;
while(p->next != NULL)
p = p->next;
p->next = code3;
code3->prev = p;
code3->next = NULL;
return code1;
}
else{
Operand t1 = new_temp();
struct InterCodes* code1 = translate_Exp(Exp->children, t1);
struct InterCodes* code2 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
code2->code.kind = SUB_;
code2->code.u.binop.result = (Operand)malloc(sizeof(struct Operand_));
code2->code.u.binop.result->kind = VARIABLE;
strcpy(code2->code.u.binop.result->u.ID, place->u.ID);
code2->code.u.binop.op1 = (Operand)malloc(sizeof(struct Operand_));
code2->code.u.binop.op1->kind = CONSTANT;
code2->code.u.binop.op1->u.value = 0;
code2->code.u.binop.op2 = t1;
struct InterCodes* p = code1;
while(p->next != NULL)
p = p->next;
p->next = code2;
code2->prev = p;
code2->next = NULL;
return code1;
}
}
else{
if(Exp->children->neighbours->neighbours != NULL){
Operand t1 = new_temp();
Operand t2 = new_temp();
struct InterCodes* code1 = translate_Exp(Exp->children->neighbours->neighbours, t1);
struct InterCodes* code2 = translate_Exp(Exp->children, t2);
struct InterCodes* p = code1;
while(p->next != NULL)
p = p->next;
p->next = code2;
code2->prev = p;
code2->next = NULL;
return code1;
}
else{
Operand t1 = new_temp();
struct InterCodes* code1 = translate_Exp(Exp->children, t1);
return code1;
}
}
}
if(strcmp(Exp->children->neighbours->name, "STAR")){
printf("Exp - Exp STAR Exp\n");
if(place != NULL){
Operand t1 = new_temp();
Operand t2 = new_temp();
struct InterCodes* code1 = translate_Exp(Exp->children->neighbours->neighbours, t1);
struct InterCodes* code2 = translate_Exp(Exp->children, t2);
struct InterCodes* code3 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
code3->code.kind = MUL_;
code3->code.u.binop.result = (Operand)malloc(sizeof(struct Operand_));
code3->code.u.binop.result->kind = VARIABLE;
strcpy(code3->code.u.binop.result->u.ID, place->u.ID);
code3->code.u.binop.op1 = t1;
code3->code.u.binop.op2 = t2;
struct InterCodes* p = code1;
while(p->next != NULL)
p = p->next;
p->next = code2;
code2->prev = p;
p = code2;
while(p->next != NULL)
p = p->next;
p->next = code3;
code3->prev = p;
code3->next = NULL;
return code1;
}
else{
Operand t1 = new_temp();
Operand t2 = new_temp();
struct InterCodes* code1 = translate_Exp(Exp->children->neighbours->neighbours, t1);
struct InterCodes* code2 = translate_Exp(Exp->children, t2);
struct InterCodes* p = code1;
while(p->next != NULL)
p = p->next;
p->next = code2;
code2->prev = p;
code2->next = NULL;
return code1;
}
}
if(strcmp(Exp->children->neighbours->name, "DIV")){
printf("Exp - Exp DIV Exp\n");
if(place != NULL){
Operand t1 = new_temp();
Operand t2 = new_temp();
struct InterCodes* code1 = translate_Exp(Exp->children->neighbours->neighbours, t1);
struct InterCodes* code2 = translate_Exp(Exp->children, t2);
struct InterCodes* code3 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
code3->code.kind = DIV_;
code3->code.u.binop.result = (Operand)malloc(sizeof(struct Operand_));
code3->code.u.binop.result->kind = VARIABLE;
strcpy(code3->code.u.binop.result->u.ID, place->u.ID);
code3->code.u.binop.op1 = t1;
code3->code.u.binop.op2 = t2;
struct InterCodes* p = code1;
while(p->next != NULL)
p = p->next;
p->next = code2;
code2->prev = p;
p = code2;
while(p->next != NULL)
p = p->next;
p->next = code3;
code3->prev = p;
code3->next = NULL;
return code1;
}
else{
Operand t1 = new_temp();
Operand t2 = new_temp();
struct InterCodes* code1 = translate_Exp(Exp->children->neighbours->neighbours, t1);
struct InterCodes* code2 = translate_Exp(Exp->children, t2);
struct InterCodes* p = code1;
while(p->next != NULL)
p = p->next;
p->next = code2;
code2->prev = p;
code2->next = NULL;
return code1;
}
}
if(strcmp(Exp->children->neighbours->name, "RELOP") || strcmp(Exp->children->neighbours->name, "NOT") || strcmp(Exp->children->neighbours->name, "AND") || strcmp(Exp->children->neighbours->name, "OR")){
printf("Exp - Exp Cond Exp\n");
if(place != NULL){
Operand label1 = new_label();
Operand label2 = new_label();
struct InterCodes* code1 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
code1->code.kind = ASSIGN_;
code1->code.u.assign.left = (Operand)malloc(sizeof(struct Operand_));
code1->code.u.assign.left->kind = VARIABLE;
strcpy(code1->code.u.assign.left->u.ID, place->u.ID);
code1->code.u.assign.right = (Operand)malloc(sizeof(struct Operand_));
code1->code.u.assign.right->kind = CONSTANT;
code1->code.u.assign.right->u.value = 0;
struct InterCodes* code2 = translate_Cond(Exp, label1, label2);
struct InterCodes* code3 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
code3->code.kind = LABEL_;
code3->code.u.labelcode.label = label1;
struct InterCodes* code4 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
code4->code.kind = ASSIGN_;
code4->code.u.assign.left = (Operand)malloc(sizeof(struct Operand_));
code4->code.u.assign.left->kind = VARIABLE;
strcpy(code4->code.u.assign.left->u.ID, place->u.ID);
code4->code.u.assign.right = (Operand)malloc(sizeof(struct Operand_));
code4->code.u.assign.right->kind = CONSTANT;
code4->code.u.assign.right->u.value = 1;
struct InterCodes* code5 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
code5->code.kind = LABEL_;
code5->code.u.labelcode.label = label2;
code1->next = code2;
code2->prev = code1;
struct InterCodes* p = code2;
while(p->next != NULL)
p = p->next;
p->next = code3;
code3->prev = p;
code3->next = code4;
code4->prev = code3;
code4->next = code5;
code5->prev = code4;
code5->next = NULL;
return code1;
}
}
}
if (strcmp(Exp->children->neighbours->name, "LP")){
printf("Exp - ID LP RP\n");
if(strcmp(Exp->children->neighbours->neighbours->value_str, "read") == 0){
struct InterCodes* code1 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
code1->code.kind = READ_;
code1->code.u.read.rd = place;
return code1;
}
else{
FieldList item = (FieldList)malloc(sizeof(struct FieldList_));
lookupTable(Exp->children->neighbours->neighbours->value_str,&item,1);//查函数
struct InterCodes* code1 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
code1->code.kind = CALL_;
code1->code.u.call.left = (Operand)malloc(sizeof(struct Operand_));
code1->code.u.call.left->kind = VARIABLE;
strcpy(code1->code.u.assign.left->u.ID, place->u.ID);
code1->code.u.call.function = (Operand)malloc(sizeof(struct Operand_));
code1->code.u.call.function->kind = VARIABLE;
strcpy(code1->code.u.call.function->u.ID, item->name);
return code1;
}
}
if(strcmp(Exp->children->neighbours->name, "Args")){
printf("Exp - ID LP Args RP\n");
struct ArgList* arg_list = NULL;
struct InterCodes* code1 = translate_Args(Exp->children->neighbours, arg_list);
if(strcpy(Exp->children->neighbours->neighbours->neighbours->value_str, "write") == 0){
struct InterCodes* code2 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
code2->code.kind = WRITE_;
code2->code.u.write.wr = arg_list->op;
struct InterCodes* p = code1;
while(p->next != NULL)
p = p->next;
p->next = code2;
code2->prev = p;
code2->next = NULL;
return code1;
}
else{
FieldList item = (FieldList)malloc(sizeof(struct FieldList_));
lookupTable(Exp->children->neighbours->neighbours->neighbours->value_str,&item,0);
struct ArgList* q = arg_list;
struct InterCodes* code2 = NULL;
while(q != NULL){
struct InterCodes* code3 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
code3->code.kind = ARG_;
code3->code.u.arg.argument = q->op;
if(code2 == NULL){
code2 = code3;
}
else{
struct InterCodes* p = code2;
while(p->next != NULL)
p = p->next;
p->next = code3;
code3->prev = p;
}
q = q->next;
}
struct InterCodes* code4 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
code4->code.kind = CALL_;
code4->code.u.call.left = (Operand)malloc(sizeof(struct Operand_));
code4->code.u.call.left->kind = VARIABLE;
strcpy(code4->code.u.assign.left->u.ID, place->u.ID);
code4->code.u.call.function = (Operand)malloc(sizeof(struct Operand_));
code4->code.u.call.function->kind = VARIABLE;
strcpy(code4->code.u.call.function->u.ID, item->name);
struct InterCodes* p = code1;
while(p->next != NULL)
p = p->next;
p->next = code2;
code2->prev = p;
p = code2;
while(p->next != NULL)
p = p->next;
p->next = code4;
code4->prev = p;
code4->next = NULL;
return code1;
}
}
if (strcmp(Exp->children->name, "RB") == 0){
printf("Exp - Exp LB Exp RB\n");
FieldList item = (FieldList)malloc(sizeof(struct FieldList_));
lookupTable(Exp->children->neighbours->neighbours->neighbours->children->value_str,&item,0);
Operand t1 = new_temp();
Operand t2 = new_temp();
Operand t3 = new_temp();
struct InterCodes* code1 = translate_Exp(Exp->children->neighbours, t1);
struct InterCodes* code2 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
code2->code.kind = MUL_;
code2->code.u.binop.result = t2;
code2->code.u.binop.op1 = t1;
code2->code.u.binop.op2 = (Operand)malloc(sizeof(struct Operand_));
code2->code.u.binop.result->kind = CONSTANT;
code2->code.u.binop.result->u.value = 4;
struct InterCodes* code3 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
code3->code.kind = ADD_;
code3->code.u.binop.result = t3;
code3->code.u.binop.op1 = (Operand)malloc(sizeof(struct Operand_));
code3->code.u.binop.op1->kind = ADDRESS;
strcpy(code3->code.u.binop.op1->u.ID, item->inter_name);
code3->code.u.binop.op2 = t2;
//place = t4;
place->kind = REFERENCE;
strcpy(place->u.ID, t3->u.ID);
struct InterCodes* p = code1;
while(p->next != NULL)
p = p->next;
p->next = code2;
code2->prev = p;
code2->next = code3;
code3->prev = code2;
code3->next = NULL;
return code1;
}
}
struct InterCodes* translate_Stmt(struct TreeNode* Stmt){
if (strcmp(Stmt->children->name, "SEMI") == 0){
if(Stmt->children->neighbours->neighbours == NULL){
printf("Stmt - Stmt SEMI\n");
return translate_Exp(Stmt->children->neighbours, NULL);
}
else{
printf("Stmt - RETURN Stmt SEMI\n");
Operand t1 = new_temp();
struct InterCodes* code1 = translate_Exp(Stmt->children->neighbours, t1);
struct InterCodes* code2 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
code2->code.kind = RETURN_;
code2->code.u.returncode.r = t1;
struct InterCodes* p = code1;
while(p->next != NULL)
p = p->next;
p->next = code2;
code2->prev = p;
code2->next = NULL;
return code1;
}
}
if(strcmp(Stmt->children->name, "CompSt") == 0){
printf("Stmt - CompSt\n");
return translate_CompSt(Stmt->children);
}
if(strcmp(Stmt->children->name, "Stmt") == 0){
if(strcmp(Stmt->children->neighbours->neighbours->neighbours->neighbours->name, "IF") == 0){
printf("Stmt - IF LP Exp RP Stmt\n");
Operand label1 = new_label();
Operand label2 = new_label();
struct InterCodes* code1 = translate_Cond(Stmt->children->neighbours->neighbours, label1, label2);
struct InterCodes* code2 = translate_Stmt(Stmt->children);
struct InterCodes* code3 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
code3->code.kind = LABEL_;
code3->code.u.labelcode.label = label1;
struct InterCodes* code4 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
code4->code.kind = LABEL_;
code4->code.u.labelcode.label = label2;
struct InterCodes* p = code1;
while(p->next != NULL)
p = p->next;
p->next = code3;
code3->prev = p;
code3->next = code2;
p = code2;
while(p->next != NULL)
p = p->next;
p->next = code4;
code4->prev = p;
code4->next = NULL;
return code1;
}
if(strcmp(Stmt->children->neighbours->neighbours->neighbours->neighbours->name, "WHILE") == 0){
printf("Stmt - WHILE LP Exp RP Stmt\n");
Operand label1 = new_label();
Operand label2 = new_label();
Operand label3 = new_label();
struct InterCodes* code1 = translate_Cond(Stmt->children->neighbours->neighbours, label2, label3);
struct InterCodes* code2 = translate_Stmt(Stmt->children->neighbours->neighbours);
struct InterCodes* code3 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
code3->code.kind = LABEL_;
code3->code.u.labelcode.label = label1;
struct InterCodes* code4 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
code4->code.kind = LABEL_;
code4->code.u.labelcode.label = label2;
struct InterCodes* code5 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
code5->code.kind = LABEL_;
code5->code.u.labelcode.label = label3;
struct InterCodes* code6 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
code6->code.kind = GOTO_;
code6->code.u.gotocode.label = label3;
code3->next = code1;
code1->prev = code3;
struct InterCodes* p = code1;
while(p->next != NULL)
p = p->next;
p->next = code4;
code4->prev = p;
code4->next = code2;
p = code2;
while(p->next != NULL)
p = p->next;
p->next = code6;
code6->prev = p;
code6->next = code5;
code5->prev = code6;
code5->next = NULL;
return code3;
}
if(strcmp(Stmt->children->neighbours->name, "ELSE") == 0){
printf("Stmt - IF LP Exp RP Stmt ELSE Stmt\n");
Operand label1 = new_label();
Operand label2 = new_label();
Operand label3 = new_label();
struct InterCodes* code1 = translate_Cond(Stmt->children->neighbours->neighbours, label1, label2);
struct InterCodes* code2 = translate_Stmt(Stmt->children->neighbours->neighbours);
struct InterCodes* code3 = translate_Stmt(Stmt->children);
struct InterCodes* code4 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
code4->code.kind = LABEL_;
code4->code.u.labelcode.label = label1;
struct InterCodes* code5 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
code5->code.kind = LABEL_;
code5->code.u.labelcode.label = label2;
struct InterCodes* code6 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
code6->code.kind = LABEL_;
code6->code.u.labelcode.label = label3;
struct InterCodes* code7 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
code7->code.kind = GOTO_;
code7->code.u.gotocode.label = label3;
struct InterCodes* p = code1;
while(p->next != NULL)
p = p->next;
p->next = code4;
code4->prev = p;
code4->next = code2;
p = code2;
while(p->next != NULL)
p = p->next;
p->next = code7;
code7->prev = p;
code7->next = code5;
code5->prev = code7;
code5->next = code3;
p = code3;
while(p->next != NULL)
p = p->next;
p->next = code6;
code6->prev = p;
code6->next = NULL;
return code1;
}
}
}
struct InterCodes* translate_Cond(struct TreeNode* Exp, Operand label_true, Operand label_false){
if(strcmp(Exp->children->neighbours->name, "RELOP") == 0){
printf("Exp - Exp RELOP Exp\n");
Operand t1 = new_temp();
Operand t2 = new_temp();
struct InterCodes* code1 = translate_Exp(Exp->children->neighbours->neighbours, t1);
struct InterCodes* code2 = translate_Exp(Exp->children, t2);
struct InterCodes* code3 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
code3->code.kind = IF_;
code3->code.u.ifcode.op1 = t1;
code3->code.u.ifcode.op2 = t2;
code3->code.u.ifcode.label = label_true;
strcpy(code3->code.u.ifcode.op_type, Exp->children->neighbours->value_str);
struct InterCodes* code4 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
code4->code.kind = GOTO_;
code4->code.u.gotocode.label = label_false;
struct InterCodes* p = code1;
while(p->next != NULL)
p = p->next;
p->next = code2;
code2->prev = p;
p = code2;
while(p->next != NULL)
p = p->next;
p->next = code3;
code3->prev = p;
code3->next = code4;
code4->prev = code3;
code4->next = NULL;
return code1;
}
if(strcmp(Exp->children->neighbours->name, "NOT") == 0){
printf("Exp - NOT Exp\n");
return translate_Cond(Exp->children, label_false, label_true);
}
if(strcmp(Exp->children->neighbours->name, "AND") == 0){
printf("Exp - Exp AND Exp\n");
Operand label1 = new_label();
struct InterCodes* code1 = translate_Cond(Exp->children->neighbours->neighbours, label1, label_false);
struct InterCodes* code2 = translate_Cond(Exp->children, label_true, label_false);
struct InterCodes* code3 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
code3->code.kind = LABEL_;
code3->code.u.labelcode.label = label1;
struct InterCodes* p = code1;
while(p->next != NULL)
p = p->next;
p->next = code3;
code3->prev = p;
code3->next = code2;
code2->prev = code3;
return code1;
}
if(strcmp(Exp->children->neighbours->name, "OR") == 0){
printf("Exp - Exp OR Exp\n");
Operand label1 = new_label();
struct InterCodes* code1 = translate_Cond(Exp->children->neighbours->neighbours, label_true, label1);
struct InterCodes* code2 = translate_Cond(Exp->children, label_true, label_false);
struct InterCodes* code3 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
code3->code.kind = LABEL_;
code3->code.u.labelcode.label = label1;
struct InterCodes* p = code1;
while(p->next != NULL)
p = p->next;
p->next = code3;
code3->prev = p;
code3->next = code2;
code2->prev = code3;
return code1;
}
else{
printf("Exp - ELSE\n");
Operand t1 = new_temp();
struct InterCodes* code1 = translate_Exp(Exp, t1);
struct InterCodes* code2 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
code2->code.kind = IF_;
code2->code.u.ifcode.op1 = t1;
code2->code.u.ifcode.op2 = (Operand)malloc(sizeof(struct Operand_));
code2->code.u.ifcode.op2->kind = CONSTANT;
code2->code.u.ifcode.op2->u.value = 0;
code2->code.u.ifcode.label = label_true;
char op[2] = {'!', '='};
strcpy(code2->code.u.ifcode.op_type, op);
struct InterCodes* code3 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
code3->code.kind = GOTO_;
code3->code.u.gotocode.label = label_false;
struct InterCodes* p = code1;
while(p->next != NULL)
p = p->next;
p->next = code2;
code2->prev = p;
code2->next = code3;
code3->prev = code2;
code3->next = NULL;
return code1;
}
}
struct InterCodes* translate_Args(struct TreeNode* Args, struct ArgList* arg_list){
if(strcmp(Args->children->name, "Exp") == 0){
printf("Args - Exp\n");
Operand t1 = new_temp();
struct InterCodes* code1 = translate_Exp(Args->children, t1);
struct ArgList* temp = (struct ArgList*)malloc(sizeof(struct ArgList));
temp->op = t1;
temp->next = arg_list;
arg_list = temp;
return code1;
}
if(strcmp(Args->children->name, "COMMA") == 0){
printf("Args - Exp COMMA Args\n");
Operand t1 = new_temp();
struct InterCodes* code1 = translate_Exp(Args->children->neighbours->neighbours, t1);
struct ArgList* temp = (struct ArgList*)malloc(sizeof(struct ArgList));
temp->op = t1;
temp->next = arg_list;
arg_list = temp;
struct InterCodes* code2 = translate_Args(Args->children, arg_list);
struct InterCodes* p = code1;
while(p->next != NULL)
p = p->next;
p->next = code2;
code2->prev = p;
code2->next = NULL;
return code1;
}
}