123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159(**************************************************************************)(* This file is part of the Codex semantics library. *)(* *)(* Copyright (C) 2013-2025 *)(* CEA (Commissariat à l'énergie atomique et aux énergies *)(* 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 LICENSE). *)(* *)(**************************************************************************)moduleIn_bits=Units.In_bits(**************** Standard abstractions for bitvectors. ****************)(* Standard abstractions that are useful for exchanging information between domains. *)moduleInterval=struct(* Min, max pair. Note that on bitvectors there is a maximum, so there
is no need for infinity to represent top. *)includeInteger_standard.ZPair;;letjoin~size:_(min1,max1)(min2,max2)=(Z.minmin1min2,Z.maxmax1max2);;letinter~size:_(min1,max1)(min2,max2)=(Z.maxmin1min2,Z.minmax1max2);;(* Any element where max is smaller than min is bottom. *)letbottom~size:_=Z.one,Z.zeroletincludes~size(min1,max1)(min2,max2)=Z.leqmin1min2&&Z.geqmax1max2;;letpretty~sizefmt(a,b)=Format.fprintffmt"[%s..%s]"(Z.to_stringa)(Z.to_stringb)endmoduleSigned_Interval=structincludeIntervalendmoduleUnsigned_Interval=structincludeIntervallettop~size=(Z.zero,Z.pred@@Z.(lsl)Z.onesize)endmoduleKnown_Bits=struct(* First bitvector = 0 where the bits must be 0, 1 otherwise.
Second bitvector = 1 where the bits must be 1, 0 otherwise.
This is the same representation than for integers, with the
additional restriction that bits at position higher than size are
zero (so we must be careful and use extract for every operation
that can change the sign or higher bits). *)includeInteger_standard.ZPairletchop~(size:In_bits.t)x=Z.extractx0(size:>int)letbottom~size=(Z.zero,chop~sizeZ.minus_one);;letis_bottom~sizex=Integer_standard.Known_Bits.is_bottomxlettop~size=(chop~sizeZ.minus_one,Z.zero);;letpretty~size=Integer_standard.Known_Bits.prettyletsingleton~sizex=letx=chop~sizexin(x,x);;(* Intersection: keep the bits known by one side. *)letinter0=Z.(land)letinter1=Z.(lor)letinter~size:(_:In_bits.t)(x0,x1)(y0,y1)=(inter0x0y0,inter1x1y1);;letjoin0=Z.(lor)letjoin1=Z.(land)letjoin~size(x0,x1)(y0,y1)=(join0x0y0,join1x1y1)letequal(x0,x1)(y0,y1)=Z.equalx0y0&&Z.equalx1y1;;letis_included~sizexy=equaly(join~sizexy);;letincludes~sizexy=is_included~sizeyx(* A widening operator from a paper by Antoine Miné. *)letwiden~(size:In_bits.t)~previous:(prev0,prev1)(new0,new1)=letres0=lett=join0prev0new0inifZ.equalprev0tthenprev0elseZ.extractZ.minus_one0(size:>int)inletres1=lett=join1prev1new1inifZ.equalprev1tthenprev1elseZ.zeroin(res0,res1);;letincludes_or_widen~size~previous_=assertfalse(* TODO. *)letis_singleton~size_x=assertfalseletis_empty~size_x=assertfalseletfold_crop_signed~size_x~inf~sup_acc_f=assertfalseletfold_crop_unsigned~size_x~inf~sup_acc_f=assertfalseletto_known_bits~sizex=xletto_unsigned_interval~size_=assertfalseletto_signed_interval~size_=assertfalseendmoduleCongruence=struct(* Represents a set q * Z + r, where q is the first element, and r
the second. - If q is negative, this represents bottom. - If q
is 0, this represents a singleton. - If q is >0, then 0 <= r <
q.
Note: as this representation is independent from the sign, most
operations are the same than for integers.
*)includeInteger_standard.ZPairletbottom~size=Integer_standard.Congruence.bottomlettop~size=Integer_standard.Congruence.topletsingleton~sizex=Z.zero,Z.extractx0sizeend(* Bitvectors are finite, thus we can explicitely enumerate bitvectors. *)moduleBVSet=structmoduleZSet=Set.Make(Z)letequal=ZSet.equalletcompare=ZSet.comparelethash_=assertfalseletpretty~sizefmtx=Format.fprintffmt"@[<hv>";x|>ZSet.iter(funx->Format.fprintffmt"%s@ "(Z.to_stringx));Format.fprintffmt"@]";;typet=ZSet.tletbottom~size=ZSet.emptylettop~size=assertfalseletjoin~size=ZSet.unionletinter~size=ZSet.interletsingleton~sizex=ZSet.singletonxend