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