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