123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177(**************************************************************************)(* *)(* This file is part of Frama-C. *)(* *)(* Copyright (C) 2007-2023 *)(* 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 licenses/LGPLv2.1). *)(* *)(**************************************************************************)openCil_datatypeopenCil_typesopenMiscmoduleAI=Abstract_interpmoduleOptions=Reduc_optionsletnot_yet~what=Options.warning"Not yet: %s"what(* [int_to_predicate ~loc lop te i] create a predicate where [te] [lop] [i]. *)letint_to_predicate~loc~lopte(i:Integer.t)=letti=Logic_const.tintiinLogic_const.prel~loc(lop,te,ti)(* [float_to_predicate ~loc lop te f] create a predicate where [te] [lop] [f]. *)letfloat_to_predicate~loc~loptf=lettf=Logic_const.treal~locfinLogic_const.prel~loc(lop,t,tf)(* [interval_to_predicate_opt ~loc te min max] may create an interval
predicate where [min] <= [te] <= [max]. *)letinterval_to_predicate_opt~locteminmax=letmin_predicate=int_to_predicate~loc~lop:Rgeteandmax_predicate=int_to_predicate~loc~lop:RleteinExtlib.merge_opt(fun_pminpmax->Logic_const.pand~loc(pmin,pmax))()(Option.mapmin_predicatemin)(Option.mapmax_predicatemax)(* [congruence_to_predicate_opt ~loc te rem modulo] may create a congruence
predicate where [te] mod [modulo] equals [rem] *)letcongruence_to_predicate_opt~locteremmodulo=ifInteger.(equalmoduloone)thenNoneelselettmodulo=Logic_const.tint~locmoduloinlettbinop=TBinOp(Mod,te,tmodulo)in(* since 0 <= rem < mod *)lettmodop=Logic_const.term~loctbinop(Linteger)inletp=int_to_predicate~loc~lop:ReqtmodopreminSomepletfval_to_predicate~loctf=matchIval.min_and_max_floatfwith|None,false->assertfalse|_,true->Logic_const.pfalse(* todo *)|Some(fmn,fmx),_->letmn=Fval.F.to_floatfmninletmx=Fval.F.to_floatfmxinletpmn=float_to_predicate~loc~lop:Rgetmninletpmx=float_to_predicate~loc~lop:RletmxinLogic_const.pand~loc(pmn,pmx)(* [ival_to_predicate_opt ~loc t ival] may create a predicate from different
domains contained in [ival]. *)letival_to_predicate_opt~loctival=ifIval.is_floativalthenSome(fval_to_predicate~loctival)elsematchIval.project_small_setivalwith|Someis->letps=List.map(int_to_predicate~loc~lop:Reqt)isinletpors=Logic_const.porspsinSomepors|None->matchIval.min_max_r_modivalwith|(None,None,_,modulo)whenAI.Int.is_onemodulo->(*Top*)None|(min,max,rem,modulo)->letpint=interval_to_predicate_opt~loctminmaxinletpcon=congruence_to_predicate_opt~loctremmoduloinExtlib.merge_opt(fun_pintpcon->Logic_const.pand~loc(pint,pcon))()pintpcon(* [valid_to_predicate_opt ~loc t valid] may create validity predicate *only*
for valid locations. *)letvalid_to_predicate_opt~loctvalid=lethere_lbl=BuiltinLabelHereinmatchvalidwith|Base.Empty->None|Base.Known(bmn,bmx)|Base.Unknown(bmn,Somebmx,_)->lettbmn=Cil.lconstant~locbmninlettbmx=Cil.lconstant~locbmxinletp=Logic_const.pvalid_range~loc(here_lbl,t,tbmn,tbmx)inSomep|Base.Unknown(_,_,_)->None|Base.Variable{Base.min_alloc=bmn}whenInteger.(equalbmnminus_one)->not_yet~what:"Invalid Base.Variable above max_alloc";None|Base.Variable{Base.min_alloc=bmn;Base.max_alloc=bmx}->not_yet~what:"Invalid Base.Variable above max_alloc";lettbmn=Cil.lconstant~locbmninlettbmx=Cil.lconstant~locbmxinletp=Logic_const.pvalid_range~loc(here_lbl,t,tbmn,tbmx)inSomep|Base.Invalid->not_yet~what:"Invalid Base";Noneletbase_to_predicate~loct(b:Base.t)=matchbwith|Base.Var(_vi,valid)|Base.Allocated(_vi,_,valid)->valid_to_predicate_opt~loctvalid|Base.CLogic_Var(_lvi,_typ,_)->raise(Not_implemented"Base.CLogic_Var")|Base.Null->assertfalse(*by projecting ival*)|Base.String_->raise(Not_implemented"Base.String")letbase_offset_to_predicate~loctbo=ifIval.equalIval.zeroothenbase_to_predicate~loctbelsebeginnot_yet~what:"Base w/ offsets";Noneendlet_base_offsets_to_predicate~loct(m:Cvalue.V.M.t)=letauxbo(acc:predicatelist)=letp_opt=base_offset_to_predicate~loctboin(Option.to_listp_opt)@accinmatchCvalue.V.M.foldauxm[]with|[]->None|ps->Some(Logic_const.pandsps)letvalue_to_predicate_opt?(loc=Location.unknown)tv=letopenCvalue.Vinmatchvwith|Top_->None|Map_m->tryletival=project_ivalvinival_to_predicate_opt~loctivalwith|Not_based_on_null->(* base_offsets_to_predicate ~loc t m *)Noneletexp_to_predicate?(loc=Location.unknown)stmte=letvalue=Eva.Results.(beforestmt|>eval_expe|>as_ival)inlette=Logic_utils.expr_to_term~coerce:falseeinOption.bind(Result.to_optionvalue)(ival_to_predicate_opt~locte)letlval_to_predicate?(loc=Location.unknown)stmtlv=letvalue=Eva.Results.(beforestmt|>eval_lvallv|>as_ival)inlete=Cil.new_exp~loc(Lvallv)inlette=Logic_utils.expr_to_term~coerce:falseeinOption.bind(Result.to_optionvalue)(ival_to_predicate_opt~locte)