made get_entity_nr() 64bit safe
[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   ir_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, ir_type *owner, ident *name, ir_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 (ir_type *owner, ident *name, ir_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 (ir_type *owner, ident *name, ir_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 = NULL;
205   }
206 }
207
208 entity *
209 copy_entity_own (entity *old, ir_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 (long)PTR_TO_INT(ent);
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 ir_type *
285 (get_entity_owner)(entity *ent) {
286   return _get_entity_owner(ent);
287 }
288
289 void
290 set_entity_owner (entity *ent, ir_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(ir_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 ir_type *
321 (get_entity_type)(entity *ent) {
322   return _get_entity_type(ent);
323 }
324
325 void
326 (set_entity_type)(entity *ent, ir_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 1;
522   case iro_Conv:
523   case iro_Cast:
524     return is_irn_const_expression(get_irn_n(n, 0));
525   default:
526     break;
527   }
528   return 0;
529 }
530
531 /*
532  * Copies a firm subgraph that complies to the restrictions for
533  * constant expressions to current_block in current_ir_graph.
534  */
535 ir_node *copy_const_value(dbg_info *dbg, ir_node *n) {
536   ir_node *nn;
537   ir_mode *m;
538
539   /* @@@ GL I think  we should implement this using the routines from irgopt for
540      dead node elimination/inlineing. */
541
542   m = get_irn_mode(n);
543   switch (get_irn_opcode(n)) {
544   case iro_Const:
545     nn = new_d_Const_type(dbg, m, get_Const_tarval(n), get_Const_type(n));
546     break;
547   case iro_SymConst:
548     nn = new_d_SymConst_type(dbg, get_SymConst_symbol(n), get_SymConst_kind(n),
549                              get_SymConst_value_type(n));
550     break;
551   case iro_Add:
552     nn = new_d_Add(dbg, copy_const_value(dbg, get_Add_left(n)),
553                  copy_const_value(dbg, get_Add_right(n)), m); break;
554   case iro_Sub:
555     nn = new_d_Sub(dbg, copy_const_value(dbg, get_Sub_left(n)),
556                  copy_const_value(dbg, get_Sub_right(n)), m); break;
557   case iro_Mul:
558     nn = new_d_Mul(dbg, copy_const_value(dbg, get_Mul_left(n)),
559                  copy_const_value(dbg, get_Mul_right(n)), m); break;
560   case iro_And:
561     nn = new_d_And(dbg, copy_const_value(dbg, get_And_left(n)),
562                  copy_const_value(dbg, get_And_right(n)), m); break;
563   case iro_Or:
564     nn = new_d_Or(dbg, copy_const_value(dbg, get_Or_left(n)),
565                 copy_const_value(dbg, get_Or_right(n)), m); break;
566   case iro_Eor:
567     nn = new_d_Eor(dbg, copy_const_value(dbg, get_Eor_left(n)),
568                  copy_const_value(dbg, get_Eor_right(n)), m); break;
569   case iro_Cast:
570     nn = new_d_Cast(dbg, copy_const_value(dbg, get_Cast_op(n)), get_Cast_type(n)); break;
571   case iro_Conv:
572     nn = new_d_Conv(dbg, copy_const_value(dbg, get_Conv_op(n)), m); break;
573   case iro_Unknown:
574     nn = new_d_Unknown(m); break;
575   default:
576     DDMN(n);
577     assert(0 && "opcode invalid or not implemented");
578     nn = NULL;
579     break;
580   }
581   return nn;
582 }
583
584 /* Creates a new compound graph path. */
585 compound_graph_path *
586 new_compound_graph_path(ir_type *tp, int length) {
587   compound_graph_path *res;
588
589   assert(is_type(tp) && is_compound_type(tp));
590   assert(length > 0);
591
592   res = xmalloc(sizeof(*res) + (length-1) * sizeof(res->list[0]));
593   memset(res, 0, sizeof(*res) + (length-1) * sizeof(res->list[0]));
594   res->kind         = k_ir_compound_graph_path;
595   res->tp           = tp;
596   res->len          = length;
597
598   return res;
599 }
600
601 /* Frees an graph path object */
602 void free_compound_graph_path (compound_graph_path *gr) {
603   assert(gr && is_compound_graph_path(gr));
604   gr->kind = k_BAD;
605   free(gr);
606 }
607
608 /* Returns non-zero if an object is a compound graph path */
609 int is_compound_graph_path(void *thing) {
610   return (get_kind(thing) == k_ir_compound_graph_path);
611 }
612
613 /* Checks whether the path up to pos is correct. If the path contains a NULL,
614  *  assumes the path is not complete and returns 'true'. */
615 int is_proper_compound_graph_path(compound_graph_path *gr, int pos) {
616   int i;
617   entity *node;
618   ir_type *owner = gr->tp;
619
620   for (i = 0; i <= pos; i++) {
621     node = get_compound_graph_path_node(gr, i);
622     if (node == NULL)
623       /* Path not yet complete. */
624       return 1;
625     if (get_entity_owner(node) != owner)
626       return 0;
627     owner = get_entity_type(node);
628   }
629   if (pos == get_compound_graph_path_length(gr))
630     if (!is_atomic_type(owner))
631       return 0;
632   return 1;
633 }
634
635 /* Returns the length of a graph path */
636 int get_compound_graph_path_length(compound_graph_path *gr) {
637   assert(gr && is_compound_graph_path(gr));
638   return gr->len;
639 }
640
641 entity *
642 get_compound_graph_path_node(compound_graph_path *gr, int pos) {
643   assert(gr && is_compound_graph_path(gr));
644   assert(pos >= 0 && pos < gr->len);
645   return gr->list[pos].node;
646 }
647
648 void
649 set_compound_graph_path_node(compound_graph_path *gr, int pos, entity *node) {
650   assert(gr && is_compound_graph_path(gr));
651   assert(pos >= 0 && pos < gr->len);
652   assert(is_entity(node));
653   gr->list[pos].node = node;
654   assert(is_proper_compound_graph_path(gr, pos));
655 }
656
657 int
658 get_compound_graph_path_array_index(compound_graph_path *gr, int pos) {
659   assert(gr && is_compound_graph_path(gr));
660   assert(pos >= 0 && pos < gr->len);
661   return gr->list[pos].index;
662 }
663
664 void
665 set_compound_graph_path_array_index(compound_graph_path *gr, int pos, int index) {
666   assert(gr && is_compound_graph_path(gr));
667   assert(pos >= 0 && pos < gr->len);
668   gr->list[pos].index = index;
669 }
670
671 /* A value of a compound entity is a pair of value and the corresponding path to a member of
672    the compound. */
673 void
674 add_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path) {
675   assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
676   ARR_APP1 (ir_node *, ent->values, val);
677   ARR_APP1 (compound_graph_path *, ent->val_paths, path);
678 }
679
680 void
681 set_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path, int pos) {
682   assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
683   ent->values[pos] = val;
684   ent->val_paths[pos] = path;
685 }
686
687 int
688 get_compound_ent_n_values(entity *ent) {
689   assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
690   return (ARR_LEN (ent->values));
691 }
692
693 ir_node  *
694 get_compound_ent_value(entity *ent, int pos) {
695   assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
696   return ent->values[pos];
697 }
698
699 compound_graph_path *
700 get_compound_ent_value_path(entity *ent, int pos) {
701   assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
702   return ent->val_paths[pos];
703 }
704
705 /**
706  * Returns non-zero, if two compound_graph_pathes are equal
707  */
708 static int equal_paths(compound_graph_path *path1, int *visited_indicees, compound_graph_path *path2) {
709   int i;
710   int len1 = get_compound_graph_path_length(path1);
711   int len2 = get_compound_graph_path_length(path2);
712
713   if (len2 > len1) return 0;
714
715   for (i = 0; i < len1; i++) {
716     ir_type *tp;
717     entity *node1 = get_compound_graph_path_node(path1, i);
718     entity *node2 = get_compound_graph_path_node(path2, i);
719
720     if (node1 != node2) return 0;
721
722     tp = get_entity_owner(node1);
723     if (is_Array_type(tp)) {
724       long low;
725
726       /* Compute the index of this node. */
727       assert(get_array_n_dimensions(tp) == 1 && "multidim not implemented");
728
729       low = get_array_lower_bound_int(tp, 0);
730       if (low + visited_indicees[i] < get_compound_graph_path_array_index(path2, i)) {
731         visited_indicees[i]++;
732         return 0;
733       }
734       else
735         assert(low + visited_indicees[i] == get_compound_graph_path_array_index(path2, i));
736     }
737   }
738   return 1;
739 }
740
741 /* Returns the position of a value with the given path.
742  *  The path must contain array indicees for all array element entities. */
743 int get_compound_ent_pos_by_path(entity *ent, compound_graph_path *path) {
744   int i, n_paths = get_compound_ent_n_values(ent);
745   int *visited_indicees = (int *)xcalloc(get_compound_graph_path_length(path), sizeof(int));
746   for (i = 0; i < n_paths; i ++) {
747     if (equal_paths(get_compound_ent_value_path(ent, i), visited_indicees, path))
748       return i;
749   }
750
751 #if 0
752   {
753     int j;
754     printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
755       printf("Entity %s : ", get_entity_name(ent));
756       for (j = 0; j < get_compound_graph_path_length(path); ++j) {
757         entity *node = get_compound_graph_path_node(path, j);
758         printf("%s", get_entity_name(node));
759         if (is_Array_type(get_entity_owner(node)))
760           printf("[%d]", get_compound_graph_path_array_index(path, j));
761       }
762     printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
763   }
764 #endif
765
766   assert(0 && "path not found");
767   return -1;
768 }
769
770 /* Returns a constant value given the access path.
771  *  The path must contain array indicees for all array element entities. */
772 ir_node *get_compound_ent_value_by_path(entity *ent, compound_graph_path *path) {
773   return get_compound_ent_value(ent, get_compound_ent_pos_by_path(ent, path));
774 }
775
776
777 void
778 remove_compound_ent_value(entity *ent, entity *value_ent) {
779   int i;
780   assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
781   for (i = 0; i < (ARR_LEN (ent->val_paths)); i++) {
782     compound_graph_path *path = ent->val_paths[i];
783     if (path->list[path->len-1].node == value_ent) {
784       for(; i < (ARR_LEN (ent->val_paths))-1; i++) {
785         ent->val_paths[i] = ent->val_paths[i+1];
786         ent->values[i]    = ent->values[i+1];
787       }
788       ARR_SETLEN(entity*,  ent->val_paths, ARR_LEN(ent->val_paths) - 1);
789       ARR_SETLEN(ir_node*, ent->values,    ARR_LEN(ent->values)    - 1);
790       break;
791     }
792   }
793 }
794
795 void
796 add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
797   compound_graph_path *path;
798   ir_type *owner_tp = get_entity_owner(member);
799   assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
800   path = new_compound_graph_path(get_entity_type(ent), 1);
801   path->list[0].node = member;
802   if (is_Array_type(owner_tp)) {
803     int max;
804     int i;
805
806     assert(get_array_n_dimensions(owner_tp) == 1 && has_array_lower_bound(owner_tp, 0));
807     max = get_array_lower_bound_int(owner_tp, 0) -1;
808     for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
809       int index = get_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0);
810       if (index > max) {
811         max = index;
812       }
813     }
814     path->list[0].index = max + 1;
815   }
816   add_compound_ent_value_w_path(ent, val, path);
817 }
818
819 /* Copies the firm subgraph referenced by val to const_code_irg and adds
820    the node as constant initialization to ent.
821    The subgraph may not contain control flow operations.
822 void
823 copy_and_add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
824   ir_graph *rem = current_ir_graph;
825
826   assert(get_entity_variability(ent) != variability_uninitialized);
827   current_ir_graph = get_const_code_irg();
828
829   val = copy_const_value(val);
830   add_compound_ent_value(ent, val, member);
831   current_ir_graph = rem;
832   }*/
833
834 /* Copies the value i of the entity to current_block in current_ir_graph.
835 ir_node *
836 copy_compound_ent_value(entity *ent, int pos) {
837   assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
838   return copy_const_value(ent->values[pos+1]);
839   }*/
840
841 entity   *
842 get_compound_ent_value_member(entity *ent, int pos) {
843   compound_graph_path *path;
844   assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
845   path = get_compound_ent_value_path(ent, pos);
846
847   return get_compound_graph_path_node(path, get_compound_graph_path_length(path)-1);
848 }
849
850 void
851 set_compound_ent_value(entity *ent, ir_node *val, entity *member, int pos) {
852   compound_graph_path *path;
853   assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
854   path = get_compound_ent_value_path(ent, pos);
855   set_compound_graph_path_node(path, 0, member);
856   set_compound_ent_value_w_path(ent, val, path, pos);
857 }
858
859 void
860 set_array_entity_values(entity *ent, tarval **values, int num_vals) {
861   int i;
862   ir_graph *rem = current_ir_graph;
863   ir_type *arrtp = get_entity_type(ent);
864   ir_node *val;
865   ir_type *elttp = get_array_element_type(arrtp);
866
867   assert(is_Array_type(arrtp));
868   assert(get_array_n_dimensions(arrtp) == 1);
869   /* One bound is sufficient, the number of constant fields makes the
870      size. */
871   assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
872   assert(get_entity_variability(ent) != variability_uninitialized);
873   current_ir_graph = get_const_code_irg();
874
875   for (i = 0; i < num_vals; i++) {
876     val = new_Const_type(values[i], elttp);
877     add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
878     set_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0, i);
879   }
880   current_ir_graph = rem;
881 }
882
883 int  get_compound_ent_value_offset_bits(entity *ent, int pos) {
884   compound_graph_path *path;
885   int i, path_len;
886   int offset = 0;
887
888   assert(get_type_state(get_entity_type(ent)) == layout_fixed);
889
890   path = get_compound_ent_value_path(ent, pos);
891   path_len = get_compound_graph_path_length(path);
892
893   for (i = 0; i < path_len; ++i) {
894     entity *node = get_compound_graph_path_node(path, i);
895     ir_type *node_tp = get_entity_type(node);
896     ir_type *owner_tp = get_entity_owner(node);
897     if (is_Array_type(owner_tp)) {
898       int size  = get_type_size_bits(node_tp);
899       int align = get_type_alignment_bits(node_tp);
900       if (size < align)
901         size = align;
902       else {
903         assert(size % align == 0);
904         /* ansonsten aufrunden */
905       }
906       offset += size * get_compound_graph_path_array_index(path, i);
907     } else {
908       offset += get_entity_offset_bits(node);
909     }
910   }
911   return offset;
912 }
913
914 int  get_compound_ent_value_offset_bytes(entity *ent, int pos) {
915   int offset = get_compound_ent_value_offset_bits(ent, pos);
916   assert(offset % 8 == 0);
917   return offset >> 3;
918 }
919
920
921 static void init_index(ir_type *arr) {
922   int init;
923   int dim = 0;
924
925   assert(get_array_n_dimensions(arr) == 1);
926
927   if (has_array_lower_bound(arr, dim))
928     init = get_array_lower_bound_int(arr, 0) -1;
929   else
930     init = get_array_upper_bound_int(arr, 0) +1;
931
932   set_entity_link(get_array_element_entity(arr), INT_TO_PTR(init));
933 }
934
935
936 static int get_next_index(entity *elem_ent) {
937   ir_type *arr = get_entity_owner(elem_ent);
938   int next;
939   int dim = 0;
940
941   assert(get_array_n_dimensions(arr) == 1);
942
943   if (has_array_lower_bound(arr, dim)) {
944     next = PTR_TO_INT(get_entity_link(elem_ent)) + 1;
945     if (has_array_upper_bound(arr, dim)) {
946       int upper = get_array_upper_bound_int(arr, dim);
947       if (next == upper) next = get_array_lower_bound_int(arr, dim);
948     }
949   } else {
950     next = PTR_TO_INT(get_entity_link(elem_ent)) - 1;
951     if (has_array_lower_bound(arr, dim)) {
952       int upper = get_array_upper_bound_int(arr, dim);
953       if (next == upper) next = get_array_upper_bound_int(arr, dim);
954     }
955   }
956
957   set_entity_link(elem_ent, INT_TO_PTR(next));
958   return next;
959 }
960
961 /* Compute the array indices in compound graph paths of initialized entities.
962  *
963  *  All arrays must have fixed lower and upper bounds.  One array can
964  *  have an open bound.  If there are several open bounds, we do
965  *  nothing.  There must be initializer elements for all array
966  *  elements.  Uses the link field in the array element entities.  The
967  *  array bounds must be representable as ints.
968  *
969  *  (If the bounds are not representable as ints we have to represent
970  *  the indices as firm nodes.  But still we must be able to
971  *  evaluate the index against the upper bound.)
972  */
973 void compute_compound_ent_array_indicees(entity *ent) {
974   ir_type *tp = get_entity_type(ent);
975   int i, n_vals;
976   entity *unknown_bound_entity = NULL;
977
978   if (!is_compound_type(tp) ||
979       (ent->variability == variability_uninitialized)) return ;
980
981   n_vals = get_compound_ent_n_values(ent);
982   if (n_vals == 0) return;
983
984   /* We can not compute the indexes if there is more than one array
985      with an unknown bound.  For this remember the first entity that
986      represents such an array. It could be ent. */
987   if (is_Array_type(tp)) {
988     int dim = 0;
989
990     assert(get_array_n_dimensions(tp) == 1 && "other not implemented");
991     if (!has_array_lower_bound(tp, dim) || !has_array_upper_bound(tp, dim))
992      unknown_bound_entity = ent;
993   }
994
995   /* Initialize the entity links to lower bound -1 and test all path elements
996      for known bounds. */
997   for (i = 0; i < n_vals; ++i) {
998     compound_graph_path *path = get_compound_ent_value_path(ent, i);
999     int j, path_len =  get_compound_graph_path_length(path);
1000     for (j = 0; j < path_len; ++j) {
1001       entity *node = get_compound_graph_path_node(path, j);
1002       ir_type *elem_tp = get_entity_type(node);
1003
1004       if (is_Array_type(elem_tp)) {
1005         int dim = 0;
1006         assert(get_array_n_dimensions(elem_tp) == 1 && "other not implemented");
1007         if (!has_array_lower_bound(elem_tp, dim) || !has_array_upper_bound(elem_tp, dim)) {
1008           if (!unknown_bound_entity) unknown_bound_entity = node;
1009           if (node != unknown_bound_entity) return;
1010         }
1011
1012         init_index(elem_tp);
1013       }
1014     }
1015   }
1016
1017   /* Finally compute the indexes ... */
1018   for (i = 0; i < n_vals; ++i) {
1019     compound_graph_path *path = get_compound_ent_value_path(ent, i);
1020     int j, path_len =  get_compound_graph_path_length(path);
1021     for (j = 0; j < path_len; ++j) {
1022       entity *node = get_compound_graph_path_node(path, j);
1023       ir_type *owner_tp = get_entity_owner(node);
1024       if (is_Array_type(owner_tp))
1025         set_compound_graph_path_array_index (path, j, get_next_index(node));
1026     }
1027   }
1028 }
1029
1030 /** resize: double the allocated buffer */
1031 static int *resize (int *buf, int *size) {
1032   int new_size =  *size * 2;
1033   int *new_buf = xcalloc(new_size, sizeof(new_buf[0]));
1034   memcpy(new_buf, buf, *size);
1035   free(buf);
1036   *size = new_size;
1037   return new_buf;
1038 }
1039
1040 /* We sort the elements by placing them at their bit offset in an
1041    array where each entry represents one bit called permutation.  In
1042    fact, we do not place the values themselves, as we would have to
1043    copy two things, the value and the path.  We only remember the
1044    position in the old order. Each value should have a distinct
1045    position in the permutation.
1046
1047    A second iteration now permutes the actual elements into two
1048    new arrays. */
1049 void sort_compound_ent_values(entity *ent) {
1050   ir_type *tp;
1051   int i, n_vals;
1052   int tp_size;
1053   int size;
1054   int *permutation;
1055
1056   int next;
1057   ir_node **my_values;
1058   compound_graph_path **my_paths;
1059
1060   assert(get_type_state(get_entity_type(ent)) == layout_fixed);
1061
1062   tp      = get_entity_type(ent);
1063   n_vals  = get_compound_ent_n_values(ent);
1064   tp_size = get_type_size_bits(tp);
1065
1066   if (!is_compound_type(tp)                           ||
1067       (ent->variability == variability_uninitialized) ||
1068       (get_type_state(tp) != layout_fixed)            ||
1069       (n_vals == 0)                                     ) return;
1070
1071   /* estimated upper bound for size. Better: use flexible array ... */
1072   size = ((tp_size > (n_vals * 32)) ? tp_size : (n_vals * 32)) * 4;
1073   permutation = xcalloc(size, sizeof(permutation[0]));
1074
1075   for (i = 0; i < n_vals; ++i) {
1076     int pos = get_compound_ent_value_offset_bits(ent, i);
1077     while (pos >= size) {
1078       permutation = resize(permutation, &size);
1079     }
1080     assert(pos < size);
1081     assert(permutation[pos] == 0 && "two values with the same offset");
1082     permutation[pos] = i + 1;         /* We initialized with 0, so we can not distinguish entry 0.
1083                      So inc all entries by one. */
1084     //fprintf(stderr, "i: %d, pos: %d \n", i, pos);
1085   }
1086
1087   next = 0;
1088   my_values = NEW_ARR_F(ir_node *, n_vals);
1089   my_paths  = NEW_ARR_F(compound_graph_path *, n_vals);
1090   for (i = 0; i < size; ++i) {
1091     int pos = permutation[i];
1092     if (pos) {
1093       //fprintf(stderr, "pos: %d i: %d  next %d \n", i, pos, next);
1094       assert(next < n_vals);
1095       pos--;   /* We increased the pos by one */
1096       my_values[next] = get_compound_ent_value     (ent, pos);
1097       my_paths [next] = get_compound_ent_value_path(ent, pos);
1098       next++;
1099     }
1100   }
1101   free(permutation);
1102
1103   DEL_ARR_F(ent->values);
1104   ent->values = my_values;
1105   DEL_ARR_F(ent->val_paths);
1106   ent->val_paths = my_paths;
1107 }
1108
1109 int
1110 (get_entity_offset_bytes)(const entity *ent) {
1111   return _get_entity_offset_bytes(ent);
1112 }
1113
1114 int
1115 (get_entity_offset_bits)(const entity *ent) {
1116   return _get_entity_offset_bits(ent);
1117 }
1118
1119 void
1120 (set_entity_offset_bytes)(entity *ent, int offset) {
1121   _set_entity_offset_bytes(ent, offset);
1122 }
1123
1124 void
1125 (set_entity_offset_bits)(entity *ent, int offset) {
1126   _set_entity_offset_bits(ent, offset);
1127 }
1128
1129 void
1130 add_entity_overwrites(entity *ent, entity *overwritten) {
1131   assert(ent && is_Class_type(get_entity_owner(ent)));
1132   ARR_APP1(entity *, ent->overwrites, overwritten);
1133   ARR_APP1(entity *, overwritten->overwrittenby, ent);
1134 }
1135
1136 int
1137 get_entity_n_overwrites(entity *ent) {
1138   assert(ent && is_Class_type(get_entity_owner(ent)));
1139   return (ARR_LEN(ent->overwrites));
1140 }
1141
1142 int
1143 get_entity_overwrites_index(entity *ent, entity *overwritten) {
1144   int i;
1145   assert(ent && is_Class_type(get_entity_owner(ent)));
1146   for (i = 0; i < get_entity_n_overwrites(ent); i++)
1147     if (get_entity_overwrites(ent, i) == overwritten)
1148       return i;
1149   return -1;
1150 }
1151
1152 entity *
1153 get_entity_overwrites   (entity *ent, int pos) {
1154   assert(ent && is_Class_type(get_entity_owner(ent)));
1155   assert(pos < get_entity_n_overwrites(ent));
1156   return ent->overwrites[pos];
1157 }
1158
1159 void
1160 set_entity_overwrites   (entity *ent, int pos, entity *overwritten) {
1161   assert(ent && is_Class_type(get_entity_owner(ent)));
1162   assert(pos < get_entity_n_overwrites(ent));
1163   ent->overwrites[pos] = overwritten;
1164 }
1165
1166 void
1167 remove_entity_overwrites(entity *ent, entity *overwritten) {
1168   int i;
1169   assert(ent && is_Class_type(get_entity_owner(ent)));
1170   for (i = 0; i < (ARR_LEN (ent->overwrites)); i++)
1171     if (ent->overwrites[i] == overwritten) {
1172       for(; i < (ARR_LEN (ent->overwrites))-1; i++)
1173     ent->overwrites[i] = ent->overwrites[i+1];
1174       ARR_SETLEN(entity*, ent->overwrites, ARR_LEN(ent->overwrites) - 1);
1175       break;
1176     }
1177 }
1178
1179 void
1180 add_entity_overwrittenby   (entity *ent, entity *overwrites) {
1181   assert(ent && is_Class_type(get_entity_owner(ent)));
1182   add_entity_overwrites(overwrites, ent);
1183 }
1184
1185 int
1186 get_entity_n_overwrittenby (entity *ent) {
1187   assert(ent && is_Class_type(get_entity_owner(ent)));
1188   return (ARR_LEN (ent->overwrittenby));
1189 }
1190
1191 int
1192 get_entity_overwrittenby_index(entity *ent, entity *overwrites) {
1193   int i;
1194   assert(ent && is_Class_type(get_entity_owner(ent)));
1195   for (i = 0; i < get_entity_n_overwrittenby(ent); i++)
1196     if (get_entity_overwrittenby(ent, i) == overwrites)
1197       return i;
1198   return -1;
1199 }
1200
1201 entity *
1202 get_entity_overwrittenby   (entity *ent, int pos) {
1203   assert(ent && is_Class_type(get_entity_owner(ent)));
1204   assert(pos < get_entity_n_overwrittenby(ent));
1205   return ent->overwrittenby[pos];
1206 }
1207
1208 void
1209 set_entity_overwrittenby   (entity *ent, int pos, entity *overwrites) {
1210   assert(ent && is_Class_type(get_entity_owner(ent)));
1211   assert(pos < get_entity_n_overwrittenby(ent));
1212   ent->overwrittenby[pos] = overwrites;
1213 }
1214
1215 void    remove_entity_overwrittenby(entity *ent, entity *overwrites) {
1216   int i;
1217   assert(ent  && is_Class_type(get_entity_owner(ent)));
1218   for (i = 0; i < (ARR_LEN (ent->overwrittenby)); i++)
1219     if (ent->overwrittenby[i] == overwrites) {
1220       for(; i < (ARR_LEN (ent->overwrittenby))-1; i++)
1221     ent->overwrittenby[i] = ent->overwrittenby[i+1];
1222       ARR_SETLEN(entity*, ent->overwrittenby, ARR_LEN(ent->overwrittenby) - 1);
1223       break;
1224     }
1225 }
1226
1227 /* A link to store intermediate information */
1228 void *
1229 (get_entity_link)(const entity *ent) {
1230   return _get_entity_link(ent);
1231 }
1232
1233 void
1234 (set_entity_link)(entity *ent, void *l) {
1235   _set_entity_link(ent, l);
1236 }
1237
1238 ir_graph *
1239 (get_entity_irg)(const entity *ent) {
1240   return _get_entity_irg(ent);
1241 }
1242
1243 void
1244 set_entity_irg(entity *ent, ir_graph *irg) {
1245   assert(ent && is_Method_type(get_entity_type(ent)));
1246   /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
1247    * Methode selbst nicht mehr aufgerufen werden kann, die Entität
1248    * aber erhalten bleiben soll?  Wandle die Entitaet in description oder
1249    * inherited um! */
1250   /* assert(irg); */
1251   assert((irg  && ent->peculiarity == peculiarity_existent) ||
1252          (!irg && (ent->peculiarity == peculiarity_existent)
1253           && (ent -> visibility == visibility_external_allocated)) ||
1254          (!irg && ent->peculiarity == peculiarity_description) ||
1255          (!irg && ent->peculiarity == peculiarity_inherited));
1256   ent->irg = irg;
1257 }
1258
1259 int
1260 (is_entity)(const void *thing) {
1261   return _is_entity(thing);
1262 }
1263
1264 int is_atomic_entity(entity *ent) {
1265   ir_type *t = get_entity_type(ent);
1266   assert(ent && ent->kind == k_entity);
1267   return (is_Primitive_type(t) || is_Pointer_type(t) ||
1268       is_Enumeration_type(t) || is_Method_type(t));
1269 }
1270
1271 int is_compound_entity(entity *ent) {
1272   ir_type *t = get_entity_type(ent);
1273   assert(ent && ent->kind == k_entity);
1274   return (is_Class_type(t) || is_Struct_type(t) ||
1275       is_Array_type(t) || is_Union_type(t));
1276 }
1277
1278 /**
1279  * @todo not implemented!!! */
1280 int equal_entity(entity *ent1, entity *ent2) {
1281   fprintf(stderr, " calling unimplemented equal entity!!! \n");
1282   return 1;
1283 }
1284
1285
1286 unsigned long (get_entity_visited)(entity *ent) {
1287   return _get_entity_visited(ent);
1288 }
1289
1290 void (set_entity_visited)(entity *ent, unsigned long num) {
1291   _set_entity_visited(ent, num);
1292 }
1293
1294 /* Sets visited field in entity to entity_visited. */
1295 void (mark_entity_visited)(entity *ent) {
1296   _mark_entity_visited(ent);
1297 }
1298
1299 int (entity_visited)(entity *ent) {
1300   return _entity_visited(ent);
1301 }
1302
1303 int (entity_not_visited)(entity *ent) {
1304   return _entity_not_visited(ent);
1305 }
1306
1307 /* Returns the mask of the additional entity properties. */
1308 unsigned get_entity_additional_properties(entity *ent) {
1309   ir_graph *irg;
1310
1311   assert(is_Method_type(get_entity_type(ent)));
1312
1313   /* first check, if the graph has additional properties */
1314   irg = get_entity_irg(ent);
1315
1316   if (irg)
1317     return get_irg_additional_properties(irg);
1318
1319   if (ent->irg_add_properties & mtp_property_inherited)
1320     return get_method_additional_properties(get_entity_type(ent));
1321
1322   return ent->irg_add_properties;
1323 }
1324
1325 /* Sets the mask of the additional graph properties. */
1326 void set_entity_additional_properties(entity *ent, unsigned property_mask)
1327 {
1328   ir_graph *irg;
1329
1330   assert(is_Method_type(get_entity_type(ent)));
1331
1332   /* first check, if the graph exists */
1333   irg = get_entity_irg(ent);
1334   if (irg)
1335     set_irg_additional_properties(irg, property_mask);
1336   else {
1337     /* do not allow to set the mtp_property_inherited flag or
1338      * the automatic inheritance of flags will not work */
1339     ent->irg_add_properties = property_mask & ~mtp_property_inherited;
1340   }
1341 }
1342
1343 /* Sets one additional graph property. */
1344 void set_entity_additional_property(entity *ent, mtp_additional_property flag)
1345 {
1346   ir_graph *irg;
1347
1348   assert(is_Method_type(get_entity_type(ent)));
1349
1350   /* first check, if the graph exists */
1351   irg = get_entity_irg(ent);
1352   if (irg)
1353     set_irg_additional_property(irg, flag);
1354   else {
1355     unsigned mask = ent->irg_add_properties;
1356
1357     if (mask & mtp_property_inherited)
1358       mask = get_method_additional_properties(get_entity_type(ent));
1359
1360     /* do not allow to set the mtp_property_inherited flag or
1361      * the automatic inheritance of flags will not work */
1362     ent->irg_add_properties = mask | (flag & ~mtp_property_inherited);
1363   }
1364 }
1365
1366 void firm_init_entity(void)
1367 {
1368   symconst_symbol sym;
1369
1370   assert(firm_unknown_type && "Call init_type() before firm_init_entity()!");
1371   assert(!unknown_entity && "Call firm_init_entity() only once!");
1372   unknown_entity = new_rd_entity(NULL, firm_unknown_type, new_id_from_str(UNKNOWN_ENTITY_NAME), firm_unknown_type);
1373   set_entity_visibility(unknown_entity, visibility_external_allocated);
1374   set_entity_ld_ident(unknown_entity, get_entity_ident(unknown_entity));
1375
1376   sym.entity_p = unknown_entity;
1377   current_ir_graph = get_const_code_irg();
1378   unknown_entity->value = new_SymConst(sym, symconst_addr_ent);
1379 }