Add support for true and false and normalizing them away
[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         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         
117         HSIteratorBoolean *getConstraints() { return constraints.iterator(); }
118
119         SATEncoder *getSATEncoder() {return satEncoder;}
120
121         void replaceBooleanWithTrue(Boolean *bexpr);
122         void replaceBooleanWithFalse(Boolean *bexpr);
123         void replaceBooleanWithBoolean(Boolean *oldb, Boolean *newb);
124         CSolver *clone();
125         void autoTune(uint budget);
126
127         void setTuner(Tuner * _tuner) { tuner = _tuner; }
128         long long getElapsedTime() { return elapsedTime; }
129         long long getEncodeTime();
130         long long getSolveTime();
131         
132         MEMALLOC;
133
134 private:
135         void handleXORFalse(BooleanLogic *bexpr, Boolean *child);
136         void handleIMPLIESTrue(BooleanLogic *bexpr, Boolean *child);
137         void handleIMPLIESFalse(BooleanLogic *bexpr, Boolean *child);
138         void handleANDTrue(BooleanLogic *bexpr, Boolean *child);
139         void handleORFalse(BooleanLogic *bexpr, Boolean *child);
140
141         /** This is a vector of constraints that must be satisfied. */
142         HashSetBoolean constraints;
143
144         /** This is a vector of all boolean structs that we have allocated. */
145         Vector<Boolean *> allBooleans;
146
147         /** This is a vector of all set structs that we have allocated. */
148         Vector<Set *> allSets;
149
150         /** This is a vector of all element structs that we have allocated. */
151         Vector<Element *> allElements;
152
153         /** This is a vector of all predicate structs that we have allocated. */
154         Vector<Predicate *> allPredicates;
155
156         /** This is a vector of all table structs that we have allocated. */
157         Vector<Table *> allTables;
158
159         /** This is a vector of all order structs that we have allocated. */
160         Vector<Order *> allOrders;
161
162         /** This is a vector of all function structs that we have allocated. */
163         Vector<Function *> allFunctions;
164
165         Boolean * boolTrue;
166         Boolean * boolFalse;
167         
168         /** These two tables are used for deduplicating entries. */
169         BooleanMatchMap boolMap;
170         ElementMatchMap elemMap;
171         
172         SATEncoder *satEncoder;
173         bool unsat;
174         Tuner *tuner;
175         
176         long long elapsedTime;
177 };
178 #endif