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