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