theory_bitvector.h

Go to the documentation of this file.
00001 /*****************************************************************************/
00002 /*!
00003  * \file theory_bitvector.h
00004  * 
00005  * Author: Vijay Ganesh
00006  * 
00007  * Created: Wed May 05 18:34:55 PDT 2004
00008  *
00009  * <hr>
00010  * Copyright (C) 2003 by the Board of Trustees of Leland Stanford
00011  * Junior University and by New York University. 
00012  *
00013  * License to use, copy, modify, sell and/or distribute this software
00014  * and its documentation for any purpose is hereby granted without
00015  * royalty, subject to the terms and conditions defined in the \ref
00016  * LICENSE file provided with this distribution.  In particular:
00017  *
00018  * - The above copyright notice and this permission notice must appear
00019  * in all copies of the software and related documentation.
00020  *
00021  * - THE SOFTWARE IS PROVIDED "AS-IS", WITHOUT ANY WARRANTIES,
00022  * EXPRESSED OR IMPLIED.  USE IT AT YOUR OWN RISK.
00023  * 
00024  * <hr>
00025  * 
00026  */
00027 /*****************************************************************************/
00028 
00029 #ifndef _cvcl__include__theory_bitvector_h_
00030 #define _cvcl__include__theory_bitvector_h_
00031 
00032 #include "theory_core.h"
00033 #include "statistics.h"
00034 
00035 namespace CVCL {
00036 
00037   class VCL;
00038   class BitvectorProofRules;
00039   
00040   typedef enum { //some new BV kinds
00041     BITVECTOR = 8000,
00042     BVCONST,
00043     HEXBVCONST,
00044     CONCAT,
00045     BVOR,
00046     BVAND,
00047     BVNEG,
00048     BVXOR,
00049     BVNAND,
00050     BVNOR,
00051     BVXNOR,
00052     EXTRACT,
00053     LEFTSHIFT,
00054     RIGHTSHIFT,
00055     VARSHIFT,
00056     BVPLUS,
00057     BVSUB,
00058     BVUMINUS,
00059     BVMULT,
00060     BOOLEXTRACT,
00061     BVLT,
00062     BVLE,
00063     BVGT,
00064     BVGE,
00065     SBVLT,
00066     SBVLE,
00067     SBVGT,
00068     SBVGE,
00069     SX,
00070     SRIGHTSHIFT,
00071     INTTOBV,
00072     BVTOINT,
00073     // A wrapper for delaying the construction of type predicates
00074     BVTYPEPRED,
00075     // Internal kind for a bitvector operator
00076     BVPARAMOP
00077   } BVKinds;
00078 
00079 /*****************************************************************************/
00080 /*!
00081  *\class TheoryBitvector
00082  *\ingroup Theories
00083  *\brief Theory of bitvectors of known length \
00084  * (operations include: @,[i:j],[i],+,.,BVAND,BVNEG)
00085  *
00086  * Author: Vijay Ganesh
00087  *
00088  * Created:Wed May  5 15:35:07 PDT 2004
00089  */
00090 /*****************************************************************************/
00091 class TheoryBitvector :public Theory {
00092   BitvectorProofRules* d_rules;
00093   //! MemoryManager index for BVConstExpr subclass
00094   size_t d_bvConstExprIndex;
00095   size_t d_bvPlusExprIndex;
00096   size_t d_bvParameterExprIndex;
00097   size_t d_bvTypePredExprIndex;
00098 
00099   //! counts delayed asserted equalities
00100   StatCounter d_bvDelayEq;
00101   //! counts asserted equalities
00102   StatCounter d_bvAssertEq;
00103   //! counts delayed asserted disequalities
00104   StatCounter d_bvDelayDiseq;
00105   //! counts asserted disequalities
00106   StatCounter d_bvAssertDiseq;
00107   //! counts type predicates
00108   StatCounter d_bvTypePreds;
00109   //! counts delayed type predicates
00110   StatCounter d_bvDelayTypePreds;
00111   //! counts bitblasted equalities
00112   StatCounter d_bvBitBlastEq;
00113   //! counts bitblasted disequalities
00114   StatCounter d_bvBitBlastDiseq;
00115 
00116 
00117   //! Command line flag: simplify bit-blasted expressions before enqueuing
00118   const bool* d_simplifyFlag;
00119   //! boolean on the fly rewrite flag
00120   const bool* d_booleanRWFlag;
00121   //! bool extract cache flag
00122   const bool* d_boolExtractCacheFlag;
00123   //! flag which indicates that all arithmetic is 32 bit with no overflow
00124   const bool* d_bv32Flag;
00125   //! Command line flag: rewrite bitvector expressions
00126   const bool* d_rewriteFlag;
00127   //! Command line flag: concatenation normal form rewrite bitvector expressions
00128   const bool* d_concatRewriteFlag;
00129   //! Command line flag: bvplus normal form rewrite bitvector expressions
00130   const bool* d_bvplusRewriteFlag;
00131   //! Commant line flag: rewrite while bit-blasting
00132   const bool* d_rwBitBlastFlag;
00133   //! Commant line flag: bit-blast equalities in CNF converter
00134   const bool* d_cnfBitBlastFlag;
00135   //! Command line flag: enable lhs-minus-rhs-rule for lhs=rhs
00136   const bool* d_lhsMinusRhsFlag;
00137   //! Command line flag: update bitvector expressions on find pointer merges
00138   const bool* d_updateFlag;
00139   //! Command line flag: setup bitvector expressions for single bit changes
00140   const bool* d_setupFlag;
00141   //! Command line flag: setup only shared subexpressions
00142   const bool* d_setupSharedFlag;
00143   //! Command line flag: enable pushnegation
00144   const bool* d_pushNegationFlag;
00145 
00146   //! Cache for storing the results of the bitBlastTerm function
00147   CDMap<Expr,Theorem> d_bitvecCache;
00148 
00149   //! Cache for pushNegation(). it is ok that this is cache is
00150   //ExprMap. it is cleared for each call of pushNegation. Does not add
00151   //value across calls of pushNegation
00152   ExprMap<Theorem> d_pushNegCache;
00153 
00154   //! Backtracking queue for equalities (to delay them till checkSat() call)
00155   CDList<Theorem> d_eq;
00156   //! Pointer to the next unasserted equality in d_eq
00157   CDO<size_t> d_eqIdx;
00158   //! Pointer to the next equality in d_eq which is not bit-blasted yet
00159   CDO<size_t> d_eqBlastIdx;
00160   //! Backtracking queue for disequalities (to delay them till checkSat() call)
00161   CDList<Theorem> d_diseq;
00162   //! Pointer to the next unasserted disequality in d_diseq
00163   CDO<size_t> d_diseqIdx;
00164   //! Database of stale bit-expansions for update()
00165   CDMap<Expr,bool> d_staleDB;
00166   //! Backtracking queue for TCCs on individual bits
00167   CDList<Theorem> d_tccs;
00168   //! Pointer to the next unasserted TCC in d_tccs
00169   CDO<size_t> d_tccsIdx;
00170   //! Backtracking database of subterms of shared terms
00171   CDMap<Expr,bool> d_sharedSubterms;
00172   //! Cache for typePredicates of non shared terms
00173   CDMap<Expr, Theorem> d_typePredsCache;
00174   //! cache for rewriteBoolean
00175   //CDMap<Expr, Theorem> d_rewriteBooleanCache;
00176   
00177   //! Constant 1-bit bit-vector 0bin0
00178   Expr d_bvZero;
00179   //! Constant 1-bit bit-vector 0bin0
00180   Expr d_bvOne;
00181   //! Return cached constant 0bin0
00182   const Expr& bvZero() const { return d_bvZero; }
00183   //! Return cached constant 0bin1
00184   const Expr& bvOne() const { return d_bvOne; }
00185 
00186   //! functions which implement the DP strategy for bitblasting
00187   Theorem bitBlastEqn(const Expr& e);
00188   //! functions which implement the DP strategy for bitblasting
00189   Theorem bitBlastDisEqn(const Theorem& e);
00190   //! functions which implement the DP strategy for bitblasting
00191   Theorem bitBlastTerm(const Expr& t, int bitPosition, int preComputed);
00192   //! function which implements the DP strtagey to bitblast Inequations
00193   Theorem TheoryBitvector::bitBlastIneqn(const Expr& e);
00194 
00195   //! strategy functions for BVPLUS_NORMAL_FORM
00196   Theorem combineLikeTerms(const Expr& e);
00197   //! strategy fucntions for BVPLUS NORMAL FORM
00198   Theorem padBVPlus(const Expr& e);
00199   //! strategy fucntions for BVPLUS NORMAL FORM
00200   Theorem flattenBVPlus(const Expr& e);
00201   
00202   //! Implementation of the concatenation normal form
00203   Theorem normalizeConcat(const Expr& e, bool useFind);
00204   //! Implementation of the n-bit arithmetic normal form
00205   Theorem normalizeBVArith(const Expr& e, bool useFind);
00206   //! Helper method for composing normalizations
00207   Theorem normalizeConcat(const Theorem& t, bool useFind) {
00208     return transitivityRule(t, normalizeConcat(t.getRHS(), useFind));
00209   }
00210   //! Helper method for composing normalizations
00211   Theorem normalizeBVArith(const Theorem& t, bool useFind) {
00212     return transitivityRule(t, normalizeBVArith(t.getRHS(), useFind));
00213   }
00214 
00215   Theorem signExtendBVLT(const Expr& e, int len, bool useFind);
00216 
00217   Theorem pushNegationRec(const Theorem& thm, bool neg);
00218   Theorem pushNegation(const Expr& e);
00219 
00220   //! Top down simplifier
00221   virtual Theorem simplifyOp(const Expr& e);
00222 
00223 
00224   //! Internal rewrite method for constants
00225   Theorem rewriteConst(const Expr& e);
00226   //! Internal rewrite method
00227   Theorem rewriteBV(const Expr& e, bool useFind);
00228   //! Rewrite children 'n' levels down (n==1 means "only the top level")
00229   Theorem rewriteBV(const Expr& e, int n, bool useFind);
00230   //! Rewrite children 'n' levels down (n==1 means "only the top level")
00231   Theorem rewriteBV(const Theorem& t, int n, bool useFind) {
00232     return transitivityRule(t, rewriteBV(t.getRHS(), n, useFind));
00233   }
00234   //! Internal rewrite method (implements the actual rewrites)
00235   Theorem rewriteBV(const Expr& e, ExprMap<Theorem>& cache, bool useFind);
00236   //! Rewrite children 'n' levels down (n==1 means "only the top level")
00237   Theorem rewriteBV(const Expr& e, ExprMap<Theorem>& cache, int n,
00238                     bool useFind);
00239   //! Rewrite children 'n' levels down (n==1 means "only the top level")
00240   Theorem rewriteBV(const Theorem& t, ExprMap<Theorem>& cache, int n,
00241                     bool useFind) {
00242     return transitivityRule(t, rewriteBV(t.getRHS(), cache, n, useFind));
00243   }
00244 
00245   //! rewrite input boolean expression e to a simpler form
00246   Theorem rewriteBoolean(const Expr& e);
00247   
00248   //! Collect subterms of shared terms
00249   void collectSharedSubterms(const Expr& e);
00250   //! Setup the NotifyList mechanism for the Expr e
00251   void setupExpr(const Expr& e);
00252 public:
00253   TheoryBitvector(TheoryCore* core);
00254   ~TheoryBitvector();
00255 
00256 
00257   ExprMap<Expr> d_bvPlusCarryCacheLeftBV;
00258   ExprMap<Expr> d_bvPlusCarryCacheRightBV;
00259 
00260   // Trusted method that creates the proof rules class (used in constructor).
00261   // Implemented in bitvector_theorem_producer.cpp
00262   BitvectorProofRules* createProofRules();
00263   Theorem pushNegationRec(const Expr& e, bool neg);
00264 
00265   // Theory interface
00266   void addSharedTerm(const Expr& e);
00267   void assertFact(const Theorem& e);
00268   void assertTypePred(const Expr& e, const Theorem& pred);
00269   void checkSat(bool fullEffort);
00270   Theorem rewrite(const Expr& e);
00271   Theorem rewriteAux(const Expr& e);
00272   Theorem rewriteAtomic(const Expr& e);
00273   void setup(const Expr& e);
00274   void update(const Theorem& e, const Expr& d);
00275   Theorem solve(const Theorem& e);
00276   void checkType(const Expr& e);
00277   void computeType(const Expr& e);
00278   void computeModelTerm(const Expr& e, std::vector<Expr>& v);
00279   void computeModel(const Expr& e, std::vector<Expr>& v);
00280   Expr computeTypePred(const Type& t, const Expr& e);
00281   Expr computeTCC(const Expr& e);
00282   ExprStream& print(ExprStream& os, const Expr& e);
00283   Expr parseExprOp(const Expr& e);
00284 
00285   //helper functions
00286   Expr rat(const Rational& r) { return getEM()->newRatExpr(r); }
00287   //!pads e to be of length len
00288   Expr pad(int len, const Expr& e);
00289 
00290   //! Return the number of bits in the bitvector expression
00291   int BVSize(const Expr& e);
00292 
00293   //helper functions: functions to construct exprs
00294   Type newBitvectorType(int i) 
00295     { return newBitvectorTypeExpr(i); }
00296   Expr newBitvectorTypePred(const Type& t, const Expr& e);
00297   Expr newBitvectorTypeExpr(int i);
00298 
00299   Expr newBVAndExpr(const Expr& t1, const Expr& t2);
00300   Expr newBVAndExpr(const std::vector<Expr>& kids);
00301   Expr newBVNandExpr(const Expr& t1, const Expr& t2);
00302   Expr newBVNandExpr(const std::vector<Expr>& kids);
00303 
00304   Expr newBVOrExpr(const Expr& t1, const Expr& t2);
00305   Expr newBVOrExpr(const std::vector<Expr>& kids);
00306   Expr newBVNorExpr(const Expr& t1, const Expr& t2);
00307   Expr newBVNorExpr(const std::vector<Expr>& kids);
00308 
00309   Expr newBVXorExpr(const Expr& t1, const Expr& t2);
00310   Expr newBVXorExpr(const std::vector<Expr>& kids);
00311   Expr newBVXnorExpr(const Expr& t1, const Expr& t2);
00312   Expr newBVXnorExpr(const std::vector<Expr>& kids);
00313   
00314   Expr newBVLTExpr(const Expr& t1, const Expr& t2);
00315   Expr newBVLEExpr(const Expr& t1, const Expr& t2);  
00316   Expr newSXExpr(const Expr& t1, int len);
00317   Expr newSBVLTExpr(const Expr& t1, const Expr& t2);
00318   Expr newSBVLEExpr(const Expr& t1, const Expr& t2);
00319 
00320   Expr newBVNegExpr(const Expr& t1);
00321   Expr newBVUminusExpr(const Expr& t1);
00322   Expr newBoolExtractExpr(const Expr& t1, int r);
00323   Expr newFixedLeftShiftExpr(const Expr& t1, int r);
00324   Expr newFixedRightShiftExpr(const Expr& t1, int r);
00325   Expr newConcatExpr(const Expr& t1, const Expr& t2);
00326   Expr newConcatExpr(const Expr& t1, const Expr& t2, const Expr& t3);
00327   Expr newConcatExpr(const std::vector<Expr>& kids);
00328   Expr newBVConstExpr(const std::string& s, int base = 2);
00329   Expr newBVConstExpr(const std::vector<bool>& bits);
00330   // Construct BVCONST of length 'len', or the min. needed length when len=0.
00331   Expr newBVConstExpr(const Rational& r, int len = 0);
00332   Expr newBVZeroString(int r);
00333   Expr newBVOneString(int r);
00334   //! hi and low are bit indices
00335   Expr newBVExtractExpr(const Expr& e, 
00336                         int hi, int low);
00337   //! 'numbits' is the number of bits in the result
00338   Expr newBVPlusExpr(int numbits, const std::vector<Expr>& k);
00339   //! accepts an integer, r, and bitvector, t1, and returns r.t1
00340   Expr newBVMultExpr(int bvLength,
00341                      const Expr& t1, const Expr& t2);
00342 
00343   // Accessors for expression parameters
00344   int getBitvectorTypeParam(const Expr& e);
00345   int getBitvectorTypeParam(const Type& t) 
00346     { return getBitvectorTypeParam(t.getExpr()); }
00347   Type getTypePredType(const Expr& tp);
00348   const Expr& getTypePredExpr(const Expr& tp);
00349   int getSXIndex(const Expr& e);
00350   int getBoolExtractIndex(const Expr& e);
00351   int getFixedLeftShiftParam(const Expr& e);
00352   int getFixedRightShiftParam(const Expr& e);
00353   int getExtractHi(const Expr& e);
00354   int getExtractLow(const Expr& e);
00355   int getBVPlusParam(const Expr& e);
00356   int getBVMultParam(const Expr& e);
00357 
00358   unsigned getBVConstSize(const Expr& e);
00359   bool getBVConstValue(const Expr& e, int i);
00360   //!computes the integer value of a bitvector constant
00361   Rational computeBVConst(const Expr& e);
00362   //!computes the integer value of ~c+1 or BVUMINUS(c)
00363   Rational computeNegBVConst(const Expr& e);
00364  
00365   //FIXME: do the same for INTTOBV, BVTOINT
00366 }; //end of class TheoryBitvector
00367 
00368   //!computes the integer value of a bitvector constant
00369   Rational computeBVConst(const Expr& e);
00370 
00371 }//end of namespace CVCL
00372 #endif

Generated on Thu Apr 13 16:57:34 2006 for CVC Lite by  doxygen 1.4.4