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