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