123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298(*
Copyright (c) 2004,
Jeremy Condit <jcondit@cs.berkeley.edu>
George C. Necula <necula@cs.berkeley.edu>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The names of the contributors may not be used to endorse or promote
products derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*)openCilopenCilintmoduleE=Errormsg(**************************************************************************)(* Helpers *)letisConstType(t:typ):bool=hasAttribute"const"(typeAttrst)(**************************************************************************)(* Expression/type comparison *)letreccompareExp(e1:exp)(e2:exp):bool=(*log "CompareExp %a and %a.\n" d_plainexp e1 d_plainexp e2; *)e1==e2||matche1,e2with|Lvallv1,Lvallv2|StartOflv1,StartOflv2|AddrOflv1,AddrOflv2->compareLvallv1lv2|BinOp(bop1,l1,r1,_),BinOp(bop2,l2,r2,_)->bop1=bop2&&compareExpl1l2&&compareExpr1r2|CastE(t1,e1),CastE(t2,e2)->t1==t2&&compareExpe1e2|_->beginmatchgetInteger(constFoldtruee1),getInteger(constFoldtruee2)withSomei1,Somei2->compare_cilinti1i2=0|_->falseendandcompareLval(lv1:lval)(lv2:lval):bool=letreccompareOffset(off1:offset)(off2:offset):bool=matchoff1,off2with|Field(fld1,off1'),Field(fld2,off2')->fld1==fld2&&compareOffsetoff1'off2'|Index(e1,off1'),Index(e2,off2')->compareExpe1e2&&compareOffsetoff1'off2'|NoOffset,NoOffset->true|_->falseinlv1==lv2||matchlv1,lv2with|(Varvi1,off1),(Varvi2,off2)->vi1==vi2&&compareOffsetoff1off2|(Meme1,off1),(Meme2,off2)->compareExpe1e2&&compareOffsetoff1off2|_->false(* Remove casts that do not effect the value of the expression, such
as casts between different pointer types. Of course, these casts
change the type, so don't use this within e.g. an arithmetic
expression.
We remove casts from pointer types to unsigned int or unsigned long.
We also prune casts between equivalent integer types, such as a
difference in sign or int vs long. But we keep other arithmetic casts,
since they actually change the value of the expression. *)letrecstripNopCasts(e:exp):exp=matchewithCastE(t,e')->beginmatchunrollType(typeOfe'),unrollTypetwithTPtr(bt1,a1),TPtr(bt2,a2)whenisConstTypebt1=isConstTypebt2->stripNopCastse'(* strip casts from pointers to unsigned int/long*)|(TPtr_ast1),(TInt(ik,_)ast2)whenbitsSizeOft1=bitsSizeOft2&¬(isSignedik)->stripNopCastse'|(TInt(ik1,_)ast1),(TInt(ik2,_)ast2)(* promotion when signedness is the same doesn't change value *)(* promotion of unsigned to signed of larger bitsize doesn't change
value *)whenbitsSizeOft1=bitsSizeOft2||(isSignedik1=isSignedik2&&bitsSizeOft1<bitsSizeOft2)||(not(isSignedik1)&&bitsSizeOft1<bitsSizeOft2)->(* Okay to strip.*)stripNopCastse'|_->eend|_->eletcompareExpStripCasts(e1:exp)(e2:exp):bool=compareExp(stripNopCastse1)(stripNopCastse2)(* A more conservative form of stripNopCasts. Here, we only strip pointer
casts if the base types have the same width. Using this on the left operand
of pointer arithmetic shouldn't change the resulting value. *)letrecstripCastsForPtrArith(e:exp):exp=matchewith|CastE(t,e')->beginmatchunrollType(typeOfe'),unrollTypetwith(* Keep casts from void to something else. Among other things,
we keep casts from void* to char* that would otherwise be
eliminated. *)|TPtr(TVoid_,_),TPtr(bt2,_)whennot(isVoidTypebt2)->e(* Remove casts between pointers with equal-sized base types. *)|TPtr(bt1,a1),TPtr(bt2,a2)->begintryifbitsSizeOfbt1=bitsSizeOfbt2&&isConstTypebt1=isConstTypebt2thenstripCastsForPtrArithe'elseewithSizeOfError_->(* bt1 or bt2 is abstract; don't strip. *)eend(* strip casts from pointers to unsigned int/long*)|(TPtr_ast1),(TInt(ik,_)ast2)whenbitsSizeOft1=bitsSizeOft2&¬(isSignedik)->stripCastsForPtrArithe'|(TInt(ik1,_)ast1),(TInt(ik2,_)ast2)(*when bitsSizeOf t1 = bitsSizeOf t2 ->*)(* Okay to strip.*)whenbitsSizeOft1=bitsSizeOft2||(isSignedik1=isSignedik2&&bitsSizeOft1<bitsSizeOft2)||(not(isSignedik1)&&bitsSizeOft1<bitsSizeOft2)->(* Okay to strip.*)stripCastsForPtrArithe'|_->eend|_->eletcompareTypes?(ignoreSign=true)?(importantAttr:attribute->bool=(fun_->true))(t1:typ)(t2:typ):bool=lettypeSigNC(t:typ):typsig=letattrFilter(attr:attribute):bool=matchattrwith|Attr("poly",_)(* TODO: hack hack! *)|Attr("assumeconst",_)|Attr("_ptrnode",_)|Attr("missing_annot",_)|Attr("const",[])|Attr("aligned",_)|Attr("volatile",[])|Attr("deprecated",[])|Attr("always_inline",[])->false|_->importantAttrattrintypeSigWithAttrs~ignoreSign(List.filterattrFilter)tin(typeSigNCt1)=(typeSigNCt2)letcompareTypesNoAttributes?(ignoreSign=true)(t1:typ)(t2:typ):bool=lettypSig=typeSigWithAttrs~ignoreSign:ignoreSign(fun_->[])inUtil.equals(typSigt1)(typSigt2)classvolatileFinderClassbr=object(self)inheritnopCilVisitormethod!vtype(t:typ)=ifhasAttribute"volatile"(typeAttrst)thenbeginbr:=true;SkipChildrenendelseDoChildrenendletisTypeVolatilet=letbr=reffalseinletvis=newvolatileFinderClassbrinignore(visitCilTypevist);!br(* strip every cast between equal pointer types *)letrecstripCastsDeepForPtrArith(e:exp):exp=matchewith|CastE(t,e')whennot(isTypeVolatilet)->beginlete'=stripCastsDeepForPtrArithe'inmatchunrollType(typeOfe'),unrollTypetwith(* Keep casts from void to something else. Among other things,
we keep casts from void* to char* that would otherwise be
eliminated. *)|TPtr(TVoid_,_),TPtr(bt2,_)whennot(isVoidTypebt2)->e(* Remove casts between pointers with equal-sized base types. *)|TPtr(bt1,a1),TPtr(bt2,a2)->begintryifbitsSizeOfbt1=bitsSizeOfbt2&&isConstTypebt1=isConstTypebt2thene'elseCastE(t,e')withSizeOfError_->(* bt1 or bt2 is abstract; don't strip. *)CastE(t,e')end|_,_->CastE(t,e')end|UnOp(op,e,t)->lete=stripCastsDeepForPtrAritheinUnOp(op,e,t)|BinOp(MinusPP,e1,e2,t)->lete1=stripCastsDeepForPtrArithe1inlete2=stripCastsDeepForPtrArithe2inifnot(compareTypesNoAttributes~ignoreSign:false(typeOfe1)(typeOfe2))thenBinOp(MinusPP,mkCast~e:e1~newt:(typeOfe2),e2,t)elseBinOp(MinusPP,e1,e2,t)|BinOp(op,e1,e2,t)->lete1=stripCastsDeepForPtrArithe1inlete2=stripCastsDeepForPtrArithe2inBinOp(op,e1,e2,t)|Lvallv->Lval(stripCastsForPtrArithLvallv)|AddrOflv->AddrOf(stripCastsForPtrArithLvallv)|StartOflv->StartOf(stripCastsForPtrArithLvallv)|_->eandstripCastsForPtrArithLval(lv:lval):lval=matchlvwith|(Varvi,off)->(Varvi,stripCastsForPtrArithOffoff)|(Meme,off)->lete=stripCastsDeepForPtrAritheinletoff=stripCastsForPtrArithOffoffin(Meme,off)andstripCastsForPtrArithOff(off:offset):offset=matchoffwith|NoOffset->NoOffset|Field(fi,off)->Field(fi,stripCastsForPtrArithOffoff)|Index(e,off)->lete=stripCastsDeepForPtrAritheinletoff=stripCastsForPtrArithOffoffinIndex(e,off)letcompareExpDeepStripCasts(e1:exp)(e2:exp):bool=compareExp(stripCastsDeepForPtrArithe1)(stripCastsDeepForPtrArithe2)letreccompareAttrParam(ap1:attrparam)(ap2:attrparam):bool=ap1==ap2||matchap1,ap2with|AInti1,AInti2->i1=i2|AStrs1,AStrs2->s1=s2|ACons(s1,apl1),ACons(s2,apl2)->s1=s2&&List.lengthapl1=List.lengthapl2&¬(List.exists2(funap1ap2->not(compareAttrParamap1ap2))apl1apl2)|ASizeOft1,ASizeOft2->compareTypest1t2|ASizeOfEap1,ASizeOfEap2->compareAttrParamap1ap2|ASizeOfSts1,ASizeOfSts2->Util.equalsts1ts2|AAlignOft1,AAlignOft2->compareTypest1t2|AAlignOfEap1,AAlignOfEap2->compareAttrParamap1ap2|AAlignOfSts1,AAlignOfSts2->Util.equalsts1ts2|AUnOp(uop1,ap1),AUnOp(uop2,ap2)->uop1=uop2&&compareAttrParamap1ap2|ABinOp(bop1,ap11,ap12),ABinOp(bop2,ap21,ap22)->bop1=bop2&&compareAttrParamap11ap21&&compareAttrParamap12ap22|ADot(ap1,s1),ADot(ap2,s2)->compareAttrParamap1ap2&&s1=s2|AStarap1,AStarap2->compareAttrParamap1ap2|AAddrOfap1,AAddrOfap2->compareAttrParamap1ap2|AIndex(ap11,ap12),AIndex(ap21,ap22)->compareAttrParamap11ap21&&compareAttrParamap12ap22|AQuestion(ap11,ap12,ap13),AQuestion(ap21,ap22,ap23)->compareAttrParamap11ap21&&compareAttrParamap12ap22&&compareAttrParamap13ap23|_,_->false