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