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