Design a method to select predicate branches based on exploration counts
[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) :
6         func_inst(func_inst),
7         entry_predicate(is_entry),
8         does_write(false),
9         exploration_count(0),
10         pred_expressions(16),
11         children(),
12         parent(NULL),
13         backedges(16)
14 {}
15
16 Predicate::~Predicate()
17 {
18         // parent and func_inst should not be deleted
19         pred_expressions.reset();
20         backedges.reset();
21         children.clear();
22 }
23
24 unsigned int pred_expr_hash(struct pred_expr * expr)
25 {
26         return (unsigned int)((uintptr_t)expr);
27 }
28
29 bool pred_expr_equal(struct pred_expr * p1, struct pred_expr * p2)
30 {
31         if (p1->token != p2->token)
32                 return false;
33         if (p1->token == EQUALITY && p1->func_inst != p2->func_inst)
34                 return false;
35         if (p1->value != p2->value)
36                 return false;
37         return true;
38 }
39
40 void Predicate::add_predicate_expr(token_t token, FuncInst * func_inst, bool value)
41 {
42         struct pred_expr *ptr = new pred_expr(token, func_inst, value);
43         pred_expressions.add(ptr);
44 }
45
46 void Predicate::add_child(Predicate * child)
47 {
48         /* check duplication? */
49         children.push_back(child);
50 }
51
52 void Predicate::copy_predicate_expr(Predicate * other)
53 {
54         PredExprSet * other_pred_expressions = other->get_pred_expressions();
55         PredExprSetIter * it = other_pred_expressions->iterator();
56
57         while (it->hasNext()) {
58                 struct pred_expr * ptr = it->next();
59                 struct pred_expr * copy = new pred_expr(ptr->token, ptr->func_inst, ptr->value);
60                 pred_expressions.add(copy);
61         }
62 }
63
64 /* Evaluate predicate expressions against the given inst_act_map */
65 ConcretePredicate * Predicate::evaluate(inst_act_map_t * inst_act_map, thread_id_t tid)
66 {
67         ConcretePredicate * concrete = new ConcretePredicate(tid);
68         PredExprSetIter * it = pred_expressions.iterator();
69
70         while (it->hasNext()) {
71                 struct pred_expr * ptr = it->next();
72                 uint64_t value = 0;
73
74                 switch(ptr->token) {
75                         case NOPREDICATE:
76                                 break;
77                         case EQUALITY:
78                                 FuncInst * to_be_compared;
79                                 ModelAction * last_act;
80
81                                 to_be_compared = ptr->func_inst;
82                                 last_act = inst_act_map->get(to_be_compared);
83                                 value = last_act->get_reads_from_value();
84                                 break;
85                         case NULLITY:
86                                 break;
87                         default:
88                                 break;
89                 }
90
91                 concrete->add_expression(ptr->token, value, ptr->value);
92         }
93
94         return concrete;
95 }
96
97 void Predicate::print_predicate()
98 {
99         model_print("\"%p\" [shape=box, label=\"\n", this);
100         if (entry_predicate) {
101                 model_print("entry node\"];\n");
102                 return;
103         }
104
105         func_inst->print();
106
107         PredExprSetIter * it = pred_expressions.iterator();
108
109         if (pred_expressions.getSize() == 0)
110                 model_print("predicate unset\n");
111
112         while (it->hasNext()) {
113                 struct pred_expr * expr = it->next();
114                 FuncInst * inst = expr->func_inst;
115                 switch (expr->token) {
116                         case NOPREDICATE:
117                                 break;
118                         case EQUALITY:
119                                 model_print("predicate token: equality, position: %s, value: %d\n", inst->get_position(), expr->value);
120                                 break;
121                         case NULLITY:
122                                 model_print("predicate token: nullity, value: %d\n", expr->value);
123                                 break;
124                         default:
125                                 model_print("unknown predicate token\n");
126                                 break;
127                 }
128         }
129
130         if (does_write) {
131                 model_print("Does write\n");
132         }
133         model_print("Count: %d\n", exploration_count);
134         model_print("\"];\n");
135 }
136
137 void Predicate::print_pred_subtree()
138 {
139         print_predicate();
140         for (uint i = 0; i < children.size(); i++) {
141                 Predicate * child = children[i];
142                 child->print_pred_subtree();
143                 model_print("\"%p\" -> \"%p\"\n", this, child);
144         }
145
146         PredSetIter * it = backedges.iterator();
147         while (it->hasNext()) {
148                 Predicate * backedge = it->next();
149                 model_print("\"%p\" -> \"%p\"[style=dashed, color=grey]\n", this, backedge);
150         }
151 }