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