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