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