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