9ba69d387841683b478699469f8d64050ce120a6
[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(),
7         children(),
8         parent(NULL),
9         backedge(NULL)
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("no predicate\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 EQUALITY:
72                                 model_print("predicate token: equality, position: %s, value: %d\n", inst->get_position(), expr->value);
73                                 break;
74                         case NULLITY:
75                                 model_print("predicate token: nullity, value: %d\n", expr->value);
76                                 break;
77                         default:
78                                 model_print("unknown predicate token\n");
79                                 break;
80                 }
81         }
82         model_print("\"];\n");
83 }
84
85 void Predicate::print_pred_subtree()
86 {
87         print_predicate();
88         for (uint i = 0; i < children.size(); i++) {
89                 Predicate * child = children[i];
90                 child->print_pred_subtree();
91                 model_print("\"%p\" -> \"%p\"\n", this, child);
92         }
93
94         if (backedge != NULL)
95                 model_print("\"%p\" -> \"%p\"[style=dashed, color=grey]\n", this, backedge);
96 }