entity offset is now in bits
[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     res->variability = variability_constant;
86     rem = current_ir_graph;
87     current_ir_graph = get_const_code_irg();
88     res->value = new_Const(mode_P_mach, new_tarval_from_entity(res, mode_P_mach));
89     current_ir_graph = rem;
90   } else {
91     res->variability = variability_uninitialized;
92     res->value  = NULL;
93     res->values = NULL;
94     res->val_paths = NULL;
95   }
96   res->peculiarity   = peculiarity_existent;
97   res->volatility    = volatility_non_volatile;
98   res->stickyness    = stickyness_unsticky;
99   res->ld_name       = NULL;
100   if (is_class_type(owner)) {
101     res->overwrites    = NEW_ARR_F(entity *, 0);
102     res->overwrittenby = NEW_ARR_F(entity *, 0);
103   } else {
104     res->overwrites    = NULL;
105     res->overwrittenby = NULL;
106   }
107   res->irg = NULL;
108
109 #ifdef DEBUG_libfirm
110   res->nr = get_irp_new_node_nr();
111 #endif
112
113   res->visit = 0;
114
115   /* Remember entity in it's owner. */
116   insert_entity_in_owner (res);
117   return res;
118 }
119 entity *
120 new_d_entity (type *owner, ident *name, type *type, dbg_info *db) {
121   entity *res = new_entity(owner, name, type);
122   set_entity_dbg_info(res, db);
123   return res;
124 }
125
126 void    free_compound_graph_path (compound_graph_path *gr);
127 int     is_compound_graph_path(void *thing);
128 int     get_compound_graph_path_length(compound_graph_path *gr);
129 entity *get_compound_graph_path_node(compound_graph_path *gr, int pos);
130 int     get_compound_ent_n_values(entity *ent);
131
132 static void free_entity_attrs(entity *ent) {
133   int i;
134   if (get_type_tpop(get_entity_owner(ent)) == type_class) {
135     DEL_ARR_F(ent->overwrites);    ent->overwrites = NULL;
136     DEL_ARR_F(ent->overwrittenby); ent->overwrittenby = NULL;
137   } else {
138     assert(ent->overwrites == NULL);
139     assert(ent->overwrittenby == NULL);
140   }
141   /* if (ent->values) DEL_ARR_F(ent->values); *//* @@@ warum nich? */
142   if (ent->val_paths) {
143     if (is_compound_entity(ent))
144       for (i = 0; i < get_compound_ent_n_values(ent); i++)
145     if (ent->val_paths[i]) ;
146     /* free_compound_graph_path(ent->val_paths[i]) ;  * @@@ warum nich? */
147     /* Geht nich: wird mehrfach verwendet!!! ==> mehrfach frei gegeben. */
148     /* DEL_ARR_F(ent->val_paths); */
149   }
150   ent->val_paths = NULL;
151   ent->values = NULL;
152 }
153
154 entity *
155 copy_entity_own (entity *old, type *new_owner) {
156   entity *new;
157   assert(old && old->kind == k_entity);
158   assert_legal_owner_of_ent(new_owner);
159
160   if (old->owner == new_owner) return old;
161   new = (entity *) xmalloc (sizeof (entity));
162   memcpy (new, old, sizeof (entity));
163   new->owner = new_owner;
164   if (is_class_type(new_owner)) {
165     new->overwrites    = NEW_ARR_F(entity *, 0);
166     new->overwrittenby = NEW_ARR_F(entity *, 0);
167   }
168 #ifdef DEBUG_libfirm
169   new->nr = get_irp_new_node_nr();
170 #endif
171
172   insert_entity_in_owner (new);
173
174   return new;
175 }
176
177 entity *
178 copy_entity_name (entity *old, ident *new_name) {
179   entity *new;
180   assert(old && old->kind == k_entity);
181
182   if (old->name == new_name) return old;
183   new = (entity *) xmalloc (sizeof (entity));
184   memcpy (new, old, sizeof (entity));
185   new->name = new_name;
186   new->ld_name = NULL;
187   if (is_class_type(new->owner)) {
188     new->overwrites    = DUP_ARR_F(entity *, old->overwrites);
189     new->overwrittenby = DUP_ARR_F(entity *, old->overwrittenby);
190   }
191 #ifdef DEBUG_libfirm
192   new->nr = get_irp_new_node_nr();
193 #endif
194
195   insert_entity_in_owner (new);
196
197   return new;
198 }
199
200
201 void
202 free_entity (entity *ent) {
203   assert(ent && ent->kind == k_entity);
204   free_tarval_entity(ent);
205   free_entity_attrs(ent);
206   ent->kind = k_BAD;
207   free(ent);
208 }
209
210 /* Outputs a unique number for this node */
211 long
212 get_entity_nr(entity *ent) {
213   assert(ent && ent->kind == k_entity);
214 #ifdef DEBUG_libfirm
215   return ent->nr;
216 #else
217   return 0;
218 #endif
219 }
220
221 const char *
222 (get_entity_name)(entity *ent) {
223   return __get_entity_name(ent);
224 }
225
226 ident *
227 (get_entity_ident)(entity *ent) {
228   return get_entity_ident(ent);
229 }
230
231 /*
232 void   set_entitye_ld_name  (entity *, char *ld_name);
233 void   set_entity_ld_ident (entity *, ident *ld_ident);
234 */
235
236 type *
237 (get_entity_owner)(entity *ent) {
238   return __get_entity_owner(ent);
239 }
240
241 void
242 set_entity_owner (entity *ent, type *owner) {
243   assert(ent && ent->kind == k_entity);
244   assert_legal_owner_of_ent(owner);
245   ent->owner = owner;
246 }
247
248 void   /* should this go into type.c? */
249 assert_legal_owner_of_ent(type *owner) {
250   assert(get_type_tpop_code(owner) == tpo_class ||
251           get_type_tpop_code(owner) == tpo_union ||
252           get_type_tpop_code(owner) == tpo_struct ||
253       get_type_tpop_code(owner) == tpo_array);   /* Yes, array has an entity
254                             -- to select fields! */
255 }
256
257 ident *
258 (get_entity_ld_ident)(entity *ent) {
259   return __get_entity_ld_ident(ent);
260 }
261
262 void
263 (set_entity_ld_ident)(entity *ent, ident *ld_ident) {
264    __set_entity_ld_ident(ent, ld_ident);
265 }
266
267 const char *
268 (get_entity_ld_name)(entity *ent) {
269   return __get_entity_ld_name(ent);
270 }
271
272 type *
273 (get_entity_type)(entity *ent) {
274   return __get_entity_type(ent);
275 }
276
277 void
278 (set_entity_type)(entity *ent, type *type) {
279   __set_entity_type(ent, type);
280 }
281
282 ent_allocation
283 (get_entity_allocation)(entity *ent) {
284   return __get_entity_allocation(ent);
285 }
286
287 void
288 (set_entity_allocation)(entity *ent, ent_allocation al) {
289   __set_entity_allocation(ent, al);
290 }
291
292 /* return the name of the visibility */
293 const char *get_allocation_name(ent_allocation all)
294 {
295 #define X(a)    case a: return #a
296   switch (all) {
297     X(allocation_automatic);
298     X(allocation_parameter);
299     X(allocation_dynamic);
300     X(allocation_static);
301     default: return "BAD VALUE";
302   }
303 #undef X
304 }
305
306
307 ent_visibility
308 (get_entity_visibility)(entity *ent) {
309   return __get_entity_visibility(ent);
310 }
311
312 void
313 set_entity_visibility (entity *ent, ent_visibility vis) {
314   assert(ent && ent->kind == k_entity);
315   if (vis != visibility_local)
316     assert((ent->allocation == allocation_static) ||
317        (ent->allocation == allocation_automatic));
318   /* @@@ Test that the owner type is not local, but how??
319          && get_class_visibility(get_entity_owner(ent)) != local));*/
320   ent->visibility = vis;
321 }
322
323 /* return the name of the visibility */
324 const char *get_visibility_name(ent_visibility vis)
325 {
326 #define X(a)    case a: return #a
327   switch (vis) {
328     X(visibility_local);
329     X(visibility_external_visible);
330     X(visibility_external_allocated);
331     default: return "BAD VALUE";
332   }
333 #undef X
334 }
335
336 ent_variability
337 (get_entity_variability)(entity *ent) {
338   return __get_entity_variability(ent);
339 }
340
341 void
342 set_entity_variability (entity *ent, ent_variability var)
343 {
344   assert(ent && ent->kind == k_entity);
345   if (var == variability_part_constant)
346     assert(is_class_type(ent->type) || is_struct_type(ent->type));
347
348   if ((is_compound_type(ent->type)) &&
349       (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
350     /* Allocate datastructures for constant values */
351     ent->values    = NEW_ARR_F(ir_node *, 0);
352     ent->val_paths = NEW_ARR_F(compound_graph_path *, 0);
353   }
354
355   if ((is_compound_type(ent->type)) &&
356       (var == variability_uninitialized) && (ent->variability != variability_uninitialized)) {
357     /* Free datastructures for constant values */
358     DEL_ARR_F(ent->values);    ent->values    = NULL;
359     DEL_ARR_F(ent->val_paths); ent->val_paths = NULL;
360   }
361   ent->variability = var;
362 }
363
364 /* return the name of the variablity */
365 const char *get_variability_name(ent_variability var)
366 {
367 #define X(a)    case a: return #a
368   switch (var) {
369     X(variability_uninitialized);
370     X(variability_initialized);
371     X(variability_part_constant);
372     X(variability_constant);
373     default: return "BAD VALUE";
374   }
375 #undef X
376 }
377
378 ent_volatility
379 (get_entity_volatility)(entity *ent) {
380   return __get_entity_volatility(ent);
381 }
382
383 void
384 (set_entity_volatility)(entity *ent, ent_volatility vol) {
385   __set_entity_volatility(ent, vol);
386 }
387
388 /* return the name of the volatility */
389 const char *get_volatility_name(ent_volatility var)
390 {
391 #define X(a)    case a: return #a
392   switch (var) {
393     X(volatility_non_volatile);
394     X(volatility_is_volatile);
395     default: return "BAD VALUE";
396   }
397 #undef X
398 }
399
400 peculiarity
401 (get_entity_peculiarity)(entity *ent) {
402   return __get_entity_peculiarity(ent);
403 }
404
405 void
406 (set_entity_peculiarity)(entity *ent, peculiarity pec) {
407   __set_entity_peculiarity(ent, pec);
408 }
409
410 /* return the name of the peculiarity */
411 const char *get_peculiarity_name(peculiarity var)
412 {
413 #define X(a)    case a: return #a
414   switch (var) {
415     X(peculiarity_description);
416     X(peculiarity_inherited);
417     X(peculiarity_existent);
418     default: return "BAD VALUE";
419   }
420 #undef X
421 }
422
423 /* Get the entity's stickyness */
424 ent_stickyness
425 (get_entity_stickyness)(entity *ent) {
426   return __get_entity_stickyness(ent);
427 }
428
429 /* Set the entity's stickyness */
430 void
431 (set_entity_stickyness)(entity *ent, ent_stickyness stickyness) {
432   __set_entity_stickyness(ent, stickyness);
433 }
434
435 /* Set has no effect for existent entities of type method. */
436 ir_node *
437 get_atomic_ent_value(entity *ent)
438 {
439   assert(ent && is_atomic_entity(ent));
440   assert(ent->variability != variability_uninitialized);
441   return skip_Id (ent->value);
442 }
443
444 void
445 set_atomic_ent_value(entity *ent, ir_node *val) {
446   assert(is_atomic_entity(ent) && (ent->variability != variability_uninitialized));
447   if (is_method_type(ent->type) && (ent->peculiarity == peculiarity_existent))
448     return;
449   ent->value = val;
450 }
451
452 /* Returns true if the the node is representable as code on
453  *  const_code_irg. */
454 int is_irn_const_expression(ir_node *n) {
455   ir_mode *m;
456
457   m = get_irn_mode(n);
458   switch(get_irn_opcode(n)) {
459   case iro_Const:
460   case iro_SymConst:
461   case iro_Unknown:
462     return true; break;
463   case iro_Add:
464   case iro_Sub:
465   case iro_Mul:
466   case iro_And:
467   case iro_Or:
468   case iro_Eor:
469     if (is_irn_const_expression(get_binop_left(n)))
470       return is_irn_const_expression(get_binop_right(n));
471   case iro_Conv:
472   case iro_Cast:
473     return is_irn_const_expression(get_irn_n(n, 0));
474   default:
475     return false;
476     break;
477   }
478   return false;
479 }
480
481
482 ir_node *copy_const_value(ir_node *n) {
483   ir_node *nn;
484   ir_mode *m;
485
486   m = get_irn_mode(n);
487   switch(get_irn_opcode(n)) {
488   case iro_Const:
489     nn = new_Const(m, get_Const_tarval(n)); break;
490   case iro_SymConst:
491     nn = new_SymConst(get_SymConst_type_or_id(n), get_SymConst_kind(n)); break;
492   case iro_Add:
493     nn = new_Add(copy_const_value(get_Add_left(n)),
494          copy_const_value(get_Add_right(n)), m); break;
495   case iro_Sub:
496     nn = new_Sub(copy_const_value(get_Sub_left(n)),
497          copy_const_value(get_Sub_right(n)), m); break;
498   case iro_Mul:
499     nn = new_Mul(copy_const_value(get_Mul_left(n)),
500          copy_const_value(get_Mul_right(n)), m); break;
501   case iro_And:
502     nn = new_And(copy_const_value(get_And_left(n)),
503          copy_const_value(get_And_right(n)), m); break;
504   case iro_Or:
505     nn = new_Or(copy_const_value(get_Or_left(n)),
506          copy_const_value(get_Or_right(n)), m); break;
507   case iro_Eor:
508     nn = new_Eor(copy_const_value(get_Eor_left(n)),
509          copy_const_value(get_Eor_right(n)), m); break;
510   case iro_Cast:
511     nn = new_Cast(copy_const_value(get_Cast_op(n)), get_Cast_type(n)); break;
512   case iro_Conv:
513     nn = new_Conv(copy_const_value(get_Conv_op(n)), m); break;
514   case iro_Unknown:
515     nn = new_Unknown(m); break;
516   default:
517     DDMN(n);
518     assert(0 && "opdope invalid or not implemented");
519     nn = NULL;
520     break;
521   }
522   return nn;
523 }
524
525 compound_graph_path *
526 new_compound_graph_path(type *tp, int length) {
527   compound_graph_path *res;
528   assert(is_type(tp) && is_compound_type(tp));
529   assert(length > 0);
530
531   res = (compound_graph_path *) malloc (sizeof(compound_graph_path) + (length-1) * sizeof(entity *));
532   res->kind = k_ir_compound_graph_path;
533   res->tp = tp;
534   res->len = length;
535   memset(res->nodes, 0, sizeof(entity *) * length);
536   return res;
537 }
538
539 void
540 free_compound_graph_path (compound_graph_path *gr) {
541   assert(gr && is_compound_graph_path(gr));
542   gr->kind = k_BAD;
543   free(gr);
544 }
545
546 int
547 is_compound_graph_path(void *thing) {
548   return (get_kind(thing) == k_ir_compound_graph_path);
549 }
550
551 /* checks whether nodes 0..pos are correct (all lie on a path.) */
552 /* @@@ not implemented */
553 int is_proper_compound_graph_path(compound_graph_path *gr, int pos) {
554   int i;
555   entity *node;
556   type *owner = gr->tp;
557   for (i = 0; i <= pos; i++) {
558     node = get_compound_graph_path_node(gr, i);
559     if (get_entity_owner(node) != owner) return false;
560     owner = get_entity_type(node);
561   }
562   if (pos == get_compound_graph_path_length(gr))
563     if (!is_atomic_type(owner)) return false;
564   return true;
565 }
566
567 int
568 get_compound_graph_path_length(compound_graph_path *gr) {
569   assert(gr && is_compound_graph_path(gr));
570   return gr->len;
571 }
572
573 entity *
574 get_compound_graph_path_node(compound_graph_path *gr, int pos) {
575   assert(gr && is_compound_graph_path(gr));
576   assert(pos >= 0 && pos < gr->len);
577   return gr->nodes[pos];
578 }
579
580 void
581 set_compound_graph_path_node(compound_graph_path *gr, int pos, entity *node) {
582   assert(gr && is_compound_graph_path(gr));
583   assert(pos >= 0 && pos < gr->len);
584   assert(is_entity(node));
585   gr->nodes[pos] = node;
586   assert(is_proper_compound_graph_path(gr, pos));
587 }
588
589 /* A value of a compound entity is a pair of value and the corresponding path to a member of
590    the compound. */
591 void
592 add_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path) {
593   assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
594   ARR_APP1 (ir_node *, ent->values, val);
595   ARR_APP1 (compound_graph_path *, ent->val_paths, path);
596 }
597
598 void
599 set_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path, int pos) {
600   assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
601   ent->values[pos] = val;
602   ent->val_paths[pos] = path;
603 }
604
605 int
606 get_compound_ent_n_values(entity *ent) {
607   assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
608   return (ARR_LEN (ent->values));
609 }
610
611 ir_node  *
612 get_compound_ent_value(entity *ent, int pos) {
613   assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
614   return ent->values[pos];
615 }
616
617 compound_graph_path *
618 get_compound_ent_value_path(entity *ent, int pos) {
619   assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
620   return ent->val_paths[pos];
621 }
622
623 void
624 remove_compound_ent_value(entity *ent, entity *value_ent) {
625   int i;
626   assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
627   for (i = 0; i < (ARR_LEN (ent->val_paths)); i++) {
628     compound_graph_path *path = ent->val_paths[i];
629     if (path->nodes[path->len-1] == value_ent) {
630       for(; i < (ARR_LEN (ent->val_paths))-1; i++) {
631     ent->val_paths[i] = ent->val_paths[i+1];
632     ent->values[i]   = ent->values[i+1];
633       }
634       ARR_SETLEN(entity*,  ent->val_paths, ARR_LEN(ent->val_paths) - 1);
635       ARR_SETLEN(ir_node*, ent->values,    ARR_LEN(ent->values)    - 1);
636       break;
637     }
638   }
639 }
640
641 void
642 add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
643   compound_graph_path *path;
644   assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
645   path = new_compound_graph_path(get_entity_owner(ent), 1);
646   path->nodes[0] = member;
647   add_compound_ent_value_w_path(ent, val, path);
648 }
649
650 /* Copies the firm subgraph referenced by val to const_code_irg and adds
651    the node as constant initialization to ent.
652    The subgraph may not contain control flow operations.
653 void
654 copy_and_add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
655   ir_graph *rem = current_ir_graph;
656
657   assert(get_entity_variability(ent) != variability_uninitialized);
658   current_ir_graph = get_const_code_irg();
659
660   val = copy_const_value(val);
661   add_compound_ent_value(ent, val, member);
662   current_ir_graph = rem;
663   }*/
664
665 /* Copies the value i of the entity to current_block in current_ir_graph.
666 ir_node *
667 copy_compound_ent_value(entity *ent, int pos) {
668   assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
669   return copy_const_value(ent->values[pos+1]);
670   }*/
671
672 entity   *
673 get_compound_ent_value_member(entity *ent, int pos) {
674   compound_graph_path *path;
675   assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
676   path = get_compound_ent_value_path(ent, pos);
677
678   return get_compound_graph_path_node(path, get_compound_graph_path_length(path)-1);
679 }
680
681 void
682 set_compound_ent_value(entity *ent, ir_node *val, entity *member, int pos) {
683   compound_graph_path *path;
684   assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
685   path = get_compound_ent_value_path(ent, pos);
686   set_compound_graph_path_node(path, 0, member);
687   set_compound_ent_value_w_path(ent, val, path, pos);
688 }
689
690 void
691 set_array_entity_values(entity *ent, tarval **values, int num_vals) {
692   int i;
693   ir_graph *rem = current_ir_graph;
694   type *arrtp = get_entity_type(ent);
695   ir_node *val;
696
697   assert(is_array_type(arrtp));
698   assert(get_array_n_dimensions(arrtp) == 1);
699   /* One bound is sufficient, the nunmber of constant fields makes the
700      size. */
701   assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
702   assert(get_entity_variability(ent) != variability_uninitialized);
703   current_ir_graph = get_const_code_irg();
704
705   for (i = 0; i < num_vals; i++) {
706     val = new_Const(get_tarval_mode (values[i]), values[i]);
707     add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
708   }
709   current_ir_graph = rem;
710 }
711
712 int
713 (get_entity_offset_bytes)(entity *ent) {
714   return __get_entity_offset_bytes(ent);
715 }
716
717 int
718 (get_entity_offset_bits)(entity *ent) {
719   return __get_entity_offset_bits(ent);
720 }
721
722 void
723 (set_entity_offset_bytes)(entity *ent, int offset) {
724   __set_entity_offset_bytes(ent, offset);
725 }
726
727 void
728 (set_entity_offset_bits)(entity *ent, int offset) {
729   __set_entity_offset_bits(ent, offset);
730 }
731
732 void
733 add_entity_overwrites(entity *ent, entity *overwritten) {
734   assert(ent && is_class_type(get_entity_owner(ent)));
735   ARR_APP1(entity *, ent->overwrites, overwritten);
736   ARR_APP1(entity *, overwritten->overwrittenby, ent);
737 }
738
739 int
740 get_entity_n_overwrites(entity *ent) {
741   assert(ent && is_class_type(get_entity_owner(ent)));
742   return (ARR_LEN(ent->overwrites));
743 }
744
745 int
746 get_entity_overwrites_index(entity *ent, entity *overwritten) {
747   int i;
748   assert(ent && is_class_type(get_entity_owner(ent)));
749   for (i = 0; i < get_entity_n_overwrites(ent); i++)
750     if (get_entity_overwrites(ent, i) == overwritten)
751       return i;
752   return -1;
753 }
754
755 entity *
756 get_entity_overwrites   (entity *ent, int pos) {
757   assert(ent && is_class_type(get_entity_owner(ent)));
758   assert(pos < get_entity_n_overwrites(ent));
759   return ent->overwrites[pos];
760 }
761
762 void
763 set_entity_overwrites   (entity *ent, int pos, entity *overwritten) {
764   assert(ent && is_class_type(get_entity_owner(ent)));
765   assert(pos < get_entity_n_overwrites(ent));
766   ent->overwrites[pos] = overwritten;
767 }
768
769 void
770 remove_entity_overwrites(entity *ent, entity *overwritten) {
771   int i;
772   assert(ent && is_class_type(get_entity_owner(ent)));
773   for (i = 0; i < (ARR_LEN (ent->overwrites)); i++)
774     if (ent->overwrites[i] == overwritten) {
775       for(; i < (ARR_LEN (ent->overwrites))-1; i++)
776     ent->overwrites[i] = ent->overwrites[i+1];
777       ARR_SETLEN(entity*, ent->overwrites, ARR_LEN(ent->overwrites) - 1);
778       break;
779     }
780 }
781
782 void
783 add_entity_overwrittenby   (entity *ent, entity *overwrites) {
784   assert(ent && is_class_type(get_entity_owner(ent)));
785   add_entity_overwrites(overwrites, ent);
786 }
787
788 int
789 get_entity_n_overwrittenby (entity *ent) {
790   assert(ent && is_class_type(get_entity_owner(ent)));
791   return (ARR_LEN (ent->overwrittenby));
792 }
793
794 int
795 get_entity_overwrittenby_index(entity *ent, entity *overwrites) {
796   int i;
797   assert(ent && is_class_type(get_entity_owner(ent)));
798   for (i = 0; i < get_entity_n_overwrittenby(ent); i++)
799     if (get_entity_overwrittenby(ent, i) == overwrites)
800       return i;
801   return -1;
802 }
803
804 entity *
805 get_entity_overwrittenby   (entity *ent, int pos) {
806   assert(ent && is_class_type(get_entity_owner(ent)));
807   assert(pos < get_entity_n_overwrittenby(ent));
808   return ent->overwrittenby[pos];
809 }
810
811 void
812 set_entity_overwrittenby   (entity *ent, int pos, entity *overwrites) {
813   assert(ent && is_class_type(get_entity_owner(ent)));
814   assert(pos < get_entity_n_overwrittenby(ent));
815   ent->overwrittenby[pos] = overwrites;
816 }
817
818 void    remove_entity_overwrittenby(entity *ent, entity *overwrites) {
819   int i;
820   assert(ent  && is_class_type(get_entity_owner(ent)));
821   for (i = 0; i < (ARR_LEN (ent->overwrittenby)); i++)
822     if (ent->overwrittenby[i] == overwrites) {
823       for(; i < (ARR_LEN (ent->overwrittenby))-1; i++)
824     ent->overwrittenby[i] = ent->overwrittenby[i+1];
825       ARR_SETLEN(entity*, ent->overwrittenby, ARR_LEN(ent->overwrittenby) - 1);
826       break;
827     }
828 }
829
830 /* A link to store intermediate information */
831 void *
832 (get_entity_link)(entity *ent) {
833   return __get_entity_link(ent);
834 }
835
836 void
837 (set_entity_link)(entity *ent, void *l) {
838   __set_entity_link(ent, l);
839 }
840
841 ir_graph *
842 (get_entity_irg)(entity *ent) {
843   return __get_entity_irg(ent);
844 }
845
846 void
847 set_entity_irg(entity *ent, ir_graph *irg) {
848   assert(ent && is_method_type(get_entity_type(ent)));
849   /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
850    * Methode selbst nicht mehr aufgerufen werden kann, die Entität
851    * aber erhalten bleiben soll. */
852   /* assert(irg); */
853   assert((irg  && ent->peculiarity == peculiarity_existent) ||
854          (!irg && ent->peculiarity == peculiarity_description) ||
855          (!irg && ent->peculiarity == peculiarity_inherited));
856   ent->irg = irg;
857 }
858
859 int
860 (is_entity)(void *thing) {
861   return __is_entity(thing);
862 }
863
864 int is_atomic_entity(entity *ent) {
865   type* t = get_entity_type(ent);
866   assert(ent && ent->kind == k_entity);
867   return (is_primitive_type(t) || is_pointer_type(t) ||
868       is_enumeration_type(t) || is_method_type(t));
869 }
870
871 int is_compound_entity(entity *ent) {
872   type* t = get_entity_type(ent);
873   assert(ent && ent->kind == k_entity);
874   return (is_class_type(t) || is_struct_type(t) ||
875       is_array_type(t) || is_union_type(t));
876 }
877
878 /**
879  * @todo not implemnted!!! */
880 bool equal_entity(entity *ent1, entity *ent2) {
881   printf(" calling unimplemented equal entity!!! \n");
882   return true;
883 }
884
885
886 unsigned long get_entity_visited(entity *ent) {
887   assert(ent && ent->kind == k_entity);
888   return ent->visit;
889 }
890 void        set_entity_visited(entity *ent, unsigned long num) {
891   assert(ent && ent->kind == k_entity);
892   ent->visit = num;
893 }
894 /* Sets visited field in entity to entity_visited. */
895 void        mark_entity_visited(entity *ent) {
896   assert(ent && ent->kind == k_entity);
897   ent->visit = type_visited;
898 }
899
900
901 bool entity_visited(entity *ent) {
902   assert(ent && ent->kind == k_entity);
903   return get_entity_visited(ent) >= type_visited;
904 }
905
906 bool entity_not_visited(entity *ent) {
907   assert(ent && ent->kind == k_entity);
908   return get_entity_visited(ent) < type_visited;
909 }
910
911 /* Need two routines because I want to assert the result. */
912 static entity *resolve_ent_polymorphy2 (type *dynamic_class, entity* static_ent) {
913   int i, n_overwrittenby;
914   entity *res = NULL;
915
916   if (get_entity_owner(static_ent) == dynamic_class) return static_ent;
917
918   n_overwrittenby = get_entity_n_overwrittenby(static_ent);
919   for (i = 0; i < n_overwrittenby; ++i) {
920     res = resolve_ent_polymorphy2(dynamic_class, get_entity_overwrittenby(static_ent, i));
921     if (res) break;
922   }
923
924   return res;
925 }
926
927 /* Returns the dynamically referenced entity if the static entity and the
928  *  dynamic type are given.
929  *  Search downwards in overwritten tree. */
930 entity *resolve_ent_polymorphy(type *dynamic_class, entity* static_ent) {
931   entity *res;
932   assert(static_ent && static_ent->kind == k_entity);
933
934   res = resolve_ent_polymorphy2(dynamic_class, static_ent);
935   if (!res) {
936     printf(" Could not find entity "); DDME(static_ent);
937     printf("  in "); DDMT(dynamic_class);
938     printf("\n");
939     dump_entity(static_ent);
940     dump_type(get_entity_owner(static_ent));
941     dump_type(dynamic_class);
942
943   }
944   assert(res);
945   return res;
946 }
947
948
949
950 /*******************************************************************/
951 /** Debug aides                                                   **/
952 /*******************************************************************/
953
954
955 #if 1 || DEBUG_libfirm
956 int dump_node_opcode(FILE *F, ir_node *n); /* from irdump.c */
957
958 #define X(a)    case a: printf(#a); break
959 void dump_entity (entity *ent) {
960   int i, j;
961   type *owner = get_entity_owner(ent);
962   type *type  = get_entity_type(ent);
963   assert(ent && ent->kind == k_entity);
964   printf("entity %s (%ld)\n", get_entity_name(ent), get_entity_nr(ent));
965   printf("  type:  %s (%ld)\n", get_type_name(type),  get_type_nr(type));
966   printf("  owner: %s (%ld)\n", get_type_name(owner), get_type_nr(owner));
967
968   if (get_entity_n_overwrites(ent) > 0) {
969     printf ("  overwrites:\n");
970     for (i = 0; i < get_entity_n_overwrites(ent); ++i) {
971       entity *ov = get_entity_overwrites(ent, i);
972       printf("    %d: %s of class %s\n", i, get_entity_name(ov), get_type_name(get_entity_owner(ov)));
973     }
974   } else {
975     printf("  Does not overwrite other entities. \n");
976   }
977   if (get_entity_n_overwrittenby(ent) > 0) {
978     printf ("  overwritten by:\n");
979     for (i = 0; i < get_entity_n_overwrittenby(ent); ++i) {
980       entity *ov = get_entity_overwrittenby(ent, i);
981       printf("    %d: %s of class %s\n", i, get_entity_name(ov), get_type_name(get_entity_owner(ov)));
982     }
983   } else {
984     printf("  Is not overwriten by other entities. \n");
985   }
986
987   printf ("  allocation:  ");
988   switch (get_entity_allocation(ent)) {
989     X(allocation_dynamic);
990     X(allocation_automatic);
991     X(allocation_static);
992     X(allocation_parameter);
993   }
994
995   printf ("\n  visibility:  ");
996   switch (get_entity_visibility(ent)) {
997     X(visibility_local);
998     X(visibility_external_visible);
999     X(visibility_external_allocated);
1000   }
1001
1002   printf ("\n  variability: ");
1003   switch (get_entity_variability(ent)) {
1004     X(variability_uninitialized);
1005     X(variability_initialized);
1006     X(variability_part_constant);
1007     X(variability_constant);
1008   }
1009
1010   if (get_entity_variability(ent) != variability_uninitialized) {
1011     if (is_atomic_entity(ent)) {
1012       printf("\n  atomic value: ");
1013       dump_node_opcode(stdout, get_atomic_ent_value(ent));
1014     } else {
1015       printf("\n  compound values:");
1016       for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
1017     compound_graph_path *path = get_compound_ent_value_path(ent, i);
1018     entity *ent0 = get_compound_graph_path_node(path, 0);
1019     printf("\n    %3d %s", get_entity_offset_bits(ent0), get_entity_name(ent0));
1020     for (j = 1; j < get_compound_graph_path_length(path); ++j)
1021       printf(".%s", get_entity_name(get_compound_graph_path_node(path, j)));
1022     printf("\t = ");
1023     dump_node_opcode(stdout, get_compound_ent_value(ent, i));
1024       }
1025     }
1026   }
1027
1028   printf ("\n  volatility:  ");
1029   switch (get_entity_volatility(ent)) {
1030     X(volatility_non_volatile);
1031     X(volatility_is_volatile);
1032   }
1033
1034   printf("\n  peculiarity: %s", get_peculiarity_string(get_entity_peculiarity(ent)));
1035   printf("\n  ld_name: %s", ent->ld_name ? get_entity_ld_name(ent) : "no yet set");
1036   printf("\n  offset:  %d", get_entity_offset_bits(ent));
1037   if (is_method_type(get_entity_type(ent))) {
1038     if (get_entity_irg(ent))   /* can be null */
1039       { printf ("\n  irg = %ld", get_irg_graph_nr(get_entity_irg(ent))); }
1040     else
1041       { printf ("\n  irg = NULL"); }
1042   }
1043   printf("\n\n");
1044 }
1045 #undef X
1046 #else  /* DEBUG_libfirm */
1047 void dump_entity (entity *ent) {}
1048 #endif /* DEBUG_libfirm */