123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154(**************************************************************************)(* 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). *)(* *)(**************************************************************************)(* Lifts a binary to its integer property. *)openSva_sigmoduleIn_bits=Units.In_bits(* TODO: En fait on a surtout besoin de top; on pourrait avoir une autre basis pour les booleens. *)moduleLift(I:INTEGERwithtypeboolean=Sva_quadrivalent.boolean):BITVECTORwithtypebitvector=I.integerandtypeboolean=I.boolean=structletname="Binary_to_integer_basis.Lift("^I.name^")"moduleBoolean_Lattice=I.Boolean_LatticemoduleBitvector_Lattice=Lattices.Bitvector_Of_Integer.Make(I.Integer_Lattice)typebitvector=I.integertypeboolean=I.booleanmoduleBoolean_Forward=I.Boolean_ForwardmoduleBoolean_Backward=I.Boolean_BackwardmoduleIL=I.Integer_LatticemoduleIF=I.Integer_ForwardmoduleIB=I.Integer_Backward(* TODO: Gerer le modulo; non seulement le debordement entier, mais
aussi le "wrap" quand on passe de signé à non signé... Le modulo
signé = ajouter 2^n-1, faire le modulo 2^n normal, et soustraire
2^n-1. Trouver les opérations pour lesquelles il est nécessaire
de faire le modulo. *)(* Devrait etre fait dans le module entier? En meme temps, on a
besoin de la taille.... *)moduleBitvector_Forward=structletbiconst~sizei=IF.iconstiletblshr~size=IF.ishrletbashr~size=IF.ishrletbshl~size~flags=IF.ishlletbiumod~sizeab=IF.imodabletbiudiv~sizeab=IF.idivabletbismod~sizeab=IF.imodabletbisdiv~sizeab=IF.idivabletbsext~size~oldsizex=xletbuext~size~oldsizex=x(* Note: wrong if x is not in the right interval *)letbofbool~size_=assertfalseletbxor~sizeab=IF.ixorabletbor~sizeab=IF.iorabletband~sizeab=IF.iandabletbextract~size~index~oldsizeb=letsize=In_bits.to_intsizeinletindex=In_bits.to_intindexinletoldsize=In_bits.to_intoldsizeinifsize==oldsizethenbelseletmodu=IF.imodb(IF.iconst(Z.shift_leftZ.one(index+size)))inletresult=IF.idivmodu(IF.iconst(Z.shift_leftZ.oneindex))inresultletbconcat~size1~size2__=I.Integer_Lattice.top()letbiule~sizeab=IF.ileabletbisle~sizeab=IF.ileabletbeq~sizeab=IF.ieqabletbimul~size~flagsab=IF.imulabletbiadd~size~flagsab=IF.iaddabletbisub~size~flagsab=IF.isubabletbimul_add~size~prod~offsetx=IF.iadd(IF.imul(IF.iconstprod)x)(IF.iconstoffset)end(* TODO: Gerer le modulo; non seulement le debordement entier, mais aussi le "wrap" de *)(* Devrait etre fait dans le module entier? En meme temps, on a
besoin de la taille.... *)moduleBitvector_Backward=structincludeSva_noop_transfer_functions.Bitvector_Backward(* Compose base operators into easier-to-use ones. *)(* Note: I don't think there is an easy way to have a DSL to
express these operations in a generic way, e.g. like a
monad. This shows in arity: the way we perform composition
depends on the arity of the main operator, and of the
operators that it calls. Of course there are some
factorisations, e.g. both predicates call substraction in the
same way. *)letunary_minus_fwda=IF.itimesZ.minus_onea;;letunary_minus_bwdares=IB.itimesZ.minus_oneares;;let_isub_fwdab=IF.iadda(unary_minus_fwdb);;let_isub_bwdabres=letminusb=unary_minus_fwdbinlet(newa,newminusb)=IB.iaddaminusbresinletnewb=matchnewminusbwith|None->None|Somenewminusb->unary_minus_bwdbnewminusbin(newa,newb);;letbiule~size=IB.ileletbisle~size=IB.ileletbiadd~size~flags=IB.iaddletbiumod~size=IB.imodletbiudiv~size=IB.idivletbismod~size=IB.imodletbisdiv~size=IB.idivletbimul_add~size~prod~offsetxres=letprod=IF.iconstprodinletintermediate=(IF.imulprodx)in(* We could check that the second arguments match the constants here, and
raise bottom if that is not the case... *)matchIB.iaddintermediate(IF.iconstoffset)res|>fstwith|None->None|Someintermediate'->IB.imulprodxintermediate'|>snd(* Note: comparison between having predicates like a == b and the need to do (a - b) == 0.
When the predicate is known to be false, the backward propagation
of a == b with "diff_if_singleton" is more efficient than
suppressing the 0 from (a - b), and adding it back, especially if
a and b are not singletons.
On the other hand, for other predicates like a <= b the
normalization clearly simplifies the work (i.e. less
implementation time); moreover it concentrates information on (a
- b), even if we cannot reduce them. E.g. it allows to prove
correct if (a != b) d = 3/a - b without any knowledge about a and
b. *)end(* let binary_to_ival _ = assert false *)(* let _binary_fold_crop ~size x ~inf ~sup acc f = *)(* Sva_ival.Integer_Lattice.fold_crop (binary_to_ival ~signed:true ~size x) ~inf ~sup f acc *)end