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