Fix warning
[c11tester.git] / predicate.cc
1 #include "funcinst.h"
2 #include "predicate.h"
3 #include "concretepredicate.h"
4
5 Predicate::Predicate(FuncInst * func_inst, bool is_entry, bool is_exit) :
6         func_inst(func_inst),
7         entry_predicate(is_entry),
8         exit_predicate(is_exit),
9         does_write(false),
10         depth(0),
11         weight(0),
12         exploration_count(0),
13         store_visible_count(0),
14         total_checking_count(0),
15         pred_expressions(16),
16         children(),
17         parent(NULL),
18         exit(NULL),
19         backedges(16)
20 {}
21
22 Predicate::~Predicate()
23 {
24         // parent and func_inst should not be deleted
25         pred_expressions.reset();
26         backedges.reset();
27         children.clear();
28 }
29
30 unsigned int pred_expr_hash(struct pred_expr * expr)
31 {
32         return (unsigned int)((uintptr_t)expr);
33 }
34
35 bool pred_expr_equal(struct pred_expr * p1, struct pred_expr * p2)
36 {
37         if (p1->token != p2->token)
38                 return false;
39         if (p1->token == EQUALITY && p1->func_inst != p2->func_inst)
40                 return false;
41         if (p1->value != p2->value)
42                 return false;
43         return true;
44 }
45
46 void Predicate::add_predicate_expr(token_t token, FuncInst * func_inst, bool value)
47 {
48         struct pred_expr *ptr = new pred_expr(token, func_inst, value);
49         pred_expressions.add(ptr);
50 }
51
52 void Predicate::add_child(Predicate * child)
53 {
54         /* check duplication? */
55         children.push_back(child);
56 }
57
58 void Predicate::set_parent(Predicate * parent_pred)
59 {
60         parent = parent_pred;
61         depth = parent_pred->get_depth() + 1;
62 }
63
64 void Predicate::set_exit(Predicate * exit_pred)
65 {
66         exit = exit_pred;
67         exit_pred->add_pre_exit_predicate(this);
68 }
69
70 void Predicate::alloc_pre_exit_predicates()
71 {
72         pre_exit_predicates = new ModelVector<Predicate *>();
73 }
74
75 void Predicate::add_pre_exit_predicate(Predicate * pred)
76 {
77         pre_exit_predicates->push_back(pred);
78 }
79
80 void Predicate::copy_predicate_expr(Predicate * other)
81 {
82         PredExprSet * other_pred_expressions = other->get_pred_expressions();
83         PredExprSetIter * it = other_pred_expressions->iterator();
84
85         while (it->hasNext()) {
86                 struct pred_expr * ptr = it->next();
87                 struct pred_expr * copy = new pred_expr(ptr->token, ptr->func_inst, ptr->value);
88                 pred_expressions.add(copy);
89         }
90 }
91
92 /* Return the single child branch of this predicate.
93  * Return NULL if this predicate has no children.
94  */
95 Predicate * Predicate::get_single_child(FuncInst * inst)
96 {
97         int size = children.size();
98         if (size == 0)
99                 return NULL;
100
101         /* Should only have one child */
102         ASSERT(size == 1);
103         Predicate * child = children[0];
104
105         ASSERT(child->get_func_inst() == inst);
106
107         return child;
108 }
109
110 /* Evaluate predicate expressions against the given inst_act_map */
111 ConcretePredicate * Predicate::evaluate(inst_act_map_t * inst_act_map, thread_id_t tid)
112 {
113         ConcretePredicate * concrete = new ConcretePredicate(tid);
114         PredExprSetIter * it = pred_expressions.iterator();
115
116         while (it->hasNext()) {
117                 struct pred_expr * ptr = it->next();
118                 uint64_t value = 0;
119
120                 switch(ptr->token) {
121                 case NOPREDICATE:
122                         break;
123                 case EQUALITY:
124                         FuncInst * to_be_compared;
125                         ModelAction * last_act;
126
127                         to_be_compared = ptr->func_inst;
128                         last_act = inst_act_map->get(to_be_compared);
129                         value = last_act->get_reads_from_value();
130                         break;
131                 case NULLITY:
132                         break;
133                 default:
134                         break;
135                 }
136
137                 concrete->add_expression(ptr->token, value, ptr->value);
138         }
139
140         return concrete;
141 }
142
143 void Predicate::print_predicate()
144 {
145         model_print("\"%p\" [shape=box, label=\"", this);
146         if (entry_predicate) {
147                 model_print("entry node\"];\n");
148                 return;
149         }
150
151         if (exit_predicate) {
152                 model_print("exit node\"];\n");
153                 return;
154         }
155
156         model_print("depth: %u; weight: %g\n", depth, weight);
157
158         func_inst->print();
159
160         if (pred_expressions.getSize() == 0)
161                 model_print("predicate unset\n");
162
163         PredExprSetIter * it = pred_expressions.iterator();
164         while (it->hasNext()) {
165                 struct pred_expr * expr = it->next();
166                 FuncInst * inst = expr->func_inst;
167                 switch (expr->token) {
168                 case NOPREDICATE:
169                         break;
170                 case EQUALITY:
171                         model_print("predicate token: equality, position: %s, value: %d\n", inst->get_position(), expr->value);
172                         break;
173                 case NULLITY:
174                         model_print("predicate token: nullity, value: %d\n", expr->value);
175                         break;
176                 default:
177                         model_print("unknown predicate token\n");
178                         break;
179                 }
180         }
181
182         if (does_write) {
183                 model_print("Does write\n");
184         }
185
186         double prob = (double) store_visible_count / total_checking_count;
187         model_print("Total checks: %d, visible count: %d; prob: %f\n", total_checking_count, store_visible_count, prob);
188         model_print("Exploration count: %d", exploration_count);
189         model_print("\"];\n");
190 }
191
192 void Predicate::print_pred_subtree()
193 {
194         print_predicate();
195         for (uint i = 0;i < children.size();i++) {
196                 Predicate * child = children[i];
197                 child->print_pred_subtree();
198                 model_print("\"%p\" -> \"%p\"\n", this, child);
199         }
200
201         PredSetIter * it = backedges.iterator();
202         while (it->hasNext()) {
203                 Predicate * backedge = it->next();
204                 model_print("\"%p\" -> \"%p\"[style=dashed, color=grey]\n", this, backedge);
205         }
206
207         if (exit) {
208                 model_print("\"%p\" -> \"%p\"[style=dashed, color=red]\n", this, exit);
209         }
210 }