-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathintercode2.c
1964 lines (1855 loc) · 73.8 KB
/
intercode2.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 "intercode.h"
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include "node.h"
#include "symtable.h"
/*
剩下的问题
3. break和continue语句如何与外面的while建立关系
*/
extern int scope;
/*
Exp -> AddExp.
翻译Exp, 结果赋值给place, 返回计算三地址码链表 ok
*/
CodeList translate_Exp(treeNode *Exp, Operand place) {
return translate_AddExp(Exp->child, place); // 直接调用translate_AddExp
}
/*
AddExp -> MulExp | AddExp('+' | '-')MulExp.
翻译AddExp, 结果赋值给place, 返回三地址码链表 ok
*/
CodeList translate_AddExp(treeNode *AddExp, Operand place)
{
if(!strcmp(AddExp->child->name,"MulExp")) {
return translate_MulExp(AddExp->child, place); // 直接调用translate_MulExp,
}
else {
Operand t1 = new_temp();
Operand t2 = new_temp(); // 两个临时变量
treeNode* AddExp_1 = AddExp->child;
treeNode* MulExp = AddExp_1->next->next; // 两边表达式
CodeList code1 = translate_AddExp(AddExp_1,t1);
CodeList code2 = translate_MulExp(MulExp,t2); // 两边表达式翻译结果存在t1 和 t2中
InterCode ic = malloc(sizeof(struct InterCode_)); // 三地址码节点
if(!strcmp(AddExp_1->next->name, "PLUS")) {
ic->kind = IR_PLUS; // 加法类型
}
else { //"MINUS"
ic->kind = IR_MINUS; // 减法类型
}
ic->u.binop.op1 = t1;
ic->u.binop.op2 = t2; // 两个运算数
ic->u.binop.result = place; // 结果存放位置 即place
CodeList code3 = new_CodeList(ic); // 三地址码链表
return join(join(code1,code2),code3); // 返回连接后的三地址码链表
}
}
/*
UnaryExp | MulExp ('*' | '/' | '%') UnaryExp
翻译MulExp, 结果赋值给place, 返回三地址码链表 ok
ok
*/
CodeList translate_MulExp(treeNode *MulExp, Operand place)
{
if(!strcmp(MulExp->child->name,"UnaryExp")) {
return translate_UnaryExp(MulExp->child,place); // 直接调用UnaryExp
}
else {
Operand t1 = new_temp();
Operand t2 = new_temp(); // 两个临时变量
treeNode* MulExp_1 = MulExp->child;
treeNode* UnaryExp = MulExp_1->next->next;
CodeList code1 = translate_MulExp(MulExp_1,t1);
CodeList code2 = translate_UnaryExp(UnaryExp,t2); // 两边表达式
InterCode ic = malloc(sizeof(struct InterCode_)); // 中间代码节点
if(!strcmp(MulExp_1->next->name, "STAR")) {
ic->kind = IR_MUL; // 乘法类型
}
else if(!strcmp(MulExp_1->next->name, "DIV")) {
ic->kind = IR_DIV; // 除法类型
}
else if(!strcmp(MulExp_1->next->name, "PERCENT")) {
ic->kind = IR_MOD; // 模除类型
}
ic->u.binop.op1 = t1;
ic->u.binop.op2 = t2; // 两个运算数
ic->u.binop.result = place; // 存储的目标
CodeList code3 = new_CodeList(ic); // 三地址码链表
return join(join(code1,code2),code3); // 返回合并后的三地址码链表
}
}
/*
Number -> IntConst
计算Number的值, 结果赋给place, 返回三地址码链表 应该ok
*/
CodeList translate_Number(treeNode *Number, Operand place)
{
int val = Number->child->int_val; // 应该是直接用这个int_val吧
fprintf(stderr, "found int val: %d\n", val);
InterCode ic = new_InterCode(IR_ASSIGN); // 赋值类型
ic->u.assign.left = place; // 目标是place
ic->u.assign.right = new_constant(val); // 值就是一个const值吧. const值不用地方保存?
return new_CodeList(ic); // 返回三地址码链表
}
/*
PrimaryExp -> LP Exp RP | LVal | Number
计算PrimaryExp的值, 结果赋给place, 返回三地址码链表 应该ok
*/
CodeList translate_PrimaryExp(treeNode *PrimaryExp,Operand place) {
if(!strcmp(PrimaryExp->child->name, "LP")) {
return translate_Exp(PrimaryExp->child->next,place); // 应该是直接返回即可--优先级是否要处理?
}
else if(!strcmp(PrimaryExp->child->name, "LVal")) { //LVal作右值,传个指针过去,再把指针指向的内容赋给place
return translate_LVal(PrimaryExp->child, place, 0); // 左值解析-有数组-重要
}
else { // !strcpy(PrimaryExp->child->name,"Number")
return translate_Number(PrimaryExp->child,place);
}
}
/*
UnaryExp -> PrimaryExp | ID LP FuncRParams RP | ID LP RP | UnaryOp UnaryExp
计算UnaryExp的值, 结果赋给place, 返回三地址码链表 ok
*/
CodeList translate_UnaryExp(treeNode *UnaryExp, Operand place) {
if(!strcmp(UnaryExp->child->name,"ID")) {
printf("Function call found: %s\n", UnaryExp->child->value);
// 函数调用 UnaryExp -> ID LP FuncRParams RP
if(!strcmp(UnaryExp->child->next->next->name, "FuncRParams")) { // 有实参
ArgList argList = NULL; // 实参链表
CodeList code1 = translate_FuncRParams(UnaryExp->child->next->next,&argList); // 获取实参链表
printf("Parse Rparam done\n");
//无READ WRITE函数
CodeList code2 = NULL;
while(argList != NULL) // 遍历实参链表-获取实参
{
InterCode ic = new_InterCode(IR_ARG); // 实参指令的中间代码
ic->u.op = argList->args;
code2 = join(code2,new_CodeList(ic)); // 中间代码链表
argList = argList->next;
}
InterCode ic = new_InterCode(IR_CALL); // 函数调用中间代码 例如t3 := CALL add
if(place != NULL) {
ic->u.call.result = place; // 函数调用结果给place
}
else {
ic->u.call.result = new_temp(); // 一些NULL的情况还需要多检查一下
}
ic->u.call.func = UnaryExp->child->value; // value应该是他真实的函数名 直接用即可 语法树应该一直都在
CodeList code3 = new_CodeList(ic);
return join(join(code1,code2),code3);
} // ok
else {
// 函数调用 UnaryExp -> ID LP RP
InterCode ic = new_InterCode(IR_CALL);
if(place != NULL) {
ic->u.call.result = place; // 直接给返回值即可
}
else {
ic->u.call.result = new_temp();
}
ic->u.call.func = UnaryExp->child->value;
return new_CodeList(ic);
}
} // ok
// UnaryExp -> UnaryOp UnaryExp 在翻译UnaryOp的情况
if(!strcmp(UnaryExp->child->name,"UnaryOp")) {
// 直接在这里处理掉UnaryOp
if(!strcmp(UnaryExp->child->child->name,"PLUS")) {
// 取正号
return translate_UnaryExp(UnaryExp->child->next,place); // 直接往下翻译即可
}
else if(!strcmp(UnaryExp->child->child->name,"MINUS")) {
// 取负号 用0做减法q
Operand t1 = new_temp();
CodeList code1 = translate_UnaryExp(UnaryExp->child->next,t1); // 后面翻译结果赋值给t1
InterCode ic = new_InterCode(IR_MINUS); // 减法
ic->u.binop.op1 = new_constant(0);
ic->u.binop.op2 = t1;
ic->u.binop.result = place;
CodeList code2 = new_CodeList(ic);
return join(code1,code2);
}
else if(!strcmp(UnaryExp->child->child->name,"NOT")) {
// 这个NOT虽然用在条件表达式中-但在文法里面其实和其他的逻辑运算都是分开的-只和表达式相关
/// 表达式解析not
//Operand label1 = new_label(); // label_true
Operand label2 = new_label(); // label_false
InterCode ic = new_InterCode(IR_ASSIGN);
ic->u.assign.left = place;
ic->u.assign.right = new_constant(0); // 赋值0
CodeList code0 = new_CodeList(ic);
// 条件解析NOT UnaryExp
Operand t1 = new_temp();
CodeList code11 = translate_UnaryExp(UnaryExp->child->next,t1); // 计算求值
InterCode ic1 = new_InterCode(IR_IFGOTO);
ic1->u.if_goto.x = t1;
ic1->u.if_goto.y = new_constant(0);
ic1->u.if_goto.z = label2; // label_true -not-> label_false
ic1->u.if_goto.relop = malloc(3);
strcpy(ic1->u.if_goto.relop, "!=");
CodeList code12 = new_CodeList(ic1);
//CodeList gotofalse = translate_Operand(label1, IR_GOTO); // gotofalse -not-> goto true
CodeList code1 = join(code11,code12);
// 跳转赋值相关
//CodeList code2 = translate_Operand(label1,IR_LABEL);
ic = new_InterCode(IR_ASSIGN);
ic->u.assign.left = place;
ic->u.assign.right = new_constant(1); // 赋值1
//code2 = join(code2, new_CodeList(ic));
CodeList code3 = translate_Operand(label2,IR_LABEL);
return join(join(code0,code1),join(new_CodeList(ic),code3));
}
}
else if(!strcmp(UnaryExp->child->name,"PrimaryExp")) {
//直接返回就行了把 UnaryExp -> PrimaryExp
return translate_PrimaryExp(UnaryExp->child,place);
}
}
/*
Stmt -> LVal '=' Exp ';' | [Exp] ';' | Block
| 'if' '( Cond ')' Stmt [ 'else' Stmt ]
| 'while' '(' Cond ')' Stmt
| 'break' ';' | 'continue' ';'
| 'return' [Exp] ';'
Stmt语句的翻译 缺少break和continue语句 缺少多维数组处理(一维已经完成-重复一下即可)
*/
CodeList translate_Stmt(treeNode* Stmt, Operand label_1, Operand label_3)
{
if(Stmt == NULL)
return NULL;
if(!strcmp(Stmt->child->name,"WHILE")) {
Operand label1 = new_label();
Operand label2 = new_label();
Operand label3 = new_label();
CodeList code1 = translate_Cond(Stmt->child->next->next,label2,label3); // 应该没问题-直接用translate_Cond即可
CodeList code2 = translate_Stmt(Stmt->child->next->next->next->next, label1, label3); //Stmt
CodeList goto1 = translate_Operand(label1, IR_GOTO);
CodeList tmp = join(translate_Operand(label1, IR_LABEL),code1);
tmp = join(tmp,translate_Operand(label2,IR_LABEL));
tmp = join(tmp,code2);
tmp = join(tmp,goto1);
tmp = join(tmp,translate_Operand(label3,IR_LABEL));
return tmp;
// ok
}
else if (!strcmp(Stmt->child->name, "BREAK")) {
return translate_Operand(label_3, IR_GOTO);
}
else if (!strcmp(Stmt->child->name, "CONTINUE")) {
return translate_Operand(label_1, IR_GOTO);
}
else if (!strcmp(Stmt->child->name, "RETURN")) {
// 两种 一个是return Exp; 一个是直接返回 return ;
if (!strcmp(Stmt->child->next->name, "Exp")) {
Operand t1 = new_temp();
CodeList code1 = translate_Exp(Stmt->child->next, t1);
CodeList code2 = translate_Operand(t1, IR_RETURN);
return join(code1, code2);
}
else {
Operand t1 = new_constant(0);
CodeList code = translate_Operand(t1, IR_RETURN);
return code; // RETURN #0 代表void
}
}
else if(!strcmp(Stmt->child->name, "IF")) {
if(Stmt->child->next->next->next->next->next == NULL) { // IF
Operand label1 = new_label();
Operand label2 = new_label();
CodeList code1 = translate_Cond(Stmt->child->next->next,label1,label2);
CodeList code2 = translate_Stmt(Stmt->child->next->next->next->next, label_1, label_3);
CodeList clabel1 = translate_Operand(label1,IR_LABEL);
CodeList clabel2 = translate_Operand(label2,IR_LABEL);
return join(join(join(code1,clabel1),code2),clabel2);
// a > b
// [IF a > b GOTO label1] [GOTO label2] // translate_Cond
// [LABEL label1] [stmt] [LABEL label2] // 后面应该正确-取决于translate_Cond的实现
// a > b && c > d
// [IF a > b GOTO label3] [GOTO label2]
// LABEL label3
// [IF c > d GOTO label1] [GOTO label2]
// [LABEL label1] [stmt] [LABEL label2] // 应该没问题
} // ok
else { // IF ELSE
Operand label1 = new_label();
Operand label2 = new_label();
Operand label3 = new_label();
CodeList code1 = translate_Cond(Stmt->child->next->next,label1,label2);
CodeList code2 = translate_Stmt(Stmt->child->next->next->next->next, label_1, label_3);
CodeList code3 = translate_Stmt(Stmt->child->next->next->next->next->next->next, label_1, label_3);
CodeList clabel1 = translate_Operand(label1,IR_LABEL);
CodeList clabel2 = translate_Operand(label2,IR_LABEL);
CodeList clabel3 = translate_Operand(label3,IR_LABEL);
CodeList goto3 = translate_Operand(label3,IR_GOTO);
CodeList tmp = join(code1,translate_Operand(label1,IR_LABEL));
tmp = join(tmp,code2);
tmp = join(tmp,goto3);
tmp = join(tmp,translate_Operand(label2,IR_LABEL));
tmp = join(tmp,code3);
tmp = join(tmp,translate_Operand(label3,IR_LABEL));
return tmp;
} // ok
}
else if(!strcmp(Stmt->child->name, "Block")) {
return translate_Block(Stmt->child, label_1, label_3, 1);
}
else if(!strcmp(Stmt->child->name,"Exp")) {
// Exp
// exp的话应该要新建一个变量暂存一下的
Operand t1 = new_temp(); // 应该是new temp
return translate_Exp(Stmt->child,t1); //还是要改的
}
else if(!strcmp(Stmt->child->name, "LVal")) {
// Stmt -> LVal ASSIGN EXP; LVal -> ID Exp_rep; Exp_rep -> Exp_rep LB Exp RB | empty
if(Stmt->child->child->next->child == NULL) {
// 没有数组-单纯的变量 第一个Exp_rep的孩子是NULL-根据yacc上语法树构造-说明这个就是只有ID
// Stmt -> LVal ASSIGN EXP; LVal -> ID Exp_rep; Exp_rep -> Exp_rep LB Exp RB | empty
Operand var = op_from_var(Stmt->child->child->value); // 根据ID获取的变量
Operand t1 = new_temp();
CodeList code1 = translate_Exp(Stmt->child->next->next,t1); // exp计算结果
InterCode ic = new_InterCode(IR_ASSIGN); // 变量赋值
ic->u.assign.left = var;
ic->u.assign.right = t1;
CodeList code2 = new_CodeList(ic);
return join(code1,code2); // sysY语言的好像没有文法能够连续给表达式赋值, 例如 a = b = 2; 那就不用解析了
}
else {
//计算LVal 获得地址
Operand t1 = new_temp();
CodeList code3 = translate_LVal(Stmt->child, t1, 1);
// 计算表达式的值
Operand t3 = new_temp();
CodeList code4 = translate_Exp(Stmt->child->next->next,t3); //Exp计算
InterCode ic = new_InterCode(IR_CHANGE_ADDR); // *x := y
ic->u.assign.left = t1;
ic->u.assign.right = t3;
CodeList code5 = new_CodeList(ic); // 这里的赋值语句没有连续赋值的情况-就不返回了
return join(join(code3, code4),code5);
}
}
else //semi
return NULL;
}
/*
Block : LC BlockItem_rep RC
直接往下翻译即可 大致ok
*/
CodeList translate_Block(treeNode *Block, Operand label_1, Operand label_3, int isFuncBlock) {
if(Block == NULL)
return NULL;
if(isFuncBlock) addScope();
CodeList tmp = translate_BlockItem_rep(Block->child->next, label_1, label_3);
if(isFuncBlock) exitScope();
return tmp;
}
/*
BlockItem_rep -> BlockItem_rep BlockItem | empty ok
*/
CodeList translate_BlockItem_rep(treeNode *BlockItem_rep, Operand label_1, Operand label_3) {
if(BlockItem_rep == NULL || BlockItem_rep->child == NULL)
return NULL;
CodeList code1 = translate_BlockItem_rep(BlockItem_rep->child, label_1, label_3);
CodeList code2 = translate_BlockItem(BlockItem_rep->child->next, label_1, label_3);
return join(code1,code2); // 先这样把-到时候再改
}
/*
BlockItem -> Decl | Stmt ok
*/
CodeList translate_BlockItem(treeNode *BlockItem, Operand label_1, Operand label_3) {
if(!strcmp(BlockItem->child->name,"Decl")) {
return translate_Decl(BlockItem->child);
}
else {
// Stmt
return translate_Stmt(BlockItem->child, label_1, label_3);
}
}
/*
LVal : ID Exp_rep
Exp_rep : Exp_rep LB Exp RB | empty
*/
CodeList translate_Exp_rep(treeNode *Exp_rep, Operand place, int* index)
{
if(Exp_rep == NULL || Exp_rep->child == NULL) return NULL;
CodeList code0 = translate_Exp_rep(Exp_rep->child, place, index);
Operand t1 = new_temp(), t2 = new_temp();
CodeList code1 = translate_Exp(Exp_rep->child->next->next, t1);
//place[*index] = t1
InterCode ic = new_InterCode(IR_PLUS);
ic->u.binop.op1 = place;
ic->u.binop.op2 = new_constant(*index);
ic->u.binop.result = t2;
CodeList code2 = new_CodeList(ic);
++(*index);
ic = new_InterCode(IR_CHANGE_ADDR);
ic->u.assign.left = t2;
ic->u.assign.right = t1;
code2 = join(code2, new_CodeList(ic));
return join(code0, join(code1, code2));
}
/*
LVal : ID Exp_rep
Exp_rep : Exp_rep LB Exp RB | empty
!!!注意:LVal既可以作为左值出现也可以作为右值出现,所以这里对于数组返回地址,此时要求下面的place参数为OP_ADDRESS类型
*/
CodeList translate_LVal(treeNode *LVal, Operand place, int isLeft) {
// 普通变量的情况
if(LVal->child->next->child == NULL) {
Operand val = op_from_var(LVal->child->value);
if(val->kind == OP_CONSTANT || val->kind == OP_VARIABLE)
{
InterCode ic = new_InterCode(IR_ASSIGN);
ic->u.assign.left = place;
ic->u.assign.right = val;
return new_CodeList(ic);
}
}
// 特别注意:单独一个Ident也可能是指针
// 数组/指针的情况, 以下注释中为方便理解,原数组记为a
// 首先获取数组的地址,储存在baseAddr中, --> baseAddr = a
Para item = querySymTable_para(LVal->child->value);
Operand baseAddr = item->op;
//预处理数组维数信息
int arrayDim = listsize(item->type);
printf("arrayDim: %d\n",arrayDim);
int* arrayDimList = (int*)malloc(sizeof(int)*arrayDim);
getArrayDimList(arrayDimList, item);
printf("arrayDimList: {");
int i;for(i=0;i<arrayDim;i++)printf("%d,",arrayDimList[i]);
printf("}\n");
//获取需要的数组下标,储存在数组中
Operand b_addr = new_var();
b_addr->kind = OP_ADDRESS;
InterCode ic = new_InterCode(IR_DEC); // 数组空间声明语句
ic->u.dec.x = b_addr;
ic->u.dec.size = arrayDim*sizeof(int); // 申明一个数组,大小为原数组的维度数,用于存放访问的index,以下记为b
CodeList code0 = new_CodeList(ic);
int accessDim = 0; //实际访问的维数总数,比如一个二维数组a[2][2],只访问a[1],accessDim就为1
code0 = join(code0, translate_Exp_rep(LVal->child->next, b_addr, &accessDim)); //获取index,存放在var中
printf("accessDim=%d\n",accessDim);
//计算最终地址
Operand t1 = new_temp();
Operand sum = new_temp(); //用于储存最终地址
//set sum to 0
ic=new_InterCode(IR_ASSIGN);
ic->u.assign.left = sum;
ic->u.assign.right = new_constant(0);
code0 = join(code0, new_CodeList(ic));
for(i=0;i<accessDim;i++)
{
//t1=b+4*i(即&b[i]);
ic = new_InterCode(IR_PLUS);
ic->u.binop.op1 = new_constant(4*i);
ic->u.binop.op2 = b_addr;
ic->u.binop.result = t1;
code0=join(code0, new_CodeList(ic));
//t1=*t1 此时t1=b[i]
ic = new_InterCode(IR_RET);
ic->u.assign.left = t1;
ic->u.assign.right = t1;
code0=join(code0, new_CodeList(ic));
//rightSize是剩下维的大小之积,比如a[5][6][7], 当前i=0, 那么当前维的size是5,5右侧维数的大小之积为6*7=42, 所以rightSize=42
int rightSize = getArraySize(arrayDimList, i+1, arrayDim);
//rightSize*=b[i]
ic = new_InterCode(IR_MUL);
ic->u.binop.op1=t1;
ic->u.binop.op2=new_constant(rightSize);
ic->u.binop.result=t1;
code0=join(code0, new_CodeList(ic));
//sum+=rightSize
ic = new_InterCode(IR_PLUS);
ic->u.binop.op1=sum;
ic->u.binop.op2=t1;
ic->u.binop.result=sum;
code0=join(code0, new_CodeList(ic));
}
//place = a+sum*4 (即&a[sum])
ic=new_InterCode(IR_MUL);
ic->u.binop.op1=sum;
ic->u.binop.op2=new_constant(4);
ic->u.binop.result=sum;
code0=join(code0, new_CodeList(ic));
ic=new_InterCode(IR_PLUS);
ic->u.binop.op1=baseAddr;
ic->u.binop.op2=sum;
ic->u.binop.result=place;
code0=join(code0, new_CodeList(ic));
if(!isLeft && accessDim == arrayDim) //右值的话取一下指针中的值
{
ic=new_InterCode(IR_RET);
ic->u.assign.left=place;
ic->u.assign.right=place;
code0=join(code0, new_CodeList(ic));
}
free(arrayDimList);
return code0;
}
/*
FuncDef : FuncType ID LP FuncFParams RP Block | FuncType ID LP RP Block
FuncFParams : FuncFParam ComFuncFParam_rep
ComFuncFParam_rep : ComFuncFParam_rep COMMA FuncFParam
FuncFParam : FuncType ID | FuncType ID LB RB LbEXPRb_rep
LbEXPRb_rep : LbEXPRb_rep LB Exp RB
函数定义语句的翻译. 总体还是没问题, 但还是有部分要修改 基本ok-还需要再修改
*/
CodeList translate_FuncDef(treeNode *FuncDef)
{
if(FuncDef == NULL) {
return NULL;
}
InterCode ic = new_InterCode(IR_FUNC); // 函数中间名中间代码
ic->u.func = FuncDef->child->next->value; // 函数名 ID的value就是他的值
printf("Funcdef: %s\n", ic->u.func);
CodeList code1 = new_CodeList(ic); // 函数名中间代码链表
CodeList code2 = NULL; // 存block部分的中间代码
addScope();
if(!strcmp(FuncDef->child->next->next->next->name,"FuncFParams")) { // 解析形参
// FuncType ID LP FuncFParams RP Block
// 我们这里应该就只有int类型
Func func = querySymTable_func(FuncDef->child->next->value); // 根据函数名获得对应但函数
vector paraList = func->paraList; // 从而获得参数链表
int i;
for(i=0; i<paraList->size; i++)
{
Para param = (Para)getItem(paraList, i);
InterCode paramCode = new_InterCode(IR_PARAM);
Operand op;
Type para_type = (Type)getFirst(param->type);
op=new_var();
paramCode->u.op = op;
param->op = op; //存入符号表
printf("name: %s\n",param->name);
printf("scope: %d\n", param->scope);
printf("current scope: %d\n", scope);
insertSymTable_para(param, 0);
CodeList tmp = new_CodeList(paramCode);
join(code1,tmp);
}
// Block
code2 = translate_Block(FuncDef->child->next->next->next->next->next, NULL, NULL, 0);
}
else {
// FuncDef -> FuncType ID LP RP Block
// Nothing
// Block
code2 = translate_Block(FuncDef->child->next->next->next->next, NULL, NULL, 0);
}
exitScope();
return join(code1,code2);
}
/*
FuncRParams : Exp comExp_rep;
comExp_rep : comExp_rep COMMA Exp | empty;
实参的翻译, 添加到实参表, 并返回计算过程的三地址码链表 基本ok
*/
CodeList translate_FuncRParams(treeNode *FuncRParams, ArgList *arg_list) {
if(FuncRParams==NULL) {
return NULL;
}
Operand t1 = new_temp(); // 临时变量
CodeList code1 = translate_Exp(FuncRParams->child,t1); // Exp求值后存到临时变量汇总
ArgList newArgList = malloc(sizeof(struct ArgList_)); // 这里的ArgList就是链表的一个结点-代表了一个内容为arg的结构-而不是链表-注意和InterCode与CodeList关系区分
newArgList->args = t1; // 表达式计算的结果即为实参
newArgList->next = *arg_list; // 链接
*arg_list = newArgList; // 头部!
CodeList code2 = translate_comExp_rep(FuncRParams->child->next,arg_list); // 继续翻译下面的实参, 注意这里的arg_list为指针
return join(code1,code2);
}
/*
comExp_rep : comExp_rep COMMA Exp | empty
跟上 基本ok
*/
CodeList translate_comExp_rep(treeNode *comExp_rep, ArgList *arg_list) {
if(comExp_rep == NULL || comExp_rep -> child == NULL) {
return NULL;
}
CodeList code1 = translate_comExp_rep(comExp_rep->child,arg_list); // 继续递归翻译-直到遇到NULL. //目前还不确定NULL的地址码解析-应该ok
Operand t1 = new_temp(); // 临时变量暂存表达式运算结果
CodeList code2 = translate_Exp(comExp_rep->child->next->next,t1); // 获取表达式运算结果
ArgList newArgList = malloc(sizeof(struct ArgList_)); // 分配地址空间
newArgList->args = t1;
newArgList->next = *arg_list;
*arg_list = newArgList; // 头部!
return join(code1,code2); //
}
/*
Utility. 根据类型新建一个三地址码节点, 返回三地址码节点 ok
*/
InterCode new_InterCode(int kind)
{
InterCode ic = (InterCode)malloc(sizeof(struct InterCode_));
ic->kind = kind;
return ic;
}
/*
Utility. 从一个三地址码节点建立一个三地址码链表 ok
*/
CodeList new_CodeList(InterCode code)
{
CodeList cl = (CodeList)malloc(sizeof(struct CodeList_));
cl->code = code;
cl->next = cl->prev = NULL; // 双向链表
return cl;
}
/*
Utility. 链接两个三地址码链表 ok
*/
CodeList join(CodeList head, CodeList body) {
if(head == NULL) {
return body;
}
CodeList tmp = head;
while(tmp->next != NULL) { // 链表结尾是NULL
tmp = tmp->next;
}
tmp->next = body;
if(body != NULL) {
body->prev = tmp;
}
return head;
}
/*
Utility. 将*单运算符*根据类型转化为三地址码链表
例如: translate_Operand(tmp, IR_RETURN). translate_Operand(label_false, IR_LABEL).
转化为三地址码(形式化结果) RETURN t3. LABEL label1 :. ok
*/
CodeList translate_Operand(Operand op, int IR_KIND) {
InterCode tmp = new_InterCode(IR_KIND);
tmp->u.op = op;
CodeList res = new_CodeList(tmp);
return res;
}
/*
Utility. 将三地址码插入全局链表. 全局变量: code_tail, code_head ok
*/
void insert_code(CodeList code) {
if(code == NULL) {
return ;
}
if(code_head==NULL) { //如果为NULL
code_head = code;
CodeList tmp = code;
while(tmp->next != NULL)
{
tmp=tmp->next;
}
code_tail = tmp;
}
else { //往尾部插入
code->prev = code_tail;
code_tail->next = code;
while(code_tail->next != NULL)
{
code_tail = code_tail->next;
}
}
}
/*
Utility. 返回常量操作数. // 目前来看-应该叫字面量(立即数)操作数?
也有用在int的值中, 例如 ic->u.assign.right = new_constant(val); 应该ok
*/
Operand new_constant(int val) {
Operand tmp = malloc(sizeof(struct Operand_));
tmp->kind = OP_CONSTANT;
tmp->u.val = val;
return tmp;
}
/*
Utility. 返回临时变量操作数(全局唯一, count下标). ok
*/
Operand new_temp() {
Operand tmp = malloc(sizeof(struct Operand_));
tmp->kind = OP_TEMP;
tmp->u.temp_no = temp_num;
temp_num++;
return tmp;
}
/*
Utility. 返回标签操作数(全局唯一, 代表一个标签). ok
*/
Operand new_label() {
Operand tmp = (Operand)malloc(sizeof(struct Operand_));
tmp->kind = OP_LABEL;
tmp->u.temp_no = label_num;
label_num++;
return tmp;
}
/*
Utility. 返回变量. ok
*/
Operand new_var() {
Operand tmp = (Operand)malloc(sizeof(struct Operand_));
tmp->kind = OP_VARIABLE;
tmp->u.var_no = var_num++;
return tmp;
}
/*
Utility. 返回函数形参. ok
*/
Operand new_fparam() {
Operand tmp = (Operand)malloc(sizeof(struct Operand_));
tmp->kind = OP_VARIABLE;
//tmp->u.a 需要事后自己修改
return tmp;
}
/*
Utility. 返回变量类型的Operand. 变量: 函数形参, 局部变量v开头.
查看变量链表-返回对应的操作数. var是变量名-可以用原本代码中的变量名?可能需要解决名字冲突问题?
大致ok--还需要解决一些问题
*/
Operand op_from_var(char* var) {
// if(var_head == NULL) { // 变量链表为空
// // 若为空 直接加
// var_head = malloc(sizeof(struct Variable_));
// var_head->name = var; // 变量名
// Operand tmp = malloc(sizeof(struct Operand_));
// tmp->kind = OP_VARIABLE;
// tmp->u.var_no = var_num; // 变量操作数
// var_num++;
// var_head->op = tmp;
// var_tail = var_head;
// return tmp;
// }
// else {
// // 遍历 查找 进行名字比较
// Variable vtmp = var_head;
// while(vtmp != NULL) {
// if(!(strcmp(vtmp->name,var))) {
// return vtmp->op; // 找到直接返回即可
// }
// vtmp = vtmp->next;
// }
// // 增加变量节点内容并返回
// Operand tmp = malloc(sizeof(struct Operand_));
// tmp->kind = OP_VARIABLE;
// tmp->u.var_no = var_num;
// var_num++;
// Variable newVar = malloc(sizeof(struct Variable_));
// newVar->name = var;
// newVar->op = tmp;
// var_tail->next = newVar;
// var_tail = newVar;
// return tmp;
// }
Para tmp = querySymTable_para(var);
if(tmp&&tmp->op!=NULL) return tmp->op; //已经在符号表中,直接返回
return new_var();
}
/*
Utility. 运算符转化为字符串. 返回转化后的字符串 大致ok-要理解修改
*/
char *Operand_toString(Operand op) {
char msg[64] = "";
if(op->kind == OP_CONSTANT) {
sprintf(msg,"#%d",op->u.val); // 常量(目前应该叫立即数). 例如 #0
}
else if(op->kind == OP_LABEL) {
sprintf(msg,"label%d",op->u.label_no); // 标签. 例如 label1. 全局唯一
}
else if(op->kind == OP_VARIABLE || op->kind == OP_ARR_STRU || op->kind == OP_ADDRESS) {
sprintf(msg, "v%d", op->u.var_no);
}
else if(op->kind==OP_TEMP) {
sprintf(msg,"t%d", op->u.temp_no);
}
char *ret = malloc(strlen(msg)+1); // 返回空间指针
strcpy(ret, msg);
return ret; ///就是最终的内容把~~
}
/*
Utility. 将一个三地址码节点翻译为一条三地址码指令. 返回三地址码指令字符串 基本ok 还需要再了解下数组 地址 指针相关
*/
char* InterCode_toString(InterCode code) {
int max_size = 64; // 最大字节数
char *msg = malloc(max_size); // 空间分配 64 字节
memset(msg,0,max_size);
printf("%d\n", code->kind);
if(code->kind == IR_LABEL) { // LABEL代码
char *x = Operand_toString(code->u.op);
sprintf(msg, "LABEL %s :",x); // 例如 LABEL label1
}
else if(code->kind == IR_FUNC) { // FUNCTION代码
char *f = code->u.func; // code->u.func应该是函数名. 在别的地方check一下
sprintf(msg, "FUNCTION %s :", f); // 例如 FUNCTION main
}
// 这几部分都和地址指针数组有关--下次再看看
else if(code->kind == IR_ASSIGN) {
Operand left = code->u.assign.left; // 左边的运算数
Operand right = code->u.assign.right; // 右边的运算数
if(right==NULL){
printf("omitted\n");
sprintf(msg, "\n");
return msg;
}
if(code->u.assign.left != NULL) {
char *x = Operand_toString(left); // 例如v1
char *y = Operand_toString(right); // 例如t1
// if(left->kind==OP_ADDRESS && right->kind!=OP_ADDRESS) {
// sprintf(msg,"%s := &%s",x,y); // 例如 v1 = &t1 产生来源: 大概是
// }
// else if(left->kind!=OP_ADDRESS && right->kind==OP_ADDRESS) {
// sprintf(msg,"%s := *%s",x,y); // 例如 v1 = *t1 产生来源: 有数组赋值语句-也就是将数组某个下标的值给某个变量
// }
// else {
// sprintf(msg,"%s := %s",x,y); // 普通赋值运算, 例如v1 = t1
// }
sprintf(msg,"%s := %s",x,y);
}
}
else if(code->kind == IR_CHANGE_ADDR) { // 这几部分都和地址指针数组有关--下次再看看
if(code->u.assign.left != NULL) {
char *x = Operand_toString(code->u.assign.left);
char *y = Operand_toString(code->u.assign.right);
sprintf(msg, "*%s := %s",x,y); // 例如 *v1 = t1
}
}
else if(code->kind == IR_PLUS) {
if(code->u.binop.result != NULL) {
char *x = Operand_toString(code->u.binop.result);
char *y = Operand_toString(code->u.binop.op1);
char *z = Operand_toString(code->u.binop.op2);
sprintf(msg, "%s := %s + %s",x,y,z); // 例如 t3 = t2 + t1
}
}
else if(code->kind == IR_MINUS) {
if(code->u.binop.result != NULL) {
char *x = Operand_toString(code->u.binop.result);
char *y = Operand_toString(code->u.binop.op1);
char *z = Operand_toString(code->u.binop.op2);
sprintf(msg, "%s := %s - %s",x,y,z); // 例如 t3 = t2 - t1
}
}
else if(code->kind == IR_MUL) {
if(code->u.binop.result != NULL) {
char *x = Operand_toString(code->u.binop.result);
char *y = Operand_toString(code->u.binop.op1);
char *z = Operand_toString(code->u.binop.op2);
////
sprintf(msg,"%s := %s * %s",x,y,z); // 例如 t3 = t2 * t1
}
}
else if(code->kind == IR_DIV) {
if(code->u.binop.result != NULL) {
char *x = Operand_toString(code->u.binop.result);
char *y = Operand_toString(code->u.binop.op1);
char *z = Operand_toString(code->u.binop.op2);
sprintf(msg,"%s := %s / %s", x,y,z); // 例如 t3 = t2 / t1
}
}
else if(code->kind == IR_MOD) {
if(code->u.binop.result != NULL) {
char *x = Operand_toString(code->u.binop.result);
char *y = Operand_toString(code->u.binop.op1);
char *z = Operand_toString(code->u.binop.op2);
sprintf(msg,"%s := %s %% %s",x,y,z); // 例如 t3 = t2 % t1
}
}
else if(code->kind == IR_GOTO) {
char *x = Operand_toString(code->u.op);
sprintf(msg,"GOTO %s",x); // 例如 GOTO label4
}
else if(code->kind == IR_IFGOTO) { // 例如 IF v2 > v1 GOTO label3
char *x = Operand_toString(code->u.if_goto.x);
char *y = Operand_toString(code->u.if_goto.y);
char *z = Operand_toString(code->u.if_goto.z);
sprintf(msg,"IF %s %s %s GOTO %s",x,code->u.if_goto.relop,y,z); // 应该可以直接用
}
else if(code->kind == IR_RETURN) {
char *x = Operand_toString(code->u.op); // 例如RETURN t8
sprintf(msg,"RETURN %s",x);
}
else if(code->kind == IR_RET) {
Operand left = code->u.assign.left; // 左边的运算数
Operand right = code->u.assign.right; // 右边的运算数
if(code->u.assign.left != NULL) {
char *x = Operand_toString(left); // 例如v1
char *y = Operand_toString(right); // 例如t1
sprintf(msg, "%s := *%s", x, y);
}
}
else if(code->kind == IR_DEC) {
char *x = Operand_toString(code->u.dec.x);
sprintf(msg,"DEC %s %d",x,code->u.dec.size); // 申请内存空间--数组相关--下次看. 例如DEC v2 8
}
else if(code->kind == IR_ARG) {
char *x = Operand_toString(code->u.op);
sprintf(msg,"ARG %s",x); // 例如ARG v2. // ARG &v2 会发生吗?
}
else if(code->kind == IR_CALL) {
char *x = Operand_toString(code->u.call.result);
char *f = code->u.call.func;
sprintf(msg,"%s := CALL %s",x,f); // 例如 x := CALL f
}
else if(code->kind == IR_PARAM) {
char *x = Operand_toString(code->u.op);
sprintf(msg,"PARAM %s",x); // 例如 PARAM v1
}
// else if(code->kind == IR_READ) { // 目前不用READ WRITE
// char *x = Operand_toString(code->u.op);
// sprintf(msg,"READ %s",x);
// }
// else if(code->kind == IR_WRITE) {
// char *x = Operand_toString(code->u.op);
// sprintf(msg,"WRITE %s",x);
// }
else if(code->kind == IR_GET_ADDR) {
char *x = Operand_toString(code->u.assign.left);
char *y = Operand_toString(code->u.assign.right);
sprintf(msg,"%s := &%s",x,y); // 数组相关--下次修改在看
}
else if(code->kind == IR_ENDFUNC) {
sprintf(msg, "ENDFUNC");
}
char *ret = malloc(strlen(msg) + 1);
strcpy(ret, msg);
free(msg);
return ret;
}
/*
Start. 翻译中间代码, 并将结果写入文件 ok
*/
CodeList translate_InterCode(treeNode* root, char* file)
{
FILE *f;
if(file != NULL) {
f = fopen(file, "w");
}
else {
f = fopen("output.ir", "w");
}
label_num = var_num = temp_num = 1; // 各种类型运算符(标签、变量、临时变量)的 起始序号
code_head = code_tail = NULL; // 中间代码链表的头和尾
var_tail = var_head = NULL; // 变量链表的头和尾 //以上均为全局变量
start_translate(root); // 翻译
printCodeList(code_head);
printf("---------------\n");
CodeList code = code_head; // 中间代码头
while(code != NULL) // 依次遍历-解析为字符串-输出
{
fprintf(f,"%s\n",InterCode_toString(code->code));
code = code->next;
}
fclose(f);
return code_head;
}
/*
Utility. 递归翻译, 得到中间代码链表 ok
*/
void start_translate(treeNode* root)
{
if(root==NULL){
return ;
}
if(!strcmp(root->name, "CompUnit")) { // 只有一个根的话-直接从根开始递归翻译就行了 应该
fprintf(stderr,"start_translate\n");
initScopeTable();