Compiles
[satune.git] / src / AST / asthash.cc
1 #include "asthash.h"
2 #include "mymemory.h"
3 #include "structs.h"
4 #include "boolean.h"
5 #include "element.h"
6
7 bool compareArray(Array<BooleanEdge> *inputs1, Array<BooleanEdge> *inputs2) {
8         if (inputs1->getSize() != inputs2->getSize())
9                 return false;
10         for (uint i = 0; i < inputs1->getSize(); i++) {
11                 if (inputs1->get(i) != inputs2->get(i))
12                         return false;
13         }
14         return true;
15 }
16
17 bool compareArray(Array<Element *> *inputs1, Array<Element *> *inputs2) {
18         if (inputs1->getSize() != inputs2->getSize())
19                 return false;
20         for (uint i = 0; i < inputs1->getSize(); i++) {
21                 if (inputs1->get(i) != inputs2->get(i))
22                         return false;
23         }
24         return true;
25 }
26
27 uint hashArray(Array<BooleanEdge> *inputs) {
28         uint hash = inputs->getSize();
29         for (uint i = 0; i < inputs->getSize(); i++) {
30                 uint newval = (uint)(uintptr_t) inputs->get(i).getRaw();
31                 hash ^= newval;
32                 hash = (hash << 3) | (hash >> 29);
33         }
34         return hash;
35 }
36
37 uint hashArray(Array<Element *> *inputs) {
38         uint hash = inputs->getSize();
39         for (uint i = 0; i < inputs->getSize(); i++) {
40                 uint newval = (uint)(uintptr_t) inputs->get(i);
41                 hash ^= newval;
42                 hash = (hash << 3) | (hash >> 29);
43         }
44         return hash;
45 }
46
47 uint hashBoolean(Boolean *b) {
48         switch (b->type) {
49         case ORDERCONST: {
50                 BooleanOrder *o = (BooleanOrder *)b;
51                 return ((uint)(uintptr_t) o->order) ^ ((uint) o->first) ^ (((uint)(o->second)) << 2);
52         }
53         case BOOLEANVAR: {
54                 return (uint)(uintptr_t) b;
55         }
56         case LOGICOP: {
57                 BooleanLogic *l = (BooleanLogic *)b;
58                 return ((uint)l->op) ^ hashArray(&l->inputs);
59         }
60         case PREDICATEOP: {
61                 BooleanPredicate *p = (BooleanPredicate *)b;
62                 return ((uint)(uintptr_t) p->predicate) ^
63                                          (((uint)(uintptr_t) p->undefStatus) << 1) ^
64                                          hashArray(&p->inputs);
65         }
66         default:
67                 ASSERT(0);
68         }
69 }
70
71 bool compareBoolean(Boolean *b1, Boolean *b2) {
72         if (b1->type != b2->type)
73                 return false;
74         switch (b1->type) {
75         case ORDERCONST: {
76                 BooleanOrder *o1 = (BooleanOrder *)b1;
77                 BooleanOrder *o2 = (BooleanOrder *)b2;
78                 return (o1->order == o2->order) && (o1->first == o2->first) && (o1->second == o2->second);
79         }
80         case BOOLEANVAR: {
81                 return b1 == b2;
82         }
83         case LOGICOP: {
84                 BooleanLogic *l1 = (BooleanLogic *)b1;
85                 BooleanLogic *l2 = (BooleanLogic *)b2;
86                 return (l1->op == l2->op) && compareArray(&l1->inputs, &l2->inputs);
87         }
88         case PREDICATEOP: {
89                 BooleanPredicate *p1 = (BooleanPredicate *)b1;
90                 BooleanPredicate *p2 = (BooleanPredicate *)b2;
91                 return (p1->predicate == p2->predicate) &&
92                                          p1->undefStatus == p2->undefStatus &&
93                                          compareArray(&p1->inputs, &p2->inputs);
94         }
95         default:
96                 ASSERT(0);
97         }
98 }
99
100 uint hashElement(Element *e) {
101         switch (e->type) {
102         case ELEMSET: {
103                 return (uint)(uintptr_t) e;
104         }
105         case ELEMFUNCRETURN: {
106                 ElementFunction *ef = (ElementFunction *) e;
107                 return ((uint)(uintptr_t) ef->function) ^
108                                          ((uint)(uintptr_t) ef->overflowstatus) ^
109                                          hashArray(&ef->inputs);
110         }
111         case ELEMCONST: {
112                 ElementConst *ec = (ElementConst *) e;
113                 return ((uint) ec->value);
114         }
115         default:
116                 ASSERT(0);
117         }
118 }
119
120 bool compareElement(Element *e1, Element *e2) {
121         if (e1->type != e2->type)
122                 return false;
123         switch (e1->type) {
124         case ELEMSET: {
125                 return e1 == e2;
126         }
127         case ELEMFUNCRETURN: {
128                 ElementFunction *ef1 = (ElementFunction *) e1;
129                 ElementFunction *ef2 = (ElementFunction *) e2;
130                 return (ef1->function == ef2->function) &&
131                                          (ef1->overflowstatus == ef2->overflowstatus) &&
132                                          compareArray(&ef1->inputs, &ef2->inputs);
133         }
134         case ELEMCONST: {
135                 ElementConst *ec1 = (ElementConst *) e1;
136                 ElementConst *ec2 = (ElementConst *) e2;
137                 return (ec1->value == ec2->value);
138         }
139         default:
140                 ASSERT(0);
141         }
142 }