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