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