1 /********************************************************************
2  * AUTHORS: Trevor Hansen
3  *
4  * BEGIN DATE: June, 2010
5  *
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23 ********************************************************************/
24 
25 #ifndef TOSATAIG_H
26 #define TOSATAIG_H
27 #include <cmath>
28 
29 #include "stp/AST/AST.h"
30 #include "stp/AbsRefineCounterExample/ArrayTransformer.h"
31 #include "stp/STPManager/STPManager.h"
32 #include "stp/ToSat/AIG/BBNodeManagerAIG.h"
33 #include "stp/ToSat/AIG/ToCNFAIG.h"
34 #include "stp/ToSat/BitBlaster.h"
35 #include "stp/Util/RunTimes.h"
36 
37 namespace stp
38 {
39 
40 class DLL_PUBLIC ToSATAIG : public ToSATBase
41 {
42 private:
43   ASTNodeToSATVar nodeToSATVar;
44   simplifier::constantBitP::ConstantBitPropagation* cb;
45 
46   ArrayTransformer* arrayTransformer;
47 
48   // don't assign or copy construct.
49   ToSATAIG& operator=(const ToSATAIG& other);
50   ToSATAIG(const ToSATAIG& other);
51 
52   // Minisat doesn't, but simplifying minisat and cryptominsat eliminate
53   // variables during their
54   // simplification phases. The problem is that we may later add clauses in that
55   // refer to those
56   // simplified-away variables. Here we mark them as frozen which prevents them
57   // from being removed.
58   void mark_variables_as_frozen(SATSolver& satSolver);
59 
60   bool runSolver(SATSolver& satSolver);
61   void handle_cnf_options(Cnf_Dat_t* cnfData, bool needAbsRef);
62 
63   int count;
64   bool first;
65 
66   ToCNFAIG toCNF;
67 
init()68   void init()
69   {
70     count = 0;
71     first = true;
72   }
73 
74   static THREAD_LOCAL int cnf_calls;
75 
76 public:
77   void add_cnf_to_solver(SATSolver& satSolver, Cnf_Dat_t* cnfData);
78   Cnf_Dat_t* bitblast(const ASTNode& input, bool needAbsRef);
79   void release_cnf_memory(Cnf_Dat_t* cnfData);
80 
cbIsDestructed()81   bool cbIsDestructed() { return cb == NULL; }
82 
ToSATAIG(STPMgr * bm,ArrayTransformer * at)83   ToSATAIG(STPMgr* bm, ArrayTransformer* at)
84       : ToSATBase(bm), toCNF(bm->UserFlags)
85   {
86     cb = NULL;
87     init();
88     arrayTransformer = at;
89   }
90 
ToSATAIG(STPMgr * bm,simplifier::constantBitP::ConstantBitPropagation * cb_,ArrayTransformer * at)91   ToSATAIG(STPMgr* bm, simplifier::constantBitP::ConstantBitPropagation* cb_,
92            ArrayTransformer* at)
93       : ToSATBase(bm), cb(cb_), toCNF(bm->UserFlags)
94   {
95     cb = cb_;
96     init();
97     arrayTransformer = at;
98   }
99 
100   ~ToSATAIG();
101 
ClearAllTables()102   void ClearAllTables() { nodeToSATVar.clear(); }
103 
104   // Used to read out the satisfiable answer.
SATVar_to_SymbolIndexMap()105   ASTNodeToSATVar& SATVar_to_SymbolIndexMap() { return nodeToSATVar; }
106 
107   bool CallSAT(SATSolver& satSolver, const ASTNode& input, bool needAbsRef);
108 };
109 }
110 
111 #endif
112