Source file mergecil.ml
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
module P = Pretty
open Cil
module E = Errormsg
module H = Hashtbl
module A = Alpha
open Trace
let debugMerge = false
let debugInlines = false
let ignore_merge_conflicts = ref false
let mergeSynonyms = true
(** Whether to use path compression *)
let usePathCompression = false
let merge_inlines = ref false
let mergeInlinesRepeat () = !merge_inlines && true
let mergeInlinesWithAlphaConvert () = !merge_inlines && true
let mergeGlobals = true
let externallyVisible vi =
match vi.vstorage with
| Static -> false
| _ -> (match !Cil.cstd, !Cil.gnu89inline, hasAttribute "gnu_inline" (typeAttrs vi.vtype) with
| Cil.C90, _, _
| _, true, _
| _, _, true -> not vi.vinline || vi.vstorage <> Extern
| _, _, _ -> not vi.vinline || vi.vstorage = Extern)
let prefix p s =
let lp = String.length p in
let ls = String.length s in
lp <= ls && String.sub s 0 lp = p
type 'a node = {
nname : string;
nfidx : int;
ndata : 'a;
mutable nloc : (location * int) option;
mutable nrep : 'a node;
mutable nmergedSyns : bool;
}
let d_nloc () (lo : (location * int) option) : P.doc =
match lo with
| None -> P.text "None"
| Some (l, idx) -> P.dprintf "Some(%d at %a)" idx d_loc l
let mkSelfNode (eq : (int * string, 'a node) H.t)
(syn : (string, 'a node) H.t) (fidx : int)
(name : string) (data : 'a) (l : (location * int) option) =
let rec res =
{
nname = name;
nfidx = fidx;
ndata = data;
nloc = l;
nrep = res;
nmergedSyns = false;
}
in
H.add eq (fidx, name) res;
if mergeSynonyms && not (prefix "__anon" name) then H.add syn name res;
res
let debugFind = false
let rec find (pathcomp : bool) (nd : 'a node) =
if debugFind then ignore (E.log " find %s(%d)\n" nd.nname nd.nfidx);
if nd.nrep == nd then (
if debugFind then ignore (E.log " = %s(%d)\n" nd.nname nd.nfidx);
nd)
else
let res = find pathcomp nd.nrep in
if usePathCompression && pathcomp && nd.nrep != res then nd.nrep <- res;
res
let union (nd1 : 'a node) (nd2 : 'a node) : 'a node * (unit -> unit) =
let nd1 = find true nd1 in
let nd2 = find true nd2 in
if nd1 == nd2 then
(
nd1,
fun x -> x )
else
let rep, norep =
if nd1.nloc != None = (nd2.nloc != None) then (
if
nd1.nfidx < nd2.nfidx
then (nd1, nd2)
else if nd1.nfidx > nd2.nfidx then (nd2, nd1)
else
match (nd1.nloc, nd2.nloc) with
| Some (_, didx1), Some (_, didx2) ->
if didx1 < didx2 then (nd1, nd2)
else if didx1 > didx2 then (nd2, nd1)
else (
ignore
(warn
"Merging two elements (%s and %s) in the same file (%d) \
with the same idx (%d) within the file"
nd1.nname nd2.nname nd1.nfidx didx1);
(nd1, nd2))
| _, _ ->
ignore
(warn
"Merging two undefined elements in the same file: %s and %s"
nd1.nname nd2.nname);
(nd1, nd2))
else if
nd1.nloc != None
then (nd1, nd2)
else (nd2, nd1)
in
let oldrep = norep.nrep in
norep.nrep <- rep;
(rep, fun () -> norep.nrep <- oldrep)
let findReplacement (pathcomp : bool) (eq : (int * string, 'a node) H.t)
(fidx : int) (name : string) : ('a * int) option =
if debugFind then ignore (E.log "findReplacement for %s(%d)\n" name fidx);
try
let nd = H.find eq (fidx, name) in
if nd.nrep == nd then (
if debugFind then ignore (E.log " is a representative\n");
None )
else
let rep = find pathcomp nd in
if rep != rep.nrep then
E.s (bug "find does not return the representative\n");
if debugFind then ignore (E.log " RES = %s(%d)\n" rep.nname rep.nfidx);
Some (rep.ndata, rep.nfidx)
with Not_found ->
if debugFind then ignore (E.log " not found in the map\n");
None
let getNode (eq : (int * string, 'a node) H.t) (syn : (string, 'a node) H.t)
(fidx : int) (name : string) (data : 'a) (l : (location * int) option) =
let debugGetNode = false in
if debugGetNode then ignore (E.log "getNode(%s(%d), %a)\n" name fidx d_nloc l);
try
let res = H.find eq (fidx, name) in
(match (res.nloc, l) with
| None, Some _ -> res.nloc <- l
| Some (old_l, old_idx), Some (l, idx) ->
if old_idx != idx then
ignore
(warn
"Duplicate definition of node %s(%d) at indices %d(%a) and \
%d(%a)"
name fidx old_idx d_loc old_l idx d_loc l)
else ()
| _, _ -> ());
if debugGetNode then ignore (E.log " node already found\n");
find false res
with Not_found ->
let res = mkSelfNode eq syn fidx name data l in
if debugGetNode then ignore (E.log " made a new one\n");
res
let dumpGraph (what : string) (eq : (int * string, 'a node) H.t) : unit =
ignore (E.log "Equivalence graph for %s is:\n" what);
H.iter
(fun (fidx, name) nd ->
ignore
(E.log " %s(%d) %s-> " name fidx
(if nd.nloc = None then "(undef)" else ""));
if nd.nrep == nd then ignore (E.log "*\n")
else ignore (E.log " %s(%d)\n" nd.nrep.nname nd.nrep.nfidx))
eq
let vEq : (int * string, varinfo node) H.t = H.create 111
let sEq : (int * string, compinfo node) H.t = H.create 111
let eEq : (int * string, enuminfo node) H.t = H.create 111
let tEq : (int * string, typeinfo node) H.t = H.create 111
let iEq : (int * string, varinfo node) H.t = H.create 111
let vSyn : (string, varinfo node) H.t = H.create 111
let iSyn : (string, varinfo node) H.t = H.create 111
let sSyn : (string, compinfo node) H.t = H.create 111
let eSyn : (string, enuminfo node) H.t = H.create 111
let tSyn : (string, typeinfo node) H.t = H.create 111
(** A global environment for variables. Put in here only the non-static
variables, indexed by their name. *)
let vEnv : (string, varinfo node) H.t = H.create 111
let inlineBodies : (P.doc, varinfo node) H.t = H.create 111
(** A number of alpha conversion tables. We ought to keep one table for each
name space. Unfortunately, because of the way the C lexer works, type
names must be different from variable names!! We one alpha table both for
variables and types. *)
let vtAlpha : (string, location A.alphaTableData ref) H.t = H.create 57
let sAlpha : (string, location A.alphaTableData ref) H.t = H.create 57
let eAlpha : (string, location A.alphaTableData ref) H.t = H.create 57
(** Keep track, for all global function definitions, of the names of the formal
arguments. They might change during merging of function types if the
prototype occurs after the function definition and uses different names.
We'll restore the names at the end *)
let formalNames : (int * string, string list) H.t = H.create 111
let theFileTypes = ref []
let theFile = ref []
let mergePushGlobal (g : global) : unit =
pushGlobal g ~types:theFileTypes ~variables:theFile
let mergePushGlobals gl = List.iter mergePushGlobal gl
let currentFidx = ref 0
let currentDeclIdx = ref 0
let fileNames : (int, string) H.t = H.create 113
let emittedCompDecls : (string, bool) H.t = H.create 113
let emittedVarDecls : (string, bool) H.t = H.create 113
let emittedFunDefn : (string, fundec * location * int) H.t = H.create 113
let emittedVarDefn : (string, varinfo * init option * location) H.t =
H.create 113
(** A mapping from the new names to the original names. Used in PASS2 when we
rename variables. *)
let originalVarNames : (string, string) H.t = H.create 113
let init () =
H.clear sAlpha;
H.clear eAlpha;
H.clear vtAlpha;
H.clear vEnv;
H.clear vEq;
H.clear sEq;
H.clear eEq;
H.clear tEq;
H.clear iEq;
H.clear vSyn;
H.clear sSyn;
H.clear eSyn;
H.clear tSyn;
H.clear iSyn;
theFile := [];
theFileTypes := [];
H.clear formalNames;
H.clear inlineBodies;
currentFidx := 0;
currentDeclIdx := 0;
H.clear fileNames;
H.clear emittedVarDecls;
H.clear emittedCompDecls;
H.clear emittedFunDefn;
H.clear emittedVarDefn;
H.clear originalVarNames
let intEnumInfo =
{
ename = "!!!intEnumInfo!!!";
eitems = [];
eattr = [];
ereferenced = false;
ekind = IInt;
}
let intEnumInfoNode =
getNode eEq eSyn 0 intEnumInfo.ename intEnumInfo (Some (locUnknown, 0))
type combineWhat =
| CombineFundef
| CombineFunarg
| CombineFunret
| CombineOther
(** Construct the composite type of [oldt] and [t] if they are compatible.
Raise [Failure] if they are incompatible. *)
let rec combineTypes (what : combineWhat) (oldfidx : int) (oldt : typ)
(fidx : int) (t : typ) : typ =
let oldq, olda = partitionQualifierAttributes (typeAttrsOuter oldt) in
let q, a = partitionQualifierAttributes (typeAttrsOuter t) in
if oldq <> q then
raise
(Failure
(P.sprint ~width:80
(P.dprintf "(different type qualifiers %a and %a)" d_attrlist oldq
d_attrlist q)))
else if q <> [] then
typeAddAttributes q
(combineTypes what oldfidx (setTypeAttrs oldt olda) fidx
(setTypeAttrs t a))
else
match (oldt, t) with
| TVoid olda, TVoid a -> TVoid (addAttributes olda a)
| TInt (oldik, olda), TInt (ik, a) ->
let combineIK oldk k =
if oldk == k then oldk
else if
oldk = IInt
&& bitsSizeOf t <= 32
&& (what = CombineFunarg || what = CombineFunret)
then k
else
let msg =
P.sprint ~width:80
(P.dprintf "(different integer types %a and %a)" d_type oldt
d_type t)
in
raise (Failure msg)
in
TInt (combineIK oldik ik, addAttributes olda a)
| TFloat (oldfk, olda), TFloat (fk, a) ->
let combineFK oldk k =
if oldk == k then oldk
else if
oldk = FDouble && k = FFloat
&& (what = CombineFunarg || what = CombineFunret)
then k
else raise (Failure "(different floating point types)")
in
TFloat (combineFK oldfk fk, addAttributes olda a)
| TEnum (oldei, olda), TEnum (ei, a) ->
matchEnumInfo oldfidx oldei fidx ei;
TEnum (oldei, addAttributes olda a)
| TEnum (oldei, olda), TInt (IInt, a) ->
TEnum (oldei, addAttributes olda a)
| TInt (IInt, olda), TEnum (ei, a) -> TEnum (ei, addAttributes olda a)
| TComp (oldci, olda), TComp (ci, a) ->
matchCompInfo oldfidx oldci fidx ci;
TComp (oldci, addAttributes olda a)
| TArray (oldbt, oldsz, olda), TArray (bt, sz, a) ->
let combbt = combineTypes CombineOther oldfidx oldbt fidx bt in
let combinesz =
match (oldsz, sz) with
| None, Some _ -> sz
| Some _, None -> oldsz
| None, None -> oldsz
| Some oldsz', Some sz' ->
let samesz =
match (constFold true oldsz', constFold true sz') with
| Const (CInt (oldi, _, _)), Const (CInt (i, _, _)) ->
Cilint.compare_cilint oldi i = 0
| _, _ -> false
in
if samesz then oldsz
else raise (Failure "(different array sizes)")
in
TArray (combbt, combinesz, addAttributes olda a)
| TPtr (oldbt, olda), TPtr (bt, a) ->
TPtr
(combineTypes CombineOther oldfidx oldbt fidx bt, addAttributes olda a)
| TFun (_, _, _, [ Attr ("missingproto", _) ]), TFun _ -> t
| TFun _, TFun (_, _, _, [ Attr ("missingproto", _) ]) -> oldt
| TFun (oldrt, oldargs, oldva, olda), TFun (rt, args, va, a) ->
let newrt =
combineTypes
(if what = CombineFundef then CombineFunret else CombineOther)
oldfidx oldrt fidx rt
in
if oldva != va then raise (Failure "(different vararg specifiers)");
let newargs =
if oldargs = None then args
else if args = None then oldargs
else
let oldargslist = argsToList oldargs in
let argslist = argsToList args in
if List.length oldargslist <> List.length argslist then
raise (Failure "(different number of arguments)")
else
Some
(List.map2
(fun (on, ot, oa) (an, at, aa) ->
let n = if an <> "" then an else on in
let t =
combineTypes
(if what = CombineFundef then CombineFunarg
else CombineOther)
oldfidx
(removeOuterQualifierAttributes ot)
fidx
(removeOuterQualifierAttributes at)
in
let a = addAttributes oa aa in
(n, t, a))
oldargslist argslist)
in
TFun (newrt, newargs, oldva, addAttributes olda a)
| TBuiltin_va_list olda, TBuiltin_va_list a ->
TBuiltin_va_list (addAttributes olda a)
| TNamed (oldt, olda), TNamed (t, a) ->
matchTypeInfo oldfidx oldt fidx t;
TNamed (oldt, addAttributes olda a)
| _, TNamed (t, a) ->
let res = combineTypes what oldfidx oldt fidx t.ttype in
typeAddAttributes a res
| TNamed (oldt, a), _ ->
let res = combineTypes what oldfidx oldt.ttype fidx t in
typeAddAttributes a res
| _ ->
let msg : string =
P.sprint ~width:1000
(P.dprintf "(different type constructors: %a vs. %a)" d_type oldt
d_type t)
in
raise (Failure msg)
and matchCompInfo (oldfidx : int) (oldci : compinfo) (fidx : int)
(ci : compinfo) : unit =
if oldci.cstruct <> ci.cstruct then
raise (Failure "(different struct/union types)");
let oldcinode = getNode sEq sSyn oldfidx oldci.cname oldci None in
let cinode = getNode sEq sSyn fidx ci.cname ci None in
if oldcinode == cinode then
()
else
let oldci = oldcinode.ndata in
let oldfidx = oldcinode.nfidx in
let ci = cinode.ndata in
let fidx = cinode.nfidx in
let old_len = List.length oldci.cfields in
let len = List.length ci.cfields in
if len <> 0 && old_len <> 0 && old_len <> len then (
let curLoc = !currentLoc in
trace "merge"
(P.dprintf "different # of fields\n%d: %a\n%d: %a\n" old_len d_global
(GCompTag (oldci, locUnknown))
len d_global
(GCompTag (ci, locUnknown)));
currentLoc := curLoc;
let msg =
Printf.sprintf "(different number of fields in %s and %s: %d != %d.)"
oldci.cname ci.cname old_len len
in
raise (Failure msg));
let newrep, undo = union oldcinode cinode in
if old_len = len then (
try
List.iter2
(fun oldf f ->
if oldf.fbitfield <> f.fbitfield then
raise (Failure "(different bitfield info)");
if oldf.fattr <> f.fattr then
raise (Failure "(different field attributes)");
let newtype =
combineTypes CombineOther oldfidx oldf.ftype fidx f.ftype
in
oldf.ftype <- newtype)
oldci.cfields ci.cfields
with Failure reason ->
undo ();
let msg =
P.sprint ~width:80
(P.dprintf
"\n\tFailed assumption that %s and %s are isomorphic %s@!%a@!%a"
(compFullName oldci) (compFullName ci) reason dn_global
(GCompTag (oldci, locUnknown))
dn_global
(GCompTag (ci, locUnknown)))
in
raise (Failure msg))
else if
old_len = 0
then oldci.cfields <- ci.cfields;
newrep.ndata.cattr <- addAttributes oldci.cattr ci.cattr;
()
and matchEnumInfo (oldfidx : int) (oldei : enuminfo) (fidx : int)
(ei : enuminfo) : unit =
let oldeinode = getNode eEq eSyn oldfidx oldei.ename oldei None in
let einode = getNode eEq eSyn fidx ei.ename ei None in
if oldeinode == einode then
()
else
let oldei = oldeinode.ndata in
let ei = einode.ndata in
try
if List.length oldei.eitems <> List.length ei.eitems then
raise (Failure "(different number of enumeration elements)");
List.iter2
(fun (old_iname, old_iv, _) (iname, iv, _) ->
if old_iname <> iname then
raise (Failure "(different names for enumeration items)");
let samev =
match (constFold true old_iv, constFold true iv) with
| Const (CInt (oldi, _, _)), Const (CInt (i, _, _)) ->
Cilint.compare_cilint oldi i = 0
| _ -> false
in
if not samev then
raise (Failure "(different values for enumeration items)"))
oldei.eitems ei.eitems;
let newrep, _ = union oldeinode einode in
newrep.ndata.eattr <- addAttributes oldei.eattr ei.eattr;
()
with Failure msg ->
(if oldeinode != intEnumInfoNode then
let _ = union oldeinode intEnumInfoNode in
());
if einode != intEnumInfoNode then
let _ = union einode intEnumInfoNode in
()
and matchTypeInfo (oldfidx : int) (oldti : typeinfo) (fidx : int)
(ti : typeinfo) : unit =
if oldti.tname = "" || ti.tname = "" then
E.s (bug "matchTypeInfo for anonymous type\n");
let oldtnode = getNode tEq tSyn oldfidx oldti.tname oldti None in
let tnode = getNode tEq tSyn fidx ti.tname ti None in
if oldtnode == tnode then
()
else
let oldti = oldtnode.ndata in
let oldfidx = oldtnode.nfidx in
let ti = tnode.ndata in
let fidx = tnode.nfidx in
(try ignore (combineTypes CombineOther oldfidx oldti.ttype fidx ti.ttype)
with Failure reason ->
let msg =
P.sprint ~width:80
(P.dprintf "\n\tFailed assumption that %s and %s are isomorphic %s"
oldti.tname ti.tname reason)
in
raise (Failure msg));
let _ = union oldtnode tnode in
()
let oneFilePass1 (f : file) : unit =
H.add fileNames !currentFidx f.fileName;
if debugMerge || !E.verboseFlag then
ignore (E.log "Pre-merging (%d) %s\n" !currentFidx f.fileName);
currentDeclIdx := 0;
if f.globinitcalled || f.globinit <> None then
E.s (E.warn "Merging file %s has global initializer" f.fileName);
let matchVarinfo (vi : varinfo) (l : location * int) =
ignore
(Alpha.registerAlphaName ~alphaTable:vtAlpha ~undolist:None
~lookupname:vi.vname ~data:!currentLoc);
let vinode = mkSelfNode vEq vSyn !currentFidx vi.vname vi (Some l) in
try
let oldvinode = find true (H.find vEnv vi.vname) in
let oldloc, _ =
match oldvinode.nloc with
| None -> E.s (bug "old variable is undefined")
| Some l -> l
in
let oldvi = oldvinode.ndata in
let newtype =
try
combineTypes CombineOther oldvinode.nfidx oldvi.vtype !currentFidx
vi.vtype
with Failure reason ->
let f = if !ignore_merge_conflicts then warn else error in
ignore
(f
"Incompatible declaration for %s (from %s(%d)).@! Previous was \
at %a (from %s (%d)) %s "
vi.vname
(H.find fileNames !currentFidx)
!currentFidx d_loc oldloc
(H.find fileNames oldvinode.nfidx)
oldvinode.nfidx reason);
raise Not_found
in
let newrep, _ = union oldvinode vinode in
if
hasAttribute "const" (typeAttrs vi.vtype)
!= hasAttribute "const" (typeAttrs oldvi.vtype)
|| hasAttribute "pconst" (typeAttrs vi.vtype)
!= hasAttribute "pconst" (typeAttrs oldvi.vtype)
then
newrep.ndata.vtype <- typeRemoveAttributes [ "const"; "pconst" ] newtype
else newrep.ndata.vtype <- newtype;
let newstorage =
if vi.vstorage = oldvi.vstorage || vi.vstorage = Extern then
oldvi.vstorage
else if oldvi.vstorage = Extern then vi.vstorage
else if oldvi.vstorage = Static && vi.vstorage = NoStorage then Static
else (
ignore
(warn
"Inconsistent storage specification for %s. Now is %a and \
previous was %a at %a"
vi.vname d_storage vi.vstorage d_storage oldvi.vstorage d_loc
oldloc);
vi.vstorage)
in
newrep.ndata.vstorage <- newstorage;
newrep.ndata.vattr <- addAttributes oldvi.vattr vi.vattr;
()
with Not_found ->
H.add vEnv vi.vname vinode
in
List.iter
(function
| GVarDecl (vi, l) | GVar (vi, _, l) ->
currentLoc := l;
incr currentDeclIdx;
vi.vreferenced <- false;
if externallyVisible vi then matchVarinfo vi (l, !currentDeclIdx)
| GFun (fdec, l) ->
currentLoc := l;
incr currentDeclIdx;
let _, args, _, _ = splitFunctionTypeVI fdec.svar in
H.add formalNames
(!currentFidx, fdec.svar.vname)
(Util.list_map (fun (fn, _, _) -> fn) (argsToList args));
fdec.svar.vreferenced <- false;
if externallyVisible fdec.svar then
matchVarinfo fdec.svar (l, !currentDeclIdx)
else if fdec.svar.vinline && !merge_inlines then
ignore
(getNode iEq iSyn !currentFidx fdec.svar.vname fdec.svar
(Some (l, !currentDeclIdx)))
| GType (t, l) -> (
incr currentDeclIdx;
t.treferenced <- false;
if t.tname <> "" then
ignore
(getNode tEq tSyn !currentFidx t.tname t
(Some (l, !currentDeclIdx)))
else
match t.ttype with
| TComp (ci, _) ->
ci.creferenced <- false;
ignore (getNode sEq sSyn !currentFidx ci.cname ci None)
| TEnum (ei, _) ->
ei.ereferenced <- false;
ignore (getNode eEq eSyn !currentFidx ei.ename ei None)
| _ -> E.s (bug "Anonymous Gtype is not TComp"))
| GCompTag (ci, l) ->
incr currentDeclIdx;
ci.creferenced <- false;
ignore
(getNode sEq sSyn !currentFidx ci.cname ci
(Some (l, !currentDeclIdx)))
| GEnumTag (ei, l) ->
incr currentDeclIdx;
ei.ereferenced <- false;
ignore
(getNode eEq eSyn !currentFidx ei.ename ei
(Some (l, !currentDeclIdx)))
| _ -> ())
f.globals
let doMergeSynonyms (syn : (string, 'a node) H.t)
(eq : (int * string, 'a node) H.t)
(compare : int -> 'a -> int -> 'a -> unit) : unit =
H.iter
(fun n node ->
if not node.nmergedSyns then
let all = H.find_all syn n in
let tryone (classes : 'a node list)
(nd : 'a node) : 'a node list
=
nd.nmergedSyns <- true;
let rec compareWithClasses = function
| [] -> [ nd ]
| c :: restc -> (
try
compare c.nfidx c.ndata nd.nfidx nd.ndata;
c :: restc
with Failure _ ->
c :: compareWithClasses restc)
in
compareWithClasses classes
in
let _ = List.fold_left tryone [] all in
())
syn
let matchInlines (oldfidx : int) (oldi : varinfo) (fidx : int) (i : varinfo) =
let oldinode = getNode iEq iSyn oldfidx oldi.vname oldi None in
let inode = getNode iEq iSyn fidx i.vname i None in
if oldinode == inode then ()
else
let oldi = oldinode.ndata in
let oldfidx = oldinode.nfidx in
let i = inode.ndata in
let fidx = inode.nfidx in
oldi.vtype <- combineTypes CombineOther oldfidx oldi.vtype fidx i.vtype;
oldi.vattr <- addAttributes oldi.vattr i.vattr;
()
(** Keep track of the functions we have used already in the file. We need
this to avoid removing an inline function that has been used already.
This can only occur if the inline function is defined after it is used
already; a bad style anyway *)
let varUsedAlready : (string, unit) H.t = H.create 111
(** A visitor that renames uses of variables and types *)
class renameVisitorClass =
object (self)
inherit nopCilVisitor
method! vvdec (vi : varinfo) = DoChildren
method! vglob (g : global) : global list visitAction =
match g with
| GVar (v, init, loc) ->
let update_init glob =
match glob with
| GVar (u, uinit, loc) -> GVar (u, u.vinit, loc)
| _ -> glob
in
let update_all_inits = List.map update_init in
let () =
match (v.vinit.init, init.init) with
| None, None -> ()
| None, Some _ -> v.vinit.init <- init.init
| Some _, None -> assert false
| Some _, Some _ -> ()
in
ChangeDoChildrenPost ([ g ], update_all_inits)
| _ -> DoChildren
method! vvrbl (vi : varinfo) : varinfo visitAction =
if not vi.vglob then DoChildren
else if vi.vreferenced then (
H.add varUsedAlready vi.vname ();
DoChildren)
else
match findReplacement true vEq !currentFidx vi.vname with
| None -> DoChildren
| Some (vi', oldfidx) ->
if debugMerge then
ignore
(E.log "Renaming use of var %s(%d) to %s(%d)\n" vi.vname
!currentFidx vi'.vname oldfidx);
vi'.vreferenced <- true;
H.add varUsedAlready vi'.vname ();
ChangeTo vi'
method! vtype (t : typ) =
match t with
| TComp (ci, a) when not ci.creferenced -> (
match findReplacement true sEq !currentFidx ci.cname with
| None -> DoChildren
| Some (ci', oldfidx) ->
if debugMerge then
ignore
(E.log "Renaming use of %s(%d) to %s(%d)\n" ci.cname
!currentFidx ci'.cname oldfidx);
ChangeTo (TComp (ci', visitCilAttributes (self :> cilVisitor) a)))
| TEnum (ei, a) when not ei.ereferenced -> (
match findReplacement true eEq !currentFidx ei.ename with
| None -> DoChildren
| Some (ei', _) ->
if ei' == intEnumInfo then
ChangeTo
(TInt (IInt, visitCilAttributes (self :> cilVisitor) a))
else
ChangeTo
(TEnum (ei', visitCilAttributes (self :> cilVisitor) a)))
| TNamed (ti, a) when not ti.treferenced -> (
match findReplacement true tEq !currentFidx ti.tname with
| None -> DoChildren
| Some (ti', _) ->
ChangeTo (TNamed (ti', visitCilAttributes (self :> cilVisitor) a))
)
| _ -> DoChildren
method! voffs =
function
| Field (f, o) -> (
if
f.fcomp.creferenced then DoChildren
else
match findReplacement true sEq !currentFidx f.fcomp.cname with
| None -> DoChildren
| Some (ci', oldfidx) ->
let rec indexOf (i : int) = function
| [] ->
E.s
(bug "Cannot find field %s in %s(%d)\n" f.fname
(compFullName f.fcomp) !currentFidx)
| f' :: rest when f' == f -> i
| _ :: rest -> indexOf (i + 1) rest
in
let index = indexOf 0 f.fcomp.cfields in
if List.length ci'.cfields <= index then
E.s
(bug "Too few fields in replacement %s(%d) for %s(%d)\n"
(compFullName ci') oldfidx (compFullName f.fcomp)
!currentFidx);
let f' = List.nth ci'.cfields index in
ChangeDoChildrenPost (Field (f', o), fun x -> x))
| _ -> DoChildren
method! vinitoffs o = self#voffs o
end
let renameVisitor = new renameVisitorClass
(** A visitor that renames uses of inline functions that were discovered in
pass 2 to be used before they are defined. This is like the renameVisitor
except it only looks at the variables (thus it is a bit more efficient)
and it also renames forward declarations of the inlines to be removed. *)
class renameInlineVisitorClass =
object (self)
inherit nopCilVisitor
method! vvrbl (vi : varinfo) : varinfo visitAction =
if not vi.vglob then DoChildren
else if vi.vreferenced then
DoChildren
else
match findReplacement true vEq !currentFidx vi.vname with
| None -> DoChildren
| Some (vi', oldfidx) ->
if debugMerge then
ignore
(E.log "Renaming var %s(%d) to %s(%d)\n" vi.vname !currentFidx
vi'.vname oldfidx);
vi'.vreferenced <- true;
ChangeTo vi'
method! vglob =
function
| GVarDecl (vi, l) when vi.vinline -> (
let origname =
try H.find originalVarNames vi.vname with Not_found -> vi.vname
in
match findReplacement true vEq !currentFidx origname with
| None -> DoChildren
| Some (vi', _) -> ChangeTo [ GVarDecl (vi', l) ])
| _ -> DoChildren
end
let renameInlinesVisitor = new renameInlineVisitorClass
let functionChecksum (dec : fundec) : int =
let rec stmtListSum (lst : stmt list) : int =
List.fold_left (fun acc s -> acc + stmtSum s) 0 lst
and stmtSum (s : stmt) : int =
match s.skind with
| Instr l -> 13 + (67 * List.length l)
| Return _ -> 17
| Goto _ -> 19
| ComputedGoto _ -> 131
| Break _ -> 23
| Continue _ -> 29
| If (_, b1, b2, _, _) -> 31 + (37 * stmtListSum b1.bstmts) + (41 * stmtListSum b2.bstmts)
| Switch (_, b, _, _, _) -> 43 + (47 * stmtListSum b.bstmts)
| Loop (b, _, _, _, _) -> 49 + (53 * stmtListSum b.bstmts)
| Block b -> 59 + (61 * stmtListSum b.bstmts)
in
let a, b, c, d, e =
( List.length dec.sformals,
0 ,
0 ,
List.length dec.sbody.bstmts,
stmtListSum dec.sbody.bstmts )
in
(2 * a) + (3 * b) + (5 * c) + (7 * d) + (11 * e)
let rec equalInits (x : init) (y : init) : bool =
match (x, y) with
| SingleInit xe, SingleInit ye -> equalExps xe ye
| CompoundInit (xt, xoil), CompoundInit (yt, yoil) ->
let rec equalLists xoil yoil : bool =
match (xoil, yoil) with
| (xo, xi) :: xrest, (yo, yi) :: yrest -> equalOffsets xo yo && equalInits xi yi && equalLists xrest yrest
| [], [] -> true
| _, _ -> false
in
equalLists xoil yoil
| _, _ -> false
and equalOffsets (x : offset) (y : offset) : bool =
match (x, y) with
| NoOffset, NoOffset -> true
| Field (xfi, xo), Field (yfi, yo) ->
xfi.fname = yfi.fname
&&
equalOffsets xo yo
| Index (xe, xo), Index (ye, yo) -> equalExps xe ye && equalOffsets xo yo
| _, _ -> false
and equalExps (x : exp) (y : exp) : bool =
match (x, y) with
| Const xc, Const yc -> (
xc = yc
||
match (xc, yc) with
| CInt (a, _, _), CInt (b, _, _) -> Cilint.is_zero_cilint a && Cilint.is_zero_cilint b
| _, _ -> false)
| Lval xl, Lval yl -> equalLvals xl yl
| SizeOf xt, SizeOf yt ->
true
| SizeOfE xe, SizeOfE ye -> equalExps xe ye
| AlignOf xt, AlignOf yt -> true
| AlignOfE xe, AlignOfE ye -> equalExps xe ye
| UnOp (xop, xe, xt), UnOp (yop, ye, yt) ->
xop = yop && equalExps xe ye && true
| BinOp (xop, xe1, xe2, xt), BinOp (yop, ye1, ye2, yt) ->
xop = yop && equalExps xe1 ye1 && equalExps xe2 ye2 && true
| CastE (xt, xe), CastE (yt, ye) ->
equalExps xe ye
| AddrOf xl, AddrOf yl -> equalLvals xl yl
| StartOf xl, StartOf yl -> equalLvals xl yl
| CastE (xt, xe), ye -> equalExps xe ye
| xe, CastE (yt, ye) -> equalExps xe ye
| _, _ -> false
and equalLvals (x : lval) (y : lval) : bool =
match (x, y) with
| (Var xv, xo), (Var yv, yo) ->
equalOffsets xo yo
| (Mem xe, xo), (Mem ye, yo) -> equalExps xe ye && equalOffsets xo yo
| _, _ -> false
let equalInitOpts (x : init option) (y : init option) : bool =
match (x, y) with
| None, None -> true
| Some xi, Some yi -> equalInits xi yi
| _, _ -> false
let printInlineForComparison fdec' g' =
let oldprintln = !lineDirectiveStyle in
lineDirectiveStyle := None;
let newname = fdec'.svar.vname in
fdec'.svar.vname <- "@@alphaname@@";
let nameId = ref 0 in
let oldNames : string list ref = ref [] in
let renameOne (v : varinfo) =
oldNames := v.vname :: !oldNames;
incr nameId;
v.vname <- "___alpha" ^ string_of_int !nameId
in
let undoRenameOne (v : varinfo) =
match !oldNames with
| n :: rest ->
oldNames := rest;
v.vname <- n
| _ -> E.s (bug "undoRenameOne")
in
let origType = fdec'.svar.vtype in
if mergeInlinesWithAlphaConvert () then (
List.iter renameOne fdec'.sformals;
setFormals fdec' fdec'.sformals;
List.iter renameOne fdec'.slocals);
let res = d_global () g' in
lineDirectiveStyle := oldprintln;
fdec'.svar.vname <- newname;
if mergeInlinesWithAlphaConvert () then (
List.iter undoRenameOne (List.rev fdec'.slocals);
List.iter undoRenameOne (List.rev fdec'.sformals);
fdec'.svar.vtype <- origType);
res
let oneFilePass2 (f : file) =
if debugMerge || !E.verboseFlag then
ignore (E.log "Final merging phase (%d): %s\n" !currentFidx f.fileName);
currentDeclIdx := 0;
H.clear varUsedAlready;
H.clear originalVarNames;
let repeatPass2 = ref false in
let savedTheFile = !theFile in
let processOneGlobal (g : global) : unit =
let processVarinfo ~isadef (vi : varinfo) (vloc : location) : varinfo =
if vi.vreferenced then vi
else if not (externallyVisible vi) then
(
let newName, _ = A.newAlphaName ~alphaTable:vtAlpha ~undolist:None ~lookupname:vi.vname ~data:!currentLoc in
H.add originalVarNames newName vi.vname;
if debugMerge then ignore (E.log "renaming %s at %a to %s\n" vi.vname d_loc vloc newName);
vi.vname <- newName;
vi.vid <- newVID ();
vi.vreferenced <- true;
vi
)
else
match findReplacement true vEq !currentFidx vi.vname with
| None -> vi
| Some (vi', _) ->
vi'.vreferenced <- true;
vi'.vaddrof <- vi.vaddrof || vi'.vaddrof;
if isadef then
vi'.vdecl <- vi.vdecl;
vi'
in
try
match g with
| GVarDecl (vi, l) as g ->
currentLoc := l;
incr currentDeclIdx;
let vi' = processVarinfo ~isadef:false vi l in
if vi != vi' then ()
else if H.mem emittedVarDecls vi'.vname then
()
else (
H.add emittedVarDecls vi'.vname true;
mergePushGlobals (visitCilGlobal renameVisitor g))
| GVar (vi, init, l) ->
currentLoc := l;
incr currentDeclIdx;
let vi' = processVarinfo ~isadef:(init.init <> None) vi l in
H.add emittedVarDecls vi.vname true;
if mergeGlobals then
match H.find_opt emittedVarDefn vi'.vname with
| None ->
H.add emittedVarDefn vi'.vname (vi', init.init, l);
mergePushGlobals (visitCilGlobal renameVisitor (GVar (vi', init, l)))
| Some (prevVar, prevInitOpt, prevLoc) ->
if equalInitOpts prevInitOpt init.init || init.init = None then
trace "mergeGlob" (P.dprintf "dropping global var %s at %a in favor of the one at %a\n" vi'.vname d_loc l d_loc prevLoc)
else if prevInitOpt = None then
mergePushGlobals (visitCilGlobal renameVisitor (GVar (vi', init, l)))
else
E.s (error "global var %s at %a has different initializer than %a" vi'.vname d_loc l d_loc prevLoc)
else
mergePushGlobals (visitCilGlobal renameVisitor (GVar (vi', init, l)))
| GFun (fdec, l) as g ->
currentLoc := l;
incr currentDeclIdx;
fdec.svar <- processVarinfo ~isadef:true fdec.svar l;
let origname = try H.find originalVarNames fdec.svar.vname with Not_found -> fdec.svar.vname in
let fdec' = match visitCilGlobal renameVisitor g with
| [ GFun (fdec', _) ] -> fdec'
| _ -> E.s (unimp "renameVisitor for GFun returned something else")
in
let g' = GFun (fdec', l) in
let _, args, _, _ = splitFunctionTypeVI fdec'.svar in
(match H.find_opt formalNames (!currentFidx, origname) with
| Some oldnames ->
let argl = argsToList args in
if List.length oldnames <> List.length argl then E.s (unimp "After merging the function has more arguments");
List.iter2 (fun oldn a -> if oldn <> "" then a.vname <- oldn) oldnames fdec.sformals;
setFormals fdec fdec.sformals
| None -> ignore (warnOpt "Cannot find %s in formalNames" origname)
);
if fdec'.svar.vinline && !merge_inlines then (
let printout = printInlineForComparison fdec' g' in
let inode = getNode vEq vSyn !currentFidx origname fdec'.svar (Some (l, !currentDeclIdx))
in
if debugInlines then (
ignore (E.log "getNode %s(%d) with loc=%a. declidx=%d\n" inode.nname inode.nfidx d_nloc inode.nloc !currentDeclIdx);
ignore (E.log "Looking for previous definition of inline %s(%d)\n" origname !currentFidx)
);
try
let oldinode = H.find inlineBodies printout in
if debugInlines then ignore (E.log " Matches %s(%d)\n" oldinode.nname oldinode.nfidx);
if H.mem varUsedAlready fdec'.svar.vname then
if mergeInlinesRepeat () then repeatPass2 := true
else (
ignore (warn "Inline function %s because it is used before it is defined" fdec'.svar.vname);
raise Not_found);
let _ = union oldinode inode in
fdec'.svar.vreferenced <- false;
fdec'.svar.vname <- origname;
()
with Not_found ->
if debugInlines then ignore (E.log " Not found\n");
H.add inlineBodies printout inode;
mergePushGlobal g')
else if mergeGlobals && not (fdec'.svar.vstorage = Static || fdec'.svar.vinline) then (
let sum = functionChecksum fdec' in
match H.find_opt emittedFunDefn fdec'.svar.vname with
| None ->
mergePushGlobal g';
H.add emittedFunDefn fdec'.svar.vname (fdec', l, sum)
| Some (prevFun, prevLoc, prevSum) ->
if sum = prevSum then
trace "mergeGlob" (P.dprintf "dropping duplicate def'n of func %s at %a in favor of that at %a\n" fdec'.svar.vname d_loc l d_loc prevLoc)
else
ignore (warn
"def'n of func %s at %a (sum %d) conflicts with the one at %a (sum %d); keeping the one at %a."
fdec'.svar.vname d_loc l sum d_loc prevLoc prevSum d_loc
prevLoc)
)
else
mergePushGlobal g'
| GCompTag (ci, l) as g -> (
currentLoc := l;
incr currentDeclIdx;
if ci.creferenced then ()
else
match findReplacement true sEq !currentFidx ci.cname with
| None ->
(try
let nd = H.find sEq (!currentFidx, ci.cname) in
if nd.nrep != nd then
E.s
(bug
"Setting creferenced for struct %s(%d) which is not \
root!\n"
ci.cname !currentFidx)
with Not_found ->
E.s
(bug
"Setting creferenced for struct %s(%d) which is not in \
the sEq!\n"
ci.cname !currentFidx));
let newname, _ =
A.newAlphaName ~alphaTable:sAlpha ~undolist:None
~lookupname:ci.cname ~data:!currentLoc
in
ci.cname <- newname;
ci.creferenced <- true;
ci.ckey <- H.hash (compFullName ci);
H.add emittedCompDecls ci.cname true;
mergePushGlobals (visitCilGlobal renameVisitor g)
| Some (oldci, oldfidx) ->
())
| GEnumTag (ei, l) as g -> (
currentLoc := l;
incr currentDeclIdx;
if ei.ereferenced then ()
else
match findReplacement true eEq !currentFidx ei.ename with
| None ->
let newname, _ =
A.newAlphaName ~alphaTable:eAlpha ~undolist:None
~lookupname:ei.ename ~data:!currentLoc
in
ei.ename <- newname;
ei.ereferenced <- true;
ei.eitems <-
Util.list_map
(fun (n, i, loc) ->
let newname, _ =
A.newAlphaName ~alphaTable:vtAlpha ~undolist:None
~lookupname:n ~data:!currentLoc
in
(newname, i, loc))
ei.eitems;
mergePushGlobals (visitCilGlobal renameVisitor g)
| Some (ei', _) ->
())
| GCompTagDecl (ci, l) ->
currentLoc := l;
if H.mem emittedCompDecls ci.cname then ()
else (
H.add emittedCompDecls ci.cname true;
mergePushGlobal g)
| GEnumTagDecl (ei, l) ->
currentLoc := l;
mergePushGlobal g
| GType (ti, l) as g -> (
currentLoc := l;
incr currentDeclIdx;
if ti.treferenced then ()
else
match findReplacement true tEq !currentFidx ti.tname with
| None ->
let newname, _ =
A.newAlphaName ~alphaTable:vtAlpha ~undolist:None
~lookupname:ti.tname ~data:!currentLoc
in
ti.tname <- newname;
ti.treferenced <- true;
mergePushGlobals (visitCilGlobal renameVisitor g)
| Some (ti', _) ->
())
| g -> mergePushGlobals (visitCilGlobal renameVisitor g)
with e ->
let globStr : string =
P.sprint ~width:1000
(P.dprintf "error when merging global %a: %s" d_global g
(Printexc.to_string e))
in
ignore (E.log "%s" globStr);
mergePushGlobal
(GText (P.sprint ~width:80 (P.dprintf "/* error at %t:" d_thisloc)));
mergePushGlobal g;
mergePushGlobal (GText "*************** end of error*/");
raise e
in
List.iter processOneGlobal f.globals;
if mergeInlinesRepeat () && !repeatPass2 then (
if debugMerge || !E.verboseFlag then
ignore
(E.log "Repeat final merging phase (%d): %s\n" !currentFidx f.fileName);
let theseGlobals : global list ref = ref [] in
let rec scanUntil (tail : 'a list) (l : 'a list) =
if tail == l then ()
else
match l with
| [] -> E.s (bug "mergecil: scanUntil could not find the marker\n")
| g :: rest ->
theseGlobals := g :: !theseGlobals;
scanUntil tail rest
in
theseGlobals := [];
scanUntil savedTheFile !theFile;
theFile := savedTheFile;
List.iter
(fun g -> theFile := visitCilGlobal renameInlinesVisitor g @ !theFile)
!theseGlobals
)
let merge (files : file list) (newname : string) : file =
init ();
currentFidx := 0;
List.iter
(fun f ->
oneFilePass1 f;
incr currentFidx)
files;
if mergeSynonyms then (
doMergeSynonyms sSyn sEq matchCompInfo;
doMergeSynonyms eSyn eEq matchEnumInfo;
doMergeSynonyms tSyn tEq matchTypeInfo;
if !merge_inlines then (
H.iter (fun k n -> H.add vEq k n) iEq;
doMergeSynonyms iSyn iEq matchInlines));
if debugMerge then (
dumpGraph "type" tEq;
dumpGraph "struct and union" sEq;
dumpGraph "enum" eEq;
dumpGraph "variable" vEq;
if !merge_inlines then dumpGraph "inline" iEq);
currentFidx := 0;
List.iter
(fun f ->
oneFilePass2 f;
incr currentFidx)
files;
let rec revonto acc = function [] -> acc | x :: t -> revonto (x :: acc) t in
let res =
{
fileName = newname;
globals = revonto (revonto [] !theFile) !theFileTypes;
globinit = None;
globinitcalled = false;
}
in
init ();
uniqueVarNames res;
res