-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparamtable.cpp
1433 lines (1429 loc) · 48.7 KB
/
paramtable.cpp
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 "paramtable.h"
void ParamTable::initAll()
{
#ifdef DEBUG
cout << "initAll" << endl;
#endif
synb.clear();
pfinf.clear();
synbl newS;
pfinfl newP;
newS.name = -1;
newS.cat = 'f';
newP.level = 0;
newP.off = 0;
newP.fn = 0;
newP.elems.clear();
newP.param.clear();
pfinf.push_back(newP);
newS.addr = pfinf.size() - 1;
synb.push_back(newS);
while (!Funcs.empty())
Funcs.pop();
Funcs.push(synb[synb.size() - 1].addr);
size_tmp = 0;
elems.clear();
table_synb.clear();
table_pfinf.clear();
table_cons.clear();
table_ainf.clear();
beginidx_id_tmp = 100;
id_tmp = beginidx_id_tmp; //临时变量的开始值,正常变量数大于100时需要修改这个值
memset(inalNums, 0, sizeof(inalNums));
}
void ParamTable::addFun(int id)
{
#ifdef DEBUG
cout << "addFun" << ' ' << id << endl;
#endif
synbl newS;
pfinfl newP;
newS.name = id;
newS.cat = 'f';
newP.level = pfinf[Funcs.top()].level + 1;
newP.off = size_pfinfl;
newP.fn = 0;
newP.param.clear();
newP.synbs.clear();
newP.elems.clear();
newP.entry = synb.size();
pfinf.push_back(newP);
newS.addr = pfinf.size() - 1;
synb.push_back(newS);
table_pfinf.insert(make_pair(id, synb.size() - 1));
pfinf[Funcs.top()].synbs.push_back(synb.size() - 1);
pfinf[Funcs.top()].elems.push_back(elems.size());
Funcs.push(synb[synb.size() - 1].addr);
elems.push_back(elem("fun", id, -1, -1));
}
void ParamTable::backFun()
{
#ifdef DEBUG
cout << "backFun" << endl;
#endif
Funcs.pop();
pfinf[Funcs.top()].elems.push_back(elems.size());
elems.push_back(elem("funend", -1, -1, -1));
}
void ParamTable::addNum(int id, bool isparam)
{
#ifdef DEBUG
cout << "addNum" << ' ' << id << ' ' << isparam << endl;
#endif
for (int i = 0; i < pfinf[Funcs.top()].synbs.size(); i++)
if (synb[pfinf[Funcs.top()].synbs[i]].name == id)
{
if (id < 100)
{
haveErr = true;
errMessagePT = "Error! redeclaration of 'int " + to_string(id) + "'";
#ifdef DEBUG
cout << "error: redeclaration of 'int " << id << "'" << endl;
#endif
}
return;
}
synbl newS;
newS.name = id;
newS.cat = 'i';
synb.push_back(newS);
if (isparam)
pfinf[Funcs.top()].param.push_back(synb.size() - 1);
else
pfinf[Funcs.top()].synbs.push_back(synb.size() - 1);
}
void ParamTable::addArray(int id, int len, bool isparam)
{
#ifdef DEBUG
cout << "addArray" << ' ' << id << endl;
#endif
for (int i = 0; i < pfinf[Funcs.top()].synbs.size(); i++)
if (synb[pfinf[Funcs.top()].synbs[i]].name == id)
{
haveErr = true;
errMessagePT = "Error! redeclaration of 'int* " + to_string(id) + "'";
#ifdef DEBUG
cout << "error: redeclaration of 'int* " << id << "'" << endl;
#endif
return;
}
synbl newS;
ainfl newA;
newS.name = id;
newS.cat = 'l';
newA.low = id_tmp;
table_ainf.insert(make_pair(id_tmp, 1984));
newA.up = id_tmp + len - 1;
ainf.push_back(newA);
newS.addr = ainf.size() - 1;
table_ainf.insert(make_pair(id, synb.size()));
synb.push_back(newS);
if (isparam)
pfinf[Funcs.top()].param.push_back(synb.size() - 1);
else
{
pfinf[Funcs.top()].synbs.push_back(synb.size() - 1);
for (int i = 1; i <= len; i++)
{
addNum(id_tmp);
inalNums[id_tmp] = 1;
id_tmp++;
}
}
}
void ParamTable::addCon(int id, int data)
{
#ifdef DEBUG
cout << "addCon" << endl;
#endif
synbl newS;
consl newC;
newS.name = id;
newS.cat = 'c';
newC.data = data;
cons.push_back(newC);
newS.addr = cons.size() - 1;
synb.push_back(newS);
table_cons.insert(make_pair(id, data));
}
void ParamTable::alGeq(string op)
{
#ifdef DEBUG
cout << "alGeq" << ' ' << op << endl;
#endif
int id1, id2, diffid1 = -1, diffid2 = -1;
if (alNums.empty())
{
haveErr = true;
errMessagePT = "Error! invalid expression";
#ifdef DEBUG
cout << "error: invalid expression" << endl;
#endif
return;
}
id1 = alNums.top();
alNums.pop();
if (table_ainf.count(id1))
{
diffid1 = alNums.top();
alNums.pop();
id1 = ainf[synb[table_ainf[id1]].addr].low;
}
if (alNums.empty())
{
haveErr = true;
errMessagePT = "Error! invalid expression";
#ifdef DEBUG
cout << "error: invalid expression" << endl;
#endif
return;
}
id2 = alNums.top();
alNums.pop();
if (table_ainf.count(id2))
{
diffid2 = alNums.top();
alNums.pop();
id2 = ainf[synb[table_ainf[id2]].addr].low;
}
#ifdef DEBUG
cout << id1 << ' ' << id2 << endl;
#endif
if (op == "=")
{
if (diffid1 != -1)
{
pfinf[Funcs.top()].elems.push_back(elems.size());
elems.push_back(elem("arr1", -1, -1, diffid1));
}
if (diffid2 != -1)
{
pfinf[Funcs.top()].elems.push_back(elems.size());
elems.push_back(elem("arr2", -1, -1, diffid2));
}
pfinf[Funcs.top()].elems.push_back(elems.size());
elems.push_back(elem(op, id1, -1, id2));
alNums.push(id2);
}
else
{
if (diffid2 != -1)
{
pfinf[Funcs.top()].elems.push_back(elems.size());
elems.push_back(elem("arr1", -1, -1, diffid2));
}
if (diffid1 != -1)
{
pfinf[Funcs.top()].elems.push_back(elems.size());
elems.push_back(elem("arr2", -1, -1, diffid1));
}
pfinf[Funcs.top()].elems.push_back(elems.size());
//if (min(id1, id2) >= beginidx_id_tmp) //减少临时变量开销
// id_tmp = min(id1, id2);
//else if (max(id1, id2) >= beginidx_id_tmp)
// id_tmp = max(id1, id2);
//else
addNum(id_tmp);
elems.push_back(elem(op, id2, id1, id_tmp));
alNums.push(id_tmp);
inalNums[id_tmp] = 1;
#ifdef DEBUG
cout << "push " << alNums.top() << endl;
#endif
id_tmp++;
}
}
void ParamTable::alPush(int id, bool isarray)
{ //之后要加一堆处理
#ifdef DEBUG
cout << "alPush" << ' ' << id << endl;
#endif
if (!isarray)
alNums.push(id);
else
{
alNums.push(id);
}
}
void ParamTable::alPop()
{
#ifdef DEBUG
cout << "alPop" << endl;
#endif
if (alNums.empty())
{
haveErr = true;
errMessagePT = "Error! alPop error";
#ifdef DEBUG
cout << "error: alPop error" << endl;
#endif
return;
}
alNums.pop();
}
void ParamTable::exIf()
{
#ifdef DEBUG
cout << "exIf" << endl;
#endif
if (alNums.empty())
{
haveErr = true;
errMessagePT = "Error! expected primary-expression before ')' token";
#ifdef DEBUG
cout << "error: expected primary-expression before ')' token" << endl;
#endif
return;
}
int id1;
id1 = alNums.top();
alNums.pop();
pfinf[Funcs.top()].elems.push_back(elems.size());
elems.push_back(elem("if", id1, -1, -1));
}
void ParamTable::exIe()
{
#ifdef DEBUG
cout << "exIe" << endl;
#endif
pfinf[Funcs.top()].elems.push_back(elems.size());
elems.push_back(elem("ie", -1, -1, -1));
}
void ParamTable::exEl()
{
#ifdef DEBUG
cout << "exEl" << endl;
#endif
pfinf[Funcs.top()].elems.push_back(elems.size());
elems.push_back(elem("el", -1, -1, -1));
}
void ParamTable::exWh()
{
#ifdef DEBUG
cout << "exWh" << endl;
#endif
pfinf[Funcs.top()].elems.push_back(elems.size());
elems.push_back(elem("wh", -1, -1, -1));
}
void ParamTable::exDo()
{
#ifdef DEBUG
cout << "exDo" << endl;
#endif
if (alNums.empty())
{
haveErr = true;
errMessagePT = "Error! expected primary-expression before ')' token";
#ifdef DEBUG
cout << "error: expected primary-expression before ')' token" << endl;
#endif
return;
}
int id1;
id1 = alNums.top();
alNums.pop();
pfinf[Funcs.top()].elems.push_back(elems.size());
elems.push_back(elem("do", id1, -1, -1));
}
void ParamTable::exWe()
{
#ifdef DEBUG
cout << "exWe" << endl;
#endif
pfinf[Funcs.top()].elems.push_back(elems.size());
elems.push_back(elem("we", -1, -1, -1));
}
void ParamTable::exIn()
{
#ifdef DEBUG
cout << "exIn" << endl;
#endif
int id0, diffid0 = -1;
pfinf[Funcs.top()].elems.push_back(elems.size());
id0 = alNums.top();
alNums.pop();
if (table_ainf.count(id0))
{
diffid0 = alNums.top();
alNums.pop();
id0 = ainf[synb[table_ainf[id0]].addr].low;
}
elems.push_back(elem("in", diffid0, -1, id0));
}
void ParamTable::exOut()
{
#ifdef DEBUG
cout << "exOut" << endl;
#endif
int id0, diffid0 = -1;
pfinf[Funcs.top()].elems.push_back(elems.size());
id0 = alNums.top();
alNums.pop();
if (table_ainf.count(id0))
{
diffid0 = alNums.top();
alNums.pop();
id0 = ainf[synb[table_ainf[id0]].addr].low;
}
elems.push_back(elem("out", diffid0, -1, id0));
}
void ParamTable::callBegin(int id)
{
#ifdef DEBUG
cout << "callBegin" << ' ' << id << endl;
#endif
if (!table_pfinf.count(id))
{
haveErr = true;
errMessagePT = "Error! function was not declared in this scope";
#ifdef DEBUG
cout << "error: '"
<< "' was not declared in this scope" << endl;
#endif
throw "callError";
return;
}
calls.push(id);
callParams.push(0);
pfinf[Funcs.top()].elems.push_back(elems.size());
elems.push_back(elem("call", id, id_tmp, -1));
addNum(id_tmp);
alNums.push(id_tmp);
inalNums[id_tmp] = 1;
id_tmp++;
}
void ParamTable::callEnd()
{
#ifdef DEBUG
cout << "callEnd" << endl;
#endif
if (calls.empty() || callParams.empty())
{
haveErr = true;
errMessagePT = "Error! internal error in callEnd";
#ifdef DEBUG
cout << "internal error in callEnd" << endl;
#endif
return;
}
if (callParams.top() < pfinf[synb[table_pfinf[calls.top()]].addr].param.size())
{
haveErr = true;
errMessagePT = "Error! too few arguments to function";
#ifdef DEBUG
cout << "error: too few arguments to function '"
<< "'" << endl;
#endif
return;
}
else if (callParams.top() > pfinf[synb[table_pfinf[calls.top()]].addr].param.size())
{
haveErr = true;
errMessagePT = "Error! too many arguments to function";
#ifdef DEBUG
cout << "error: too many arguments to function '"
<< "'" << endl;
#endif
return;
}
calls.pop();
callParams.pop();
pfinf[Funcs.top()].elems.push_back(elems.size());
elems.push_back(elem("callend", -1, -1, -1));
}
void ParamTable::callParam()
{
#ifdef DEBUG
cout << "callParam" << endl;
#endif
if (alNums.empty())
{
haveErr = true;
errMessagePT = "Error! expected primary-expression before ')' token";
#ifdef DEBUG
cout << "error: expected primary-expression before ')' token" << endl;
#endif
return;
}
int id1, diffid1 = -1;
id1 = alNums.top();
alNums.pop();
if (table_ainf.count(id1))
{
diffid1 = alNums.top();
alNums.pop();
id1 = ainf[synb[table_ainf[id1]].addr].low;
}
int num;
num = callParams.top();
callParams.pop();
callParams.push(++num);
if(diffid1!=-1)
{
pfinf[Funcs.top()].elems.push_back(elems.size());
elems.push_back(elem("arr1", -1, -1, diffid1));
}
pfinf[Funcs.top()].elems.push_back(elems.size());
elems.push_back(elem("params", -1, -1, id1));
}
void ParamTable::retNum()
{
int id1;
id1 = alNums.top();
alNums.pop();
pfinf[Funcs.top()].elems.push_back(elems.size());
elems.push_back(elem("retnum", -1, -1, id1));
}
void ParamTable::retNonum()
{
pfinf[Funcs.top()].elems.push_back(elems.size());
elems.push_back(elem("ret", -1, -1, -1));
}
void ParamTable::genValls() //生成活动记录表
{
basicValls.clear();
vall nowvall;
int i, sum, k;
for (k = 0; k < pfinf.size(); k++)
{
nowvall.var.clear();
nowvall.par.clear();
nowvall.size = 0;
sum = 0;
for (i = 0; i < pfinf[k].param.size(); i++)
{
nowvall.par.insert(make_pair(synb[pfinf[k].param[i]].name, sum));
nowvall.var.insert(make_pair(synb[pfinf[k].param[i]].name, sum));
sum += 2;
}
nowvall.parsize = sum;
sum += 2; //为call内的ip留个位置
for (i = 0; i < pfinf[k].synbs.size(); i++)
{
nowvall.var.insert(make_pair(synb[pfinf[k].synbs[i]].name, sum));
sum += 2;
/*if (synb[pfinf[k].synbs[i]].cat == 'l')
{
sum += 2 * (ainf[synb[pfinf[k].synbs[i]].addr].up - ainf[synb[pfinf[k].synbs[i]].addr].low);
}*/
}
nowvall.size = sum;
basicValls.push_back(nowvall);
}
}
void ParamTable::gen4elem() //处理四元式的if,while等语句的跳转位置,四元式优化,并输出四元式用来调试
{
#ifdef DEBUG
cout << "wow we are generating elems!" << endl;
#endif
for (int i = 0; i < synb.size(); i++)
{
table_synb.insert(make_pair(synb[i].name, i));
}
table_synb[-1] = -1;
stack<int> sem;
int i, j, k;
while (!sem.empty())
sem.pop();
for (i = 0; i < elems.size(); i++)
{
if (elems[i].st == "if")
sem.push(i);
if (elems[i].st == "el")
{
elems[sem.top()].id0 = i + 1;
elems[i + 1].needtag = true;
sem.pop();
sem.push(i);
}
if (elems[i].st == "ie")
{
elems[sem.top()].id0 = i + 1;
elems[i + 1].needtag = true;
sem.pop();
}
if (elems[i].st == "wh")
sem.push(i);
if (elems[i].st == "do")
sem.push(i);
if (elems[i].st == "we")
{
elems[sem.top()].id0 = i + 1;
elems[i + 1].needtag = true;
sem.pop();
elems[i].id0 = sem.top() + 1;
elems[sem.top() + 1].needtag = true;
sem.pop();
}
}
vector<int> tmpelems;
int iscons[1000], used[1000], lastmodify[1000]; //是否现在是常数/是否被使用了(否则就是垃圾语句)/上次修改位置
map<pair<int, int>, pair<int, int>> oldtonew; //<id1,id2>-><id0,location>
map<int, pair<int, int>> newtoold;
elem tmpelem("", -1, -1, -1);
int piecel, piecer, l, r, num;
for (k = 1; k < pfinf.size(); k++) //常数表达式优化(含常数推进)
{
memset(iscons, 255, sizeof(iscons));
for (i = 0; i < pfinf[k].elems.size(); i++)
{
tmpelem = elems[pfinf[k].elems[i]];
if (tmpelem.iscntop())
{
if ((table_cons.count(tmpelem.id1) || iscons[tmpelem.id1] != -1) && (table_cons.count(tmpelem.id2) || iscons[tmpelem.id2] != -1))
{
if (iscons[tmpelem.id1] != -1)
l = iscons[tmpelem.id1];
else
l = table_cons[tmpelem.id1];
if (iscons[tmpelem.id2] != -1)
r = iscons[tmpelem.id2];
else
r = table_cons[tmpelem.id2];
if (tmpelem.st == "+")
num = l + r;
else if (tmpelem.st == "-")
num = l - r;
else if (tmpelem.st == "*")
num = l * r;
else if (tmpelem.st == "/")
num = l / r;
elems[pfinf[k].elems[i]] = elem("=", id_tmp, -1, tmpelem.id0);
addCon(id_tmp, num);
id_tmp++;
iscons[tmpelem.id0] = num;
}
else
iscons[tmpelem.id0] = -1;
}
else
{
if (tmpelem.st == "=")
{
if (table_cons.count(tmpelem.id1) || iscons[tmpelem.id1] != -1)
{
if (iscons[tmpelem.id1] != -1)
l = iscons[tmpelem.id1];
else
l = table_cons[tmpelem.id1];
iscons[tmpelem.id0] = l;
}
else
{
iscons[tmpelem.id0] = -1;
}
}
else
memset(iscons, 255, sizeof(iscons));
}
}
}
for (k = 1; k < pfinf.size(); k++) //dag优化
{
memset(used, 255, sizeof(used));
memset(lastmodify, 255, sizeof(lastmodify));
oldtonew.clear();
newtoold.clear();
for (i = 0; i < pfinf[k].elems.size(); i++)
{
tmpelem = elems[pfinf[k].elems[i]];
if (tmpelem.iscntop() || tmpelem.st == "=" || tmpelem.st == "arr1" || tmpelem.st == "arr2")
{
if (tmpelem.iscntop())
{
if (tmpelem.id1 != tmpelem.id0 && tmpelem.id2 != tmpelem.id0 && lastmodify[tmpelem.id0] != -1 && used[tmpelem.id0] < lastmodify[tmpelem.id0]) //如果上次修改后到现在未使用过,删除上次对应的四元式
{
elems[pfinf[k].elems[lastmodify[tmpelem.id0]]] = elem("deleted", -1, -1, tmpelem.id0);
oldtonew.erase(make_pair(tmpelem.id1, tmpelem.id2));
if (tmpelem.st == "+" || tmpelem.st == "*")
oldtonew.erase(make_pair(tmpelem.id2, tmpelem.id1));
}
if (oldtonew.count(make_pair(tmpelem.id1, tmpelem.id2))) //之前有过同样的表达式且目标变量未改变过时直接复用
{
int location = oldtonew[make_pair(tmpelem.id1, tmpelem.id2)].second;
int newid = oldtonew[make_pair(tmpelem.id1, tmpelem.id2)].first;
if (location > lastmodify[tmpelem.id1] && location > lastmodify[tmpelem.id2])
{
elems[pfinf[k].elems[i]] = elem("=", newid, -1, tmpelem.id0);
used[newid] = i;
lastmodify[tmpelem.id0] = i;
}
}
else
{
used[tmpelem.id1] = used[tmpelem.id2] = i;
lastmodify[tmpelem.id0] = i;
oldtonew.insert(make_pair(make_pair(tmpelem.id1, tmpelem.id2), make_pair(tmpelem.id0, i)));
if (tmpelem.st == "+" || tmpelem.st == "*")
oldtonew.insert(make_pair(make_pair(tmpelem.id2, tmpelem.id1), make_pair(tmpelem.id0, i)));
}
}
else if (tmpelem.st == "=")
{
if (lastmodify[tmpelem.id0] != -1 && used[tmpelem.id0] < lastmodify[tmpelem.id0]) //如果上次修改后到现在未使用过,删除上次对应的四元式
{
elems[pfinf[k].elems[lastmodify[tmpelem.id0]]] = elem("deleted", tmpelem.id1, -1, tmpelem.id0);
}
used[tmpelem.id1] = i;
lastmodify[tmpelem.id0] = i;
}
else
{
used[tmpelem.id0] = i;
}
}
else
{
memset(used, 255, sizeof(used));
memset(lastmodify, 255, sizeof(lastmodify));
oldtonew.clear();
newtoold.clear();
}
}
}
int idx;
for (i = 0; i < elems.size(); i++) //初始化活跃信息表
{
qt[0].push_back(true);
qt[1].push_back(true);
qt[2].push_back(true);
}
for (k = 1; k < pfinf.size(); k++) //计算活跃信息
{
for (i = 0; i < pfinf[k].elems.size(); i++)
{
tmpelem = elems[pfinf[k].elems[i]];
idx = pfinf[k].elems[i];
if (tmpelem.iscntop() || tmpelem.st == "=" || tmpelem.st == "arr1" || tmpelem.st == "arr2")
{
if (tmpelem.iscntop())
{
qt[0][idx] = symbl[tmpelem.id0];
symbl[tmpelem.id0] = false;
qt[1][idx] = symbl[tmpelem.id1];
symbl[tmpelem.id1] = true;
qt[2][idx] = symbl[tmpelem.id2];
symbl[tmpelem.id2] = true;
}
else if (tmpelem.st == "=")
{
qt[0][idx] = symbl[tmpelem.id0];
symbl[tmpelem.id0] = false;
qt[1][idx] = symbl[tmpelem.id1];
symbl[tmpelem.id1] = true;
}
else
{
qt[0][idx] = symbl[tmpelem.id0];
symbl[tmpelem.id0] = true;
}
}
else
{
memset(symbl, 0, sizeof(symbl));
for (j = 0; j < 100; j++)
symbl[i] = true;
}
}
}
#ifdef DEBUG
for (i = 0; i < elems.size(); i++)
{
printf("%d: ", i);
elems[i].output();
}
#endif
}
void ParamTable::outputParam()
{ //输出参数表内容
int i, j;
printf("-----synbl-----\n");
printf("name--cat--addr\n");
for (i = 0; i < synb.size(); i++)
{
printf("%d: %d %c %d\n", i, synb[i].name, synb[i].cat, synb[i].addr);
}
printf("\n-----ainfl-----\n");
printf("low-----up\n");
for (i = 0; i < ainf.size(); i++)
{
printf("%d: %d %d\n", i, ainf[i].low, ainf[i].up);
}
printf("\n-----consl-----\n");
printf("data\n");
for (i = 0; i < cons.size(); i++)
{
printf("%d: %d\n", i, cons[i].data);
}
printf("\n-----pfinfl-----\n");
printf("synbs-----param\n");
for (i = 0; i < pfinf.size(); i++)
{
printf("params: ");
for (j = 0; j < pfinf[i].param.size(); j++)
printf("%d ", pfinf[i].param[j]);
printf("\nsynbs: ");
for (j = 0; j < pfinf[i].synbs.size(); j++)
printf("%d ", pfinf[i].synbs[j]);
printf("\n");
}
map<int, int>::iterator it;
printf("\n-----vall-----\n");
printf("par---var---size\n");
for (i = 0; i < basicValls.size(); i++)
{
printf("par");
for (it = basicValls[i].par.begin(); it != basicValls[i].par.end(); it++)
printf("(%d-%d) ", it->first, it->second);
printf("\nvar");
for (it = basicValls[i].var.begin(); it != basicValls[i].var.end(); it++)
printf("(%d-%d) ", it->first, it->second);
printf("\nsize");
printf("%d\n", basicValls[i].size);
}
}
void ParamTable::toax(int k, int id, int diffid) //输出到从内存提取到ax的汇编指令
{
if (locra == id && locradiff == diffid) //考虑要找的数据已经在寄存器的情况
return;
if (locrc == id && locrcdiff == diffid)
{
locra = id;
locradiff = diffid;
assemblyRes.push_back("MOV AX,CX");
return;
}
locra = id;
locradiff = diffid;
if (table_cons.count(id)) //为常数
{
//cout << "MOV AX," << table_cons[id] << endl;
assemblyRes.push_back("MOV AX," + to_string(table_cons[id]));
}
else if (basicValls[k].var.count(id)) //考虑是局部变量还是全局变量
{
id = basicValls[k].var[id];
if (diffid == -1)
//cout << "MOV AX,[BP-" << id + 2 << "]" << endl;
assemblyRes.push_back("MOV AX,[BP-" + to_string(id + 2) + "]");
else //为数组,考虑偏移量是否是全局变量
{
if (table_cons.count(diffid))
{
//cout << "MOV SI," << table_cons[diffid] << endl;
assemblyRes.push_back("MOV SI," + to_string(table_cons[diffid]));
}
else if (basicValls[0].var.count(diffid))
{
diffid = basicValls[0].var[diffid];
//cout << "MOV SI,[BX+" << diffid << "]" << endl;
assemblyRes.push_back("MOV SI,[BX+" + to_string(diffid) + "]");
}
else
{
diffid = basicValls[k].var[diffid];
//cout << "MOV SI,[BP-" << diffid + 2 << "]" << endl;
assemblyRes.push_back("MOV SI,[BP-" + to_string(diffid + 2) + "]");
}
//cout << "SHL SI,1" << endl;
//cout << "NEG SI" << endl;
//cout << "MOV AX,[BP+SI-" << id + 2 << "]" << endl;
assemblyRes.push_back("SHL SI,1");
assemblyRes.push_back("NEG SI");
assemblyRes.push_back("MOV AX,[BP+SI-" + to_string(id + 2) + "]");
}
}
else
{
id = basicValls[0].var[id];
if (diffid == -1)
//cout << "MOV AX,[BX+" << id << "]" << endl;
assemblyRes.push_back("MOV AX,[BX+" + to_string(id) + "]");
else //为数组,考虑偏移量是否是全局变量
{
if (table_cons.count(diffid))
{
//cout << "MOV DI," << table_cons[diffid] << endl;
assemblyRes.push_back("MOV DI," + to_string(table_cons[diffid]));
}
else if (basicValls[0].var.count(diffid))
{
diffid = basicValls[0].var[diffid];
//cout << "MOV DI,[BX+" << diffid << "]" << endl;
assemblyRes.push_back("MOV DI,[BX+" + to_string(diffid) + "]");
}
else
{
diffid = basicValls[k].var[diffid];
//cout << "MOV DI,[BP-" << diffid + 2 << "]" << endl;
assemblyRes.push_back("MOV DI,[BP-" + to_string(diffid + 2) + "]");
}
//cout << "SHL DI,1" << endl;
//cout << "MOV AX,[BX+DI+" << id << "]" << endl;
assemblyRes.push_back("SHL DI,1");
assemblyRes.push_back("MOV AX,[BX+DI+" + to_string(id) + "]");
}
}
}
void ParamTable::tocx(int k, int id, int diffid) //输出到从内存提取到ax的汇编指令
{
if (locrc == id && locrcdiff == diffid) //考虑要找的数据已经在寄存器的情况
return;
if (locra == id && locradiff == diffid)
{
locrc = id;
locrcdiff = diffid;
assemblyRes.push_back("MOV CX,AX");
return;
}
locrc = id;
locrcdiff = diffid;
if (table_cons.count(id))
{
//cout << "MOV CX," << table_cons[id] << endl;
assemblyRes.push_back("MOV CX," + to_string(table_cons[id]));
}
else if (basicValls[k].var.count(id)) //考虑是局部变量还是全局变量
{
id = basicValls[k].var[id];
if (diffid == -1)
//cout << "MOV CX,[BP-" << id + 2 << "]" << endl;
assemblyRes.push_back("MOV CX,[BP-" + to_string(id + 2) + "]");
else //为数组,考虑偏移量是否是全局变量
{
if (table_cons.count(diffid))
{
//cout << "MOV SI," << table_cons[diffid] << endl;
assemblyRes.push_back("MOV SI," + to_string(table_cons[diffid]));
}
else if (basicValls[0].var.count(diffid))
{
diffid = basicValls[0].var[diffid];
//cout << "MOV SI,[BX+" << diffid << "]" << endl;
assemblyRes.push_back("MOV SI,[BX+" + to_string(diffid) + "]");
}
else
{
diffid = basicValls[k].var[diffid];
//cout << "MOV SI,[BP-" << diffid + 2 << "]" << endl;
assemblyRes.push_back("MOV SI,[BP-" + to_string(diffid + 2) + "]");
}
//cout << "SHL SI,1" << endl;
//cout << "NEG SI" << endl;
//cout << "MOV CX,[BP+SI-" << id + 2 << "]" << endl;
assemblyRes.push_back("SHL SI,1");
assemblyRes.push_back("NEG SI");
assemblyRes.push_back("MOV CX,[BP+SI-" + to_string(id + 2) + "]");
}
}
else
{
id = basicValls[0].var[id];
if (diffid == -1)
//cout << "MOV CX,[BX+" << id << "]" << endl;
assemblyRes.push_back("MOV CX,[BX+" + to_string(id) + "]");
else //为数组,考虑偏移量是否是全局变量
{
if (table_cons.count(diffid))
{
//cout << "MOV DI," << table_cons[diffid] << endl;
assemblyRes.push_back("MOV DI," + to_string(table_cons[diffid]));
}
else if (basicValls[0].var.count(diffid))
{
diffid = basicValls[0].var[diffid];
//cout << "MOV DI,[BX+" << diffid << "]" << endl;
assemblyRes.push_back("MOV DI,[BX+" + to_string(diffid) + "]");
}
else
{
diffid = basicValls[k].var[diffid];
//cout << "MOV DI,[BP-" << diffid + 2 << "]" << endl;
assemblyRes.push_back("MOV DI,[BP-" + to_string(diffid + 2) + "]");
}
//cout << "SHL DI,1" << endl;
//cout << "MOV CX,[BX+DI+" << id << "]" << endl;
assemblyRes.push_back("SHL DI,1");
assemblyRes.push_back("MOV CX,[BX+DI+" + to_string(id) + "]");
}
}
}
void ParamTable::axto(int k, int id, int diffid) //输出到从ax提取到内存的汇编指令
{
if (table_cons.count(id))
{
//cout << "MOV CX," << table_cons[id] << endl;
assemblyRes.push_back("MOV CX," + to_string(table_cons[id]));
}
else if (basicValls[k].var.count(id)) //考虑是局部变量还是全局变量
{
id = basicValls[k].var[id];
if (diffid == -1)
//cout << "MOV [BP-" << id + 2 << "],AX" << endl;
assemblyRes.push_back("MOV [BP-" + to_string(id + 2) + "],AX");
else //为数组,考虑偏移量是否是全局变量
{
if (table_cons.count(diffid))
{
//cout << "MOV SI," << table_cons[diffid] << endl;
assemblyRes.push_back("MOV SI," + to_string(table_cons[diffid]));
}
else if (basicValls[0].var.count(diffid))
{
diffid = basicValls[0].var[diffid];
//cout << "MOV SI,[BX+" << diffid << "]" << endl;
assemblyRes.push_back("MOV SI,[BX+" + to_string(diffid) + "]");
}
else
{
diffid = basicValls[k].var[diffid];
//cout << "MOV SI,[BP-" << diffid + 2 << "]" << endl;
assemblyRes.push_back("MOV SI,[BP-" + to_string(diffid + 2) + "]");
}
//cout << "SHL SI,1" << endl;
//cout << "NEG SI" << endl;
//cout << "MOV [BP+SI-" << id + 2 << "],AX" << endl;
assemblyRes.push_back("SHL SI,1");
assemblyRes.push_back("NEG SI");
assemblyRes.push_back("MOV [BP+SI-" + to_string(id + 2) + "],AX");
}
}
else
{
id = basicValls[0].var[id];
if (diffid == -1)
//cout << "MOV [BX+" << id << "],AX" << endl;
assemblyRes.push_back("MOV [BX+" + to_string(id) + "],AX");
else //为数组,考虑偏移量是否是全局变量
{