Add edges between FuncNodes
[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 void Predicate::print_predicate()
62 {
63         model_print("\"%p\" [shape=box, label=\"\n", this);
64         if (entry_predicate) {
65                 model_print("entry node\"];\n");
66                 return;
67         }
68
69         func_inst->print();
70
71         PredExprSetIter * it = pred_expressions.iterator();
72
73         if (pred_expressions.getSize() == 0)
74                 model_print("predicate unset\n");
75
76         while (it->hasNext()) {
77                 struct pred_expr * expr = it->next();
78                 FuncInst * inst = expr->func_inst;
79                 switch (expr->token) {
80                         case NOPREDICATE:
81                                 break;
82                         case EQUALITY:
83                                 model_print("predicate token: equality, position: %s, value: %d\n", inst->get_position(), expr->value);
84                                 break;
85                         case NULLITY:
86                                 model_print("predicate token: nullity, value: %d\n", expr->value);
87                                 break;
88                         default:
89                                 model_print("unknown predicate token\n");
90                                 break;
91                 }
92         }
93
94         if (does_write) {
95                 model_print("Does write\n");
96         }
97         model_print("\"];\n");
98 }
99
100 void Predicate::print_pred_subtree()
101 {
102         print_predicate();
103         for (uint i = 0; i < children.size(); i++) {
104                 Predicate * child = children[i];
105                 child->print_pred_subtree();
106                 model_print("\"%p\" -> \"%p\"\n", this, child);
107         }
108
109         PredSetIter * it = backedges.iterator();
110         while (it->hasNext()) {
111                 Predicate * backedge = it->next();
112                 model_print("\"%p\" -> \"%p\"[style=dashed, color=grey]\n", this, backedge);
113         }
114 }