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