123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187(**************************************************************************)(* *)(* This file is part of WP plug-in of Frama-C. *)(* *)(* Copyright (C) 2007-2023 *)(* CEA (Commissariat a l'energie atomique et aux energies *)(* alternatives) *)(* *)(* you can redistribute it and/or modify it under the terms of the GNU *)(* Lesser General Public License as published by the Free Software *)(* Foundation, version 2.1. *)(* *)(* It is distributed in the hope that it will be useful, *)(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *)(* GNU Lesser General Public License for more details. *)(* *)(* See the GNU Lesser General Public License version 2.1 *)(* for more details (enclosed in the file licenses/LGPLv2.1). *)(* *)(**************************************************************************)(* -------------------------------------------------------------------------- *)(* --- Generic Sigma Factory --- *)(* -------------------------------------------------------------------------- *)openLang.FmoduleMake(C:Sigs.Chunk)(H:Qed.Collection.Swithtypet=C.t):Sigs.Sigmawithtypechunk=C.tandmoduleChunk=H=structtypechunk=C.tmoduleChunk=Htypedomain=H.Set.tletempty=H.Set.emptyletunion=H.Set.uniontypet={id:int;mutablemap:varH.map}letid=ref0(* for debugging purpose *)letbuildmap=letk=!idinincrid;{id=k;map=map}letcreate()=buildH.Map.emptyletcopys=builds.mapletnewchunkc=Lang.freshvar~basename:(C.basename_of_chunkc)(C.tau_of_chunkc)letmergeab=letpa=refPassive.emptyinletpb=refPassive.emptyinletmerge_chunkcxy=ifVar.equalxythenxelseletz=newchunkcinpa:=Passive.bind~fresh:z~bound:x!pa;pb:=Passive.bind~fresh:z~bound:y!pb;zinletw=H.Map.unionmerge_chunka.mapb.mapinbuildw,!pa,!pbtypekind=|UsedofLang.F.var|Unusedletmerge_listl=(* Get a map of the chunks (the data is not important) *)letunion=List.fold_left(funacce->H.Map.union(fun_v1_->v1)acce.map)H.Map.emptylin(* The goal is to build a matrix chunk -> elt of the list -> Used/Unused
*)(* Set the data of the map to []. *)letunion=H.Map.map(fun_->[])unionin(* For each elements of the list tell if each chunk is used *)letmerge_me=matchm,ewith|Somem,Somee->Some(Usede::m)|Somem,None->Some(Unused::m)|None,_->assertfalseinletunion=List.fold_left(funacce->H.Map.mergemergeacce.map)union(* important so that the list in the map are in the correct order *)(List.revl)in(* Build the passive for each element of the list, and the final domain *)letp=ref(List.map(fun_->Passive.empty)l)inletmapcl=matchList.filter(funx->not(Unused=x))lwith|[]->assertfalse(* If all the sigmas use the same variable *)|(Useda)::lwhenList.for_all(function|Unused->true|Usedx->Var.equalxa)l->a|_->letz=newchunkcinletmap2p=function|Unused->p|Useda->Passive.bind~fresh:z~bound:apinp:=List.map2map2!pl;zinletunion=H.Map.mapimapunioninbuildunion,!pletchooseab=letmerge_chunck_xy=ifVar.comparexy<=0thenxelseyinbuild(H.Map.unionmerge_chuncka.mapb.map)letgetwc=tryH.Map.findcw.mapwithNot_found->letx=newchunkcinw.map<-H.Map.addcxw.map;xletmemwc=H.Map.memcw.mapletjoinab=ifa==bthenPassive.emptyelseletp=refPassive.emptyinH.Map.iter2(funchunkxy->matchx,ywith|Somex,Somey->p:=Passive.joinxy!p|Somex,None->b.map<-H.Map.addchunkxb.map|None,Somey->a.map<-H.Map.addchunkya.map|None,None->())a.mapb.map;!pletassigned~pre~postwritten=letp=refBag.emptyinH.Map.iter2(funchunkxy->ifnot(H.Set.memchunkwritten)thenmatchx,ywith|Somex,Someywhenx!=y->p:=Bag.add(p_equal(e_varx)(e_vary))!p|Somex,None->post.map<-H.Map.addchunkxpost.map|None,Somey->pre.map<-H.Map.addchunkypre.map|_->())pre.mappost.map;!pletvaluewc=e_var(getwc)letiterfw=H.Map.iterfw.mapletiter2fw1w2=H.Map.iter2fw1.mapw2.maplethavocwxs=letys=H.Set.mappingnewchunkxsinbuild(H.Map.union(fun_c_oldy->y)w.mapys)lethavoc_chunkwc=letx=newchunkcinbuild(H.Map.addcxw.map)lethavoc_any~callw=letframercx=ifcall&&C.is_framedcthenxelsenewchunkcinbuild(H.Map.mapiframerw.map)letremove_chunkswxs=build(H.Map.filter(func_->not(H.Set.memcxs))w.map)letdomainw=H.Map.domainw.mapletprettyfmtw=beginFormat.fprintffmt"@[<hov 2>@@%s%d["C.selfw.id;H.Map.iter(funcx->Format.fprintffmt"@ %a:%a"C.prettycVar.prettyx)w.map;Format.fprintffmt" ]@]";endletwritesseq=leteffect=refChunk.Set.emptyiniter2(funchunkuv->letwritten=matchu,vwith|Somex,Somey->not(Var.equalxy)|None,Some_->true|Some_,None->false(* no need to create a new so it is the same *)|None,None->assertfalseinifwrittentheneffect:=Chunk.Set.addchunk!effect)seq.Sigs.preseq.Sigs.post;!effectend