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