57f7b6a5b27abf9ec8cb661295451c272bad6663
[satune.git] / src / AST / element.c
1 #include "element.h"
2 #include "structs.h"
3 #include "set.h"
4 #include "constraint.h"
5 #include "function.h"
6 #include "table.h"
7
8 Element *allocElementSet(Set * s) {
9         ElementSet * tmp=(ElementSet *)ourmalloc(sizeof(ElementSet));
10         GETELEMENTTYPE(tmp)= ELEMSET;
11         tmp->set=s;
12         allocInlineDefVectorASTNode(GETELEMENTPARENTS(tmp));
13         initElementEncoding(&tmp->encoding, (Element *) tmp);
14         return &tmp->base;
15 }
16
17 Element* allocElementFunction(Function * function, Element ** array, uint numArrays, Boolean * overflowstatus){
18         ElementFunction* tmp = (ElementFunction*) ourmalloc(sizeof(ElementFunction));
19         GETELEMENTTYPE(tmp)= ELEMFUNCRETURN;
20         tmp->function=function;
21         tmp->overflowstatus = overflowstatus;
22         allocInlineArrayInitElement(&tmp->inputs, array, numArrays);
23         allocInlineDefVectorASTNode(GETELEMENTPARENTS(tmp));
24         for(uint i=0;i<numArrays;i++)
25                 pushVectorASTNode(GETELEMENTPARENTS(array[i]), (ASTNode *) tmp);
26         initElementEncoding(&tmp->domainencoding, (Element *) tmp);
27         initFunctionEncoding(&tmp->functionencoding, (Element *) tmp);
28         return &tmp->base;
29 }
30
31 Set* getElementSet(Element* This){
32         switch(GETELEMENTTYPE(This)){
33                 case ELEMSET:
34                         return ((ElementSet*)This)->set;
35                 case ELEMFUNCRETURN:
36                         ;//Nope is needed for label assignment. i.e. next instruction isn't 
37                         Function* func = ((ElementFunction*)This)->function;
38                         switch(GETFUNCTIONTYPE(func)){
39                                 case TABLEFUNC:
40                                         return ((FunctionTable*)func)->table->range;
41                                 case OPERATORFUNC:
42                                         return ((FunctionOperator*)func)->range;
43                                 default:
44                                         ASSERT(0);
45                         }
46                 default:
47                         ASSERT(0);
48         }
49         ASSERT(0);
50         return NULL;
51 }
52
53 uint getElemEncodingInUseVarsSize(ElementEncoding* This){
54         uint size=0;
55         for(uint i=0; i<This->encArraySize; i++){
56                 if(isinUseElement(This, i)){
57                         size++;
58                 }
59         }
60         return size;
61 }
62
63
64 Constraint * getElementValueBinaryIndexConstraint(Element* This, uint64_t value) {
65         ASTNodeType type = GETELEMENTTYPE(This);
66         ASSERT(type == ELEMSET || type == ELEMFUNCRETURN);
67         ElementEncoding* elemEnc = getElementEncoding(This);
68         for(uint i=0; i<elemEnc->encArraySize; i++){
69                 if( isinUseElement(elemEnc, i) && elemEnc->encodingArray[i]==value){
70                         return generateBinaryConstraint(elemEnc->numVars,
71                                 elemEnc->variables, i);
72                 }
73         }
74         ASSERT(0);
75         return NULL;
76 }
77
78 void deleteElement(Element *This) {
79         switch(GETELEMENTTYPE(This)) {
80         case ELEMFUNCRETURN: {
81                 ElementFunction *ef = (ElementFunction *) This;
82                 deleteInlineArrayElement(&ef->inputs);
83                 deleteElementEncoding(&ef->domainencoding);
84                 deleteFunctionEncoding(&ef->functionencoding);
85                 break;
86         }
87         case ELEMSET: {
88                 ElementSet *es = (ElementSet *) This;
89                 deleteElementEncoding(&es->encoding);
90                 break;
91         }
92         default:
93                 ;
94         }
95         deleteVectorArrayASTNode(GETELEMENTPARENTS(This));
96
97         ourfree(This);
98 }