bug..forgot to return value
[repair.git] / Repair / RepairCompiler / MCC / CRuntime / tmap.c
1 #include <stdio.h>
2 #include "tmap.h"
3 #include "size.h"
4 #include "stack.h"
5 #include <stdlib.h>
6
7 #define CHECKTYPE
8 #define CHECKMEMORY
9
10 struct typemap * allocatetypemap() {
11   struct typemap *thisvar=(struct typemap *) malloc(sizeof(struct typemap));
12   thisvar->alloctree=rbinit();
13   thisvar->typetree=rbinit();
14   thisvar->low=GC_linux_stack_base();
15   return thisvar;
16 }
17
18 void freefunction(void *ptr) {
19   if(ptr!=NULL) {
20     free((struct structuremap *)ptr);
21   }
22 }
23
24 void freetypemap(struct typemap * ptr) {
25   rbdestroy(ptr->typetree,freefunction);
26   rbdestroy(ptr->alloctree,freefunction);
27   free(ptr);
28 }
29
30 void typemapreset(struct typemap *ptr) {
31   rbdestroy(ptr->typetree,freefunction);
32   ptr->typetree=rbinit();
33   if (ptr->low<ptr->high)
34     rbdelete(ptr->low,ptr->alloctree);
35   else
36     rbdelete(ptr->high,ptr->alloctree);
37 }
38
39 void initializetypemapstack(struct typemap * ptr, void *high) {
40   ptr->high=high;
41   if (ptr->low<ptr->high)
42     rbinsert(ptr->low,ptr->high,NULL,ptr->alloctree);
43   else
44     rbinsert(ptr->high,ptr->low,NULL,ptr->alloctree);
45 }
46
47 struct structuremap * allocatestructuremap(int s) {
48   struct structuremap *ptr=(struct structuremap *)malloc(sizeof(struct structuremap));
49   ptr->str=s;
50   ptr->typetree=rbinit();
51   return ptr;
52 }
53
54 void freestructuremap(struct structuremap *ptr) {
55   rbdestroy(ptr->typetree,freefunction);
56   free(ptr);
57 }
58
59 bool typemapasserttype(struct typemap *thisvar, void *ptr, int s) {
60   int toadd=sizeBytes(s);
61   return typemapasserttypeB(thisvar, ptr,((char *) ptr)+toadd,s);
62 }
63
64 bool typemapasserttypeB(struct typemap * thisvar, void *ptr, void *high, int s) {
65 #ifdef CHECKTYPE
66   bool b=typemapchecktype(thisvar,true,ptr,s);
67   if (!b) {
68     printf("Assertion failure\n");
69     {
70       bool testb=typemapchecktype(thisvar,true,ptr,s);
71     }
72   }
73   return b;
74 #endif
75   return typemapassertvalidmemoryB(thisvar,ptr, high);
76 }
77
78 bool typemapassertvalidmemory(struct typemap * thisvar, void* low, int s) {
79   int toadd=sizeBytes(s);
80   return typemapassertvalidmemoryB(thisvar, low,((char *)low)+toadd);
81 }
82
83 bool typemapassertvalidmemoryB(struct typemap * thisvar, void* low, void* high) {
84 #ifdef CHECKMEMORY
85   return typemapcheckmemory(thisvar, low, high);
86 #endif
87   return true;
88 }
89
90 bool typemapistype(struct typemap *thisvar, void *ptr, void *high, int s) {
91 #ifdef CHECKTYPE
92   bool b=typemapchecktype(thisvar, false,ptr,s);
93   if (!b) {
94     printf("Verify failure\n");
95     {
96       bool testb=typemapchecktype(thisvar, false,ptr,s);
97     }
98   }
99   return b;
100 #endif
101   return typemapassertvalidmemoryB(thisvar, ptr, high);
102 }
103
104 void typemapallocate(struct typemap *thisvar,void *ptr, int size) {
105   void *low=ptr;
106   void *high=((char *)ptr)+size;
107   int val=rbinsert(low,high,NULL,thisvar->alloctree);
108   if (val==0)
109     printf("Error\n");
110 }
111
112 inline int sizeinbytes(unsigned int bits) {
113   int bytes=bits>>3;
114   if (bits %8)
115     bytes++;
116   return bytes;
117 }
118
119 int typemapfindoffsetstructure(struct typemap * thisvar, int s, int offset) {
120   int count=0;
121   int i;
122   int increment;
123   for(i=0;i<getnumfields(s);i++) {
124     int mult=1;
125     int ttype=getfield(s,i);
126     if (isArray(s,i)) {
127       mult=numElements(s,i);
128     }
129     increment=size(ttype);
130     if (increment%8) {
131       int delt=offset-count;
132       int byteincrement=increment/8;
133       if (delt<mult*byteincrement) {
134         if (delt%byteincrement==0) {
135           return ttype;
136         } else
137           return -1;
138       }
139     } else {
140       if ((count+sizeinbytes(mult*increment))>offset)
141         return -1;
142     }
143     count+=sizeinbytes(mult*increment);
144   }
145   return -1;
146 }
147
148 void typemapdeallocate(struct typemap * thisvar,void *ptr) {
149   if (rbdelete(ptr,thisvar->alloctree)==NULL)
150     printf("Freeing unallocated memory\n");
151 }
152
153 bool typemapcheckmemory(struct typemap *thisvar, void* low, void* high) {
154   struct pair allocp=rbfind(low,high,thisvar->alloctree);
155   if (allocp.low == NULL) {
156     return false;
157   } else if ((allocp.low > low) || (allocp.high < high)) { /* make sure this block is used */
158     return false;
159   } else {
160     return true;
161   }
162 }
163
164
165 bool typemapchecktype(struct typemap *thisvar, bool doaction,void *ptr, int structure) {
166   int ssize=sizeBytes(structure);
167   void *low=ptr;
168   void *high=((char *)low)+ssize;
169   struct pair allocp=rbfind(low,high,thisvar->alloctree);
170   if (allocp.low==NULL)
171     return false;
172   if (allocp.low>low||allocp.high<high) /* make sure this block is used */
173     return false;
174   {
175     struct pair typep=rbfind(low,high,thisvar->typetree);
176     struct structuremap *smap=(struct structuremap *)rblookup(low,high,thisvar->typetree);
177     if (typep.low==NULL) {
178       if(!doaction)
179         return true;
180       {
181         struct structuremap *sm=allocatestructuremap(structure);
182         int flag=rbinsert(low, high, sm, thisvar->typetree);
183         if (flag==0) {
184           printf("Error in asserttype\n");
185           return false;
186         } else
187           return true;
188       }
189     }
190     return typemapchecktypeB(thisvar, doaction, low,high, structure, thisvar->typetree);
191   }
192 }
193
194 bool typemapchecktypeB(struct typemap *thisvar, bool doaction, void *low, void *high, int structure, struct rbtree *ttree) {
195   struct pair typep=rbfind(low,high,ttree);
196   struct structuremap *smap=(struct structuremap *)rblookup(low,high,ttree);
197   if (typep.low==low&&typep.high==high) {
198     /* Recast */
199     if (issubtype(structure,smap->str)) {
200       /* narrowing cast */
201       if (!doaction)
202         return true;
203       smap->str=structure;
204       return true;
205     } else if (issubtype(smap->str,structure)) {
206       /* widening cast */
207       return true;
208     } else
209       return false; /* incompatible types */
210   } else if (typep.low<=low&&typep.high>=high) {
211     /* See if it matches up with structure inside typep */
212     if (rbsearch(low,high,smap->typetree)) {
213       /* recurse */
214       return typemapchecktypeB(thisvar,doaction,low,high, structure, smap->typetree);
215     } else {
216       /* check to see if data lines up correctly */
217       int offset=((char *)low)-((char *)typep.low);
218       int st=typemapfindoffsetstructure(thisvar, smap->str,offset);
219       if (st==-1)
220         return false;
221       if (issubtype(structure,st)) {
222         if (!doaction)
223           return true;
224         {
225           struct structuremap *newsm=allocatestructuremap(structure);
226           int flag=rbinsert(low, high, newsm, smap->typetree);
227           return (flag==1);
228         }
229       } else if (issubtype(st,structure)) {
230         if (!doaction)
231           return true;
232         {
233           struct structuremap *newsm=allocatestructuremap(st);
234           int flag=rbinsert(low, high, newsm, smap->typetree);
235           return (flag==1);
236         }
237       } else
238         return false;
239     }
240   } else
241     return false;
242 }