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