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