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