- graph passes can be added to prog managers now
[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 "xmalloc.h"
33 #include "entity_t.h"
34 #include "array.h"
35 #include "irtools.h"
36 #include "irhooks.h"
37 #include "irprintf.h"
38
39 #include "irprog_t.h"
40 #include "ircons.h"
41 #include "tv_t.h"
42 #include "irdump.h"
43 #include "irgraph_t.h"
44 #include "callgraph.h"
45 #include "error.h"
46
47 /*-----------------------------------------------------------------*/
48 /** general                                                       **/
49 /*-----------------------------------------------------------------*/
50
51 ir_entity *unknown_entity = NULL;
52
53 ir_entity *get_unknown_entity(void) { return unknown_entity; }
54
55 /** The name of the unknown entity. */
56 #define UNKNOWN_ENTITY_NAME "unknown_entity"
57
58 /*-----------------------------------------------------------------*/
59 /* ENTITY                                                          */
60 /*-----------------------------------------------------------------*/
61
62 /**
63  * Add an entity to it's already set owner type.
64  */
65 static inline void insert_entity_in_owner(ir_entity *ent) {
66         ir_type *owner = ent->owner;
67         switch (get_type_tpop_code(owner)) {
68         case tpo_class:
69                 add_class_member(owner, ent);
70                 break;
71         case tpo_struct:
72                 add_struct_member(owner, ent);
73                 break;
74         case tpo_union:
75                 add_union_member(owner, ent);
76                 break;
77         case tpo_array:
78                 set_array_element_entity(owner, ent);
79                 break;
80         default:
81                 panic("Unsupported type kind");
82         }
83 }  /* insert_entity_in_owner */
84
85 /**
86  * Creates a new entity. This entity is NOT inserted in the owner type.
87  *
88  * @param db     debug info for this entity
89  * @param owner  the owner type of the new entity
90  * @param name   the name of the new entity
91  * @param type   the type of the new entity
92  *
93  * @return the new created entity
94  */
95 static inline ir_entity *
96 new_rd_entity(dbg_info *db, ir_type *owner, ident *name, ir_type *type)
97 {
98         ir_entity *res;
99         ir_graph *rem;
100
101         assert(!id_contains_char(name, ' ') && "entity name should not contain spaces");
102
103         res = XMALLOCZ(ir_entity);
104
105         res->kind    = k_entity;
106         res->name    = name;
107         res->ld_name = NULL;
108         res->type    = type;
109         res->owner   = owner;
110
111         res->allocation           = allocation_automatic;
112         res->visibility           = visibility_local;
113         res->volatility           = volatility_non_volatile;
114         res->aligned              = align_is_aligned;
115         res->stickyness           = stickyness_unsticky;
116         res->peculiarity          = peculiarity_existent;
117         res->usage                = ir_usage_unknown;
118         res->final                = 0;
119         res->compiler_gen         = 0;
120         res->backend_marked       = 0;
121         res->offset               = -1;
122         res->offset_bit_remainder = 0;
123         res->alignment            = 0;
124         res->link                 = NULL;
125         res->repr_class           = NULL;
126
127         if (is_Method_type(type)) {
128                 symconst_symbol sym;
129                 ir_mode *mode = is_Method_type(type) ? mode_P_code : mode_P_data;
130                 sym.entity_p            = res;
131                 rem                     = current_ir_graph;
132                 current_ir_graph        = get_const_code_irg();
133                 res->value              = new_SymConst(mode, sym, symconst_addr_ent);
134                 current_ir_graph        = rem;
135                 res->allocation         = allocation_static;
136                 res->variability        = variability_constant;
137                 res->attr.mtd_attr.irg_add_properties = mtp_property_inherited;
138                 res->attr.mtd_attr.vtable_number      = VTABLE_NUM_NOT_SET;
139                 res->attr.mtd_attr.param_access       = NULL;
140                 res->attr.mtd_attr.param_weight       = NULL;
141                 res->attr.mtd_attr.irg                = NULL;
142         } else if (is_compound_type(type)) {
143                 res->variability = variability_uninitialized;
144                 res->value       = NULL;
145                 res->attr.cmpd_attr.values    = NULL;
146                 res->attr.cmpd_attr.val_paths = NULL;
147         } else if (is_code_type(type)) {
148                 res->attr.code_attr.label = (ir_label_t) -1;
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_r_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 unsigned
516 (get_entity_alignment)(const ir_entity *ent) {
517         return _get_entity_alignment(ent);
518 }
519
520 void
521 (set_entity_alignment)(ir_entity *ent, unsigned alignment) {
522         _set_entity_alignment(ent, alignment);
523 }
524
525 /* Return the name of the alignment. */
526 const char *get_align_name(ir_align a)
527 {
528 #define X(a)    case a: return #a
529         switch (a) {
530         X(align_non_aligned);
531         X(align_is_aligned);
532         default: return "BAD VALUE";
533         }
534 #undef X
535 }  /* get_align_name */
536
537 void
538 set_entity_label(ir_entity *ent, ir_label_t label)
539 {
540         ent->attr.code_attr.label = label;
541 }
542
543 ir_label_t get_entity_label(const ir_entity *ent)
544 {
545         return ent->attr.code_attr.label;
546 }
547
548 ir_peculiarity
549 (get_entity_peculiarity)(const ir_entity *ent) {
550         return _get_entity_peculiarity(ent);
551 }  /* get_entity_peculiarity */
552
553 void
554 (set_entity_peculiarity)(ir_entity *ent, ir_peculiarity pec) {
555         _set_entity_peculiarity(ent, pec);
556 }  /* set_entity_peculiarity */
557
558 /* Checks if an entity cannot be overridden anymore. */
559 int (is_entity_final)(const ir_entity *ent) {
560         return _is_entity_final(ent);
561 }  /* is_entity_final */
562
563 /* Sets/resets the final flag of an entity. */
564 void (set_entity_final)(ir_entity *ent, int final) {
565         _set_entity_final(ent, final);
566 }  /* set_entity_final */
567
568 /* Checks if an entity is compiler generated */
569 int (is_entity_compiler_generated)(const ir_entity *ent) {
570         return _is_entity_compiler_generated(ent);
571 }  /* is_entity_compiler_generated */
572
573 /* Sets/resets the compiler generated flag */
574 void (set_entity_compiler_generated)(ir_entity *ent, int flag) {
575         _set_entity_compiler_generated(ent, flag);
576 }  /* set_entity_compiler_generated */
577
578 /* Checks if an entity is marked by the backend */
579 int (is_entity_backend_marked)(const ir_entity *ent) {
580         return _is_entity_backend_marked(ent);
581 }  /* is_entity_backend_marked */
582
583 /* Sets/resets the compiler generated flag */
584 void (set_entity_backend_marked)(ir_entity *ent, int flag) {
585         _set_entity_backend_marked(ent, flag);
586 }  /* set_entity_backend_marked */
587
588 ir_entity_usage (get_entity_usage)(const ir_entity *ent) {
589         return _get_entity_usage(ent);
590 }
591
592 void (set_entity_usage)(ir_entity *ent, ir_entity_usage flags) {
593         _set_entity_usage(ent, flags);
594 }
595
596 /* Get the entity's stickyness */
597 ir_stickyness
598 (get_entity_stickyness)(const ir_entity *ent) {
599         return _get_entity_stickyness(ent);
600 }  /* get_entity_stickyness */
601
602 /* Set the entity's stickyness */
603 void
604 (set_entity_stickyness)(ir_entity *ent, ir_stickyness stickyness) {
605         _set_entity_stickyness(ent, stickyness);
606 }  /* set_entity_stickyness */
607
608 /* Set has no effect for existent entities of type method. */
609 ir_node *
610 get_atomic_ent_value(ir_entity *ent)
611 {
612         assert(ent && is_atomic_entity(ent));
613         assert(ent->variability != variability_uninitialized);
614         return skip_Id(ent->value);
615 }  /* get_atomic_ent_value */
616
617 void
618 set_atomic_ent_value(ir_entity *ent, ir_node *val) {
619         assert(is_atomic_entity(ent) && (ent->variability != variability_uninitialized));
620         if (is_Method_type(ent->type) && (ent->peculiarity == peculiarity_existent))
621                 return;
622         assert(is_Dummy(val) || get_irn_mode(val) == get_type_mode(ent->type));
623         ent->value = val;
624 }  /* set_atomic_ent_value */
625
626 /* Returns true if the the node is representable as code on
627  *  const_code_irg. */
628 int is_irn_const_expression(ir_node *n) {
629         ir_mode *m;
630
631         /* we are in danger iff an exception will arise. TODO: be more precisely,
632          * for instance Div. will NOT rise if divisor != 0
633          */
634         if (is_binop(n) && !is_fragile_op(n))
635                 return is_irn_const_expression(get_binop_left(n)) && is_irn_const_expression(get_binop_right(n));
636
637         m = get_irn_mode(n);
638         switch (get_irn_opcode(n)) {
639         case iro_Const:
640         case iro_SymConst:
641         case iro_Unknown:
642                 return 1;
643         case iro_Conv:
644         case iro_Cast:
645                 return is_irn_const_expression(get_irn_n(n, 0));
646         default:
647                 break;
648         }
649         return 0;
650 }  /* is_irn_const_expression */
651
652 /*
653  * Copies a firm subgraph that complies to the restrictions for
654  * constant expressions to current_block in current_ir_graph.
655  */
656 ir_node *copy_const_value(dbg_info *dbg, ir_node *n) {
657         ir_node *nn;
658         ir_mode *m;
659
660         /* @@@ GL I think  we should implement this using the routines from irgopt for
661                dead node elimination/inlineing. */
662
663         m = get_irn_mode(n);
664         switch (get_irn_opcode(n)) {
665         case iro_Const:
666                 nn = new_d_Const_type(dbg, get_Const_tarval(n), get_Const_type(n));
667                 break;
668         case iro_SymConst:
669                 nn = new_d_SymConst_type(dbg, get_irn_mode(n), get_SymConst_symbol(n), get_SymConst_kind(n),
670                         get_SymConst_value_type(n));
671                 break;
672         case iro_Add:
673                 nn = new_d_Add(dbg, copy_const_value(dbg, get_Add_left(n)),
674                         copy_const_value(dbg, get_Add_right(n)), m); break;
675         case iro_Sub:
676                 nn = new_d_Sub(dbg, copy_const_value(dbg, get_Sub_left(n)),
677                         copy_const_value(dbg, get_Sub_right(n)), m); break;
678         case iro_Mul:
679                 nn = new_d_Mul(dbg, copy_const_value(dbg, get_Mul_left(n)),
680                         copy_const_value(dbg, get_Mul_right(n)), m); break;
681         case iro_And:
682                 nn = new_d_And(dbg, copy_const_value(dbg, get_And_left(n)),
683                         copy_const_value(dbg, get_And_right(n)), m); break;
684         case iro_Or:
685                 nn = new_d_Or(dbg, copy_const_value(dbg, get_Or_left(n)),
686                         copy_const_value(dbg, get_Or_right(n)), m); break;
687         case iro_Eor:
688                 nn = new_d_Eor(dbg, copy_const_value(dbg, get_Eor_left(n)),
689                         copy_const_value(dbg, get_Eor_right(n)), m); break;
690         case iro_Cast:
691                 nn = new_d_Cast(dbg, copy_const_value(dbg, get_Cast_op(n)), get_Cast_type(n)); break;
692         case iro_Conv:
693                 nn = new_d_Conv(dbg, copy_const_value(dbg, get_Conv_op(n)), m); break;
694         case iro_Unknown:
695                 nn = new_Unknown(m); break;
696         default:
697                 assert(0 && "opcode invalid or not implemented");
698                 nn = NULL;
699                 break;
700         }
701         return nn;
702 }  /* copy_const_value */
703
704 /** Return the name of the initializer kind. */
705 const char *get_initializer_kind_name(ir_initializer_kind_t ini)
706 {
707 #define X(a)    case a: return #a
708         switch (ini) {
709         X(IR_INITIALIZER_CONST);
710         X(IR_INITIALIZER_TARVAL);
711         X(IR_INITIALIZER_NULL);
712         X(IR_INITIALIZER_COMPOUND);
713     default: return "BAD VALUE";
714         }
715 #undef X
716 }
717
718 static ir_initializer_t null_initializer = { IR_INITIALIZER_NULL };
719
720 ir_initializer_t *get_initializer_null(void)
721 {
722         return &null_initializer;
723 }
724
725 ir_initializer_t *create_initializer_const(ir_node *value)
726 {
727         struct obstack *obst = get_irg_obstack(get_const_code_irg());
728
729         ir_initializer_t *initializer
730                 = obstack_alloc(obst, sizeof(ir_initializer_const_t));
731         initializer->kind         = IR_INITIALIZER_CONST;
732         initializer->consti.value = value;
733
734         return initializer;
735 }
736
737 ir_initializer_t *create_initializer_tarval(tarval *tv)
738 {
739         struct obstack *obst = get_irg_obstack(get_const_code_irg());
740
741         ir_initializer_t *initializer
742                 = obstack_alloc(obst, sizeof(ir_initializer_tarval_t));
743         initializer->kind         = IR_INITIALIZER_TARVAL;
744         initializer->tarval.value = tv;
745
746         return initializer;
747 }
748
749 ir_initializer_t *create_initializer_compound(unsigned n_entries)
750 {
751         struct obstack *obst = get_irg_obstack(get_const_code_irg());
752
753         size_t i;
754         size_t size  = sizeof(ir_initializer_compound_t)
755                      + (n_entries-1) * sizeof(ir_initializer_t*);
756
757         ir_initializer_t *initializer = obstack_alloc(obst, size);
758         initializer->kind                    = IR_INITIALIZER_COMPOUND;
759         initializer->compound.n_initializers = n_entries;
760
761         for(i = 0; i < n_entries; ++i) {
762                 initializer->compound.initializers[i] = get_initializer_null();
763         }
764
765         return initializer;
766 }
767
768 ir_node *get_initializer_const_value(const ir_initializer_t *initializer)
769 {
770         assert(initializer->kind == IR_INITIALIZER_CONST);
771         return skip_Id(initializer->consti.value);
772 }
773
774 tarval *get_initializer_tarval_value(const ir_initializer_t *initializer)
775 {
776         assert(initializer->kind == IR_INITIALIZER_TARVAL);
777         return initializer->tarval.value;
778 }
779
780 unsigned get_initializer_compound_n_entries(const ir_initializer_t *initializer)
781 {
782         assert(initializer->kind == IR_INITIALIZER_COMPOUND);
783         return initializer->compound.n_initializers;
784 }
785
786 void set_initializer_compound_value(ir_initializer_t *initializer,
787                                     unsigned index, ir_initializer_t *value)
788 {
789         assert(initializer->kind == IR_INITIALIZER_COMPOUND);
790         assert(index < initializer->compound.n_initializers);
791
792         initializer->compound.initializers[index] = value;
793 }
794
795 ir_initializer_t *get_initializer_compound_value(
796                 const ir_initializer_t *initializer, unsigned index)
797 {
798         assert(initializer->kind == IR_INITIALIZER_COMPOUND);
799         assert(index < initializer->compound.n_initializers);
800
801         return initializer->compound.initializers[index];
802 }
803
804 ir_initializer_kind_t get_initializer_kind(const ir_initializer_t *initializer)
805 {
806         return initializer->kind;
807 }
808
809 static void check_entity_initializer(ir_entity *entity)
810 {
811         /* TODO */
812         (void) entity;
813 }
814
815 void set_entity_initializer(ir_entity *entity, ir_initializer_t *initializer)
816 {
817         entity->attr.initializer = initializer;
818         entity->has_initializer  = 1;
819         check_entity_initializer(entity);
820 }
821
822 int has_entity_initializer(const ir_entity *entity)
823 {
824         return entity->has_initializer;
825 }
826
827 ir_initializer_t *get_entity_initializer(const ir_entity *entity)
828 {
829         assert(entity->has_initializer);
830         return entity->attr.initializer;
831 }
832
833 /* Creates a new compound graph path. */
834 compound_graph_path *
835 new_compound_graph_path(ir_type *tp, int length) {
836         compound_graph_path *res;
837
838         assert(is_compound_type(tp));
839         assert(length > 0);
840
841         res = xmalloc(sizeof(*res) + (length-1) * sizeof(res->list[0]));
842         memset(res, 0, sizeof(*res) + (length-1) * sizeof(res->list[0]));
843         res->kind         = k_ir_compound_graph_path;
844         res->tp           = tp;
845         res->len          = length;
846
847         return res;
848 }  /* new_compound_graph_path */
849
850 /* Frees an graph path object */
851 void free_compound_graph_path (compound_graph_path *gr) {
852         assert(gr && is_compound_graph_path(gr));
853         gr->kind = k_BAD;
854         free(gr);
855 }  /* free_compound_graph_path */
856
857 /* Returns non-zero if an object is a compound graph path */
858 int is_compound_graph_path(const void *thing) {
859         return (get_kind(thing) == k_ir_compound_graph_path);
860 }  /* is_compound_graph_path */
861
862 /* Checks whether the path up to pos is correct. If the path contains a NULL,
863  *  assumes the path is not complete and returns 'true'. */
864 int is_proper_compound_graph_path(compound_graph_path *gr, int pos) {
865         int i;
866         ir_entity *node;
867         ir_type *owner = gr->tp;
868
869         for (i = 0; i <= pos; i++) {
870                 node = get_compound_graph_path_node(gr, i);
871                 if (node == NULL)
872                         /* Path not yet complete. */
873                         return 1;
874                 if (get_entity_owner(node) != owner)
875                         return 0;
876                 owner = get_entity_type(node);
877         }
878         if (pos == get_compound_graph_path_length(gr))
879                 if (!is_atomic_type(owner))
880                         return 0;
881                 return 1;
882 }  /* is_proper_compound_graph_path */
883
884 /* Returns the length of a graph path */
885 int get_compound_graph_path_length(const compound_graph_path *gr) {
886         assert(gr && is_compound_graph_path(gr));
887         return gr->len;
888 }  /* get_compound_graph_path_length */
889
890 ir_entity *
891 get_compound_graph_path_node(const compound_graph_path *gr, int pos) {
892         assert(gr && is_compound_graph_path(gr));
893         assert(pos >= 0 && pos < gr->len);
894         return gr->list[pos].node;
895 }  /* get_compound_graph_path_node */
896
897 void
898 set_compound_graph_path_node(compound_graph_path *gr, int pos, ir_entity *node) {
899         assert(gr && is_compound_graph_path(gr));
900         assert(pos >= 0 && pos < gr->len);
901         assert(is_entity(node));
902         gr->list[pos].node = node;
903         assert(is_proper_compound_graph_path(gr, pos));
904 }  /* set_compound_graph_path_node */
905
906 int
907 get_compound_graph_path_array_index(const compound_graph_path *gr, int pos) {
908         assert(gr && is_compound_graph_path(gr));
909         assert(pos >= 0 && pos < gr->len);
910         return gr->list[pos].index;
911 }  /* get_compound_graph_path_array_index */
912
913 void
914 set_compound_graph_path_array_index(compound_graph_path *gr, int pos, int index) {
915         assert(gr && is_compound_graph_path(gr));
916         assert(pos >= 0 && pos < gr->len);
917         gr->list[pos].index = index;
918 }  /* set_compound_graph_path_array_index */
919
920 ir_type *
921 get_compound_graph_path_type(const compound_graph_path *gr) {
922         assert(gr && is_compound_graph_path(gr));
923         return gr->tp;
924 }
925
926 /* A value of a compound entity is a pair of value and the corresponding path to a member of
927    the compound. */
928 void
929 add_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path) {
930         assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
931         assert(is_compound_graph_path(path));
932         ARR_APP1(ir_node *, ent->attr.cmpd_attr.values, val);
933         ARR_APP1(compound_graph_path *, ent->attr.cmpd_attr.val_paths, path);
934 }  /* add_compound_ent_value_w_path */
935
936 void
937 set_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path, int pos) {
938         assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
939         assert(is_compound_graph_path(path));
940         assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
941         ent->attr.cmpd_attr.values[pos]    = val;
942         ent->attr.cmpd_attr.val_paths[pos] = path;
943 }  /* set_compound_ent_value_w_path */
944
945 int
946 get_compound_ent_n_values(ir_entity *ent) {
947         assert(!ent->has_initializer);
948         assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
949         return ARR_LEN(ent->attr.cmpd_attr.values);
950 }  /* get_compound_ent_n_values */
951
952 ir_node *
953 get_compound_ent_value(ir_entity *ent, int pos) {
954         assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
955         assert(!ent->has_initializer);
956         assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
957         return skip_Id(ent->attr.cmpd_attr.values[pos]);
958 }  /* get_compound_ent_value */
959
960 compound_graph_path *
961 get_compound_ent_value_path(ir_entity *ent, int pos) {
962         assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
963         assert(!ent->has_initializer);
964         assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.val_paths));
965         return ent->attr.cmpd_attr.val_paths[pos];
966 }  /* get_compound_ent_value_path */
967
968 /**
969  * Returns non-zero, if two compound_graph_pathes are equal
970  *
971  * @param path1            the first path
972  * @param path2            the second path
973  */
974 static int equal_paths(compound_graph_path *path1, compound_graph_path *path2) {
975         int i;
976         int len1 = get_compound_graph_path_length(path1);
977         int len2 = get_compound_graph_path_length(path2);
978
979         if (len2 != len1) return 0;
980
981         for (i = 0; i < len1; i++) {
982                 ir_type *tp;
983                 ir_entity *node1 = get_compound_graph_path_node(path1, i);
984                 ir_entity *node2 = get_compound_graph_path_node(path2, i);
985
986                 if (node1 != node2) return 0;
987
988                 tp = get_entity_owner(node1);
989                 if (is_Array_type(tp)) {
990                         int index1 = get_compound_graph_path_array_index(path1, i);
991                         int index2 = get_compound_graph_path_array_index(path2, i);
992                         if (index1 != index2)
993                                 return 0;
994                 }
995         }
996         return 1;
997 }  /* equal_paths */
998
999 /**
1000  * Returns the position of a value with the given path.
1001  * The path must contain array indices for all array element entities.
1002  *
1003  * @todo  This implementation is very slow (O(number of initializers * |path|)
1004  *        and should be replaced when the new tree oriented
1005  *        value representation is finally implemented.
1006  */
1007 static int get_compound_ent_pos_by_path(ir_entity *ent, compound_graph_path *path) {
1008         int i, n_paths = get_compound_ent_n_values(ent);
1009
1010         for (i = 0; i < n_paths; i ++) {
1011                 compound_graph_path *gr = get_compound_ent_value_path(ent, i);
1012                 if (equal_paths(gr, path))
1013                         return i;
1014         }
1015         return -1;
1016 }  /* get_compound_ent_pos_by_path */
1017
1018 /* Returns a constant value given the access path.
1019  *  The path must contain array indices for all array element entities. */
1020 ir_node *get_compound_ent_value_by_path(ir_entity *ent, compound_graph_path *path) {
1021         int pos = get_compound_ent_pos_by_path(ent, path);
1022         if (pos >= 0)
1023                 return get_compound_ent_value(ent, pos);
1024         return NULL;
1025 }  /* get_compound_ent_value_by_path */
1026
1027
1028 void
1029 remove_compound_ent_value(ir_entity *ent, ir_entity *value_ent) {
1030         int i, n;
1031         assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
1032
1033         n = ARR_LEN(ent->attr.cmpd_attr.val_paths);
1034         for (i = 0; i < n; ++i) {
1035                 compound_graph_path *path = ent->attr.cmpd_attr.val_paths[i];
1036                 if (path->list[path->len-1].node == value_ent) {
1037                         for (; i < n - 1; ++i) {
1038                                 ent->attr.cmpd_attr.val_paths[i] = ent->attr.cmpd_attr.val_paths[i+1];
1039                                 ent->attr.cmpd_attr.values[i]    = ent->attr.cmpd_attr.values[i+1];
1040                         }
1041                         ARR_SETLEN(ir_entity*, ent->attr.cmpd_attr.val_paths, n - 1);
1042                         ARR_SETLEN(ir_node*,   ent->attr.cmpd_attr.values,    n - 1);
1043                         break;
1044                 }
1045         }
1046 }  /* remove_compound_ent_value */
1047
1048 void
1049 add_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member) {
1050         compound_graph_path *path;
1051         ir_type *owner_tp = get_entity_owner(member);
1052         assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
1053         path = new_compound_graph_path(get_entity_type(ent), 1);
1054         path->list[0].node = member;
1055         if (is_Array_type(owner_tp)) {
1056                 int max;
1057                 int i, n;
1058
1059                 assert(get_array_n_dimensions(owner_tp) == 1 && has_array_lower_bound(owner_tp, 0));
1060                 max = get_array_lower_bound_int(owner_tp, 0) -1;
1061                 for (i = 0, n = get_compound_ent_n_values(ent); i < n; ++i) {
1062                         int index = get_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0);
1063                         if (index > max) {
1064                                 max = index;
1065                         }
1066                 }
1067                 path->list[0].index = max + 1;
1068         }
1069         add_compound_ent_value_w_path(ent, val, path);
1070 }  /* add_compound_ent_value */
1071
1072
1073 ir_entity *
1074 get_compound_ent_value_member(ir_entity *ent, int pos) {
1075         compound_graph_path *path;
1076         assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
1077         path = get_compound_ent_value_path(ent, pos);
1078
1079         return get_compound_graph_path_node(path, get_compound_graph_path_length(path)-1);
1080 }  /* get_compound_ent_value_member */
1081
1082 void
1083 set_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member, int pos) {
1084         compound_graph_path *path;
1085         assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
1086         path = get_compound_ent_value_path(ent, pos);
1087         set_compound_graph_path_node(path, 0, member);
1088         set_compound_ent_value_w_path(ent, val, path, pos);
1089 }  /* set_compound_ent_value */
1090
1091 void
1092 set_array_entity_values(ir_entity *ent, tarval **values, int num_vals) {
1093         int i;
1094         ir_graph *rem = current_ir_graph;
1095         ir_type *arrtp = get_entity_type(ent);
1096         ir_node *val;
1097         ir_type *elttp = get_array_element_type(arrtp);
1098
1099         assert(is_Array_type(arrtp));
1100         assert(get_array_n_dimensions(arrtp) == 1);
1101         /* One bound is sufficient, the number of constant fields makes the
1102            size. */
1103         assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
1104         assert(get_entity_variability(ent) != variability_uninitialized);
1105         current_ir_graph = get_const_code_irg();
1106
1107         for (i = 0; i < num_vals; i++) {
1108                 val = new_Const_type(values[i], elttp);
1109                 add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
1110                 set_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0, i);
1111         }
1112         current_ir_graph = rem;
1113 }  /* set_array_entity_values */
1114
1115 /* Return the overall offset of value at position pos in bytes. */
1116 unsigned get_compound_ent_value_offset_bytes(ir_entity *ent, int pos) {
1117         compound_graph_path *path;
1118         int path_len, i;
1119         unsigned offset = 0;
1120         ir_type *curr_tp;
1121
1122         assert(get_type_state(get_entity_type(ent)) == layout_fixed);
1123
1124         path     = get_compound_ent_value_path(ent, pos);
1125         path_len = get_compound_graph_path_length(path);
1126         curr_tp  = path->tp;
1127
1128         for (i = 0; i < path_len; ++i) {
1129                 if (is_Array_type(curr_tp)) {
1130                         ir_type *elem_type = get_array_element_type(curr_tp);
1131                         unsigned size      = get_type_size_bytes(elem_type);
1132                         unsigned align     = get_type_alignment_bytes(elem_type);
1133                         int      idx;
1134
1135                         assert(size > 0);
1136                         if(size % align > 0) {
1137                                 size += align - (size % align);
1138                         }
1139                         idx = get_compound_graph_path_array_index(path, i);
1140                         assert(idx >= 0);
1141                         offset += size * idx;
1142                         curr_tp = elem_type;
1143                 } else {
1144                         ir_entity *node = get_compound_graph_path_node(path, i);
1145                         offset += get_entity_offset(node);
1146                         curr_tp = get_entity_type(node);
1147                 }
1148         }
1149
1150         return offset;
1151 }  /* get_compound_ent_value_offset_bytes */
1152
1153 /* Return the offset in bits from the last byte address. */
1154 unsigned get_compound_ent_value_offset_bit_remainder(ir_entity *ent, int pos) {
1155         compound_graph_path *path;
1156         int path_len;
1157         ir_entity *last_node;
1158
1159         assert(get_type_state(get_entity_type(ent)) == layout_fixed);
1160
1161         path      = get_compound_ent_value_path(ent, pos);
1162         path_len  = get_compound_graph_path_length(path);
1163         last_node = get_compound_graph_path_node(path, path_len - 1);
1164
1165         if(last_node == NULL)
1166                 return 0;
1167
1168         return get_entity_offset_bits_remainder(last_node);
1169 }  /* get_compound_ent_value_offset_bit_remainder */
1170
1171 int
1172 (get_entity_offset)(const ir_entity *ent) {
1173         return _get_entity_offset(ent);
1174 }  /* get_entity_offset */
1175
1176 void
1177 (set_entity_offset)(ir_entity *ent, int offset) {
1178         _set_entity_offset(ent, offset);
1179 }  /* set_entity_offset */
1180
1181 unsigned char
1182 (get_entity_offset_bits_remainder)(const ir_entity *ent) {
1183         return _get_entity_offset_bits_remainder(ent);
1184 }  /* get_entity_offset_bits_remainder */
1185
1186 void
1187 (set_entity_offset_bits_remainder)(ir_entity *ent, unsigned char offset) {
1188         _set_entity_offset_bits_remainder(ent, offset);
1189 }  /* set_entity_offset_bits_remainder */
1190
1191 void
1192 add_entity_overwrites(ir_entity *ent, ir_entity *overwritten) {
1193 #ifndef NDEBUG
1194         ir_type *owner     = get_entity_owner(ent);
1195         ir_type *ovw_ovner = get_entity_owner(overwritten);
1196         assert(is_Class_type(owner));
1197         assert(is_Class_type(ovw_ovner));
1198         assert(! is_class_final(ovw_ovner));
1199 #endif /* NDEBUG */
1200         ARR_APP1(ir_entity *, ent->overwrites, overwritten);
1201         ARR_APP1(ir_entity *, overwritten->overwrittenby, ent);
1202 }  /* add_entity_overwrites */
1203
1204 int
1205 get_entity_n_overwrites(ir_entity *ent) {
1206         assert(is_Class_type(get_entity_owner(ent)));
1207         return (ARR_LEN(ent->overwrites));
1208 }  /* get_entity_n_overwrites */
1209
1210 int
1211 get_entity_overwrites_index(ir_entity *ent, ir_entity *overwritten) {
1212         int i, n;
1213         assert(is_Class_type(get_entity_owner(ent)));
1214         n = get_entity_n_overwrites(ent);
1215         for (i = 0; i < n; ++i) {
1216                 if (get_entity_overwrites(ent, i) == overwritten)
1217                         return i;
1218         }
1219         return -1;
1220 }  /* get_entity_overwrites_index */
1221
1222 ir_entity *
1223 get_entity_overwrites(ir_entity *ent, int pos) {
1224         assert(is_Class_type(get_entity_owner(ent)));
1225         assert(pos < get_entity_n_overwrites(ent));
1226         return ent->overwrites[pos];
1227 }  /* get_entity_overwrites */
1228
1229 void
1230 set_entity_overwrites(ir_entity *ent, int pos, ir_entity *overwritten) {
1231         assert(is_Class_type(get_entity_owner(ent)));
1232         assert(pos < get_entity_n_overwrites(ent));
1233         ent->overwrites[pos] = overwritten;
1234 }  /* set_entity_overwrites */
1235
1236 void
1237 remove_entity_overwrites(ir_entity *ent, ir_entity *overwritten) {
1238         int i, n;
1239         assert(is_Class_type(get_entity_owner(ent)));
1240         n = ARR_LEN(ent->overwrites);
1241         for (i = 0; i < n; ++i) {
1242                 if (ent->overwrites[i] == overwritten) {
1243                         for (; i < n - 1; i++)
1244                                 ent->overwrites[i] = ent->overwrites[i+1];
1245                         ARR_SETLEN(ir_entity*, ent->overwrites, n - 1);
1246                         break;
1247                 }
1248         }
1249 }  /* remove_entity_overwrites */
1250
1251 void
1252 add_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites) {
1253         add_entity_overwrites(overwrites, ent);
1254 }  /* add_entity_overwrittenby */
1255
1256 int
1257 get_entity_n_overwrittenby(ir_entity *ent) {
1258         assert(is_Class_type(get_entity_owner(ent)));
1259         return ARR_LEN(ent->overwrittenby);
1260 }  /* get_entity_n_overwrittenby */
1261
1262 int
1263 get_entity_overwrittenby_index(ir_entity *ent, ir_entity *overwrites) {
1264         int i, n;
1265         assert(is_Class_type(get_entity_owner(ent)));
1266         n = get_entity_n_overwrittenby(ent);
1267         for (i = 0; i < n; ++i) {
1268                 if (get_entity_overwrittenby(ent, i) == overwrites)
1269                         return i;
1270         }
1271         return -1;
1272 }  /* get_entity_overwrittenby_index */
1273
1274 ir_entity *
1275 get_entity_overwrittenby(ir_entity *ent, int pos) {
1276         assert(is_Class_type(get_entity_owner(ent)));
1277         assert(pos < get_entity_n_overwrittenby(ent));
1278         return ent->overwrittenby[pos];
1279 }  /* get_entity_overwrittenby */
1280
1281 void
1282 set_entity_overwrittenby(ir_entity *ent, int pos, ir_entity *overwrites) {
1283         assert(is_Class_type(get_entity_owner(ent)));
1284         assert(pos < get_entity_n_overwrittenby(ent));
1285         ent->overwrittenby[pos] = overwrites;
1286 }  /* set_entity_overwrittenby */
1287
1288 void remove_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites) {
1289         int i, n;
1290         assert(is_Class_type(get_entity_owner(ent)));
1291
1292         n = ARR_LEN(ent->overwrittenby);
1293         for (i = 0; i < n; ++i) {
1294                 if (ent->overwrittenby[i] == overwrites) {
1295                         for(; i < n - 1; ++i)
1296                                 ent->overwrittenby[i] = ent->overwrittenby[i+1];
1297                         ARR_SETLEN(ir_entity*, ent->overwrittenby, n - 1);
1298                         break;
1299                 }
1300         }
1301 }  /* remove_entity_overwrittenby */
1302
1303 /* A link to store intermediate information */
1304 void *
1305 (get_entity_link)(const ir_entity *ent) {
1306         return _get_entity_link(ent);
1307 }  /* get_entity_link */
1308
1309 void
1310 (set_entity_link)(ir_entity *ent, void *l) {
1311         _set_entity_link(ent, l);
1312 }  /* set_entity_link */
1313
1314 ir_graph *
1315 (get_entity_irg)(const ir_entity *ent) {
1316         return _get_entity_irg(ent);
1317 }  /* get_entity_irg */
1318
1319 void
1320 set_entity_irg(ir_entity *ent, ir_graph *irg) {
1321         assert(is_method_entity(ent));
1322         /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
1323          * Methode selbst nicht mehr aufgerufen werden kann, die Entität
1324          * aber erhalten bleiben soll?  Wandle die Entitaet in description oder
1325          * inherited um! */
1326         /* assert(irg); */
1327         assert((irg  && ent->peculiarity == peculiarity_existent) ||
1328                 (!irg && (ent->peculiarity == peculiarity_existent)
1329                 && (ent -> visibility == visibility_external_allocated)) ||
1330                 (!irg && ent->peculiarity == peculiarity_description) ||
1331                 (!irg && ent->peculiarity == peculiarity_inherited));
1332         ent->attr.mtd_attr.irg = irg;
1333 }  /* set_entity_irg */
1334
1335 unsigned get_entity_vtable_number(const ir_entity *ent) {
1336         assert(is_method_entity((ir_entity *)ent));
1337         return ent->attr.mtd_attr.vtable_number;
1338 }  /* get_entity_vtable_number */
1339
1340 void set_entity_vtable_number(ir_entity *ent, unsigned vtable_number) {
1341         assert(is_method_entity(ent));
1342         ent->attr.mtd_attr.vtable_number = vtable_number;
1343 }  /* set_entity_vtable_number */
1344
1345 int
1346 (is_entity)(const void *thing) {
1347         return _is_entity(thing);
1348 }  /* is_entity */
1349
1350 int is_atomic_entity(ir_entity *ent) {
1351         ir_type *t      = get_entity_type(ent);
1352         const tp_op *op = get_type_tpop(t);
1353         return (op == type_primitive || op == type_pointer ||
1354                 op == type_enumeration || op == type_method);
1355 }  /* is_atomic_entity */
1356
1357 int is_compound_entity(ir_entity *ent) {
1358         ir_type     *t  = get_entity_type(ent);
1359         const tp_op *op = get_type_tpop(t);
1360         return (op == type_class || op == type_struct ||
1361                 op == type_array || op == type_union);
1362 }  /* is_compound_entity */
1363
1364 int is_method_entity(ir_entity *ent) {
1365         ir_type *t = get_entity_type(ent);
1366         return is_Method_type(t);
1367 }  /* is_method_entity */
1368
1369 /**
1370  * @todo not implemented!!! */
1371 int equal_entity(ir_entity *ent1, ir_entity *ent2) {
1372         (void) ent1;
1373         (void) ent2;
1374         fprintf(stderr, " calling unimplemented equal entity!!! \n");
1375         return 1;
1376 }  /* equal_entity */
1377
1378
1379 ir_visited_t (get_entity_visited)(ir_entity *ent) {
1380         return _get_entity_visited(ent);
1381 }  /* get_entity_visited */
1382
1383 void (set_entity_visited)(ir_entity *ent, ir_visited_t num) {
1384         _set_entity_visited(ent, num);
1385 }  /* set_entity_visited */
1386
1387 /* Sets visited field in ir_entity to entity_visited. */
1388 void (mark_entity_visited)(ir_entity *ent) {
1389         _mark_entity_visited(ent);
1390 }  /* mark_entity_visited */
1391
1392 int (entity_visited)(ir_entity *ent) {
1393         return _entity_visited(ent);
1394 }  /* entity_visited */
1395
1396 int (entity_not_visited)(ir_entity *ent) {
1397         return _entity_not_visited(ent);
1398 }  /* entity_not_visited */
1399
1400 /* Returns the mask of the additional entity properties. */
1401 unsigned get_entity_additional_properties(ir_entity *ent) {
1402         ir_graph *irg;
1403
1404         assert(is_method_entity(ent));
1405
1406         /* first check, if the graph has additional properties */
1407         irg = get_entity_irg(ent);
1408
1409         if (irg)
1410                 return get_irg_additional_properties(irg);
1411
1412         if (ent->attr.mtd_attr.irg_add_properties & mtp_property_inherited)
1413                 return get_method_additional_properties(get_entity_type(ent));
1414
1415         return ent->attr.mtd_attr.irg_add_properties;
1416 }  /* get_entity_additional_properties */
1417
1418 /* Sets the mask of the additional graph properties. */
1419 void set_entity_additional_properties(ir_entity *ent, unsigned property_mask)
1420 {
1421         ir_graph *irg;
1422
1423         assert(is_method_entity(ent));
1424
1425         /* first check, if the graph exists */
1426         irg = get_entity_irg(ent);
1427         if (irg)
1428                 set_irg_additional_properties(irg, property_mask);
1429         else {
1430     /* do not allow to set the mtp_property_inherited flag or
1431                 * the automatic inheritance of flags will not work */
1432                 ent->attr.mtd_attr.irg_add_properties = property_mask & ~mtp_property_inherited;
1433         }
1434 }  /* set_entity_additional_properties */
1435
1436 /* Sets one additional graph property. */
1437 void set_entity_additional_property(ir_entity *ent, mtp_additional_property flag)
1438 {
1439         ir_graph *irg;
1440
1441         assert(is_method_entity(ent));
1442
1443         /* first check, if the graph exists */
1444         irg = get_entity_irg(ent);
1445         if (irg)
1446                 set_irg_additional_property(irg, flag);
1447         else {
1448                 unsigned mask = ent->attr.mtd_attr.irg_add_properties;
1449
1450                 if (mask & mtp_property_inherited)
1451                         mask = get_method_additional_properties(get_entity_type(ent));
1452
1453                         /* do not allow to set the mtp_property_inherited flag or
1454                 * the automatic inheritance of flags will not work */
1455                 ent->attr.mtd_attr.irg_add_properties = mask | (flag & ~mtp_property_inherited);
1456         }
1457 }  /* set_entity_additional_property */
1458
1459 /* Returns the class type that this type info entity represents or NULL
1460    if ent is no type info entity. */
1461 ir_type *(get_entity_repr_class)(const ir_entity *ent) {
1462         return _get_entity_repr_class(ent);
1463 }  /* get_entity_repr_class */
1464
1465 dbg_info *(get_entity_dbg_info)(const ir_entity *ent) {
1466         return _get_entity_dbg_info(ent);
1467 }  /* get_entity_dbg_info */
1468
1469 void (set_entity_dbg_info)(ir_entity *ent, dbg_info *db) {
1470         _set_entity_dbg_info(ent, db);
1471 }  /* set_entity_dbg_info */
1472
1473 /* Initialize entity module. */
1474 void firm_init_entity(void)
1475 {
1476         symconst_symbol sym;
1477
1478         assert(firm_unknown_type && "Call init_type() before firm_init_entity()!");
1479         assert(!unknown_entity && "Call firm_init_entity() only once!");
1480
1481         unknown_entity = new_rd_entity(NULL, firm_unknown_type, new_id_from_str(UNKNOWN_ENTITY_NAME), firm_unknown_type);
1482         set_entity_visibility(unknown_entity, visibility_external_allocated);
1483         set_entity_ld_ident(unknown_entity, get_entity_ident(unknown_entity));
1484
1485         current_ir_graph      = get_const_code_irg();
1486         sym.entity_p          = unknown_entity;
1487         /* TODO: we need two unknown_entities here, one for code and one for data */
1488         unknown_entity->value = new_SymConst(mode_P_data, sym, symconst_addr_ent);
1489 }  /* firm_init_entity */