63ee0fdf057e84b155467f0f66a27105c1ba0813
[satune.git] / src / csolver.h
1 #ifndef CSOLVER_H
2 #define CSOLVER_H
3 #include "classlist.h"
4 #include "ops.h"
5 #include "structs.h"
6 #include "asthash.h"
7
8 class CSolver {
9 public:
10         CSolver();
11         ~CSolver();
12
13         /** This function creates a set containing the elements passed in the array. */
14         Set *createSet(VarType type, uint64_t *elements, uint num);
15
16         /** This function creates a set from lowrange to highrange (inclusive). */
17
18         Set *createRangeSet(VarType type, uint64_t lowrange, uint64_t highrange);
19
20         /** This function creates a mutable set. */
21
22         MutableSet *createMutableSet(VarType type);
23
24         /** This function adds a new item to a set. */
25
26         void addItem(MutableSet *set, uint64_t element);
27
28         /** This function adds a new unique item to the set and returns it.
29             This function cannot be used in conjunction with manually adding
30             items to the set. */
31
32         uint64_t createUniqueItem(MutableSet *set);
33
34         /** This function creates an element variable over a set. */
35
36         Element *getElementVar(Set *set);
37
38         /** This function creates an element constrant. */
39         Element *getElementConst(VarType type, uint64_t value);
40
41         /** This function creates a boolean variable. */
42
43         Boolean *getBooleanVar(VarType type);
44
45         /** This function creates a function operator. */
46
47         Function *createFunctionOperator(ArithOp op, Set **domain, uint numDomain, Set *range,
48                                                                                                                                          OverFlowBehavior overflowbehavior);
49
50         /** This function creates a predicate operator. */
51
52         Predicate *createPredicateOperator(CompOp op, Set **domain, uint numDomain);
53
54         Predicate *createPredicateTable(Table *table, UndefinedBehavior behavior);
55
56         /** This function creates an empty instance table.*/
57
58         Table *createTable(Set **domains, uint numDomain, Set *range);
59
60         Table *createTableForPredicate(Set **domains, uint numDomain);
61         /** This function adds an input output relation to a table. */
62
63         void addTableEntry(Table *table, uint64_t *inputs, uint inputSize, uint64_t result);
64
65         /** This function converts a completed table into a function. */
66
67         Function *completeTable(Table *, UndefinedBehavior behavior);
68
69         /** This function applies a function to the Elements in its input. */
70
71         Element *applyFunction(Function *function, Element **array, uint numArrays, Boolean *overflowstatus);
72
73         /** This function applies a predicate to the Elements in its input. */
74
75         Boolean *applyPredicateTable(Predicate *predicate, Element **inputs, uint numInputs, Boolean *undefinedStatus);
76
77         Boolean *applyPredicate(Predicate *predicate, Element **inputs, uint numInputs);
78
79         /** This function applies a logical operation to the Booleans in its input. */
80
81         Boolean *applyLogicalOperation(LogicOp op, Boolean **array, uint asize);
82
83         /** This function adds a boolean constraint to the set of constraints
84             to be satisfied */
85
86         void addConstraint(Boolean *constraint);
87
88         /** This function instantiates an order of type type over the set set. */
89         Order *createOrder(OrderType type, Set *set);
90
91         /** This function instantiates a boolean on two items in an order. */
92         Boolean *orderConstraint(Order *order, uint64_t first, uint64_t second);
93
94         /** When everything is done, the client calls this function and then csolver starts to encode*/
95         int startEncoding();
96
97         /** After getting the solution from the SAT solver, client can get the value of an element via this function*/
98         uint64_t getElementValue(Element *element);
99
100         /** After getting the solution from the SAT solver, client can get the value of a boolean via this function*/
101         bool getBooleanValue(Boolean *boolean);
102
103         HappenedBefore getOrderConstraintValue(Order *order, uint64_t first, uint64_t second);
104
105         void setUnSAT() { unsat = true; }
106
107         bool isUnSAT() { return unsat; }
108
109         Vector<Order *> *getOrders() { return &allOrders;}
110
111         Tuner *getTuner() { return tuner; }
112         
113         HSIteratorBoolean *getConstraints() { return constraints.iterator(); }
114
115         SATEncoder *getSATEncoder() {return satEncoder;}
116
117         void replaceBooleanWithTrue(Boolean *bexpr);
118         void replaceBooleanWithFalse(Boolean *bexpr);
119         void replaceBooleanWithBoolean(Boolean *oldb, Boolean *newb);
120         CSolver *clone();
121         void autoTune(uint budget);
122
123         void setTuner(Tuner * _tuner) { tuner = _tuner; }
124         long long getElapsedTime() { return elapsedTime; }
125         long long getEncodeTime();
126         long long getSolveTime();
127         
128         MEMALLOC;
129
130 private:
131         void handleXORFalse(BooleanLogic *bexpr, Boolean *child);
132         void handleIMPLIESTrue(BooleanLogic *bexpr, Boolean *child);
133         void handleIMPLIESFalse(BooleanLogic *bexpr, Boolean *child);
134         void handleANDTrue(BooleanLogic *bexpr, Boolean *child);
135         void handleORFalse(BooleanLogic *bexpr, Boolean *child);
136
137         /** This is a vector of constraints that must be satisfied. */
138         HashSetBoolean constraints;
139
140         /** This is a vector of all boolean structs that we have allocated. */
141         Vector<Boolean *> allBooleans;
142
143         /** This is a vector of all set structs that we have allocated. */
144         Vector<Set *> allSets;
145
146         /** This is a vector of all element structs that we have allocated. */
147         Vector<Element *> allElements;
148
149         /** This is a vector of all predicate structs that we have allocated. */
150         Vector<Predicate *> allPredicates;
151
152         /** This is a vector of all table structs that we have allocated. */
153         Vector<Table *> allTables;
154
155         /** This is a vector of all order structs that we have allocated. */
156         Vector<Order *> allOrders;
157
158         /** This is a vector of all function structs that we have allocated. */
159         Vector<Function *> allFunctions;
160
161         /** These two tables are used for deduplicating entries. */
162         BooleanMatchMap boolMap;
163         ElementMatchMap elemMap;
164         
165         SATEncoder *satEncoder;
166         bool unsat;
167         Tuner *tuner;
168         
169         long long elapsedTime;
170 };
171 #endif