Fix some memory leaks
[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
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 printConstraints();
138
139         Vector<Order *> *getOrders() { return &allOrders;}
140         HashsetOrder *getActiveOrders() { return &activeOrders;}
141
142         Tuner *getTuner() { return tuner; }
143
144         SetIteratorBooleanEdge *getConstraints() { return constraints.iterator(); }
145
146         SATEncoder *getSATEncoder() {return satEncoder;}
147
148         void replaceBooleanWithTrue(BooleanEdge bexpr);
149         void replaceBooleanWithTrueNoRemove(BooleanEdge bexpr);
150         void replaceBooleanWithFalse(BooleanEdge bexpr);
151         void replaceBooleanWithBoolean(BooleanEdge oldb, BooleanEdge newb);
152         CSolver *clone();
153         void serialize();
154         void autoTune(uint budget);
155
156         void setTuner(Tuner *_tuner) { tuner = _tuner; }
157         long long getElapsedTime() { return elapsedTime; }
158         long long getEncodeTime();
159         long long getSolveTime();
160
161         CMEMALLOC;
162
163 private:
164         void handleIFFTrue(BooleanLogic *bexpr, BooleanEdge child);
165         void handleANDTrue(BooleanLogic *bexpr, BooleanEdge child);
166
167         //These two functions are helpers if the client has a pointer to a
168         //Boolean object that we have since replaced
169         BooleanEdge rewriteLogicalOperation(LogicOp op, BooleanEdge *array, uint asize);
170         BooleanEdge doRewrite(BooleanEdge b);
171         /** This is a vector of constraints that must be satisfied. */
172         HashsetBooleanEdge constraints;
173
174         /** This is a vector of all boolean structs that we have allocated. */
175         Vector<Boolean *> allBooleans;
176
177         /** This is a vector of all set structs that we have allocated. */
178         Vector<Set *> allSets;
179
180         /** This is a vector of all element structs that we have allocated. */
181         Vector<Element *> allElements;
182
183         /** This is a vector of all predicate structs that we have allocated. */
184         Vector<Predicate *> allPredicates;
185
186         /** This is a vector of all table structs that we have allocated. */
187         Vector<Table *> allTables;
188
189         /** This is a vector of all order structs that we have allocated. */
190         Vector<Order *> allOrders;
191
192         HashsetOrder activeOrders;
193
194         /** This is a vector of all function structs that we have allocated. */
195         Vector<Function *> allFunctions;
196
197         BooleanEdge boolTrue;
198         BooleanEdge boolFalse;
199
200         /** These two tables are used for deduplicating entries. */
201         BooleanMatchMap boolMap;
202         ElementMatchMap elemMap;
203
204         SATEncoder *satEncoder;
205         bool unsat;
206         Tuner *tuner;
207         long long elapsedTime;
208 };
209 #endif