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