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