Reorganize codes
[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 void Predicate::print_predicate()
97 {
98         model_print("\"%p\" [shape=box, label=\"\n", this);
99         if (entry_predicate) {
100                 model_print("entry node\"];\n");
101                 return;
102         }
103
104         func_inst->print();
105
106         PredExprSetIter * it = pred_expressions.iterator();
107
108         if (pred_expressions.getSize() == 0)
109                 model_print("predicate unset\n");
110
111         while (it->hasNext()) {
112                 struct pred_expr * expr = it->next();
113                 FuncInst * inst = expr->func_inst;
114                 switch (expr->token) {
115                         case NOPREDICATE:
116                                 break;
117                         case EQUALITY:
118                                 model_print("predicate token: equality, position: %s, value: %d\n", inst->get_position(), expr->value);
119                                 break;
120                         case NULLITY:
121                                 model_print("predicate token: nullity, value: %d\n", expr->value);
122                                 break;
123                         default:
124                                 model_print("unknown predicate token\n");
125                                 break;
126                 }
127         }
128
129         if (does_write) {
130                 model_print("Does write\n");
131         }
132         model_print("\"];\n");
133 }
134
135 void Predicate::print_pred_subtree()
136 {
137         print_predicate();
138         for (uint i = 0; i < children.size(); i++) {
139                 Predicate * child = children[i];
140                 child->print_pred_subtree();
141                 model_print("\"%p\" -> \"%p\"\n", this, child);
142         }
143
144         PredSetIter * it = backedges.iterator();
145         while (it->hasNext()) {
146                 Predicate * backedge = it->next();
147                 model_print("\"%p\" -> \"%p\"[style=dashed, color=grey]\n", this, backedge);
148         }
149 }