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