make libfirm faster
[libfirm] / ir / tr / entity.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/tr/entity.c
4  * Purpose:     Representation of all program known entities.
5  * Author:      Martin Trapp, Christian Schaefer
6  * Modified by: Goetz Lindenmaier
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1998-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 #ifdef HAVE_CONFIG_H
14 # include <config.h>
15 #endif
16
17 # include <stdlib.h>
18 # include <stddef.h>
19 # include <string.h>
20
21 # include "entity_t.h"
22 # include "mangle.h"
23 # include "typegmod.h"
24 # include "array.h"
25 /* All this is needed to build the constant node for methods: */
26 # include "irprog_t.h"
27 # include "ircons.h"
28 # include "tv_t.h"
29
30 /*******************************************************************/
31 /** general                                                       **/
32 /*******************************************************************/
33
34 void
35 init_entity (void)
36 {
37 }
38
39 /*******************************************************************/
40 /** ENTITY                                                        **/
41 /*******************************************************************/
42
43 /* redeclared to declare INLINE. */
44 INLINE entity *get_entity_overwrites   (entity *ent, int pos);
45 INLINE entity *get_entity_overwrittenby   (entity *ent, int pos);
46 INLINE type   *get_entity_owner (entity *ent);
47
48 INLINE void insert_entity_in_owner (entity *ent) {
49   type *owner = ent->owner;
50   switch (get_type_tpop_code(owner)) {
51   case tpo_class: {
52     add_class_member (owner, ent);
53   } break;
54   case tpo_struct: {
55     add_struct_member (owner, ent);
56   } break;
57   case tpo_union: {
58     add_union_member (owner, ent);
59   } break;
60   case tpo_array: {
61     set_array_element_entity(owner, ent);
62   } break;
63   default: assert(0);
64   }
65 }
66
67 entity *
68 new_entity (type *owner, ident *name, type *type)
69 {
70   entity *res;
71   ir_graph *rem;
72
73   assert(!id_contains_char(name, ' ') && "entity name should not contain spaces");
74
75   res = (entity *) xmalloc (sizeof (entity));
76   res->kind = k_entity;
77   assert_legal_owner_of_ent(owner);
78   res->owner = owner;
79   res->name = name;
80   res->type = type;
81
82   if (get_type_tpop(type) == type_method)
83     res->allocation = allocation_static;
84   else
85     res->allocation = allocation_automatic;
86
87   res->visibility = visibility_local;
88   res->offset = -1;
89   if (is_method_type(type)) {
90     res->variability = variability_constant;
91     rem = current_ir_graph;
92     current_ir_graph = get_const_code_irg();
93     res->value = new_Const(mode_P_mach, new_tarval_from_entity(res, mode_P_mach));
94     current_ir_graph = rem;
95   } else {
96     res->variability = variability_uninitialized;
97     res->value  = NULL;
98     res->values = NULL;
99     res->val_paths = NULL;
100   }
101   res->peculiarity   = peculiarity_existent;
102   res->volatility    = volatility_non_volatile;
103   res->ld_name       = NULL;
104   res->overwrites    = NEW_ARR_F(entity *, 0);
105   res->overwrittenby = NEW_ARR_F(entity *, 0);
106
107   res->irg = NULL;
108
109 #ifdef DEBUG_libfirm
110   res->nr = get_irp_new_node_nr();
111 #endif
112
113   res->visit = 0;
114
115   /* Remember entity in it's owner. */
116   insert_entity_in_owner (res);
117   return res;
118 }
119 entity *
120 new_d_entity (type *owner, ident *name, type *type, dbg_info *db) {
121   entity *res = new_entity(owner, name, type);
122   set_entity_dbg_info(res, db);
123   return res;
124 }
125
126 INLINE void    free_compound_graph_path (compound_graph_path *gr);
127 INLINE int     is_compound_graph_path(void *thing);
128 INLINE int     get_compound_graph_path_length(compound_graph_path *gr);
129 INLINE entity *get_compound_graph_path_node(compound_graph_path *gr, int pos);
130 INLINE int     get_compound_ent_n_values(entity *ent);
131
132 INLINE void free_entity_attrs(entity *ent) {
133   int i;
134   assert(ent);
135   if (get_type_tpop(get_entity_owner(ent)) == type_class) {
136     DEL_ARR_F(ent->overwrites);    ent->overwrites = NULL;
137     DEL_ARR_F(ent->overwrittenby); ent->overwrittenby = NULL;
138   }
139   //if (ent->values) DEL_ARR_F(ent->values); /* @@@ warum nich? */
140   if (ent->val_paths) {
141     if (is_compound_entity(ent))
142       for (i = 0; i < get_compound_ent_n_values(ent); i++)
143         if (ent->val_paths[i]) ;
144           /* free_compound_graph_path(ent->val_paths[i]) ;  * @@@ warum nich? */
145           /* Geht nich: wird mehrfach verwendet!!! ==> mehrfach frei gegeben. */
146     //DEL_ARR_F(ent->val_paths);
147   }
148   ent->val_paths = NULL;
149   ent->values = NULL;
150 }
151
152 entity *
153 copy_entity_own (entity *old, type *new_owner) {
154   entity *new;
155
156   assert_legal_owner_of_ent(new_owner);
157   if (old->owner == new_owner) return old;
158   new = (entity *) xmalloc (sizeof (entity));
159   memcpy (new, old, sizeof (entity));
160   new->owner = new_owner;
161   /*
162   if ((get_type_tpop(get_entity_owner(old)) == type_class) &&
163       (get_type_tpop(new_owner) == type_class)) {
164     new->overwrites = DUP_ARR_F(entity *, old->overwrites);
165     new->overwrittenby = DUP_ARR_F(entity *, old->overwrittenby);
166   } else if ((get_type_tpop(get_entity_owner(old)) != type_class) &&
167              (get_type_tpop(new_owner) == type_class)) {
168     new->overwrites = NEW_ARR_F(entity *, 0);
169     new->overwrittenby = NEW_ARR_F(entity *, 0);
170   }
171   */
172   if (is_class_type(new_owner)) {
173     new->overwrites = NEW_ARR_F(entity *, 0);
174     new->overwrittenby = NEW_ARR_F(entity *, 0);
175   }
176 #ifdef DEBUG_libfirm
177   new->nr = get_irp_new_node_nr();
178 #endif
179
180   insert_entity_in_owner (new);
181
182   return new;
183 }
184
185 entity *
186 copy_entity_name (entity *old, ident *new_name) {
187   entity *new;
188
189   if (old->name == new_name) return old;
190   new = (entity *) xmalloc (sizeof (entity));
191   memcpy (new, old, sizeof (entity));
192   new->name = new_name;
193   new->ld_name = NULL;
194   if (is_class_type(new->owner)) {
195     new->overwrites = DUP_ARR_F(entity *, old->overwrites);
196     new->overwrittenby = DUP_ARR_F(entity *, old->overwrittenby);
197   }
198 #ifdef DEBUG_libfirm
199   new->nr = get_irp_new_node_nr();
200 #endif
201
202   insert_entity_in_owner (new);
203
204   return new;
205 }
206
207
208 void
209 free_entity (entity *ent) {
210   free_tarval_entity(ent);
211   free_entity_attrs(ent);
212   ent->kind = k_BAD;
213   free(ent);
214 }
215
216 /* Outputs a unique number for this node */
217 INLINE long
218 get_entity_nr(entity *ent) {
219   assert(ent);
220 #ifdef DEBUG_libfirm
221   return ent->nr;
222 #else
223   return 0;
224 #endif
225 }
226
227 INLINE const char *
228 get_entity_name (entity *ent) {
229   assert (ent);
230   return get_id_str(get_entity_ident(ent));
231 }
232
233 ident *
234 get_entity_ident    (entity *ent) {
235   assert(ent);
236   return ent->name;
237 }
238
239 /*
240 void   set_entitye_ld_name  (entity *, char *ld_name);
241 void   set_entity_ld_ident (entity *, ident *ld_ident);
242 */
243
244 INLINE type *
245 get_entity_owner (entity *ent) {
246   return ent->owner = skip_tid(ent->owner);
247 }
248
249 INLINE void
250 set_entity_owner (entity *ent, type *owner) {
251   assert_legal_owner_of_ent(owner);
252   ent->owner = owner;
253 }
254
255 INLINE void   /* should this go into type.c? */
256 assert_legal_owner_of_ent(type *owner) {
257   assert (get_type_tpop_code(owner) == tpo_class ||
258           get_type_tpop_code(owner) == tpo_union ||
259           get_type_tpop_code(owner) == tpo_struct ||
260           get_type_tpop_code(owner) == tpo_array);   /* Yes, array has an entity
261                                                         -- to select fields! */
262 }
263
264 INLINE ident *
265 get_entity_ld_ident (entity *ent)
266 {
267   if (ent->ld_name == NULL)
268     ent->ld_name = mangle_entity (ent);
269   return ent->ld_name;
270 }
271
272 INLINE void
273 set_entity_ld_ident (entity *ent, ident *ld_ident) {
274   ent->ld_name = ld_ident;
275 }
276
277 INLINE const char *
278 get_entity_ld_name (entity *ent) {
279   return get_id_str(get_entity_ld_ident(ent));
280 }
281
282 /*
283 char  *get_entity_ld_name  (entity *);
284 void   set_entity_ld_name  (entity *, char *ld_name);
285 */
286
287 INLINE type *
288 get_entity_type (entity *ent) {
289   return ent->type = skip_tid(ent->type);
290 }
291
292 INLINE void
293 set_entity_type (entity *ent, type *type) {
294   ent->type = type;
295 }
296
297
298 INLINE ent_allocation
299 get_entity_allocation (entity *ent) {
300   return ent->allocation;
301 }
302
303 INLINE void
304 set_entity_allocation (entity *ent, ent_allocation al) {
305   ent->allocation = al;
306 }
307
308 /* return the name of the visibility */
309 const char *get_allocation_name(ent_allocation all)
310 {
311 #define X(a)    case a: return #a
312   switch (all) {
313     X(allocation_automatic);
314     X(allocation_parameter);
315     X(allocation_dynamic);
316     X(allocation_static);
317     default: return "BAD VALUE";
318   }
319 #undef X
320 }
321
322
323 INLINE ent_visibility
324 get_entity_visibility (entity *ent) {
325   return ent->visibility;
326 }
327
328 INLINE void
329 set_entity_visibility (entity *ent, ent_visibility vis) {
330   if (vis != visibility_local)
331     assert((ent->allocation == allocation_static) ||
332            (ent->allocation == allocation_automatic));
333   /* @@@ Test that the owner type is not local, but how??
334          && get_class_visibility(get_entity_owner(ent)) != local));*/
335   ent->visibility = vis;
336 }
337
338 /* return the name of the visibility */
339 const char *get_visibility_name(ent_visibility vis)
340 {
341 #define X(a)    case a: return #a
342   switch (vis) {
343     X(visibility_local);
344     X(visibility_external_visible);
345     X(visibility_external_allocated);
346     default: return "BAD VALUE";
347   }
348 #undef X
349 }
350
351 INLINE ent_variability
352 get_entity_variability (entity *ent) {
353   return ent->variability;
354 }
355
356 INLINE void
357 set_entity_variability (entity *ent, ent_variability var)
358 {
359   if (var == variability_part_constant)
360     assert(is_class_type(ent->type) || is_struct_type(ent->type));
361
362   if ((is_compound_type(ent->type)) &&
363       (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
364     /* Allocate datastructures for constant values */
365     ent->values    = NEW_ARR_F(ir_node *, 0);
366     ent->val_paths = NEW_ARR_F(compound_graph_path *, 0);
367   }
368
369   if ((is_compound_type(ent->type)) &&
370       (var == variability_uninitialized) && (ent->variability != variability_uninitialized)) {
371     /* Free datastructures for constant values */
372     DEL_ARR_F(ent->values);    ent->values    = NULL;
373     DEL_ARR_F(ent->val_paths); ent->val_paths = NULL;
374   }
375   ent->variability = var;
376 }
377
378 /* return the name of the variablity */
379 const char *get_variability_name(ent_variability var)
380 {
381 #define X(a)    case a: return #a
382   switch (var) {
383     X(variability_uninitialized);
384     X(variability_initialized);
385     X(variability_part_constant);
386     X(variability_constant);
387     default: return "BAD VALUE";
388   }
389 #undef X
390 }
391
392 INLINE ent_volatility
393 get_entity_volatility (entity *ent) {
394   assert (ent);
395   return ent->volatility;
396 }
397
398 INLINE void
399 set_entity_volatility (entity *ent, ent_volatility vol) {
400   assert (ent);
401   ent->volatility = vol;
402 }
403
404 /* return the name of the volatility */
405 const char *get_volatility_name(ent_volatility var)
406 {
407 #define X(a)    case a: return #a
408   switch (var) {
409     X(volatility_non_volatile);
410     X(volatility_is_volatile);
411     default: return "BAD VALUE";
412   }
413 #undef X
414 }
415
416 INLINE peculiarity
417 get_entity_peculiarity (entity *ent) {
418   assert (ent);
419   return ent->peculiarity;
420 }
421
422 INLINE void
423 set_entity_peculiarity (entity *ent, peculiarity pec) {
424   assert (ent);
425   /* @@@ why peculiarity only for methods? */
426   assert (is_method_type(ent->type));
427   ent->peculiarity = pec;
428 }
429
430 /* return the name of the peculiarity */
431 const char *get_peculiarity_name(peculiarity var)
432 {
433 #define X(a)    case a: return #a
434   switch (var) {
435     X(peculiarity_description);
436     X(peculiarity_inherited);
437     X(peculiarity_existent);
438     default: return "BAD VALUE";
439   }
440 #undef X
441 }
442
443 /* Set has no effect for existent entities of type method. */
444 INLINE ir_node *
445 get_atomic_ent_value(entity *ent)
446 {
447   assert(ent);
448   assert(is_atomic_entity(ent));
449   assert(ent->variability != variability_uninitialized);
450   return ent->value;
451 }
452
453 INLINE void
454 set_atomic_ent_value(entity *ent, ir_node *val) {
455   assert(ent && is_atomic_entity(ent) && (ent->variability != variability_uninitialized));
456   if (is_method_type(ent->type) && (ent->peculiarity == peculiarity_existent))
457     return;
458   ent->value = val;
459 }
460
461 /* Returns true if the the node is representable as code on
462  *  const_code_irg. */
463 int is_irn_const_expression(ir_node *n) {
464   ir_mode *m;
465
466   m = get_irn_mode(n);
467   switch(get_irn_opcode(n)) {
468   case iro_Const:
469   case iro_SymConst:
470   case iro_Unknown:
471     return true; break;
472   case iro_Add:
473     if (is_irn_const_expression(get_Add_left(n)))
474       return is_irn_const_expression(get_Add_right(n));
475   case iro_Conv:
476   case iro_Cast:
477     return is_irn_const_expression(get_irn_n(n, 0));
478   default:
479     return false;
480     break;
481   }
482   return false;
483 }
484
485
486 ir_node *copy_const_value(ir_node *n) {
487   ir_node *nn;
488   ir_mode *m;
489
490   m = get_irn_mode(n);
491   switch(get_irn_opcode(n)) {
492   case iro_Const:
493     nn = new_Const(m, get_Const_tarval(n)); break;
494   case iro_SymConst:
495     nn = new_SymConst(get_SymConst_type_or_id(n), get_SymConst_kind(n)); break;
496   case iro_Add:
497     nn = new_Add(copy_const_value(get_Add_left(n)),
498                  copy_const_value(get_Add_right(n)), m); break;
499   case iro_Cast:
500     nn = new_Cast(copy_const_value(get_Cast_op(n)), get_Cast_type(n)); break;
501   case iro_Conv:
502     nn = new_Conv(copy_const_value(get_Conv_op(n)), m); break;
503   case iro_Unknown:
504     nn = new_Unknown(m); break;
505   default:
506     DDMN(n);
507     assert(0 && "opdope invalid or not implemented");
508     nn = NULL;
509     break;
510   }
511   return nn;
512 }
513
514 compound_graph_path *
515 new_compound_graph_path(type *tp, int length) {
516   compound_graph_path *res;
517   assert(is_type(tp) && is_compound_type(tp));
518   assert(length > 0);
519
520   res = (compound_graph_path *) malloc (sizeof(compound_graph_path) + (length-1) * sizeof(entity *));
521   res->kind = k_ir_compound_graph_path;
522   res->tp = tp;
523   res->len = length;
524   memset(res->nodes, 0, sizeof(entity *) * length);
525   return res;
526 }
527
528 INLINE void
529 free_compound_graph_path (compound_graph_path *gr) {
530   assert(gr && is_compound_graph_path(gr));
531   gr->kind = k_BAD;
532   free(gr);
533 }
534
535 INLINE int
536 is_compound_graph_path(void *thing) {
537   return (get_kind(thing) == k_ir_compound_graph_path);
538 }
539
540 /* checks whether nodes 0..pos are correct (all lie on a path.) */
541 /* @@@ not implemented */
542 INLINE int is_proper_compound_graph_path(compound_graph_path *gr, int pos) {
543   int i;
544   entity *node;
545   type *owner = gr->tp;
546   for (i = 0; i <= pos; i++) {
547     node = get_compound_graph_path_node(gr, i);
548     if (get_entity_owner(node) != owner) return false;
549     owner = get_entity_type(node);
550   }
551   if (pos == get_compound_graph_path_length(gr))
552     if (!is_atomic_type(owner)) return false;
553   return true;
554 }
555
556 INLINE int
557 get_compound_graph_path_length(compound_graph_path *gr) {
558   assert(gr && is_compound_graph_path(gr));
559   return gr->len;
560 }
561
562 INLINE entity *
563 get_compound_graph_path_node(compound_graph_path *gr, int pos) {
564   assert(gr && is_compound_graph_path(gr));
565   assert(pos >= 0 && pos < gr->len);
566   return gr->nodes[pos];
567 }
568
569 INLINE void
570 set_compound_graph_path_node(compound_graph_path *gr, int pos, entity *node) {
571   assert(gr && is_compound_graph_path(gr));
572   assert(pos >= 0 && pos < gr->len);
573   assert(is_entity(node));
574   gr->nodes[pos] = node;
575   assert(is_proper_compound_graph_path(gr, pos));
576 }
577
578 /* A value of a compound entity is a pair of value and the corresponding path to a member of
579    the compound. */
580 INLINE void
581 add_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path) {
582   assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
583   ARR_APP1 (ir_node *, ent->values, val);
584   ARR_APP1 (compound_graph_path *, ent->val_paths, path);
585 }
586
587 INLINE void
588 set_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path, int pos) {
589   assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
590   ent->values[pos] = val;
591   ent->val_paths[pos] = path;
592 }
593
594 INLINE int
595 get_compound_ent_n_values(entity *ent) {
596   assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
597   return (ARR_LEN (ent->values));
598 }
599
600 INLINE ir_node  *
601 get_compound_ent_value(entity *ent, int pos) {
602   assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
603   return ent->values[pos];
604 }
605
606 INLINE compound_graph_path *
607 get_compound_ent_value_path(entity *ent, int pos) {
608   assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
609   return ent->val_paths[pos];
610 }
611
612 void
613 remove_compound_ent_value(entity *ent, entity *value_ent) {
614   int i;
615   assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
616   for (i = 0; i < (ARR_LEN (ent->val_paths)); i++) {
617     compound_graph_path *path = ent->val_paths[i];
618     if (path->nodes[path->len-1] == value_ent) {
619       for(; i < (ARR_LEN (ent->val_paths))-1; i++) {
620         ent->val_paths[i] = ent->val_paths[i+1];
621         ent->values[i]   = ent->values[i+1];
622       }
623       ARR_SETLEN(entity*,  ent->val_paths, ARR_LEN(ent->val_paths) - 1);
624       ARR_SETLEN(ir_node*, ent->values,    ARR_LEN(ent->values)    - 1);
625       break;
626     }
627   }
628 }
629
630 INLINE void
631 add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
632   compound_graph_path *path;
633   assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
634   path = new_compound_graph_path(get_entity_owner(ent), 1);
635   path->nodes[0] = member;
636   add_compound_ent_value_w_path(ent, val, path);
637 }
638
639 /* Copies the firm subgraph referenced by val to const_code_irg and adds
640    the node as constant initialization to ent.
641    The subgraph may not contain control flow operations.
642 INLINE void
643 copy_and_add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
644   ir_graph *rem = current_ir_graph;
645
646   assert(get_entity_variability(ent) != variability_uninitialized);
647   current_ir_graph = get_const_code_irg();
648
649   val = copy_const_value(val);
650   add_compound_ent_value(ent, val, member);
651   current_ir_graph = rem;
652   }*/
653
654 /* Copies the value i of the entity to current_block in current_ir_graph.
655 ir_node *
656 copy_compound_ent_value(entity *ent, int pos) {
657   assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
658   return copy_const_value(ent->values[pos+1]);
659   }*/
660
661 INLINE entity   *
662 get_compound_ent_value_member(entity *ent, int pos) {
663   compound_graph_path *path;
664   assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
665   path = get_compound_ent_value_path(ent, pos);
666
667   return get_compound_graph_path_node(path, get_compound_graph_path_length(path)-1);
668 }
669
670 INLINE void
671 set_compound_ent_value(entity *ent, ir_node *val, entity *member, int pos) {
672   compound_graph_path *path;
673   assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
674   path = get_compound_ent_value_path(ent, pos);
675   set_compound_graph_path_node(path, 0, member);
676   set_compound_ent_value_w_path(ent, val, path, pos);
677 }
678
679 void
680 set_array_entity_values(entity *ent, tarval **values, int num_vals) {
681   int i;
682   ir_graph *rem = current_ir_graph;
683   type *arrtp = get_entity_type(ent);
684   ir_node *val;
685
686   assert(is_array_type(arrtp));
687   assert(get_array_n_dimensions(arrtp) == 1);
688   /* One bound is sufficient, the nunmber of constant fields makes the
689      size. */
690   assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
691   assert(get_entity_variability(ent) != variability_uninitialized);
692   current_ir_graph = get_const_code_irg();
693
694   for (i = 0; i < num_vals; i++) {
695     val = new_Const(get_tarval_mode (values[i]), values[i]);
696     add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
697   }
698   current_ir_graph = rem;
699 }
700
701 INLINE int
702 get_entity_offset (entity *ent) {
703   return ent->offset;
704 }
705
706 INLINE void
707 set_entity_offset (entity *ent, int offset) {
708   ent->offset = offset;
709 }
710
711 INLINE void
712 add_entity_overwrites   (entity *ent, entity *overwritten) {
713   assert(ent);
714   assert(is_class_type(get_entity_owner(ent)));
715   ARR_APP1 (entity *, ent->overwrites, overwritten);
716   ARR_APP1 (entity *, overwritten->overwrittenby, ent);
717 }
718
719 INLINE int
720 get_entity_n_overwrites (entity *ent) {
721   assert(ent);
722   assert(is_class_type(get_entity_owner(ent)));
723   return (ARR_LEN (ent->overwrites));
724 }
725
726 int
727 get_entity_overwrites_index(entity *ent, entity *overwritten) {
728   int i;
729   assert(ent && is_class_type(get_entity_owner(ent)));
730   for (i = 0; i < get_entity_n_overwrites(ent); i++)
731     if (get_entity_overwrites(ent, i) == overwritten)
732       return i;
733   return -1;
734 }
735
736 INLINE entity *
737 get_entity_overwrites   (entity *ent, int pos) {
738   assert(ent);
739   assert(is_class_type(get_entity_owner(ent)));
740   assert(pos < get_entity_n_overwrites(ent));
741   return ent->overwrites[pos];
742 }
743
744 INLINE void
745 set_entity_overwrites   (entity *ent, int pos, entity *overwritten) {
746   assert(ent);
747   assert(is_class_type(get_entity_owner(ent)));
748   assert(pos < get_entity_n_overwrites(ent));
749   ent->overwrites[pos] = overwritten;
750 }
751
752 void
753 remove_entity_overwrites(entity *ent, entity *overwritten) {
754   int i;
755   assert(ent && is_class_type(get_entity_owner(ent)));
756   for (i = 0; i < (ARR_LEN (ent->overwrites)); i++)
757     if (ent->overwrites[i] == overwritten) {
758       for(; i < (ARR_LEN (ent->overwrites))-1; i++)
759         ent->overwrites[i] = ent->overwrites[i+1];
760       ARR_SETLEN(entity*, ent->overwrites, ARR_LEN(ent->overwrites) - 1);
761       break;
762     }
763 }
764
765 INLINE void
766 add_entity_overwrittenby   (entity *ent, entity *overwrites) {
767   assert(ent);
768   assert(is_class_type(get_entity_owner(ent)));
769   add_entity_overwrites(overwrites, ent);
770 }
771
772 INLINE int
773 get_entity_n_overwrittenby (entity *ent) {
774   assert(ent);
775   assert(is_class_type(get_entity_owner(ent)));
776   return (ARR_LEN (ent->overwrittenby));
777 }
778
779 int
780 get_entity_overwrittenby_index(entity *ent, entity *overwrites) {
781   int i;
782   assert(ent && is_class_type(get_entity_owner(ent)));
783   for (i = 0; i < get_entity_n_overwrittenby(ent); i++)
784     if (get_entity_overwrittenby(ent, i) == overwrites)
785       return i;
786   return -1;
787 }
788
789 INLINE entity *
790 get_entity_overwrittenby   (entity *ent, int pos) {
791   assert(ent);
792   assert(is_class_type(get_entity_owner(ent)));
793   assert(pos < get_entity_n_overwrittenby(ent));
794   return ent->overwrittenby[pos];
795 }
796
797 INLINE void
798 set_entity_overwrittenby   (entity *ent, int pos, entity *overwrites) {
799   assert(ent);
800   assert(is_class_type(get_entity_owner(ent)));
801   assert(pos < get_entity_n_overwrittenby(ent));
802   ent->overwrittenby[pos] = overwrites;
803 }
804
805 void    remove_entity_overwrittenby(entity *ent, entity *overwrites) {
806   int i;
807   assert(ent && is_class_type(get_entity_owner(ent)));
808   for (i = 0; i < (ARR_LEN (ent->overwrittenby)); i++)
809     if (ent->overwrittenby[i] == overwrites) {
810       for(; i < (ARR_LEN (ent->overwrittenby))-1; i++)
811         ent->overwrittenby[i] = ent->overwrittenby[i+1];
812       ARR_SETLEN(entity*, ent->overwrittenby, ARR_LEN(ent->overwrittenby) - 1);
813       break;
814     }
815 }
816
817 /* A link to store intermediate information */
818 void *
819 get_entity_link(entity *ent) {
820   assert(ent);
821   return ent->link;
822 }
823
824 void
825 set_entity_link(entity *ent, void *l) {
826   assert(ent);
827   ent->link = l;
828 }
829
830 INLINE ir_graph *
831 get_entity_irg(entity *ent) {
832   assert (ent);
833   assert (is_method_type(ent->type));
834   return ent->irg;
835 }
836
837 INLINE void
838 set_entity_irg(entity *ent, ir_graph *irg) {
839   assert (ent && ent->type);
840   /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
841    * Methode selbst nicht mehr aufgerufen werden kann, die Entität
842    * aber erhalten bleiben soll. */
843   /* assert (irg); */
844   assert (is_method_type(ent->type));
845   assert (ent->peculiarity == peculiarity_existent);
846   ent->irg = irg;
847 }
848
849 int is_entity (void *thing) {
850   assert(thing);
851   if (get_kind(thing) == k_entity)
852     return 1;
853   else
854     return 0;
855 }
856
857 int is_atomic_entity(entity *ent) {
858   type* t = get_entity_type(ent);
859   return (is_primitive_type(t) || is_pointer_type(t) ||
860           is_enumeration_type(t) || is_method_type(t));
861 }
862
863 int is_compound_entity(entity *ent) {
864   type* t = get_entity_type(ent);
865   return (is_class_type(t) || is_struct_type(t) ||
866           is_array_type(t) || is_union_type(t));
867 }
868
869 /* @@@ not implemnted!!! */
870 bool equal_entity(entity *ent1, entity *ent2) {
871   printf(" calling unimplemented equal entity!!! \n");
872   return true;
873 }
874
875
876 unsigned long get_entity_visited(entity *ent) {
877   assert (ent);
878   return ent->visit;
879 }
880 void        set_entity_visited(entity *ent, unsigned long num) {
881   assert (ent);
882   ent->visit = num;
883 }
884 /* Sets visited field in entity to entity_visited. */
885 void        mark_entity_visited(entity *ent) {
886   assert (ent);
887   ent->visit = type_visited;
888 }
889
890
891 INLINE bool entity_visited(entity *ent) {
892   return get_entity_visited(ent) >= type_visited;
893 }
894
895 INLINE bool entity_not_visited(entity *ent) {
896   return get_entity_visited(ent) < type_visited;
897 }
898
899 /* Need two routines because I want to assert the result. */
900 static INLINE entity *resolve_ent_polymorphy2 (type *dynamic_class, entity* static_ent) {
901   int i, n_overwrittenby;
902   entity *res = NULL;
903
904   if (get_entity_owner(static_ent) == dynamic_class) return static_ent;
905
906   n_overwrittenby = get_entity_n_overwrittenby(static_ent);
907   for (i = 0; i < n_overwrittenby; ++i) {
908     res = resolve_ent_polymorphy2(dynamic_class, get_entity_overwrittenby(static_ent, i));
909     if (res) break;
910   }
911
912   return res;
913 }
914
915 /* Returns the dynamically referenced entity if the static entity and the
916  *  dynamic type are given.
917  *  Search downwards in overwritten tree. */
918 entity *resolve_ent_polymorphy(type *dynamic_class, entity* static_ent) {
919   entity *res = resolve_ent_polymorphy2(dynamic_class, static_ent);
920   if (!res) {
921     printf(" Could not find entity "); DDME(static_ent);
922     printf("  in "); DDMT(dynamic_class);
923     printf("\n");
924     dump_entity(static_ent);
925     dump_type(get_entity_owner(static_ent));
926     dump_type(dynamic_class);
927
928   }
929   assert(res);
930   return res;
931 }
932
933
934
935 /*******************************************************************/
936 /** Debug aides                                                   **/
937 /*******************************************************************/
938
939
940 #if 1 || DEBUG_libfirm
941 INLINE int dump_node_opcode(FILE *F, ir_node *n); /* from irdump.c */
942
943 #define X(a)    case a: printf(#a); break
944 void dump_entity (entity *ent) {
945   int i, j;
946   type *owner = get_entity_owner(ent);
947   type *type  = get_entity_type(ent);
948   printf("entity %s (%ld)\n", get_entity_name(ent), get_entity_nr(ent));
949   printf("  type:  %s (%ld)\n", get_type_name(type),  get_type_nr(type));
950   printf("  owner: %s (%ld)\n", get_type_name(owner), get_type_nr(owner));
951
952   printf ("  allocation:  ");
953   switch (get_entity_allocation(ent)) {
954     X(allocation_dynamic);
955     X(allocation_automatic);
956     X(allocation_static);
957     X(allocation_parameter);
958   }
959
960   printf ("\n  visibility:  ");
961   switch (get_entity_visibility(ent)) {
962     X(visibility_local);
963     X(visibility_external_visible);
964     X(visibility_external_allocated);
965   }
966
967   printf ("\n  variability: ");
968   switch (get_entity_variability(ent)) {
969     X(variability_uninitialized);
970     X(variability_initialized);
971     X(variability_part_constant);
972     X(variability_constant);
973   }
974
975   if (get_entity_variability(ent) != variability_uninitialized) {
976     if (is_atomic_entity(ent)) {
977       printf("\n  atomic value: ");
978       dump_node_opcode(stdout, get_atomic_ent_value(ent));
979     } else {
980       printf("\n  compound values:");
981       for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
982         compound_graph_path *path = get_compound_ent_value_path(ent, i);
983         entity *ent0 = get_compound_graph_path_node(path, 0);
984         printf("\n    %2d %s", get_entity_offset(ent0), get_entity_name(ent0));
985         for (j = 1; j < get_compound_graph_path_length(path); ++j)
986           printf(".%s", get_entity_name(get_compound_graph_path_node(path, j)));
987         printf("\t = ");
988         dump_node_opcode(stdout, get_compound_ent_value(ent, i));
989       }
990     }
991   }
992
993   printf ("\n  volatility:  ");
994   switch (get_entity_volatility(ent)) {
995     X(volatility_non_volatile);
996     X(volatility_is_volatile);
997   }
998
999   printf("\n  peculiarity: %s", get_peculiarity_string(get_entity_peculiarity(ent)));
1000   printf("\n  ld_name: %s", ent->ld_name ? get_entity_ld_name(ent) : "no yet set");
1001   printf("\n  offset:  %d", get_entity_offset(ent));
1002   if (is_method_type(get_entity_type(ent))) {
1003     if (get_entity_irg(ent))   /* can be null */
1004       { printf ("\n  irg = %ld", get_irg_graph_nr(get_entity_irg(ent))); }
1005     else
1006       { printf ("\n  irg = NULL"); }
1007   }
1008   printf("\n\n");
1009 }
1010 #undef X
1011 #else  /* DEBUG_libfirm */
1012 void dump_entity (entity *ent) {}
1013 #endif /* DEBUG_libfirm */