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