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