Clear headers
[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         pred_expressions(16),
10         children(),
11         parent(NULL),
12         backedges(16)
13 {}
14
15 Predicate::~Predicate()
16 {
17         // parent and func_inst should not be deleted
18         pred_expressions.reset();
19         backedges.reset();
20         children.clear();
21 }
22
23 unsigned int pred_expr_hash(struct pred_expr * expr)
24 {
25         return (unsigned int)((uintptr_t)expr);
26 }
27
28 bool pred_expr_equal(struct pred_expr * p1, struct pred_expr * p2)
29 {
30         if (p1->token != p2->token)
31                 return false;
32         if (p1->token == EQUALITY && p1->func_inst != p2->func_inst)
33                 return false;
34         if (p1->value != p2->value)
35                 return false;
36         return true;
37 }
38
39 void Predicate::add_predicate_expr(token_t token, FuncInst * func_inst, bool value)
40 {
41         struct pred_expr *ptr = new pred_expr(token, func_inst, value);
42         pred_expressions.add(ptr);
43 }
44
45 void Predicate::add_child(Predicate * child)
46 {
47         /* check duplication? */
48         children.push_back(child);
49 }
50
51 void Predicate::copy_predicate_expr(Predicate * other)
52 {
53         PredExprSet * other_pred_expressions = other->get_pred_expressions();
54         PredExprSetIter * it = other_pred_expressions->iterator();
55
56         while (it->hasNext()) {
57                 struct pred_expr * ptr = it->next();
58                 struct pred_expr * copy = new pred_expr(ptr->token, ptr->func_inst, ptr->value);
59                 pred_expressions.add(copy);
60         }
61 }
62
63 /* Evaluate predicate expressions against the given inst_act_map */
64 ConcretePredicate * Predicate::evaluate(inst_act_map_t * inst_act_map, thread_id_t tid)
65 {
66         ConcretePredicate * concrete = new ConcretePredicate(tid);
67         PredExprSetIter * it = pred_expressions.iterator();
68
69         while (it->hasNext()) {
70                 struct pred_expr * ptr = it->next();
71                 uint64_t value = 0;
72
73                 switch(ptr->token) {
74                         case NOPREDICATE:
75                                 break;
76                         case EQUALITY:
77                                 FuncInst * to_be_compared;
78                                 ModelAction * last_act;
79
80                                 to_be_compared = ptr->func_inst;
81                                 last_act = inst_act_map->get(to_be_compared);
82                                 value = last_act->get_reads_from_value();
83                                 break;
84                         case NULLITY:
85                                 break;
86                         default:
87                                 break;
88                 }
89
90                 concrete->add_expression(ptr->token, value, ptr->value);
91         }
92
93         return concrete;
94 }
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("\"];\n");
134 }
135
136 void Predicate::print_pred_subtree()
137 {
138         print_predicate();
139         for (uint i = 0; i < children.size(); i++) {
140                 Predicate * child = children[i];
141                 child->print_pred_subtree();
142                 model_print("\"%p\" -> \"%p\"\n", this, child);
143         }
144
145         PredSetIter * it = backedges.iterator();
146         while (it->hasNext()) {
147                 Predicate * backedge = it->next();
148                 model_print("\"%p\" -> \"%p\"[style=dashed, color=grey]\n", this, backedge);
149         }
150 }