123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119(**************************************************************************)(* *)(* 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). *)(* *)(**************************************************************************)openLangletselect_opf=letrewritedescruv=Tactical.rewrite[descr,F.p_true,u,v]inletrewrite_lslean=(* from selection e='a<<n', rewrites the sequent 'Hs |- G' into:
- Hs[e := a*2^n] |- G[e := a*2^n)] *)letb=F.e_mula(F.e_int(1lsln))inrewrite"shift"ebinletrewrite_lsrean=(* from selection e='a>>n', rewrites the sequent 'Hs |- G' into:
- Hs |- 0<=a
- Hs[e := a*2^n] |- G[e := a*2^n] *)letb=F.e_diva(F.e_int(1lsln))in(funseq->("positive",(fstseq,F.p_leqF.e_zeroa))::rewrite"shift"ebseq)iniff==Cint.f_lslthenrewrite_lslelseiff==Cint.f_lsrthenrewrite_lsrelseraiseNot_foundletselect_intn=matchF.reprnwith|Qed.Logic.Kintn->(tryInteger.to_int_exnnwithZ.Overflow->raiseNot_found)|_->raiseNot_foundclassshift=objectinheritTactical.make~id:"Wp.shift"~title:"Logical Shift"~descr:"Transform Shifts into Div/Mult"~params:[]methodselectfeedbackselection=lete=Tactical.selectedselectioninletopenQed.LogicinmatchF.reprewith|Fun(f,[a;n])->beginletrewrite_shift=select_opfinletn=select_intninifn>64thenfeedback#set_error"Too large shift (64 max.)";ifn<0thenfeedback#set_error"Negative shift (0 min.)";Tactical.Applicable(rewrite_shiftean)end|_->Tactical.Not_applicableendlettactical=Tactical.export(newshift)letstrategy=Strategy.maketactical~arguments:[](* -------------------------------------------------------------------------- *)(* --- Auto Shift --- *)(* -------------------------------------------------------------------------- *)letis_shifte=tryletopenQed.LogicinmatchF.reprewith|Fun(f,[_;n])->let_ignore=select_opfinlet_=select_intnintrue|_->falsewithNot_found->falseletrecscanmfe=ifnot(F.Tset.meme!m)thenbeginm:=F.Tset.adde!m;ifis_shiftethenfeelseifF.lc_closedethenF.lc_iter(scanmf)eendclassautoshift=objectmethodid="wp:bitshift"methodtitle="Auto Bit-Shift"methoddescr="Apply Bit-Shift in Goal"methodsearchpush(seq:Conditions.sequent)=letgoal=sndseqinletapplye=letselection=Tactical.(Inside(Goalgoal,e))inpush(strategy~priority:0.5selection)inscan(refF.Tset.empty)apply(F.e_propgoal)endlet()=Strategy.register(newautoshift)