123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170(****************************************************************************)(* *)(* This file is part of MOPSA, a Modular Open Platform for Static Analysis. *)(* *)(* Copyright (C) 2017-2019 The MOPSA Project. *)(* *)(* This program is free software: 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, either version 3 of the License, or *)(* (at your option) any later version. *)(* *)(* This program 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. *)(* *)(* You should have received a copy of the GNU Lesser General Public License *)(* along with this program. If not, see <http://www.gnu.org/licenses/>. *)(* *)(****************************************************************************)(** Congruence abstraction of integer values. *)openMopsaopenSig.Abstraction.Simplified_valueopenAstopenBotmoduleValue=structmoduleC=CongUtils.IntCongtypev=C.ttypet=vwith_botincludeGenValueId(structtypenonrect=tletname="universal.numeric.values.congruences"letdisplay="congruences"end)letaccept_type=function|T_int|T_bool->true|_->falseletbottom=BOTlettop=Nb(C.minf_inf)letis_bottomabs=bot_dfl1true(funitv->not(C.is_validitv))absletsubset(a1:t)(a2:t):bool=C.included_bota1a2letjoin(a1:t)(a2:t):t=C.join_bota1a2letmeet(a1:t)(a2:t):t=C.meet_bota1a2letwidenctx(a1:t)(a2:t):t=joina1a2letprintprinter(a:t)=unformatC.fprint_botprinteraincludeDefaultValueFunctionsletconstantct=matchcwith|C_booltrue->Nb(C.cst_int1)|C_boolfalse->Nb(C.cst_int0)|C_inti->Nb(C.csti)|C_int_interval(i1,i2)->Nb(C.of_boundi1i2)|C_avalue(Common.V_int_congr_interval,(_,c))->c|_->topletunopoptatr=matchopwith|O_log_not->bot_lift1C.log_nota|O_minus->bot_lift1C.nega|O_plus->a|O_abs->bot_lift1C.absa|O_wrap(l,u)->bot_lift1(funa->C.wrapalu)a|O_bit_invert->bot_lift1C.bit_nota|_->topletbinopopt1a1t2a2tr=matchopwith|O_plus->bot_lift2C.adda1a2|O_minus->bot_lift2C.suba1a2|O_mult->bot_lift2C.mula1a2|O_div->bot_absorb2C.diva1a2|O_log_or->bot_lift2C.log_ora1a2|O_log_and->bot_lift2C.log_anda1a2|O_log_xor->bot_lift2C.log_xora1a2|O_mod->bot_absorb2C.rema1a2|O_bit_rshift->bot_absorb2C.shift_righta1a2|O_bit_lshift->bot_absorb2C.shift_lefta1a2|_->topletfilterbta=ifbthenbot_absorb1C.meet_nonzeroaelsebot_absorb1C.meet_zeroaletbackward_unopoptatrr=tryleta,r=bot_to_exna,bot_to_exnrinletaa=matchopwith|O_minus->bot_to_exn(C.bwd_negar)|_->ainNbaawithFound_BOT->bottomletbackward_binopopt1a1t2a2trr=tryleta1,a2,r=bot_to_exna1,bot_to_exna2,bot_to_exnrinletaa1,aa2=matchopwith|O_plus->bot_to_exn(C.bwd_adda1a2r)|O_minus->bot_to_exn(C.bwd_suba1a2r)|O_mult->bot_to_exn(C.bwd_mula1a2r)|O_div->bot_to_exn(C.bwd_diva1a2r)|O_mod->bot_to_exn(C.bwd_rema1a2r)|O_bit_rshift->bot_to_exn(C.bwd_shift_righta1a2r)|O_bit_lshift->bot_to_exn(C.bwd_shift_lefta1a2r)|_->a1,a2inNbaa1,Nbaa2withFound_BOT->bottom,bottomletcompareopbt1a1t2a2=tryleta1,a2=bot_to_exna1,bot_to_exna2inletop=ifbthenopelsenegate_comparison_opopinletaa1,aa2=matchopwith|O_eq->bot_to_exn(C.filter_eqa1a2)|O_ne->bot_to_exn(C.filter_neqa1a2)|O_lt->bot_to_exn(C.filter_lta1a2)|O_gt->bot_to_exn(C.filter_gta1a2)|O_le->bot_to_exn(C.filter_leqa1a2)|O_ge->bot_to_exn(C.filter_geqa1a2)|_->a1,a2inNbaa1,Nbaa2withFound_BOT->bottom,bottomletavalue:typer.ravalue_kind->t->roption=funavalc->matchavalwith|Common.V_int_congr_interval->letret=matchcwith|BOT->Intervals.Integer.Value.bottom,bottom|_->Intervals.Integer.Value.top,cinOptionExt.returnret|_->Noneendlet()=register_simplified_value_abstraction(moduleValue)