c8e3989133fddedb009a8bc8544dfe48d893fdb4
[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         pred_expressions(16),
7         children(),
8         parent(NULL),
9         backedges(16)
10 {}
11
12 unsigned int pred_expr_hash(struct pred_expr * expr)
13 {
14         return (unsigned int)((uintptr_t)expr);
15 }
16
17 bool pred_expr_equal(struct pred_expr * p1, struct pred_expr * p2)
18 {
19         if (p1->token != p2->token)
20                 return false;
21         if (p1->token == EQUALITY && p1->func_inst != p2->func_inst)
22                 return false;
23         if (p1->value != p2->value)
24                 return false;
25         return true;
26 }
27
28 void Predicate::add_predicate_expr(token_t token, FuncInst * func_inst, bool value)
29 {
30         struct pred_expr *ptr = new pred_expr(token, func_inst, value);
31         pred_expressions.add(ptr);
32 }
33
34 void Predicate::add_child(Predicate * child)
35 {
36         /* check duplication? */
37         children.push_back(child);
38 }
39
40 void Predicate::copy_predicate_expr(Predicate * other)
41 {
42         PredExprSet * other_pred_expressions = other->get_pred_expressions();
43         PredExprSetIter * it = other_pred_expressions->iterator();
44
45         while (it->hasNext()) {
46                 struct pred_expr * ptr = it->next();
47                 struct pred_expr * copy = new pred_expr(ptr->token, ptr->func_inst, ptr->value);
48                 pred_expressions.add(copy);
49         }
50 }
51
52 void Predicate::print_predicate()
53 {
54         model_print("\"%p\" [shape=box, label=\"\n", this);
55         if (entry_predicate) {
56                 model_print("entry node\"];\n");
57                 return;
58         }
59
60         func_inst->print();
61
62         PredExprSetIter * it = pred_expressions.iterator();
63
64         if (pred_expressions.getSize() == 0)
65                 model_print("predicate unset\n");
66
67         while (it->hasNext()) {
68                 struct pred_expr * expr = it->next();
69                 FuncInst * inst = expr->func_inst;
70                 switch (expr->token) {
71                         case NOPREDICATE:
72                                 break;
73                         case EQUALITY:
74                                 model_print("predicate token: equality, position: %s, value: %d\n", inst->get_position(), expr->value);
75                                 break;
76                         case NULLITY:
77                                 model_print("predicate token: nullity, value: %d\n", expr->value);
78                                 break;
79                         default:
80                                 model_print("unknown predicate token\n");
81                                 break;
82                 }
83         }
84         model_print("\"];\n");
85 }
86
87 void Predicate::print_pred_subtree()
88 {
89         print_predicate();
90         for (uint i = 0; i < children.size(); i++) {
91                 Predicate * child = children[i];
92                 child->print_pred_subtree();
93                 model_print("\"%p\" -> \"%p\"\n", this, child);
94         }
95
96         PredSetIter * it = backedges.iterator();
97         while (it->hasNext()) {
98                 Predicate * backedge = it->next();
99                 model_print("\"%p\" -> \"%p\"[style=dashed, color=grey]\n", this, backedge);
100         }
101 }