renamed some firm enum types to have the ir_ prefix
[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, Michael Beck
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1998-2006 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12 #ifdef HAVE_CONFIG_H
13 # include "config.h"
14 #endif
15
16 #ifdef HAVE_STRING_H
17 # include <string.h>
18 #endif
19 #ifdef HAVE_STDLIB_H
20 # include <stdlib.h>
21 #endif
22 #ifdef HAVE_STDDEF_H
23 # include <stddef.h>
24 #endif
25 #ifdef HAVE_MALLOC_H
26 # include <malloc.h>
27 #endif
28 #ifdef HAVE_ALLOCA_H
29 # include <alloca.h>
30 #endif
31
32 #include "firm_common_t.h"
33
34 #include "xmalloc.h"
35 #include "entity_t.h"
36 #include "mangle.h"
37 #include "typegmod.h"
38 #include "array.h"
39 #include "irtools.h"
40 #include "irhooks.h"
41 #include "irprintf.h"
42
43 /* All this is needed to build the constant node for methods: */
44 #include "irprog_t.h"
45 #include "ircons.h"
46 #include "tv_t.h"
47 #include "irdump.h"  /* for output if errors occur. */
48
49 #include "callgraph.h"  /* for dumping debug output */
50
51 /*******************************************************************/
52 /** general                                                       **/
53 /*******************************************************************/
54
55 entity *unknown_entity = NULL;
56
57 entity *get_unknown_entity(void) { return unknown_entity; }
58
59 #define UNKNOWN_ENTITY_NAME "unknown_entity"
60
61 /*-----------------------------------------------------------------*/
62 /* ENTITY                                                          */
63 /*-----------------------------------------------------------------*/
64
65 static INLINE void insert_entity_in_owner (entity *ent) {
66   ir_type *owner = ent->owner;
67   switch (get_type_tpop_code(owner)) {
68   case tpo_class: {
69     add_class_member (owner, ent);
70   } break;
71   case tpo_struct: {
72     add_struct_member (owner, ent);
73   } break;
74   case tpo_union: {
75     add_union_member (owner, ent);
76   } break;
77   case tpo_array: {
78     set_array_element_entity(owner, ent);
79   } break;
80   default: assert(0);
81   }
82 }
83
84 /**
85  * Creates a new entity. This entity is NOT inserted in the owner type.
86  *
87  * @param db     debug info for this entity
88  * @param owner  the owner type of the new entity
89  * @param name   the name of the new entity
90  * @param type   the type of the new entity
91  *
92  * @return the new created entity
93  */
94 static INLINE entity *
95 new_rd_entity (dbg_info *db, ir_type *owner, ident *name, ir_type *type)
96 {
97   entity *res;
98   ir_graph *rem;
99
100   assert(!id_contains_char(name, ' ') && "entity name should not contain spaces");
101
102   res = xmalloc(sizeof(*res));
103   memset(res, 0, sizeof(*res));
104
105   res->kind    = k_entity;
106   res->name    = name;
107   res->ld_name = NULL;
108   res->owner   = owner;
109   res->type    = type;
110
111   res->allocation  = allocation_automatic;
112   res->visibility  = visibility_local;
113   res->volatility  = volatility_non_volatile;
114   res->stickyness  = stickyness_unsticky;
115   res->offset      = -1;
116   res->peculiarity = peculiarity_existent;
117   res->link        = NULL;
118
119   if (is_Method_type(type)) {
120     symconst_symbol sym;
121     sym.entity_p            = res;
122     rem                     = current_ir_graph;
123     current_ir_graph        = get_const_code_irg();
124     res->value              = new_SymConst(sym, symconst_addr_ent);
125     current_ir_graph        = rem;
126     res->allocation         = allocation_static;
127     res->variability        = variability_constant;
128     res->attr.mtd_attr.irg_add_properties = mtp_property_inherited;
129     res->attr.mtd_attr.vtable_number      = VTABLE_NUM_NOT_SET;
130     res->attr.mtd_attr.param_access       = NULL;
131     res->attr.mtd_attr.param_weight       = NULL;
132     res->attr.mtd_attr.irg                = NULL;
133     res->attr.mtd_attr.section            = section_text;
134   }
135   else if (is_compound_type(type)) {
136     res->variability = variability_uninitialized;
137     res->value       = NULL;
138     res->attr.cmpd_attr.values    = NULL;
139     res->attr.cmpd_attr.val_paths = NULL;
140   }
141   else {
142     res->variability = variability_uninitialized;
143     res->value       = NULL;
144   }
145
146   if (is_Class_type(owner)) {
147     res->overwrites    = NEW_ARR_F(entity *, 0);
148     res->overwrittenby = NEW_ARR_F(entity *, 0);
149   } else {
150     res->overwrites    = NULL;
151     res->overwrittenby = NULL;
152   }
153
154 #ifdef DEBUG_libfirm
155   res->nr = get_irp_new_node_nr();
156 #endif /* DEBUG_libfirm */
157
158   res->visit = 0;
159   set_entity_dbg_info(res, db);
160
161   return res;
162 }
163
164 entity *
165 new_d_entity (ir_type *owner, ident *name, ir_type *type, dbg_info *db) {
166   entity *res;
167
168   assert_legal_owner_of_ent(owner);
169   res = new_rd_entity(db, owner, name, type);
170   /* Remember entity in it's owner. */
171   insert_entity_in_owner (res);
172
173   hook_new_entity(res);
174   return res;
175 }
176
177 entity *
178 new_entity (ir_type *owner, ident *name, ir_type *type) {
179   return new_d_entity(owner, name, type, NULL);
180 }
181
182 /**
183  * Free entity attributes.
184  *
185  * @param ent  the entity
186  */
187 static void free_entity_attrs(entity *ent) {
188   int i;
189   if (get_type_tpop(get_entity_owner(ent)) == type_class) {
190     DEL_ARR_F(ent->overwrites);    ent->overwrites = NULL;
191     DEL_ARR_F(ent->overwrittenby); ent->overwrittenby = NULL;
192   } else {
193     assert(ent->overwrites == NULL);
194     assert(ent->overwrittenby == NULL);
195   }
196   if (is_compound_entity(ent)) {
197     if (ent->attr.cmpd_attr.val_paths) {
198       for (i = 0; i < get_compound_ent_n_values(ent); i++)
199         if (ent->attr.cmpd_attr.val_paths[i]) {
200           /* free_compound_graph_path(ent->attr.cmpd_attr.val_paths[i]) ;  * @@@ warum nich? */
201           /* Geht nich: wird mehrfach verwendet!!! ==> mehrfach frei gegeben. */
202           /* DEL_ARR_F(ent->attr.cmpd_attr.val_paths); */
203         }
204       ent->attr.cmpd_attr.val_paths = NULL;
205     }
206     /* if (ent->attr.cmpd_attr.values) DEL_ARR_F(ent->attr.cmpd_attr.values); *//* @@@ warum nich? */
207     ent->attr.cmpd_attr.values = NULL;
208   }
209   else if (is_method_entity(ent)) {
210     if (ent->attr.mtd_attr.param_access) {
211       DEL_ARR_F(ent->attr.mtd_attr.param_access);
212       ent->attr.mtd_attr.param_access = NULL;
213     }
214     if (ent->attr.mtd_attr.param_weight) {
215       DEL_ARR_F(ent->attr.mtd_attr.param_weight);
216       ent->attr.mtd_attr.param_weight = NULL;
217     }
218   }
219 }
220
221 entity *
222 copy_entity_own (entity *old, ir_type *new_owner) {
223   entity *newe;
224   assert(old && old->kind == k_entity);
225   assert_legal_owner_of_ent(new_owner);
226
227   if (old->owner == new_owner) return old;
228   newe = xmalloc(sizeof(*newe));
229   memcpy (newe, old, sizeof(*newe));
230   newe->owner = new_owner;
231   if (is_Class_type(new_owner)) {
232     newe->overwrites    = NEW_ARR_F(entity *, 0);
233     newe->overwrittenby = NEW_ARR_F(entity *, 0);
234   }
235 #ifdef DEBUG_libfirm
236   newe->nr = get_irp_new_node_nr();
237 #endif
238
239   insert_entity_in_owner (newe);
240
241   return newe;
242 }
243
244 entity *
245 copy_entity_name (entity *old, ident *new_name) {
246   entity *newe;
247   assert(old && old->kind == k_entity);
248
249   if (old->name == new_name) return old;
250   newe = xmalloc(sizeof(*newe));
251   memcpy(newe, old, sizeof(*newe));
252   newe->name = new_name;
253   newe->ld_name = NULL;
254   if (is_Class_type(newe->owner)) {
255     newe->overwrites    = DUP_ARR_F(entity *, old->overwrites);
256     newe->overwrittenby = DUP_ARR_F(entity *, old->overwrittenby);
257   }
258 #ifdef DEBUG_libfirm
259   newe->nr = get_irp_new_node_nr();
260 #endif
261
262   insert_entity_in_owner (newe);
263
264   return newe;
265 }
266
267
268 void
269 free_entity (entity *ent) {
270   assert(ent && ent->kind == k_entity);
271   free_entity_attrs(ent);
272   ent->kind = k_BAD;
273   free(ent);
274 }
275
276 /* Outputs a unique number for this node */
277 long
278 get_entity_nr(entity *ent) {
279   assert(ent && ent->kind == k_entity);
280 #ifdef DEBUG_libfirm
281   return ent->nr;
282 #else
283   return (long)PTR_TO_INT(ent);
284 #endif
285 }
286
287 const char *
288 (get_entity_name)(const entity *ent) {
289   return _get_entity_name(ent);
290 }
291
292 ident *
293 (get_entity_ident)(const entity *ent) {
294   return _get_entity_ident(ent);
295 }
296
297 void
298 (set_entity_ident)(entity *ent, ident *id) {
299   _set_entity_ident(ent, id);
300 }
301
302 ir_type *
303 (get_entity_owner)(entity *ent) {
304   return _get_entity_owner(ent);
305 }
306
307 void
308 set_entity_owner (entity *ent, ir_type *owner) {
309   assert(ent && ent->kind == k_entity);
310   assert_legal_owner_of_ent(owner);
311   ent->owner = owner;
312 }
313
314 void   /* should this go into type.c? */
315 assert_legal_owner_of_ent(ir_type *owner) {
316   assert(get_type_tpop_code(owner) == tpo_class ||
317           get_type_tpop_code(owner) == tpo_union ||
318           get_type_tpop_code(owner) == tpo_struct ||
319       get_type_tpop_code(owner) == tpo_array);   /* Yes, array has an entity
320                             -- to select fields! */
321 }
322
323 ident *
324 (get_entity_ld_ident)(entity *ent) {
325   return _get_entity_ld_ident(ent);
326 }
327
328 void
329 (set_entity_ld_ident)(entity *ent, ident *ld_ident) {
330    _set_entity_ld_ident(ent, ld_ident);
331 }
332
333 const char *
334 (get_entity_ld_name)(entity *ent) {
335   return _get_entity_ld_name(ent);
336 }
337
338 ir_type *
339 (get_entity_type)(entity *ent) {
340   return _get_entity_type(ent);
341 }
342
343 void
344 (set_entity_type)(entity *ent, ir_type *type) {
345   _set_entity_type(ent, type);
346 }
347
348 ir_allocation
349 (get_entity_allocation)(const entity *ent) {
350   return _get_entity_allocation(ent);
351 }
352
353 void
354 (set_entity_allocation)(entity *ent, ir_allocation al) {
355   _set_entity_allocation(ent, al);
356 }
357
358 /* return the name of the visibility */
359 const char *get_allocation_name(ir_allocation all)
360 {
361 #define X(a)    case a: return #a
362   switch (all) {
363     X(allocation_automatic);
364     X(allocation_parameter);
365     X(allocation_dynamic);
366     X(allocation_static);
367     default: return "BAD VALUE";
368   }
369 #undef X
370 }
371
372
373 ir_visibility
374 (get_entity_visibility)(const entity *ent) {
375   return _get_entity_visibility(ent);
376 }
377
378 void
379 set_entity_visibility(entity *ent, ir_visibility vis) {
380   assert(ent && ent->kind == k_entity);
381   if (vis != visibility_local)
382     assert((ent->allocation == allocation_static) ||
383        (ent->allocation == allocation_automatic));
384   /* @@@ Test that the owner type is not local, but how??
385          && get_class_visibility(get_entity_owner(ent)) != local));*/
386   ent->visibility = vis;
387 }
388
389 /* return the name of the visibility */
390 const char *get_visibility_name(ir_visibility vis)
391 {
392 #define X(a)    case a: return #a
393   switch (vis) {
394     X(visibility_local);
395     X(visibility_external_visible);
396     X(visibility_external_allocated);
397     default: return "BAD VALUE";
398   }
399 #undef X
400 }
401
402 ir_variability
403 (get_entity_variability)(const entity *ent) {
404   return _get_entity_variability(ent);
405 }
406
407 void
408 set_entity_variability (entity *ent, ir_variability var)
409 {
410   assert(ent && ent->kind == k_entity);
411   if (var == variability_part_constant)
412     assert(is_Class_type(ent->type) || is_Struct_type(ent->type));
413
414   if ((is_compound_type(ent->type)) &&
415       (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
416     /* Allocate data structures for constant values */
417     ent->attr.cmpd_attr.values    = NEW_ARR_F(ir_node *, 0);
418     ent->attr.cmpd_attr.val_paths = NEW_ARR_F(compound_graph_path *, 0);
419   }
420   if ((is_atomic_type(ent->type)) &&
421       (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
422     /* Set default constant value. */
423     ent->value = new_rd_Unknown(get_const_code_irg(), get_type_mode(ent->type));
424   }
425
426   if ((is_compound_type(ent->type)) &&
427       (var == variability_uninitialized) && (ent->variability != variability_uninitialized)) {
428     /* Free data structures for constant values */
429     DEL_ARR_F(ent->attr.cmpd_attr.values);    ent->attr.cmpd_attr.values    = NULL;
430     DEL_ARR_F(ent->attr.cmpd_attr.val_paths); ent->attr.cmpd_attr.val_paths = NULL;
431   }
432   ent->variability = var;
433 }
434
435 /* return the name of the variability */
436 const char *get_variability_name(ir_variability var)
437 {
438 #define X(a)    case a: return #a
439   switch (var) {
440     X(variability_uninitialized);
441     X(variability_initialized);
442     X(variability_part_constant);
443     X(variability_constant);
444     default: return "BAD VALUE";
445   }
446 #undef X
447 }
448
449 ir_volatility
450 (get_entity_volatility)(const entity *ent) {
451   return _get_entity_volatility(ent);
452 }
453
454 void
455 (set_entity_volatility)(entity *ent, ir_volatility vol) {
456   _set_entity_volatility(ent, vol);
457 }
458
459 /* return the name of the volatility */
460 const char *get_volatility_name(ir_volatility var)
461 {
462 #define X(a)    case a: return #a
463   switch (var) {
464     X(volatility_non_volatile);
465     X(volatility_is_volatile);
466     default: return "BAD VALUE";
467   }
468 #undef X
469 }
470
471 ir_peculiarity
472 (get_entity_peculiarity)(const entity *ent) {
473   return _get_entity_peculiarity(ent);
474 }
475
476 void
477 (set_entity_peculiarity)(entity *ent, ir_peculiarity pec) {
478   _set_entity_peculiarity(ent, pec);
479 }
480
481 /* Get the entity's stickyness */
482 ir_stickyness
483 (get_entity_stickyness)(const entity *ent) {
484   return _get_entity_stickyness(ent);
485 }
486
487 /* Set the entity's stickyness */
488 void
489 (set_entity_stickyness)(entity *ent, ir_stickyness stickyness) {
490   _set_entity_stickyness(ent, stickyness);
491 }
492
493 /* Set has no effect for existent entities of type method. */
494 ir_node *
495 get_atomic_ent_value(entity *ent)
496 {
497   assert(ent && is_atomic_entity(ent));
498   assert(ent->variability != variability_uninitialized);
499   return skip_Id (ent->value);
500 }
501
502 void
503 set_atomic_ent_value(entity *ent, ir_node *val) {
504   assert(is_atomic_entity(ent) && (ent->variability != variability_uninitialized));
505   if (is_Method_type(ent->type) && (ent->peculiarity == peculiarity_existent))
506     return;
507   ent->value = val;
508 }
509
510 /* Returns true if the the node is representable as code on
511  *  const_code_irg. */
512 int is_irn_const_expression(ir_node *n) {
513   ir_mode *m;
514
515   /* we are in danger iff an exception will arise. TODO: be more precisely,
516    * for instance Div. will NOT rise if divisor != 0
517    */
518   if (is_binop(n) && !is_fragile_op(n))
519     return is_irn_const_expression(get_binop_left(n)) && is_irn_const_expression(get_binop_right(n));
520
521   m = get_irn_mode(n);
522   switch(get_irn_opcode(n)) {
523   case iro_Const:
524   case iro_SymConst:
525   case iro_Unknown:
526     return 1;
527   case iro_Conv:
528   case iro_Cast:
529     return is_irn_const_expression(get_irn_n(n, 0));
530   default:
531     break;
532   }
533   return 0;
534 }
535
536 /*
537  * Copies a firm subgraph that complies to the restrictions for
538  * constant expressions to current_block in current_ir_graph.
539  */
540 ir_node *copy_const_value(dbg_info *dbg, ir_node *n) {
541   ir_node *nn;
542   ir_mode *m;
543
544   /* @@@ GL I think  we should implement this using the routines from irgopt for
545      dead node elimination/inlineing. */
546
547   m = get_irn_mode(n);
548   switch (get_irn_opcode(n)) {
549   case iro_Const:
550     nn = new_d_Const_type(dbg, m, get_Const_tarval(n), get_Const_type(n));
551     break;
552   case iro_SymConst:
553     nn = new_d_SymConst_type(dbg, get_SymConst_symbol(n), get_SymConst_kind(n),
554                              get_SymConst_value_type(n));
555     break;
556   case iro_Add:
557     nn = new_d_Add(dbg, copy_const_value(dbg, get_Add_left(n)),
558                  copy_const_value(dbg, get_Add_right(n)), m); break;
559   case iro_Sub:
560     nn = new_d_Sub(dbg, copy_const_value(dbg, get_Sub_left(n)),
561                  copy_const_value(dbg, get_Sub_right(n)), m); break;
562   case iro_Mul:
563     nn = new_d_Mul(dbg, copy_const_value(dbg, get_Mul_left(n)),
564                  copy_const_value(dbg, get_Mul_right(n)), m); break;
565   case iro_And:
566     nn = new_d_And(dbg, copy_const_value(dbg, get_And_left(n)),
567                  copy_const_value(dbg, get_And_right(n)), m); break;
568   case iro_Or:
569     nn = new_d_Or(dbg, copy_const_value(dbg, get_Or_left(n)),
570                 copy_const_value(dbg, get_Or_right(n)), m); break;
571   case iro_Eor:
572     nn = new_d_Eor(dbg, copy_const_value(dbg, get_Eor_left(n)),
573                  copy_const_value(dbg, get_Eor_right(n)), m); break;
574   case iro_Cast:
575     nn = new_d_Cast(dbg, copy_const_value(dbg, get_Cast_op(n)), get_Cast_type(n)); break;
576   case iro_Conv:
577     nn = new_d_Conv(dbg, copy_const_value(dbg, get_Conv_op(n)), m); break;
578   case iro_Unknown:
579     nn = new_d_Unknown(m); break;
580   default:
581     DDMN(n);
582     assert(0 && "opcode invalid or not implemented");
583     nn = NULL;
584     break;
585   }
586   return nn;
587 }
588
589 /* Creates a new compound graph path. */
590 compound_graph_path *
591 new_compound_graph_path(ir_type *tp, int length) {
592   compound_graph_path *res;
593
594   assert(is_type(tp) && is_compound_type(tp));
595   assert(length > 0);
596
597   res = xmalloc(sizeof(*res) + (length-1) * sizeof(res->list[0]));
598   memset(res, 0, sizeof(*res) + (length-1) * sizeof(res->list[0]));
599   res->kind         = k_ir_compound_graph_path;
600   res->tp           = tp;
601   res->len          = length;
602
603   return res;
604 }
605
606 /* Frees an graph path object */
607 void free_compound_graph_path (compound_graph_path *gr) {
608   assert(gr && is_compound_graph_path(gr));
609   gr->kind = k_BAD;
610   free(gr);
611 }
612
613 /* Returns non-zero if an object is a compound graph path */
614 int is_compound_graph_path(void *thing) {
615   return (get_kind(thing) == k_ir_compound_graph_path);
616 }
617
618 /* Checks whether the path up to pos is correct. If the path contains a NULL,
619  *  assumes the path is not complete and returns 'true'. */
620 int is_proper_compound_graph_path(compound_graph_path *gr, int pos) {
621   int i;
622   entity *node;
623   ir_type *owner = gr->tp;
624
625   for (i = 0; i <= pos; i++) {
626     node = get_compound_graph_path_node(gr, i);
627     if (node == NULL)
628       /* Path not yet complete. */
629       return 1;
630     if (get_entity_owner(node) != owner)
631       return 0;
632     owner = get_entity_type(node);
633   }
634   if (pos == get_compound_graph_path_length(gr))
635     if (!is_atomic_type(owner))
636       return 0;
637   return 1;
638 }
639
640 /* Returns the length of a graph path */
641 int get_compound_graph_path_length(compound_graph_path *gr) {
642   assert(gr && is_compound_graph_path(gr));
643   return gr->len;
644 }
645
646 entity *
647 get_compound_graph_path_node(compound_graph_path *gr, int pos) {
648   assert(gr && is_compound_graph_path(gr));
649   assert(pos >= 0 && pos < gr->len);
650   return gr->list[pos].node;
651 }
652
653 void
654 set_compound_graph_path_node(compound_graph_path *gr, int pos, entity *node) {
655   assert(gr && is_compound_graph_path(gr));
656   assert(pos >= 0 && pos < gr->len);
657   assert(is_entity(node));
658   gr->list[pos].node = node;
659   assert(is_proper_compound_graph_path(gr, pos));
660 }
661
662 int
663 get_compound_graph_path_array_index(compound_graph_path *gr, int pos) {
664   assert(gr && is_compound_graph_path(gr));
665   assert(pos >= 0 && pos < gr->len);
666   return gr->list[pos].index;
667 }
668
669 void
670 set_compound_graph_path_array_index(compound_graph_path *gr, int pos, int index) {
671   assert(gr && is_compound_graph_path(gr));
672   assert(pos >= 0 && pos < gr->len);
673   gr->list[pos].index = index;
674 }
675
676 /* A value of a compound entity is a pair of value and the corresponding path to a member of
677    the compound. */
678 void
679 add_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path) {
680   assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
681   assert(is_compound_graph_path(path));
682   ARR_APP1(ir_node *, ent->attr.cmpd_attr.values, val);
683   ARR_APP1(compound_graph_path *, ent->attr.cmpd_attr.val_paths, path);
684 }
685
686 void
687 set_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path, int pos) {
688   assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
689   assert(is_compound_graph_path(path));
690   assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
691   ent->attr.cmpd_attr.values[pos]    = val;
692   ent->attr.cmpd_attr.val_paths[pos] = path;
693 }
694
695 int
696 get_compound_ent_n_values(entity *ent) {
697   assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
698   return ARR_LEN(ent->attr.cmpd_attr.values);
699 }
700
701 ir_node *
702 get_compound_ent_value(entity *ent, int pos) {
703   assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
704   assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
705   return ent->attr.cmpd_attr.values[pos];
706 }
707
708 compound_graph_path *
709 get_compound_ent_value_path(entity *ent, int pos) {
710   assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
711   assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.val_paths));
712   return ent->attr.cmpd_attr.val_paths[pos];
713 }
714
715 /**
716  * Returns non-zero, if two compound_graph_pathes are equal
717  */
718 static int equal_paths(compound_graph_path *path1, int *visited_indicees, compound_graph_path *path2) {
719   int i;
720   int len1 = get_compound_graph_path_length(path1);
721   int len2 = get_compound_graph_path_length(path2);
722
723   if (len2 > len1) return 0;
724
725   for (i = 0; i < len1; i++) {
726     ir_type *tp;
727     entity *node1 = get_compound_graph_path_node(path1, i);
728     entity *node2 = get_compound_graph_path_node(path2, i);
729
730     if (node1 != node2) return 0;
731
732     tp = get_entity_owner(node1);
733     if (is_Array_type(tp)) {
734       long low;
735
736       /* Compute the index of this node. */
737       assert(get_array_n_dimensions(tp) == 1 && "multidim not implemented");
738
739       low = get_array_lower_bound_int(tp, 0);
740       if (low + visited_indicees[i] < get_compound_graph_path_array_index(path2, i)) {
741         visited_indicees[i]++;
742         return 0;
743       }
744       else
745         assert(low + visited_indicees[i] == get_compound_graph_path_array_index(path2, i));
746     }
747   }
748   return 1;
749 }
750
751 /* Returns the position of a value with the given path.
752  *  The path must contain array indicees for all array element entities. */
753 int get_compound_ent_pos_by_path(entity *ent, compound_graph_path *path) {
754   int i, n_paths = get_compound_ent_n_values(ent);
755   int *visited_indicees;
756   int path_len = get_compound_graph_path_length(path);
757
758   NEW_ARR_A(int *, visited_indicees, path_len);
759   memset(visited_indicees, 0, sizeof(*visited_indicees) * path_len);
760   for (i = 0; i < n_paths; i ++) {
761     if (equal_paths(get_compound_ent_value_path(ent, i), visited_indicees, path))
762       return i;
763   }
764
765 #if 0
766   {
767     int j;
768     printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
769       printf("Entity %s : ", get_entity_name(ent));
770       for (j = 0; j < get_compound_graph_path_length(path); ++j) {
771         entity *node = get_compound_graph_path_node(path, j);
772         printf("%s", get_entity_name(node));
773         if (is_Array_type(get_entity_owner(node)))
774           printf("[%d]", get_compound_graph_path_array_index(path, j));
775       }
776     printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
777   }
778 #endif
779
780   assert(0 && "path not found");
781   return -1;
782 }
783
784 /* Returns a constant value given the access path.
785  *  The path must contain array indicees for all array element entities. */
786 ir_node *get_compound_ent_value_by_path(entity *ent, compound_graph_path *path) {
787   return get_compound_ent_value(ent, get_compound_ent_pos_by_path(ent, path));
788 }
789
790
791 void
792 remove_compound_ent_value(entity *ent, entity *value_ent) {
793   int i;
794   assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
795   for (i = 0; i < (ARR_LEN(ent->attr.cmpd_attr.val_paths)); ++i) {
796     compound_graph_path *path = ent->attr.cmpd_attr.val_paths[i];
797     if (path->list[path->len-1].node == value_ent) {
798       for (; i < (ARR_LEN(ent->attr.cmpd_attr.val_paths))-1; ++i) {
799         ent->attr.cmpd_attr.val_paths[i] = ent->attr.cmpd_attr.val_paths[i+1];
800         ent->attr.cmpd_attr.values[i]    = ent->attr.cmpd_attr.values[i+1];
801       }
802       ARR_SETLEN(entity*,  ent->attr.cmpd_attr.val_paths, ARR_LEN(ent->attr.cmpd_attr.val_paths) - 1);
803       ARR_SETLEN(ir_node*, ent->attr.cmpd_attr.values,    ARR_LEN(ent->attr.cmpd_attr.values)    - 1);
804       break;
805     }
806   }
807 }
808
809 void
810 add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
811   compound_graph_path *path;
812   ir_type *owner_tp = get_entity_owner(member);
813   assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
814   path = new_compound_graph_path(get_entity_type(ent), 1);
815   path->list[0].node = member;
816   if (is_Array_type(owner_tp)) {
817     int max;
818     int i;
819
820     assert(get_array_n_dimensions(owner_tp) == 1 && has_array_lower_bound(owner_tp, 0));
821     max = get_array_lower_bound_int(owner_tp, 0) -1;
822     for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
823       int index = get_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0);
824       if (index > max) {
825         max = index;
826       }
827     }
828     path->list[0].index = max + 1;
829   }
830   add_compound_ent_value_w_path(ent, val, path);
831 }
832
833 /* Copies the firm subgraph referenced by val to const_code_irg and adds
834    the node as constant initialization to ent.
835    The subgraph may not contain control flow operations.
836 void
837 copy_and_add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
838   ir_graph *rem = current_ir_graph;
839
840   assert(get_entity_variability(ent) != variability_uninitialized);
841   current_ir_graph = get_const_code_irg();
842
843   val = copy_const_value(val);
844   add_compound_ent_value(ent, val, member);
845   current_ir_graph = rem;
846   }*/
847
848 /* Copies the value i of the entity to current_block in current_ir_graph.
849 ir_node *
850 copy_compound_ent_value(entity *ent, int pos) {
851   assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
852   return copy_const_value(ent->values[pos+1]);
853   }*/
854
855 entity   *
856 get_compound_ent_value_member(entity *ent, int pos) {
857   compound_graph_path *path;
858   assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
859   path = get_compound_ent_value_path(ent, pos);
860
861   return get_compound_graph_path_node(path, get_compound_graph_path_length(path)-1);
862 }
863
864 void
865 set_compound_ent_value(entity *ent, ir_node *val, entity *member, int pos) {
866   compound_graph_path *path;
867   assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
868   path = get_compound_ent_value_path(ent, pos);
869   set_compound_graph_path_node(path, 0, member);
870   set_compound_ent_value_w_path(ent, val, path, pos);
871 }
872
873 void
874 set_array_entity_values(entity *ent, tarval **values, int num_vals) {
875   int i;
876   ir_graph *rem = current_ir_graph;
877   ir_type *arrtp = get_entity_type(ent);
878   ir_node *val;
879   ir_type *elttp = get_array_element_type(arrtp);
880
881   assert(is_Array_type(arrtp));
882   assert(get_array_n_dimensions(arrtp) == 1);
883   /* One bound is sufficient, the number of constant fields makes the
884      size. */
885   assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
886   assert(get_entity_variability(ent) != variability_uninitialized);
887   current_ir_graph = get_const_code_irg();
888
889   for (i = 0; i < num_vals; i++) {
890     val = new_Const_type(values[i], elttp);
891     add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
892     set_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0, i);
893   }
894   current_ir_graph = rem;
895 }
896
897 int  get_compound_ent_value_offset_bits(entity *ent, int pos) {
898   compound_graph_path *path;
899   int i, path_len;
900   int offset = 0;
901
902   assert(get_type_state(get_entity_type(ent)) == layout_fixed);
903
904   path = get_compound_ent_value_path(ent, pos);
905   path_len = get_compound_graph_path_length(path);
906
907   for (i = 0; i < path_len; ++i) {
908     entity *node = get_compound_graph_path_node(path, i);
909     ir_type *node_tp = get_entity_type(node);
910     ir_type *owner_tp = get_entity_owner(node);
911     if (is_Array_type(owner_tp)) {
912       int size  = get_type_size_bits(node_tp);
913       int align = get_type_alignment_bits(node_tp);
914       if (size < align)
915         size = align;
916       else {
917         assert(size % align == 0);
918         /* ansonsten aufrunden */
919       }
920       offset += size * get_compound_graph_path_array_index(path, i);
921     } else {
922       offset += get_entity_offset_bits(node);
923     }
924   }
925   return offset;
926 }
927
928 int  get_compound_ent_value_offset_bytes(entity *ent, int pos) {
929   int offset = get_compound_ent_value_offset_bits(ent, pos);
930   assert(offset % 8 == 0);
931   return offset >> 3;
932 }
933
934
935 static void init_index(ir_type *arr) {
936   int init;
937   int dim = 0;
938
939   assert(get_array_n_dimensions(arr) == 1);
940
941   if (has_array_lower_bound(arr, dim))
942     init = get_array_lower_bound_int(arr, 0) -1;
943   else
944     init = get_array_upper_bound_int(arr, 0) +1;
945
946   set_entity_link(get_array_element_entity(arr), INT_TO_PTR(init));
947 }
948
949
950 static int get_next_index(entity *elem_ent) {
951   ir_type *arr = get_entity_owner(elem_ent);
952   int next;
953   int dim = 0;
954
955   assert(get_array_n_dimensions(arr) == 1);
956
957   if (has_array_lower_bound(arr, dim)) {
958     next = PTR_TO_INT(get_entity_link(elem_ent)) + 1;
959     if (has_array_upper_bound(arr, dim)) {
960       int upper = get_array_upper_bound_int(arr, dim);
961       if (next == upper) next = get_array_lower_bound_int(arr, dim);
962     }
963   } else {
964     next = PTR_TO_INT(get_entity_link(elem_ent)) - 1;
965     if (has_array_lower_bound(arr, dim)) {
966       int upper = get_array_upper_bound_int(arr, dim);
967       if (next == upper) next = get_array_upper_bound_int(arr, dim);
968     }
969   }
970
971   set_entity_link(elem_ent, INT_TO_PTR(next));
972   return next;
973 }
974
975 /* Compute the array indices in compound graph paths of initialized entities.
976  *
977  *  All arrays must have fixed lower and upper bounds.  One array can
978  *  have an open bound.  If there are several open bounds, we do
979  *  nothing.  There must be initializer elements for all array
980  *  elements.  Uses the link field in the array element entities.  The
981  *  array bounds must be representable as ints.
982  *
983  *  (If the bounds are not representable as ints we have to represent
984  *  the indices as firm nodes.  But still we must be able to
985  *  evaluate the index against the upper bound.)
986  */
987 void compute_compound_ent_array_indicees(entity *ent) {
988   ir_type *tp = get_entity_type(ent);
989   int i, n_vals;
990   entity *unknown_bound_entity = NULL;
991
992   if (!is_compound_type(tp) ||
993       (ent->variability == variability_uninitialized)) return ;
994
995   n_vals = get_compound_ent_n_values(ent);
996   if (n_vals == 0) return;
997
998   /* We can not compute the indexes if there is more than one array
999      with an unknown bound.  For this remember the first entity that
1000      represents such an array. It could be ent. */
1001   if (is_Array_type(tp)) {
1002     int dim = 0;
1003
1004     assert(get_array_n_dimensions(tp) == 1 && "other not implemented");
1005     if (!has_array_lower_bound(tp, dim) || !has_array_upper_bound(tp, dim))
1006      unknown_bound_entity = ent;
1007   }
1008
1009   /* Initialize the entity links to lower bound -1 and test all path elements
1010      for known bounds. */
1011   for (i = 0; i < n_vals; ++i) {
1012     compound_graph_path *path = get_compound_ent_value_path(ent, i);
1013     int j, path_len =  get_compound_graph_path_length(path);
1014     for (j = 0; j < path_len; ++j) {
1015       entity *node = get_compound_graph_path_node(path, j);
1016       ir_type *elem_tp = get_entity_type(node);
1017
1018       if (is_Array_type(elem_tp)) {
1019         int dim = 0;
1020         assert(get_array_n_dimensions(elem_tp) == 1 && "other not implemented");
1021         if (!has_array_lower_bound(elem_tp, dim) || !has_array_upper_bound(elem_tp, dim)) {
1022           if (!unknown_bound_entity) unknown_bound_entity = node;
1023           if (node != unknown_bound_entity) return;
1024         }
1025
1026         init_index(elem_tp);
1027       }
1028     }
1029   }
1030
1031   /* Finally compute the indexes ... */
1032   for (i = 0; i < n_vals; ++i) {
1033     compound_graph_path *path = get_compound_ent_value_path(ent, i);
1034     int j, path_len =  get_compound_graph_path_length(path);
1035     for (j = 0; j < path_len; ++j) {
1036       entity *node = get_compound_graph_path_node(path, j);
1037       ir_type *owner_tp = get_entity_owner(node);
1038       if (is_Array_type(owner_tp))
1039         set_compound_graph_path_array_index (path, j, get_next_index(node));
1040     }
1041   }
1042 }
1043
1044 /** resize: double the allocated buffer */
1045 static int *resize (int *buf, int *size) {
1046   int new_size =  *size * 2;
1047   int *new_buf = xcalloc(new_size, sizeof(new_buf[0]));
1048   memcpy(new_buf, buf, *size);
1049   free(buf);
1050   *size = new_size;
1051   return new_buf;
1052 }
1053
1054 /* We sort the elements by placing them at their bit offset in an
1055    array where each entry represents one bit called permutation.  In
1056    fact, we do not place the values themselves, as we would have to
1057    copy two things, the value and the path.  We only remember the
1058    position in the old order. Each value should have a distinct
1059    position in the permutation.
1060
1061    A second iteration now permutes the actual elements into two
1062    new arrays. */
1063 void sort_compound_ent_values(entity *ent) {
1064   ir_type *tp;
1065   int i, n_vals;
1066   int tp_size;
1067   int size;
1068   int *permutation;
1069
1070   int next;
1071   ir_node **my_values;
1072   compound_graph_path **my_paths;
1073
1074   assert(get_type_state(get_entity_type(ent)) == layout_fixed);
1075
1076   tp      = get_entity_type(ent);
1077   n_vals  = get_compound_ent_n_values(ent);
1078   tp_size = get_type_size_bits(tp);
1079
1080   if (!is_compound_type(tp)                           ||
1081       (ent->variability == variability_uninitialized) ||
1082       (get_type_state(tp) != layout_fixed)            ||
1083       (n_vals == 0)                                     ) return;
1084
1085   /* estimated upper bound for size. Better: use flexible array ... */
1086   size = ((tp_size > (n_vals * 32)) ? tp_size : (n_vals * 32)) * 4;
1087   permutation = xcalloc(size, sizeof(permutation[0]));
1088
1089   for (i = 0; i < n_vals; ++i) {
1090     int pos = get_compound_ent_value_offset_bits(ent, i);
1091     while (pos >= size) {
1092       permutation = resize(permutation, &size);
1093     }
1094     assert(pos < size);
1095     assert(permutation[pos] == 0 && "two values with the same offset");
1096     permutation[pos] = i + 1;         /* We initialized with 0, so we can not distinguish entry 0.
1097                      So inc all entries by one. */
1098     //fprintf(stderr, "i: %d, pos: %d \n", i, pos);
1099   }
1100
1101   next = 0;
1102   my_values = NEW_ARR_F(ir_node *, n_vals);
1103   my_paths  = NEW_ARR_F(compound_graph_path *, n_vals);
1104   for (i = 0; i < size; ++i) {
1105     int pos = permutation[i];
1106     if (pos) {
1107       //fprintf(stderr, "pos: %d i: %d  next %d \n", i, pos, next);
1108       assert(next < n_vals);
1109       pos--;   /* We increased the pos by one */
1110       my_values[next] = get_compound_ent_value     (ent, pos);
1111       my_paths [next] = get_compound_ent_value_path(ent, pos);
1112       next++;
1113     }
1114   }
1115   free(permutation);
1116
1117   DEL_ARR_F(ent->attr.cmpd_attr.values);
1118   ent->attr.cmpd_attr.values = my_values;
1119   DEL_ARR_F(ent->attr.cmpd_attr.val_paths);
1120   ent->attr.cmpd_attr.val_paths = my_paths;
1121 }
1122
1123 int
1124 (get_entity_offset_bytes)(const entity *ent) {
1125   return _get_entity_offset_bytes(ent);
1126 }
1127
1128 int
1129 (get_entity_offset_bits)(const entity *ent) {
1130   return _get_entity_offset_bits(ent);
1131 }
1132
1133 void
1134 (set_entity_offset_bytes)(entity *ent, int offset) {
1135   _set_entity_offset_bytes(ent, offset);
1136 }
1137
1138 void
1139 (set_entity_offset_bits)(entity *ent, int offset) {
1140   _set_entity_offset_bits(ent, offset);
1141 }
1142
1143 void
1144 add_entity_overwrites(entity *ent, entity *overwritten) {
1145   assert(ent && is_Class_type(get_entity_owner(ent)));
1146   ARR_APP1(entity *, ent->overwrites, overwritten);
1147   ARR_APP1(entity *, overwritten->overwrittenby, ent);
1148 }
1149
1150 int
1151 get_entity_n_overwrites(entity *ent) {
1152   assert(ent && is_Class_type(get_entity_owner(ent)));
1153   return (ARR_LEN(ent->overwrites));
1154 }
1155
1156 int
1157 get_entity_overwrites_index(entity *ent, entity *overwritten) {
1158   int i;
1159   assert(ent && is_Class_type(get_entity_owner(ent)));
1160   for (i = 0; i < get_entity_n_overwrites(ent); i++)
1161     if (get_entity_overwrites(ent, i) == overwritten)
1162       return i;
1163   return -1;
1164 }
1165
1166 entity *
1167 get_entity_overwrites   (entity *ent, int pos) {
1168   assert(ent && is_Class_type(get_entity_owner(ent)));
1169   assert(pos < get_entity_n_overwrites(ent));
1170   return ent->overwrites[pos];
1171 }
1172
1173 void
1174 set_entity_overwrites   (entity *ent, int pos, entity *overwritten) {
1175   assert(ent && is_Class_type(get_entity_owner(ent)));
1176   assert(pos < get_entity_n_overwrites(ent));
1177   ent->overwrites[pos] = overwritten;
1178 }
1179
1180 void
1181 remove_entity_overwrites(entity *ent, entity *overwritten) {
1182   int i;
1183   assert(ent && is_Class_type(get_entity_owner(ent)));
1184   for (i = 0; i < (ARR_LEN (ent->overwrites)); i++)
1185     if (ent->overwrites[i] == overwritten) {
1186       for(; i < (ARR_LEN (ent->overwrites))-1; i++)
1187     ent->overwrites[i] = ent->overwrites[i+1];
1188       ARR_SETLEN(entity*, ent->overwrites, ARR_LEN(ent->overwrites) - 1);
1189       break;
1190     }
1191 }
1192
1193 void
1194 add_entity_overwrittenby   (entity *ent, entity *overwrites) {
1195   assert(ent && is_Class_type(get_entity_owner(ent)));
1196   add_entity_overwrites(overwrites, ent);
1197 }
1198
1199 int
1200 get_entity_n_overwrittenby (entity *ent) {
1201   assert(ent && is_Class_type(get_entity_owner(ent)));
1202   return (ARR_LEN (ent->overwrittenby));
1203 }
1204
1205 int
1206 get_entity_overwrittenby_index(entity *ent, entity *overwrites) {
1207   int i;
1208   assert(ent && is_Class_type(get_entity_owner(ent)));
1209   for (i = 0; i < get_entity_n_overwrittenby(ent); i++)
1210     if (get_entity_overwrittenby(ent, i) == overwrites)
1211       return i;
1212   return -1;
1213 }
1214
1215 entity *
1216 get_entity_overwrittenby   (entity *ent, int pos) {
1217   assert(ent && is_Class_type(get_entity_owner(ent)));
1218   assert(pos < get_entity_n_overwrittenby(ent));
1219   return ent->overwrittenby[pos];
1220 }
1221
1222 void
1223 set_entity_overwrittenby   (entity *ent, int pos, entity *overwrites) {
1224   assert(ent && is_Class_type(get_entity_owner(ent)));
1225   assert(pos < get_entity_n_overwrittenby(ent));
1226   ent->overwrittenby[pos] = overwrites;
1227 }
1228
1229 void    remove_entity_overwrittenby(entity *ent, entity *overwrites) {
1230   int i;
1231   assert(ent  && is_Class_type(get_entity_owner(ent)));
1232   for (i = 0; i < (ARR_LEN (ent->overwrittenby)); i++)
1233     if (ent->overwrittenby[i] == overwrites) {
1234       for(; i < (ARR_LEN (ent->overwrittenby))-1; i++)
1235     ent->overwrittenby[i] = ent->overwrittenby[i+1];
1236       ARR_SETLEN(entity*, ent->overwrittenby, ARR_LEN(ent->overwrittenby) - 1);
1237       break;
1238     }
1239 }
1240
1241 /* A link to store intermediate information */
1242 void *
1243 (get_entity_link)(const entity *ent) {
1244   return _get_entity_link(ent);
1245 }
1246
1247 void
1248 (set_entity_link)(entity *ent, void *l) {
1249   _set_entity_link(ent, l);
1250 }
1251
1252 ir_graph *
1253 (get_entity_irg)(const entity *ent) {
1254   return _get_entity_irg(ent);
1255 }
1256
1257 void
1258 set_entity_irg(entity *ent, ir_graph *irg) {
1259   assert(ent && is_method_entity(ent));
1260   /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
1261    * Methode selbst nicht mehr aufgerufen werden kann, die Entität
1262    * aber erhalten bleiben soll?  Wandle die Entitaet in description oder
1263    * inherited um! */
1264   /* assert(irg); */
1265   assert((irg  && ent->peculiarity == peculiarity_existent) ||
1266          (!irg && (ent->peculiarity == peculiarity_existent)
1267           && (ent -> visibility == visibility_external_allocated)) ||
1268          (!irg && ent->peculiarity == peculiarity_description) ||
1269          (!irg && ent->peculiarity == peculiarity_inherited));
1270   ent->attr.mtd_attr.irg = irg;
1271 }
1272
1273 unsigned get_entity_vtable_number(entity *ent) {
1274   assert(ent && is_method_entity(ent));
1275   return ent->attr.mtd_attr.vtable_number;
1276 }
1277
1278 void set_entity_vtable_number(entity *ent, unsigned vtable_number) {
1279   assert(ent && is_method_entity(ent));
1280   ent->attr.mtd_attr.vtable_number = vtable_number;
1281 }
1282
1283 /* Returns the section of a method. */
1284 ir_img_section get_method_img_section(const entity *ent) {
1285   assert(ent && is_method_entity(ent));
1286   return ent->attr.mtd_attr.section;
1287 }
1288
1289 /* Sets the section of a method. */
1290 void set_method_img_section(entity *ent, ir_img_section section) {
1291   assert(ent && is_method_entity(ent));
1292   ent->attr.mtd_attr.section = section;
1293 }
1294
1295 int
1296 (is_entity)(const void *thing) {
1297   return _is_entity(thing);
1298 }
1299
1300 int is_atomic_entity(entity *ent) {
1301   ir_type *t = get_entity_type(ent);
1302   assert(ent && ent->kind == k_entity);
1303   return (is_Primitive_type(t) || is_Pointer_type(t) ||
1304       is_Enumeration_type(t) || is_Method_type(t));
1305 }
1306
1307 int is_compound_entity(entity *ent) {
1308   ir_type *t = get_entity_type(ent);
1309   assert(ent && ent->kind == k_entity);
1310   return (is_Class_type(t) || is_Struct_type(t) ||
1311       is_Array_type(t) || is_Union_type(t));
1312 }
1313
1314 int is_method_entity(entity *ent) {
1315   ir_type *t = get_entity_type(ent);
1316   assert(ent && ent->kind == k_entity);
1317   return (is_Method_type(t));
1318 }
1319
1320 /**
1321  * @todo not implemented!!! */
1322 int equal_entity(entity *ent1, entity *ent2) {
1323   fprintf(stderr, " calling unimplemented equal entity!!! \n");
1324   return 1;
1325 }
1326
1327
1328 unsigned long (get_entity_visited)(entity *ent) {
1329   return _get_entity_visited(ent);
1330 }
1331
1332 void (set_entity_visited)(entity *ent, unsigned long num) {
1333   _set_entity_visited(ent, num);
1334 }
1335
1336 /* Sets visited field in entity to entity_visited. */
1337 void (mark_entity_visited)(entity *ent) {
1338   _mark_entity_visited(ent);
1339 }
1340
1341 int (entity_visited)(entity *ent) {
1342   return _entity_visited(ent);
1343 }
1344
1345 int (entity_not_visited)(entity *ent) {
1346   return _entity_not_visited(ent);
1347 }
1348
1349 /* Returns the mask of the additional entity properties. */
1350 unsigned get_entity_additional_properties(entity *ent) {
1351   ir_graph *irg;
1352
1353   assert(is_method_entity(ent));
1354
1355   /* first check, if the graph has additional properties */
1356   irg = get_entity_irg(ent);
1357
1358   if (irg)
1359     return get_irg_additional_properties(irg);
1360
1361   if (ent->attr.mtd_attr.irg_add_properties & mtp_property_inherited)
1362     return get_method_additional_properties(get_entity_type(ent));
1363
1364   return ent->attr.mtd_attr.irg_add_properties;
1365 }
1366
1367 /* Sets the mask of the additional graph properties. */
1368 void set_entity_additional_properties(entity *ent, unsigned property_mask)
1369 {
1370   ir_graph *irg;
1371
1372   assert(is_method_entity(ent));
1373
1374   /* first check, if the graph exists */
1375   irg = get_entity_irg(ent);
1376   if (irg)
1377     set_irg_additional_properties(irg, property_mask);
1378   else {
1379     /* do not allow to set the mtp_property_inherited flag or
1380      * the automatic inheritance of flags will not work */
1381     ent->attr.mtd_attr.irg_add_properties = property_mask & ~mtp_property_inherited;
1382   }
1383 }
1384
1385 /* Sets one additional graph property. */
1386 void set_entity_additional_property(entity *ent, mtp_additional_property flag)
1387 {
1388   ir_graph *irg;
1389
1390   assert(is_method_entity(ent));
1391
1392   /* first check, if the graph exists */
1393   irg = get_entity_irg(ent);
1394   if (irg)
1395     set_irg_additional_property(irg, flag);
1396   else {
1397     unsigned mask = ent->attr.mtd_attr.irg_add_properties;
1398
1399     if (mask & mtp_property_inherited)
1400       mask = get_method_additional_properties(get_entity_type(ent));
1401
1402     /* do not allow to set the mtp_property_inherited flag or
1403      * the automatic inheritance of flags will not work */
1404     ent->attr.mtd_attr.irg_add_properties = mask | (flag & ~mtp_property_inherited);
1405   }
1406 }
1407
1408 /* Initialize entity module. */
1409 void firm_init_entity(void)
1410 {
1411   symconst_symbol sym;
1412
1413   assert(firm_unknown_type && "Call init_type() before firm_init_entity()!");
1414   assert(!unknown_entity && "Call firm_init_entity() only once!");
1415
1416   unknown_entity = new_rd_entity(NULL, firm_unknown_type, new_id_from_str(UNKNOWN_ENTITY_NAME), firm_unknown_type);
1417   set_entity_visibility(unknown_entity, visibility_external_allocated);
1418   set_entity_ld_ident(unknown_entity, get_entity_ident(unknown_entity));
1419
1420   current_ir_graph      = get_const_code_irg();
1421   sym.entity_p          = unknown_entity;
1422   unknown_entity->value = new_SymConst(sym, symconst_addr_ent);
1423 }