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