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