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