Handle ICMD_INVOKExxx arguments a little bit different on x86_64.
[cacao.git] / jit / sets.c
1 #include <assert.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 #include "sets.h"
7
8
9 /*
10  * set.c - functions to manipulate ptr sets.
11  */
12  
13 /*------------------------------------------------------------*/
14 /*-- fieldinfo call set fns */
15 /*------------------------------------------------------------*/
16 fldSetNode  *inFldSet    (fldSetNode *s, fieldinfo *f)
17  {
18  fldSetNode* i;
19  for (i=s; i != NULL; i = i->nextfldRef) {
20    if (i->fldRef == f) {
21       return i; /* true = found */
22       }
23    }
24  return NULL;
25  }
26
27 /*------------------------------------------------------------*/
28 /* */
29 fldSetNode *addFldRef(fldSetNode *s,  fieldinfo *f)
30  {
31  fldSetNode *s1 = s;
32  if (!inFldSet(s,f)) {
33    s1 = (fldSetNode *)malloc(sizeof(fldSetNode));
34    s1->nextfldRef  = s;
35    s1->fldRef      = f;
36    s1->writePUT     = false;
37    s1->readGET    = false;
38    s1->lastptrPUT = NULL;
39    s1->lastptrGET = NULL;
40
41    if (s == NULL)
42      s1->index = 1;
43    else
44      s1->index = s->index+1; 
45    }
46  return s1;
47  }
48
49 /*------------------------------------------------------------*/
50 fldSet *add2FldSet(fldSet *sf,  fieldinfo *f, bool wput, bool rget)
51  {
52  fldSetNode *s1;
53  fldSetNode *s;
54  
55  if (sf == NULL) {
56         sf = createFldSet();
57         }
58  s = sf->head;
59  s1 = inFldSet(s,f);
60  if (s1 == NULL) {
61    s1 = (fldSetNode *)malloc(sizeof(fldSetNode));
62    if (sf->head == NULL) {
63         sf->head  = s1;
64         sf->pos   = s1;
65         s1->index = 1;
66         }        
67    else {
68         sf->tail->nextfldRef  = s1;
69         sf->length++;
70         s1->index = sf->length;
71         } 
72    s1->nextfldRef  = NULL;
73    s1->fldRef      = f;
74    s1->writePUT    = wput;
75    s1->readGET     = rget;
76    s1->lastptrPUT = NULL;
77    s1->lastptrGET = NULL;
78    sf->tail = s1;
79    }
80  else   {
81         if ((s1->writePUT == false) && (wput)) 
82                 s1->writePUT = wput;
83         if ((s1->readGET == false)  && (rget)) 
84                 s1->readGET  = rget;
85         }
86  return sf;
87  }
88
89 /*------------------------------------------------------------*/
90 fldSet *createFldSet( )
91  {
92  fldSet *s;
93  s = (fldSet *)malloc(sizeof(fldSet));
94  s->head = NULL;
95  s->tail = NULL;
96  s->pos  = NULL;
97  s->length = 0;
98  return s;
99  }
100
101 /*------------------------------------------------------------*/
102 /*-- methodinfo call set fns */
103 /*------------------------------------------------------------*/
104 int  inMethSet    (methSetNode *s, methodinfo *m)
105  {
106  methSetNode* i;
107  for (i=s; i != NULL; i = i->nextmethRef) {
108    if (i->methRef == m) {
109       return (int)1; /* true = found */
110       }
111    }
112  return (int)0;
113  }
114
115 /*------------------------------------------------------------*/
116 methSetNode *addMethRef(methSetNode *s,  methodinfo *m)
117  {
118  methSetNode *s1 = s;
119  if (!inMethSet(s,m)) {
120    s1 = (methSetNode *)malloc(sizeof(methSetNode));
121    s1->nextmethRef= s;
122    s1->methRef = m;
123    s1->lastptrIntoClassSet2 = NULL;
124    if (s == NULL)
125      s1->index = 1;
126    else
127      s1->index = s->index+1; 
128    s1->monoPoly = MONO; 
129    }
130   
131  return s1;
132  }
133
134 /*------------------------------------------------------------*/
135 methSet *add2MethSet(methSet *sm,  methodinfo *m)
136  {
137  methSetNode *s1;
138  methSetNode *s;
139  
140  if (sm == NULL) {
141         sm = createMethSet();
142         }
143  s = sm->head;
144  if (!inMethSet(s,m)) {
145    s1 = (methSetNode *)malloc(sizeof(methSetNode));
146    if (sm->head == NULL) {
147         sm->head = s1;
148         sm->pos   = s1;
149         s1->index = 1;
150         }        
151    else {
152         sm->tail->nextmethRef  = s1;
153         sm->length++;
154         s1->index = sm->length;
155         }
156    s1->monoPoly = MONO; 
157    s1->nextmethRef= NULL;
158    s1->methRef = m;
159    s1->lastptrIntoClassSet2 = NULL;
160    sm->tail = s1;
161    }
162  return sm;
163  }
164  
165 /*------------------------------------------------------------*/
166 methSet *createMethSet( )
167  {
168  methSet *s;
169  s = (methSet *)malloc(sizeof(methSet));
170  s->head = NULL;
171  s->tail = NULL;
172  s->pos  = NULL;
173  s->length = 0;
174  return s;
175  }
176
177 /*------------------------------------------------------------*/
178 /*-- classinfo XTA set fns  */
179 /*------------------------------------------------------------*/
180 int  inSet    (classSetNode *s, classinfo *c)
181  {
182  classSetNode* i;
183  for (i=s; i != NULL; i = i->nextClass) {
184    if (i->classType == c) {
185       return  ((i->index)+1); /* true = found */
186       }
187    }
188  return (int)0;
189  }
190
191 /*------------------------------------------------------------*/
192 classSetNode *addElement(classSetNode *s,  classinfo *c)
193  {
194  classSetNode *s1 = s;
195  if (!inSet(s,c)) {
196    s1 = (classSetNode *)malloc(sizeof(classSetNode));
197    s1->nextClass= s;
198    s1->classType = c;
199    if (s == NULL)
200      s1->index = 1;
201    else
202      s1->index = s->index+1; 
203    }
204  return s1;
205  }
206
207 /*------------------------------------------------------------*/
208 classSet *add2ClassSet(classSet *sc,  classinfo *c)
209  {
210  classSetNode *s1;
211  classSetNode *s;
212  
213  if (sc == NULL) {
214         sc = createClassSet();
215         }
216  s = sc->head;
217         
218  if (!inSet(s,c)) {
219    s1 = (classSetNode *)malloc(sizeof(classSetNode));
220    if (sc->head == NULL) {
221         sc->head  = s1;
222         sc->pos   = s1;
223         s1->index = 1;
224         }        
225    else {
226         sc->tail->nextClass  = s1;
227         sc->length++;
228         s1->index = sc->length;
229         } 
230    s1->classType = c;
231    s1->nextClass= NULL;
232    sc->tail  = s1;
233    }
234  return sc;
235  }
236
237 /*------------------------------------------------------------*/
238 classSet *createClassSet( )
239  {
240  classSet *s;
241  s = (classSet *)malloc(sizeof(classSet));
242  s->head = NULL;
243  s->tail = NULL;
244  s->pos  = NULL;
245  s->length = 0;
246  return s;
247  }
248
249 /*------------------------------------------------------------*/
250 /* Returns:                                                   */
251 /*    -1  c is a subclass   of an existing set element        */
252 /*     0  c class type cone does not overlap any set element  */
253 /*     1  c is a superclass of an existing set element        */
254
255 int inRange (classSetNode *s, classinfo *c)
256  {
257  classSetNode* i;
258  int rc=0;
259
260  for (i=s; i != NULL; i = i->nextClass) {
261     classinfo *cs = i->classType;
262     if (cs->vftbl->baseval <= c->vftbl->baseval) {
263         if (c->vftbl->baseval <= (cs->vftbl->baseval+cs->vftbl->diffval)) {
264                 rc = -1;  /* subtype */
265                 }
266         }
267     else {
268         if (cs->vftbl->baseval < (c->vftbl->baseval+c->vftbl->diffval)) {
269                 i->classType = c;   /* replace element with its new super */
270                 rc  = 1; /* super */
271                 }
272         }
273     }
274  return rc;
275  }
276
277 /*------------------------------------------------------------*/
278 /* adds class if not subtype of an existing set element       */
279 /* if "new" class is super class of an existing element       */
280 /* then replace the existing element with the "new" class     */
281
282 classSetNode *addClassCone(classSetNode *s,  classinfo *c)
283  {
284  classSetNode *s1 = s;
285  
286 if (inRange(s,c) == 0) {
287         /* not in set nor cone of an existing element so add */
288         s1 = (classSetNode *)malloc(sizeof(classSetNode));
289         s1->nextClass= s;
290         s1->classType = c;
291         if (s == NULL)
292                 s1->index = 1;
293         else
294                 s1->index = s->index+1; 
295         }
296  return s1;
297  }
298
299 /*------------------------------------------------------------*/
300 classSetNode * intersectSubtypesWithSet(classinfo *t, classSetNode *s) {
301  classSetNode *s1 = NULL;
302  classSetNode *c;
303
304  /* for each s class */
305  for (c=s; c != NULL; c = c->nextClass) {
306         vftbl *t_cl_vt = t->vftbl;
307         vftbl *c_cl_vt = c->classType->vftbl;
308
309         /* if s class is in the t Class range */
310         if (  (t_cl_vt->baseval <=  c_cl_vt->baseval)
311         && (c_cl_vt->baseval <= (t_cl_vt->baseval+t_cl_vt->diffval)) ) {
312
313                 /*    add s class to return class set */
314                 s1 = addElement(s1,c->classType);
315                 }
316         }
317  return s1;
318  }
319
320 /*------------------------------------------------------------*/
321 int sizeOfSet(classSetNode *s) {
322 /*** need to update */
323   int cnt=0;
324   classSetNode * i;
325   for (i=s; i != NULL; i = i->nextClass) cnt++;
326   return cnt;
327   }
328   
329 /*------------------------------------------------------------*/
330 int printSet(classSetNode *s)
331   {
332   classSetNode* i;
333   int cnt=0;
334
335   if (s == NULL) {
336         printf("Set of types: <");
337         printf("\t\tEmpty Set\n");
338         }
339   else  {
340         printf("<%i>Set of types: ",s->index);
341         for (i=s; i != NULL; i = i->nextClass) {
342                 printf("\t#%i: ",cnt);
343                 if (i->classType == NULL)  {
344                         printf("NULL CLASS");
345                         fflush(stdout);
346                         }
347                 else    {
348                         utf_display(i->classType->name);
349                         fflush(stdout); 
350                         printf("<b%i/d%i> ",i->classType->vftbl->baseval,i->classType->vftbl->diffval); 
351                         fflush(stdout);
352                         }
353                 cnt++;
354                 }
355         printf(">\n");
356         }
357   return cnt;
358   }
359 /*------------------------------------------------------------*/
360 int printClassSet(classSet *sc) {
361 if (sc == NULL) {
362         printf("Class Set not yet created\n");
363         return 0;
364         }
365 else
366         return (printSet(sc->head));
367 }
368
369 /*------------------------------------------------------------*/
370 int printMethSet(methSetNode *s)
371   {
372   methSetNode* i;
373   int cnt=0;
374
375   if (s == NULL) {
376         printf("Set of Methods: "); fflush(stdout);
377         printf("\t\tEmpty Set\n"); fflush(stdout);
378          }
379   else  {
380         printf("<%i>Set of Methods: ",s->index);fflush(stdout); 
381         for (i=s; i != NULL; i = i->nextmethRef) {
382                 printf("\t#%i: ",cnt);
383
384                 /* class.method */
385                 utf_display(i->methRef->class->name);
386                 printf(".");
387                 method_display(i->methRef);
388
389                 /* lastptr <class> */
390                 printf("\t<");
391                 if (i->lastptrIntoClassSet2 != NULL)
392                         utf_display(i->lastptrIntoClassSet2->classType->name);
393                 printf(">\n");
394
395                 cnt++;
396                 }
397         printf("\n");
398         }
399   return cnt;
400   }
401 /*------------------------------------------------------------*/
402 int printMethodSet(methSet *sm) {
403 if (sm == NULL) {
404         printf("Method Set not yet created\n");
405         return 0;
406         }
407 else
408         return (printMethSet(sm->head));
409 }
410 /*------------------------------------------------------------*/
411 int printFldSet(fldSetNode *s)
412   {
413   fldSetNode* i;
414   int cnt=0;
415
416   if (s == NULL) {
417         printf("Set of Fields: ");
418         printf("\tEmpty Set\n");
419         }
420   else  {
421         printf("<%i>Set of Fields: ",s->index);
422         for (i=s; i != NULL; i = i->nextfldRef) {
423                 printf("\t#%i: ",cnt);
424                 printf("(%ir/%iw)",i->writePUT,i->readGET);
425                 field_display(i->fldRef);
426                 cnt++;
427                 }
428         printf("\n");
429         }
430   return cnt;
431   }
432
433 /*------------------------------------------------------------*/
434 int printFieldSet(fldSet *sf) {
435 if (sf == NULL) {
436         printf("Field Set not yet created\n");
437         return 0;
438         }
439 else
440         return (printFldSet(sf->head));
441 }
442 /*------------------------------------------------------------*/
443 /*void destroy_set */
444