9b329f105cb9e9b8169a1c8b770ee857b11f5207
[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(100),
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 }
68
69 void Predicate::copy_predicate_expr(Predicate * other)
70 {
71         PredExprSet * other_pred_expressions = other->get_pred_expressions();
72         PredExprSetIter * it = other_pred_expressions->iterator();
73
74         while (it->hasNext()) {
75                 struct pred_expr * ptr = it->next();
76                 struct pred_expr * copy = new pred_expr(ptr->token, ptr->func_inst, ptr->value);
77                 pred_expressions.add(copy);
78         }
79
80         delete it;
81 }
82
83 /* Follow the child if any child whose FuncInst matches with inst
84  *
85  * @param inst must be an ATOMIC_WRITE FuncInst
86  * @return NULL if no such child is found.
87  */
88 Predicate * Predicate::follow_write_child(FuncInst * inst)
89 {
90         action_type type = inst->get_type();
91         ASSERT(type == ATOMIC_WRITE || type == ATOMIC_INIT);
92
93         for (uint i = 0;i < children.size();i++) {
94                 Predicate * child = children[i];
95                 if (child->get_func_inst() == inst)
96                         return child;
97         }
98
99         return NULL;
100 }
101
102 /* Evaluate predicate expressions against the given inst_act_map */
103 ConcretePredicate * Predicate::evaluate(thread_id_t tid)
104 {
105         /*
106         ConcretePredicate * concrete = new ConcretePredicate(tid);
107         PredExprSetIter * it = pred_expressions.iterator();
108
109         while (it->hasNext()) {
110                 struct pred_expr * ptr = it->next();
111                 uint64_t value = 0;
112
113                 switch(ptr->token) {
114                 case NOPREDICATE:
115                         break;
116                 case EQUALITY:
117                         FuncInst * to_be_compared;
118                         ModelAction * last_act;
119
120                         to_be_compared = ptr->func_inst;
121                         last_act = inst_act_map->get(to_be_compared);
122                         value = last_act->get_reads_from_value();
123                         break;
124                 case NULLITY:
125                         break;
126                 default:
127                         break;
128                 }
129
130                 concrete->add_expression(ptr->token, value, ptr->value);
131         }
132
133         delete it;
134         return concrete;
135         */
136
137         return NULL;
138 }
139
140 void Predicate::print_predicate()
141 {
142         model_print("\"%p\" [shape=box, label=\"", this);
143         if (entry_predicate) {
144                 model_print("entry node\"];\n");
145                 return;
146         }
147
148         if (exit_predicate) {
149                 model_print("exit node\"];\n");
150                 return;
151         }
152
153         model_print("depth: %u; weight: %g\n", depth, weight);
154
155         func_inst->print();
156
157         if (pred_expressions.getSize() == 0)
158                 model_print("predicate unset\n");
159
160         PredExprSetIter * it = pred_expressions.iterator();
161         while (it->hasNext()) {
162                 struct pred_expr * expr = it->next();
163                 FuncInst * inst = expr->func_inst;
164                 switch (expr->token) {
165                 case NOPREDICATE:
166                         break;
167                 case EQUALITY:
168                         model_print("predicate token: equality, position: %s, value: %d\n", inst->get_position(), expr->value);
169                         break;
170                 case NULLITY:
171                         model_print("predicate token: nullity, value: %d\n", expr->value);
172                         break;
173                 default:
174                         model_print("unknown predicate token\n");
175                         break;
176                 }
177         }
178
179         if (does_write) {
180                 model_print("Does write\n");
181         }
182
183         double prob = (double) store_visible_count / total_checking_count;
184         model_print("Total checks: %d, visible count: %d; prob: %f\n", total_checking_count, store_visible_count, prob);
185         model_print("Exploration count: %d, failure count: %d", exploration_count, failure_count);
186         model_print("\"];\n");
187
188         delete it;
189 }
190
191 void Predicate::print_pred_subtree()
192 {
193         print_predicate();
194         for (uint i = 0;i < children.size();i++) {
195                 Predicate * child = children[i];
196                 child->print_pred_subtree();
197                 model_print("\"%p\" -> \"%p\"\n", this, child);
198         }
199
200         PredSetIter * it = backedges.iterator();
201         while (it->hasNext()) {
202                 Predicate * backedge = it->next();
203                 model_print("\"%p\" -> \"%p\"[style=dashed, color=grey]\n", this, backedge);
204         }
205
206         if (exit) {
207                 model_print("\"%p\" -> \"%p\"[style=dashed, color=red]\n", this, exit);
208         }
209
210         delete it;
211 }