Bug patches
[satune.git] / src / csolver.h
1 #ifndef CSOLVER_H
2 #define CSOLVER_H
3 #include "classes.h"
4 #include "ops.h"
5 #include "corestructs.h"
6 #include "asthash.h"
7 #include "solver_interface.h"
8
9 class CSolver {
10 public:
11         CSolver();
12         ~CSolver();
13
14         /** This function creates a set containing the elements passed in the array. */
15         Set *createSet(VarType type, uint64_t *elements, uint num);
16
17         /** This function creates a set from lowrange to highrange (inclusive). */
18
19         Set *createRangeSet(VarType type, uint64_t lowrange, uint64_t highrange);
20
21         Element *createRangeVar(VarType type, uint64_t lowrange, uint64_t highrange);
22
23         /** This function creates a mutable set. */
24
25         MutableSet *createMutableSet(VarType type);
26
27         /** This function adds a new item to a set. */
28
29         //Deprecating this unless we need it...
30         void addItem(MutableSet *set, uint64_t element);
31
32         /** This function adds a new unique item to the set and returns it.
33             This function cannot be used in conjunction with manually adding
34             items to the set. */
35
36         uint64_t createUniqueItem(MutableSet *set);
37         
38         /**
39          * Freeze and finalize the mutableSet ...
40          */
41         void finalizeMutableSet(MutableSet* set);
42
43         /** This function creates an element variable over a set. */
44
45         Element *getElementVar(Set *set);
46
47         /** This function creates an element constrant. */
48         Element *getElementConst(VarType type, uint64_t value);
49
50         BooleanEdge getBooleanTrue();
51
52         BooleanEdge getBooleanFalse();
53
54         /** This function creates a boolean variable. */
55
56         BooleanEdge getBooleanVar(VarType type);
57
58         /** This function creates a function operator. */
59
60         Function *createFunctionOperator(ArithOp op, Set **domain, uint numDomain, Set *range,
61                                                                                                                                          OverFlowBehavior overflowbehavior);
62
63         /** This function creates a predicate operator. */
64
65         Predicate *createPredicateOperator(CompOp op, Set **domain, uint numDomain);
66
67         Predicate *createPredicateTable(Table *table, UndefinedBehavior behavior);
68
69         /** This function creates an empty instance table.*/
70
71         Table *createTable(Set **domains, uint numDomain, Set *range);
72
73         Table *createTableForPredicate(Set **domains, uint numDomain);
74         /** This function adds an input output relation to a table. */
75
76         void addTableEntry(Table *table, uint64_t *inputs, uint inputSize, uint64_t result);
77
78         /** This function converts a completed table into a function. */
79
80         Function *completeTable(Table *, UndefinedBehavior behavior);
81
82         /** This function applies a function to the Elements in its input. */
83
84         Element *applyFunction(Function *function, Element **array, uint numArrays, BooleanEdge overflowstatus);
85
86         /** This function applies a predicate to the Elements in its input. */
87
88         BooleanEdge applyPredicateTable(Predicate *predicate, Element **inputs, uint numInputs, BooleanEdge undefinedStatus);
89
90         BooleanEdge applyPredicate(Predicate *predicate, Element **inputs, uint numInputs);
91
92         /** This function applies a logical operation to the Booleans in its input. */
93
94         BooleanEdge applyLogicalOperation(LogicOp op, BooleanEdge *array, uint asize);
95
96         /** This function applies a logical operation to the Booleans in its input. */
97
98         BooleanEdge applyLogicalOperation(LogicOp op, BooleanEdge arg1, BooleanEdge arg2);
99
100         /** This function applies a logical operation to the Booleans in its input. */
101
102         BooleanEdge applyLogicalOperation(LogicOp op, BooleanEdge arg);
103
104         /** This function adds a boolean constraint to the set of constraints
105             to be satisfied */
106
107         void addConstraint(BooleanEdge constraint);
108
109         /** This function instantiates an order of type type over the set set. */
110         Order *createOrder(OrderType type, Set *set);
111
112         /** This function instantiates a boolean on two items in an order. */
113         BooleanEdge orderConstraint(Order *order, uint64_t first, uint64_t second);
114
115         /** When everything is done, the client calls this function and then csolver starts to encode*/
116         int solve();
117
118         /** After getting the solution from the SAT solver, client can get the value of an element via this function*/
119         uint64_t getElementValue(Element *element);
120
121         /** After getting the solution from the SAT solver, client can get the value of a boolean via this function*/
122         bool getBooleanValue(BooleanEdge boolean);
123
124         bool getOrderConstraintValue(Order *order, uint64_t first, uint64_t second);
125
126         bool isTrue(BooleanEdge b);
127         bool isFalse(BooleanEdge b);
128
129         void setUnSAT() { unsat = true; }
130
131         bool isUnSAT() { return unsat; }
132
133         Vector<Order *> *getOrders() { return &allOrders;}
134         HashsetOrder * getActiveOrders() { return &activeOrders;}
135
136         Tuner *getTuner() { return tuner; }
137
138         SetIteratorBooleanEdge * getConstraints() { return constraints.iterator(); }
139
140         SATEncoder *getSATEncoder() {return satEncoder;}
141
142         void replaceBooleanWithTrue(BooleanEdge bexpr);
143         void replaceBooleanWithTrueNoRemove(BooleanEdge bexpr);
144         void replaceBooleanWithFalse(BooleanEdge bexpr);
145         void replaceBooleanWithBoolean(BooleanEdge oldb, BooleanEdge newb);
146         CSolver *clone();
147         void serialize();
148         void autoTune(uint budget);
149
150         void setTuner(Tuner *_tuner) { tuner = _tuner; }
151         long long getElapsedTime() { return elapsedTime; }
152         long long getEncodeTime();
153         long long getSolveTime();
154
155         CMEMALLOC;
156
157 private:
158         void handleIFFTrue(BooleanLogic *bexpr, BooleanEdge child);
159         void handleANDTrue(BooleanLogic *bexpr, BooleanEdge child);
160
161         //These two functions are helpers if the client has a pointer to a
162         //Boolean object that we have since replaced
163         BooleanEdge rewriteLogicalOperation(LogicOp op, BooleanEdge *array, uint asize);
164         BooleanEdge doRewrite(BooleanEdge b);
165         /** This is a vector of constraints that must be satisfied. */
166         HashsetBooleanEdge constraints;
167
168         /** This is a vector of all boolean structs that we have allocated. */
169         Vector<Boolean *> allBooleans;
170
171         /** This is a vector of all set structs that we have allocated. */
172         Vector<Set *> allSets;
173
174         /** This is a vector of all element structs that we have allocated. */
175         Vector<Element *> allElements;
176
177         /** This is a vector of all predicate structs that we have allocated. */
178         Vector<Predicate *> allPredicates;
179
180         /** This is a vector of all table structs that we have allocated. */
181         Vector<Table *> allTables;
182
183         /** This is a vector of all order structs that we have allocated. */
184         Vector<Order *> allOrders;
185
186         HashsetOrder activeOrders;
187         
188         /** This is a vector of all function structs that we have allocated. */
189         Vector<Function *> allFunctions;
190
191         BooleanEdge boolTrue;
192         BooleanEdge boolFalse;
193
194         /** These two tables are used for deduplicating entries. */
195         BooleanMatchMap boolMap;
196         ElementMatchMap elemMap;
197
198         SATEncoder *satEncoder;
199         bool unsat;
200         Tuner *tuner;
201         long long elapsedTime;
202 };
203 #endif