-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathmergecil.ml
1790 lines (1631 loc) · 69.5 KB
/
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
(*
*
* Copyright (c) 2001-2002,
* George C. Necula <[email protected]>
* Scott McPeak <[email protected]>
* Wes Weimer <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. The names of the contributors may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*)
(* mergecil.ml *)
(* This module is responsible for merging multiple CIL source trees into
* a single, coherent CIL tree which contains the union of all the
* definitions in the source files. It effectively acts like a linker,
* but at the source code level instead of the object code level. *)
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
(* Try to merge structure with the same name. However, do not complain if
* they are not the same *)
let mergeSynonyms = true
(** Whether to use path compression *)
let usePathCompression = false
(* Try to merge definitions of inline functions. They can appear in multiple
* files and we would like them all to be the same. This can slow down the
* merger an order of magnitude !!! *)
let mergeInlines = true
let mergeInlinesRepeat = mergeInlines && true
let mergeInlinesWithAlphaConvert = mergeInlines && true
(* when true, merge duplicate definitions of externally-visible functions;
* this uses a mechanism which is faster than the one for inline functions,
* but only probabilistically accurate *)
let mergeGlobals = true
(* Return true if 's' starts with the prefix 'p' *)
let prefix p s =
let lp = String.length p in
let ls = String.length s in
lp <= ls && String.sub s 0 lp = p
(* A name is identified by the index of the file in which it occurs (starting
* at 0 with the first file) and by the actual name. We'll keep name spaces
* separate *)
(* We define a data structure for the equivalence classes *)
type 'a node =
{ nname: string; (* The actual name *)
nfidx: int; (* The file index *)
ndata: 'a; (* Data associated with the node *)
mutable nloc: (location * int) option;
(* location where defined and index within the file of the definition.
* If None then it means that this node actually DOES NOT appear in the
* given file. In rare occasions we need to talk in a given file about
* types that are not defined in that file. This happens with undefined
* structures but also due to cross-contamination of types in a few of
* the cases of combineType (see the definition of combineTypes). We
* try never to choose as representatives nodes without a definition.
* We also choose as representative the one that appears earliest *)
mutable nrep: 'a node; (* A pointer to another node in its class (one
* closer to the representative). The nrep node
* is always in an earlier file, except for the
* case where a name is undefined in one file
* and defined in a later file. If this pointer
* points to the node itself then this is the
* representative. *)
mutable nmergedSyns: bool (* Whether we have merged the synonyms for
* the node of this name *)
}
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
(* Make a node with a self loop. This is quite tricky. *)
let mkSelfNode (eq: (int * string, 'a node) H.t) (* The equivalence table *)
(syn: (string, 'a node) H.t) (* The synonyms table *)
(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; (* Add it to the proper table *)
if mergeSynonyms && not (prefix "__anon" name) then
H.add syn name res;
res
let debugFind = false
(* Find the representative with or without path compression *)
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 begin
if debugFind then
ignore (E.log " = %s(%d)\n" nd.nname nd.nfidx);
nd
end else begin
let res = find pathcomp nd.nrep in
if usePathCompression && pathcomp && nd.nrep != res then
nd.nrep <- res; (* Compress the paths *)
res
end
(* Union two nodes and return the new representative. We prefer as the
* representative a node defined earlier. We try not to use as
* representatives nodes that are not defined in their files. We return a
* function for undoing the union. Make sure that between the union and the
* undo you do not do path compression *)
let union (nd1: 'a node) (nd2: 'a node) : 'a node * (unit -> unit) =
(* Move to the representatives *)
let nd1 = find true nd1 in
let nd2 = find true nd2 in
if nd1 == nd2 then begin
(* It can happen that we are trying to union two nodes that are already
* equivalent. This is because between the time we check that two nodes
* are not already equivalent and the time we invoke the union operation
* we check type isomorphism which might change the equivalence classes *)
(*
ignore (warn "unioning already equivalent nodes for %s(%d)"
nd1.nname nd1.nfidx);
*)
nd1, fun x -> x
end else begin
let rep, norep = (* Choose the representative *)
if (nd1.nloc != None) = (nd2.nloc != None) then
(* They have the same defined status. Choose the earliest *)
if nd1.nfidx < nd2.nfidx then nd1, nd2
else if nd1.nfidx > nd2.nfidx then nd2, nd1
else (* In the same file. Choose the one with the earliest index *) begin
match nd1.nloc, nd2.nloc with
Some (_, didx1), Some (_, didx2) ->
if didx1 < didx2 then nd1, nd2 else
if didx1 > didx2 then nd2, nd1
else begin
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
end
| _, _ -> (* both none. Does not matter which one we choose. Should
* not happen though. *)
(* sm: it does happen quite a bit when, e.g. merging STLport with
* some client source; I'm disabling the warning since it supposedly
* is harmless anyway, so is useless noise *)
(* sm: re-enabling on claim it now will probably not happen *)
ignore (warn "Merging two undefined elements in the same file: %s and %s" nd1.nname nd2.nname);
nd1, nd2
end
else (* One is defined, the other is not. Choose the defined one *)
if nd1.nloc != None then nd1, nd2 else nd2, nd1
in
let oldrep = norep.nrep in
norep.nrep <- rep;
rep, (fun () -> norep.nrep <- oldrep)
end
(*
let union (nd1: 'a node) (nd2: 'a node) : 'a node * (unit -> unit) =
if nd1 == nd2 && nd1.nname = "!!!intEnumInfo!!!" then begin
ignore (warn "unioning two identical nodes for %s(%d)"
nd1.nname nd1.nfidx);
nd1, fun x -> x
end else
union nd1 nd2
*)
(* Find the representative for a node and compress the paths in the process *)
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 begin
if debugFind then
ignore (E.log " is a representative\n");
None (* No replacement if this is the representative of its class *)
end 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 -> begin
if debugFind then
ignore (E.log " not found in the map\n");
None
end
(* Make a node if one does not already exist. Otherwise return the
* representative *)
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
(* Maybe we have a better location now *)
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 (* No path compression *)
with Not_found -> begin
let res = mkSelfNode eq syn fidx name data l in
if debugGetNode then
ignore (E.log " made a new one\n");
res
end
(* Dump a graph *)
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
(* For each name space we define a set of equivalence classes *)
let vEq: (int * string, varinfo node) H.t = H.create 111 (* Vars *)
let sEq: (int * string, compinfo node) H.t = H.create 111 (* Struct + union *)
let eEq: (int * string, enuminfo node) H.t = H.create 111 (* Enums *)
let tEq: (int * string, typeinfo node) H.t = H.create 111 (* Type names*)
let iEq: (int * string, varinfo node) H.t = H.create 111 (* Inlines *)
(* Sometimes we want to merge synonyms. We keep some tables indexed by names.
* Each name is mapped to multiple exntries *)
let vSyn: (string, varinfo node) H.t = H.create 111 (* Not actually used *)
let iSyn: (string, varinfo node) H.t = H.create 111 (* Inlines *)
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
(* A set of inline functions indexed by their printout ! *)
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 (* Variables and
* types *)
let sAlpha : (string, location A.alphaTableData ref) H.t
= H.create 57 (* Structures and
* unions have
* the same name
* space *)
let eAlpha : (string, location A.alphaTableData ref) H.t
= H.create 57 (* Enumerations *)
(** 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
(* Accumulate here the globals in the merged file *)
let theFileTypes = ref []
let theFile = ref []
(* add 'g' to the merged file *)
let mergePushGlobal (g: global) : unit =
pushGlobal g ~types:theFileTypes ~variables:theFile
let mergePushGlobals gl = List.iter mergePushGlobal gl
(* The index of the current file being scanned *)
let currentFidx = ref 0
let currentDeclIdx = ref 0 (* The index of the definition in a file. This is
* maintained both in pass 1 and in pass 2. Make
* sure you count the same things in both passes. *)
(* Keep here the file names *)
let fileNames : (int, string) H.t = H.create 113
(* Remember the composite types that we have already declared *)
let emittedCompDecls: (string, bool) H.t = H.create 113
(* Remember the variables also *)
let emittedVarDecls: (string, bool) H.t = H.create 113
(* also keep track of externally-visible function definitions;
* name maps to declaration, location, and semantic checksum *)
let emittedFunDefn: (string, fundec * location * int) H.t = H.create 113
(* and same for variable definitions; name maps to GVar fields *)
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
(* Initialize the module *)
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
(* Some enumerations have to be turned into an integer. We implement this by
* introducing a special enumeration type which we'll recognize later to be
* an integer *)
let intEnumInfo =
{ ename = "!!!intEnumInfo!!!"; (* This is otherwise invalid *)
eitems = [];
eattr = [];
ereferenced = false;
ekind = IInt;
}
(* And add it to the equivalence graph *)
let intEnumInfoNode =
getNode eEq eSyn 0 intEnumInfo.ename intEnumInfo
(Some (locUnknown, 0))
(* Combine the types. Raises the Failure exception with an error message.
* isdef says whether the new type is for a definition *)
type combineWhat =
CombineFundef (* The new definition is for a function definition. The old
* is for a prototype *)
| CombineFunarg (* Comparing a function argument type with an old prototype
* arg *)
| CombineFunret (* Comparing the return of a function with that from an old
* prototype *)
| CombineOther
let rec combineTypes (what: combineWhat)
(oldfidx: int) (oldt: typ)
(fidx: int) (t: typ) : typ =
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
(* GCC allows a function definition to have a more precise integer
* type than a prototype that says "int" *)
if not !msvcMode && 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
(* GCC allows a function definition to have a more precise integer
* type than a prototype that says "double" *)
if not !msvcMode && 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) ->
(* Matching enumerations always succeeds. But sometimes it maps both
* enumerations to integers *)
matchEnumInfo oldfidx oldei fidx ei;
TEnum (oldei, addAttributes olda a)
(* Strange one. But seems to be handled by GCC *)
| TEnum (oldei, olda) , TInt(IInt, a) -> TEnum(oldei,
addAttributes olda a)
(* Strange one. But seems to be handled by GCC. Warning. Here we are
* leaking types from new to old *)
| TInt(IInt, olda), TEnum (ei, a) -> TEnum(ei, addAttributes olda a)
| TComp (oldci, olda) , TComp (ci, a) ->
matchCompInfo oldfidx oldci fidx ci;
(* If we get here we were successful *)
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(CInt64(oldi, _, _)), Const(CInt64(i, _, _)) -> oldi = i
| _, _ -> 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)
(* WARNING: In this case we are leaking types from new to old !! *)
| 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 "(diferent vararg specifiers)");
(* If one does not have arguments, believe the one with the
* arguments *)
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 begin
(* Go over the arguments and update the old ones with the
* adjusted types *)
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 ot fidx at
in
let a = addAttributes oa aa in
(n, t, a))
oldargslist argslist)
end
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;
(* If we get here we were able to match *)
TNamed(oldt, addAttributes olda a)
(* Unroll first the new type *)
| _, TNamed (t, a) ->
let res = combineTypes what oldfidx oldt fidx t.ttype in
typeAddAttributes a res
(* And unroll the old type as well if necessary *)
| TNamed (oldt, a), _ ->
let res = combineTypes what oldfidx oldt.ttype fidx t in
typeAddAttributes a res
| _ -> (
(* raise (Failure "(different type constructors)") *)
let msg:string = (P.sprint 1000 (P.dprintf "(different type constructors: %a vs. %a)"
d_type oldt d_type t)) in
raise (Failure msg)
)
(* Match two compinfos and throw a Failure if they do not match *)
and matchCompInfo (oldfidx: int) (oldci: compinfo)
(fidx: int) (ci: compinfo) : unit =
if oldci.cstruct <> ci.cstruct then
raise (Failure "(different struct/union types)");
(* See if we have a mapping already *)
(* Make the nodes if not already made. Actually return the
* representatives *)
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 (* We already know they are the same *)
()
else begin
(* Replace with the representative data *)
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
(* It is easy to catch here the case when the new structure is undefined
* and the old one was defined. We just reuse the old *)
(* More complicated is the case when the old one is not defined but the
* new one is. We still reuse the old one and we'll take care of defining
* it later with the new fields.
* GN: 7/10/04, I could not find when is "later", so I added it below *)
if len <> 0 && old_len <> 0 && old_len <> len then (
let curLoc = !currentLoc in (* d_global blows this away.. *)
(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)
);
(* We check that they are defined in the same way. While doing this there
* might be recursion and we have to watch for going into an infinite
* loop. So we add the assumption that they are equal *)
let newrep, undo = union oldcinode cinode in
(* We check the fields but watch for Failure. We only do the check when
* the lengths are the same. Due to the code above this the other
* possibility is that one of the length is 0, in which case we reuse the
* old compinfo. *)
(* But what if the old one is the empty one ? *)
if old_len = len then begin
(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)");
(* Make sure the types are compatible *)
let newtype =
combineTypes CombineOther oldfidx oldf.ftype fidx f.ftype
in
(* Change the type in the representative *)
oldf.ftype <- newtype;
)
oldci.cfields ci.cfields
with Failure reason -> begin
(* Our assumption was wrong. Forget the isomorphism *)
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)
end)
end else begin
(* We will reuse the old one. One of them is empty. If the old one is
* empty, copy over the fields from the new one. Won't this result in
* all sorts of undefined types??? *)
if old_len = 0 then
oldci.cfields <- ci.cfields;
end;
(* We get here when we succeeded checking that they are equal, or one of
* them was empty *)
newrep.ndata.cattr <- addAttributes oldci.cattr ci.cattr;
()
end
(* Match two enuminfos and throw a Failure if they do not match *)
and matchEnumInfo (oldfidx: int) (oldei: enuminfo)
(fidx: int) (ei: enuminfo) : unit =
(* Find the node for this enum, no path compression. *)
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 (* We already know they are the same *)
()
else begin
(* Replace with the representative data *)
let oldei = oldeinode.ndata in
let ei = einode.ndata in
(* Try to match them. But if you cannot just make them both integers *)
try
(* We do not have a mapping. They better be defined in the same way *)
if List.length oldei.eitems <> List.length ei.eitems then
raise (Failure "(different number of enumeration elements)");
(* We check that they are defined in the same way. This is a fairly
* conservative check. *)
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(CInt64(oldi, _, _)), Const(CInt64(i, _, _)) -> oldi = i
| _ -> false
in
if not samev then
raise (Failure "(different values for enumeration items)"))
oldei.eitems ei.eitems;
(* Set the representative *)
let newrep, _ = union oldeinode einode in
(* We get here if the enumerations match *)
newrep.ndata.eattr <- addAttributes oldei.eattr ei.eattr;
()
with Failure msg -> begin
(* Get here if you cannot merge two enumeration nodes *)
if oldeinode != intEnumInfoNode then begin
let _ = union oldeinode intEnumInfoNode in ()
end;
if einode != intEnumInfoNode then begin
let _ = union einode intEnumInfoNode in ()
end;
end
end
(* Match two typeinfos and throw a Failure if they do not match *)
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");
(* Find the node for this enum, no path compression. *)
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 (* We already know they are the same *)
()
else begin
(* Replace with the representative data *)
let oldti = oldtnode.ndata in
let oldfidx = oldtnode.nfidx in
let ti = tnode.ndata in
let fidx = tnode.nfidx in
(* Check that they are the same *)
(try
ignore (combineTypes CombineOther oldfidx oldti.ttype fidx ti.ttype);
with Failure reason -> begin
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)
end);
let _ = union oldtnode tnode in
()
end
(* Scan all files and do two things *)
(* 1. Initialize the alpha renaming tables with the names of the globals so
* that when we come in the second pass to generate new names, we do not run
* into conflicts. *)
(* 2. For all declarations of globals unify their types. In the process
* construct a set of equivalence classes on type names, structure and
* enumeration tags *)
(* 3. We clean the referenced flags *)
let rec 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);
(* We scan each file and we look at all global varinfo. We see if globals
* with the same name have been encountered before and we merge those types
* *)
let matchVarinfo (vi: varinfo) (l: location * int) =
ignore (Alpha.registerAlphaName vtAlpha None vi.vname !currentLoc);
(* Make a node for it and put it in vEq *)
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
(* There is an old definition. We must combine the types. Do this first
* because it might fail *)
let newtype =
try
combineTypes CombineOther
oldvinode.nfidx oldvi.vtype
!currentFidx vi.vtype;
with (Failure reason) -> begin
(* Go ahead *)
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
end
in
let newrep, _ = union oldvinode vinode in
(* We do not want to turn non-"const" globals into "const" one. That
* can happen if one file declares the variable a non-const while
* others declare it as "const". *)
if hasAttribute "const" (typeAttrs vi.vtype) !=
hasAttribute "const" (typeAttrs oldvi.vtype) then begin
newrep.ndata.vtype <- typeRemoveAttributes ["const"] newtype;
end else begin
newrep.ndata.vtype <- newtype;
end;
(* clean up the storage. *)
let newstorage =
if vi.vstorage = oldvi.vstorage || vi.vstorage = Extern then
oldvi.vstorage
else if oldvi.vstorage = Extern then vi.vstorage
(* Sometimes we turn the NoStorage specifier into Static for inline
* functions *)
else if oldvi.vstorage = Static &&
vi.vstorage = NoStorage then Static
else begin
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
end
in
newrep.ndata.vstorage <- newstorage;
newrep.ndata.vattr <- addAttributes oldvi.vattr vi.vattr;
()
with Not_found -> (* Not present in the previous files. Remember it for
* later *)
H.add vEnv vi.vname vinode
in
List.iter
(function
| GVarDecl (vi, l) | GVar (vi, _, l) ->
currentLoc := l;
incr currentDeclIdx;
vi.vreferenced <- false;
if vi.vstorage <> Static then begin
matchVarinfo vi (l, !currentDeclIdx);
end
| GFun (fdec, l) ->
currentLoc := l;
incr currentDeclIdx;
(* Save the names of the formal arguments *)
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;
(* Force inline functions to be static. *)
(* GN: This turns out to be wrong. inline functions are external,
* unless specified to be static. *)
(*
if fdec.svar.vinline && fdec.svar.vstorage = NoStorage then
fdec.svar.vstorage <- Static;
*)
if fdec.svar.vstorage <> Static then begin
matchVarinfo fdec.svar (l, !currentDeclIdx)
end else begin
if fdec.svar.vinline && mergeInlines then
(* Just create the nodes for inline functions *)
ignore (getNode iEq iSyn !currentFidx
fdec.svar.vname fdec.svar (Some (l, !currentDeclIdx)))
end
(* Make nodes for the defined type and structure tags *)
| GType (t, l) ->
incr currentDeclIdx;
t.treferenced <- false;
if t.tname <> "" then (* The empty names are just for introducing
* undefined comp tags *)
ignore (getNode tEq tSyn !currentFidx t.tname t
(Some (l, !currentDeclIdx)))
else begin (* Go inside and clean the referenced flag for the
* declared tags *)
match t.ttype with
TComp (ci, _) ->
ci.creferenced <- false;
(* Create a node for it *)
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")
end
| 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
(* Try to merge synonyms. Do not give an error if they fail to merge *)
let doMergeSynonyms
(syn : (string, 'a node) H.t)
(eq : (int * string, 'a node) H.t)
(compare : int -> 'a -> int -> 'a -> unit) (* A comparison function that
* throws Failure if no match *)
: unit =
H.iter (fun n node ->
if not node.nmergedSyns then begin
(* find all the nodes for the same name *)
let all = H.find_all syn n in
let rec tryone (classes: 'a node list) (* A number of representatives
* for this name *)
(nd: 'a node) : 'a node list (* Returns an expanded set
* of classes *) =
nd.nmergedSyns <- true;
(* Compare in turn with all the classes we have so far *)
let rec compareWithClasses = function
[] -> [nd](* No more classes. Add this as a new class *)
| c :: restc ->
try
compare c.nfidx c.ndata nd.nfidx nd.ndata;
(* Success. Stop here the comparison *)
c :: restc
with Failure _ -> (* Failed. Try next class *)
c :: (compareWithClasses restc)
in
compareWithClasses classes
in
(* Start with an empty set of classes for this name *)
let _ = List.fold_left tryone [] all in
()
end)
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 begin
(* Replace with the representative data *)
let oldi = oldinode.ndata in
let oldfidx = oldinode.nfidx in
let i = inode.ndata in
let fidx = inode.nfidx in
(* There is an old definition. We must combine the types. Do this first
* because it might fail *)
oldi.vtype <-
combineTypes CombineOther
oldfidx oldi.vtype fidx i.vtype;
(* We get here if we have success *)
(* Combine the attributes as well *)
oldi.vattr <- addAttributes oldi.vattr i.vattr;
(* Do not union them yet because we do not know that they are the same.
* We have checked only the types so far *)
()
end
(************************************************************
*
* PASS 2
*
*
************************************************************)
(** 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
(* This is either a global variable which we took care of, or a local
* variable. Must do its type and attributes. *)
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