formatted, more verbose debug output,
[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   if ((is_atomic_type(ent->type)) &&
350       (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
351     /* Set default constant value. */
352     ent->value = new_rd_Unknown(get_const_code_irg(), get_type_mode(ent->type));
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
492     nn = new_SymConst(get_SymConst_symbol(n), get_SymConst_kind(n));
493     break;
494   case iro_Add:
495     nn = new_Add(copy_const_value(get_Add_left(n)),
496          copy_const_value(get_Add_right(n)), m); break;
497   case iro_Sub:
498     nn = new_Sub(copy_const_value(get_Sub_left(n)),
499          copy_const_value(get_Sub_right(n)), m); break;
500   case iro_Mul:
501     nn = new_Mul(copy_const_value(get_Mul_left(n)),
502          copy_const_value(get_Mul_right(n)), m); break;
503   case iro_And:
504     nn = new_And(copy_const_value(get_And_left(n)),
505          copy_const_value(get_And_right(n)), m); break;
506   case iro_Or:
507     nn = new_Or(copy_const_value(get_Or_left(n)),
508          copy_const_value(get_Or_right(n)), m); break;
509   case iro_Eor:
510     nn = new_Eor(copy_const_value(get_Eor_left(n)),
511          copy_const_value(get_Eor_right(n)), m); break;
512   case iro_Cast:
513     nn = new_Cast(copy_const_value(get_Cast_op(n)), get_Cast_type(n)); break;
514   case iro_Conv:
515     nn = new_Conv(copy_const_value(get_Conv_op(n)), m); break;
516   case iro_Unknown:
517     nn = new_Unknown(m); break;
518   default:
519     DDMN(n);
520     assert(0 && "opdope invalid or not implemented");
521     nn = NULL;
522     break;
523   }
524   return nn;
525 }
526
527 compound_graph_path *
528 new_compound_graph_path(type *tp, int length) {
529   compound_graph_path *res;
530   assert(is_type(tp) && is_compound_type(tp));
531   assert(length > 0);
532
533   res = (compound_graph_path *) calloc (1, sizeof(compound_graph_path) + (length-1) * sizeof(entity *));
534   res->kind          = k_ir_compound_graph_path;
535   res->tp            = tp;
536   res->len           = length;
537   res ->arr_indicees = (int *) calloc(length, sizeof(int));
538   return res;
539 }
540
541 void
542 free_compound_graph_path (compound_graph_path *gr) {
543   assert(gr && is_compound_graph_path(gr));
544   gr->kind = k_BAD;
545   free(gr ->arr_indicees);
546   free(gr);
547 }
548
549 int
550 is_compound_graph_path(void *thing) {
551   return (get_kind(thing) == k_ir_compound_graph_path);
552 }
553
554 /* checks whether nodes 0..pos are correct (all lie on a path.) */
555 /* @@@ not implemented */
556 int is_proper_compound_graph_path(compound_graph_path *gr, int pos) {
557   int i;
558   entity *node;
559   type *owner = gr->tp;
560   for (i = 0; i <= pos; i++) {
561     node = get_compound_graph_path_node(gr, i);
562     if (get_entity_owner(node) != owner) return false;
563     owner = get_entity_type(node);
564   }
565   if (pos == get_compound_graph_path_length(gr))
566     if (!is_atomic_type(owner)) return false;
567   return true;
568 }
569
570 int
571 get_compound_graph_path_length(compound_graph_path *gr) {
572   assert(gr && is_compound_graph_path(gr));
573   return gr->len;
574 }
575
576 entity *
577 get_compound_graph_path_node(compound_graph_path *gr, int pos) {
578   assert(gr && is_compound_graph_path(gr));
579   assert(pos >= 0 && pos < gr->len);
580   return gr->nodes[pos];
581 }
582
583 void
584 set_compound_graph_path_node(compound_graph_path *gr, int pos, entity *node) {
585   assert(gr && is_compound_graph_path(gr));
586   assert(pos >= 0 && pos < gr->len);
587   assert(is_entity(node));
588   gr->nodes[pos] = node;
589   assert(is_proper_compound_graph_path(gr, pos));
590 }
591
592 int
593 get_compound_graph_path_array_index(compound_graph_path *gr, int pos) {
594   assert(gr && is_compound_graph_path(gr));
595   assert(pos >= 0 && pos < gr->len);
596   return gr->arr_indicees[pos];
597 }
598
599 void
600 set_compound_graph_path_array_index(compound_graph_path *gr, int pos, int index) {
601   assert(gr && is_compound_graph_path(gr));
602   assert(pos >= 0 && pos < gr->len);
603   gr->arr_indicees[pos] = index;
604 }
605
606 /* A value of a compound entity is a pair of value and the corresponding path to a member of
607    the compound. */
608 void
609 add_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path) {
610   assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
611   ARR_APP1 (ir_node *, ent->values, val);
612   ARR_APP1 (compound_graph_path *, ent->val_paths, path);
613 }
614
615 void
616 set_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path, int pos) {
617   assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
618   ent->values[pos] = val;
619   ent->val_paths[pos] = path;
620 }
621
622 int
623 get_compound_ent_n_values(entity *ent) {
624   assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
625   return (ARR_LEN (ent->values));
626 }
627
628 ir_node  *
629 get_compound_ent_value(entity *ent, int pos) {
630   assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
631   return ent->values[pos];
632 }
633
634 compound_graph_path *
635 get_compound_ent_value_path(entity *ent, int pos) {
636   assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
637   return ent->val_paths[pos];
638 }
639
640 void
641 remove_compound_ent_value(entity *ent, entity *value_ent) {
642   int i;
643   assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
644   for (i = 0; i < (ARR_LEN (ent->val_paths)); i++) {
645     compound_graph_path *path = ent->val_paths[i];
646     if (path->nodes[path->len-1] == value_ent) {
647       for(; i < (ARR_LEN (ent->val_paths))-1; i++) {
648     ent->val_paths[i] = ent->val_paths[i+1];
649     ent->values[i]   = ent->values[i+1];
650       }
651       ARR_SETLEN(entity*,  ent->val_paths, ARR_LEN(ent->val_paths) - 1);
652       ARR_SETLEN(ir_node*, ent->values,    ARR_LEN(ent->values)    - 1);
653       break;
654     }
655   }
656 }
657
658 void
659 add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
660   compound_graph_path *path;
661   type *owner_tp = get_entity_owner(ent);
662   assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
663   path = new_compound_graph_path(owner_tp, 1);
664   path->nodes[0] = member;
665   if (is_array_type(owner_tp)) {
666     int max;
667     int i;
668
669     assert(get_array_n_dimensions(owner_tp) == 1 && has_array_lower_bound(owner_tp, 0));
670     max = get_array_lower_bound_int(owner_tp, 0) -1;
671     for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
672       int index = get_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0);
673       if (index > max) {
674         max = index;
675       }
676     }
677     path->arr_indicees[0] = max + 1;
678   }
679   add_compound_ent_value_w_path(ent, val, path);
680 }
681
682 /* Copies the firm subgraph referenced by val to const_code_irg and adds
683    the node as constant initialization to ent.
684    The subgraph may not contain control flow operations.
685 void
686 copy_and_add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
687   ir_graph *rem = current_ir_graph;
688
689   assert(get_entity_variability(ent) != variability_uninitialized);
690   current_ir_graph = get_const_code_irg();
691
692   val = copy_const_value(val);
693   add_compound_ent_value(ent, val, member);
694   current_ir_graph = rem;
695   }*/
696
697 /* Copies the value i of the entity to current_block in current_ir_graph.
698 ir_node *
699 copy_compound_ent_value(entity *ent, int pos) {
700   assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
701   return copy_const_value(ent->values[pos+1]);
702   }*/
703
704 entity   *
705 get_compound_ent_value_member(entity *ent, int pos) {
706   compound_graph_path *path;
707   assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
708   path = get_compound_ent_value_path(ent, pos);
709
710   return get_compound_graph_path_node(path, get_compound_graph_path_length(path)-1);
711 }
712
713 void
714 set_compound_ent_value(entity *ent, ir_node *val, entity *member, int pos) {
715   compound_graph_path *path;
716   assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
717   path = get_compound_ent_value_path(ent, pos);
718   set_compound_graph_path_node(path, 0, member);
719   set_compound_ent_value_w_path(ent, val, path, pos);
720 }
721
722 void
723 set_array_entity_values(entity *ent, tarval **values, int num_vals) {
724   int i;
725   ir_graph *rem = current_ir_graph;
726   type *arrtp = get_entity_type(ent);
727   ir_node *val;
728
729   assert(is_array_type(arrtp));
730   assert(get_array_n_dimensions(arrtp) == 1);
731   /* One bound is sufficient, the nunmber of constant fields makes the
732      size. */
733   assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
734   assert(get_entity_variability(ent) != variability_uninitialized);
735   current_ir_graph = get_const_code_irg();
736
737   for (i = 0; i < num_vals; i++) {
738     val = new_Const(get_tarval_mode (values[i]), values[i]);
739     add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
740     set_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0, i);
741   }
742   current_ir_graph = rem;
743 }
744
745 int  get_compound_ent_value_offset_bits(entity *ent, int pos) {
746   assert(get_type_state(get_entity_type(ent)) == layout_fixed);
747
748   compound_graph_path *path = get_compound_ent_value_path(ent, pos);
749   int i, path_len = get_compound_graph_path_length(path);
750   int offset = 0;
751
752   for (i = 0; i < path_len; ++i) {
753     entity *node = get_compound_graph_path_node(path, i);
754     type *node_tp = get_entity_type(node);
755     type *owner_tp = get_entity_owner(node);
756     if (is_array_type(owner_tp)) {
757       int size  = get_mode_size_bits (get_type_mode(node_tp));
758       int align = get_mode_align_bits(get_type_mode(node_tp));
759       if (size < align)
760     size = align;
761       else {
762     assert(size % align == 0);
763     /* ansonsten aufrunden */
764       }
765       offset += size * get_compound_graph_path_array_index(path, i);
766     } else {
767       offset += get_entity_offset_bits(node);
768     }
769   }
770   return offset;
771 }
772
773 int  get_compound_ent_value_offset_bytes(entity *ent, int pos) {
774   int offset = get_compound_ent_value_offset_bits(ent, pos);
775   assert(offset % 8 == 0);
776   return offset >> 3;
777 }
778
779
780 static void init_index(type *arr) {
781   int init;
782   int dim = 0;
783
784   assert(get_array_n_dimensions(arr) == 1);
785
786   if (has_array_lower_bound(arr, dim))
787     init = get_array_lower_bound_int(arr, 0) -1;
788   else
789     init = get_array_upper_bound_int(arr, 0) +1;
790
791   set_entity_link(get_array_element_entity(arr), (void *)init);
792 }
793
794
795 static int get_next_index(entity *elem_ent) {
796   type *arr = get_entity_owner(elem_ent);
797   int next;
798   int dim = 0;
799
800   assert(get_array_n_dimensions(arr) == 1);
801
802   if (has_array_lower_bound(arr, dim)) {
803     next = (int)get_entity_link(elem_ent) +1;
804     if (has_array_upper_bound(arr, dim)) {
805       int upper = get_array_upper_bound_int(arr, dim);
806       if (next == upper) next = get_array_lower_bound_int(arr, dim);
807     }
808   } else {
809     next = (int)get_entity_link(elem_ent) -1;
810     if (has_array_lower_bound(arr, dim)) {
811       int upper = get_array_upper_bound_int(arr, dim);
812       if (next == upper) next = get_array_upper_bound_int(arr, dim);
813     }
814   }
815
816   set_entity_link(elem_ent, (void *)next);
817   return next;
818 }
819
820 /* Compute the array indicees in compound graph paths of initialized entities.
821  *
822  *  All arrays must have fixed lower and upper bounds.  One array can
823  *  have an open bound.  If there are several open bounds, we do
824  *  nothing.  There must be initializer elements for all array
825  *  elements.  Uses the link field in the array element entities.  The
826  *  array bounds must be representable as ints.
827  *
828  *  (If the bounds are not representable as ints we have to represent
829  *  the indicees as firm nodes.  But the still we must be able to
830  *  evaluate the index against the upper bound.)
831  */
832 void compute_compound_ent_array_indicees(entity *ent) {
833   type *tp = get_entity_type(ent);
834   int i, n_vals;
835   entity *unknown_bound_entity = NULL;
836
837   if (!is_compound_type(tp) ||
838       (ent->variability == variability_uninitialized)) return ;
839
840   n_vals = get_compound_ent_n_values(ent);
841   if (n_vals == 0) return;
842
843   /* We can not compute the indicees if there is more than one array
844      with an unknown bound.  For this remember the first entity that
845      represents such an array. It could be ent. */
846   if (is_array_type(tp)) {
847     assert(get_array_n_dimensions(tp) == 1 && "other not implemented");
848     int dim = 0;
849     if (!has_array_lower_bound(tp, dim) || !has_array_upper_bound(tp, dim))
850      unknown_bound_entity = ent;
851   }
852
853   /* Initialize the entity links to lower bound -1 and test all path elements
854      for known bounds. */
855   for (i = 0; i < n_vals; ++i) {
856     compound_graph_path *path = get_compound_ent_value_path(ent, i);
857     int j, path_len =  get_compound_graph_path_length(path);
858     for (j = 0; j < path_len; ++j) {
859       entity *node = get_compound_graph_path_node(path, j);
860       type *elem_tp = get_entity_type(node);
861
862       if (is_array_type(elem_tp)) {
863     assert(get_array_n_dimensions(elem_tp) == 1 && "other not implemented");
864     int dim = 0;
865     if (!has_array_lower_bound(elem_tp, dim) || !has_array_upper_bound(elem_tp, dim)) {
866       if (!unknown_bound_entity) unknown_bound_entity = node;
867       if (node != unknown_bound_entity) return;
868     }
869
870     init_index(elem_tp);
871       }
872     }
873   }
874
875   /* Finally compute the indicees ... */
876   for (i = 0; i < n_vals; ++i) {
877     compound_graph_path *path = get_compound_ent_value_path(ent, i);
878     int j, path_len =  get_compound_graph_path_length(path);
879     for (j = 0; j < path_len; ++j) {
880       entity *node = get_compound_graph_path_node(path, j);
881       type *owner_tp = get_entity_owner(node);
882       if (is_array_type(owner_tp))
883     set_compound_graph_path_array_index (path, j, get_next_index(node));
884     }
885   }
886
887 }
888
889
890 static int *resize (int *buf, int new_size) {
891   int *new_buf = (int *)calloc(new_size, 4);
892   memcpy(new_buf, buf, new_size>1);
893   free(buf);
894   return new_buf;
895 }
896
897 /* We sort the elements by placing them at their bit offset in an
898    array where each entry represents one bit called permutation.  In
899    fact, we do not place the values themselves, as we would have to
900    copy two things, the value and the path.  We only remember the
901    position in the old order. Each value should have a distinct
902    position in the permutation.
903
904    A second iteration now permutes the actual elements into two
905    new arrays. */
906 void sort_compound_ent_values(entity *ent) {
907   assert(get_type_state(get_entity_type(ent)) == layout_fixed);
908
909   type *tp = get_entity_type(ent);
910   int i, n_vals = get_compound_ent_n_values(ent);
911   int tp_size = get_type_size_bits(tp);
912   int size;
913   int *permutation;
914
915   if (!is_compound_type(tp)                           ||
916       (ent->variability == variability_uninitialized) ||
917       (get_type_state(tp) != layout_fixed)            ||
918       (n_vals == 0)                                     ) return;
919
920   /* estimated upper bound for size. Better: use flexible array ... */
921   size = ((tp_size > (n_vals * 32)) ? tp_size : (n_vals * 32)) * 4;
922   permutation = (int *)calloc(size, 4);
923   for (i = 0; i < n_vals; ++i) {
924     int pos = get_compound_ent_value_offset_bits(ent, i);
925     while (pos >= size) {
926       size = size + size;
927       permutation = resize(permutation, size);
928     }
929     assert(pos < size);
930     assert(permutation[pos] == 0 && "two values with the same offset");
931     permutation[pos] = i + 1;         /* We initialized with 0, so we can not distinguish entry 0.
932                      So inc all entries by one. */
933     //fprintf(stderr, "i: %d, pos: %d \n", i, pos);
934   }
935
936   int next = 0;
937   ir_node **my_values = NEW_ARR_F(ir_node *, n_vals);
938   compound_graph_path **my_paths  = NEW_ARR_F(compound_graph_path *, n_vals);
939   for (i = 0; i < size; ++i) {
940     int pos = permutation[i];
941     if (pos) {
942       //fprintf(stderr, "pos: %d i: %d  next %d \n", i, pos, next);
943       assert(next < n_vals);
944       pos--;   /* We increased the pos by one */
945       my_values[next] = get_compound_ent_value     (ent, pos);
946       my_paths [next] = get_compound_ent_value_path(ent, pos);
947       next++;
948     }
949   }
950   free(permutation);
951
952   DEL_ARR_F(ent->values);
953   ent->values = my_values;
954   DEL_ARR_F(ent->val_paths);
955   ent->val_paths = my_paths;
956 }
957
958 int
959 (get_entity_offset_bytes)(entity *ent) {
960   return __get_entity_offset_bytes(ent);
961 }
962
963 int
964 (get_entity_offset_bits)(entity *ent) {
965   return __get_entity_offset_bits(ent);
966 }
967
968 void
969 (set_entity_offset_bytes)(entity *ent, int offset) {
970   __set_entity_offset_bytes(ent, offset);
971 }
972
973 void
974 (set_entity_offset_bits)(entity *ent, int offset) {
975   __set_entity_offset_bits(ent, offset);
976 }
977
978 void
979 add_entity_overwrites(entity *ent, entity *overwritten) {
980   assert(ent && is_class_type(get_entity_owner(ent)));
981   ARR_APP1(entity *, ent->overwrites, overwritten);
982   ARR_APP1(entity *, overwritten->overwrittenby, ent);
983 }
984
985 int
986 get_entity_n_overwrites(entity *ent) {
987   assert(ent && is_class_type(get_entity_owner(ent)));
988   return (ARR_LEN(ent->overwrites));
989 }
990
991 int
992 get_entity_overwrites_index(entity *ent, entity *overwritten) {
993   int i;
994   assert(ent && is_class_type(get_entity_owner(ent)));
995   for (i = 0; i < get_entity_n_overwrites(ent); i++)
996     if (get_entity_overwrites(ent, i) == overwritten)
997       return i;
998   return -1;
999 }
1000
1001 entity *
1002 get_entity_overwrites   (entity *ent, int pos) {
1003   assert(ent && is_class_type(get_entity_owner(ent)));
1004   assert(pos < get_entity_n_overwrites(ent));
1005   return ent->overwrites[pos];
1006 }
1007
1008 void
1009 set_entity_overwrites   (entity *ent, int pos, entity *overwritten) {
1010   assert(ent && is_class_type(get_entity_owner(ent)));
1011   assert(pos < get_entity_n_overwrites(ent));
1012   ent->overwrites[pos] = overwritten;
1013 }
1014
1015 void
1016 remove_entity_overwrites(entity *ent, entity *overwritten) {
1017   int i;
1018   assert(ent && is_class_type(get_entity_owner(ent)));
1019   for (i = 0; i < (ARR_LEN (ent->overwrites)); i++)
1020     if (ent->overwrites[i] == overwritten) {
1021       for(; i < (ARR_LEN (ent->overwrites))-1; i++)
1022     ent->overwrites[i] = ent->overwrites[i+1];
1023       ARR_SETLEN(entity*, ent->overwrites, ARR_LEN(ent->overwrites) - 1);
1024       break;
1025     }
1026 }
1027
1028 void
1029 add_entity_overwrittenby   (entity *ent, entity *overwrites) {
1030   assert(ent && is_class_type(get_entity_owner(ent)));
1031   add_entity_overwrites(overwrites, ent);
1032 }
1033
1034 int
1035 get_entity_n_overwrittenby (entity *ent) {
1036   assert(ent && is_class_type(get_entity_owner(ent)));
1037   return (ARR_LEN (ent->overwrittenby));
1038 }
1039
1040 int
1041 get_entity_overwrittenby_index(entity *ent, entity *overwrites) {
1042   int i;
1043   assert(ent && is_class_type(get_entity_owner(ent)));
1044   for (i = 0; i < get_entity_n_overwrittenby(ent); i++)
1045     if (get_entity_overwrittenby(ent, i) == overwrites)
1046       return i;
1047   return -1;
1048 }
1049
1050 entity *
1051 get_entity_overwrittenby   (entity *ent, int pos) {
1052   assert(ent && is_class_type(get_entity_owner(ent)));
1053   assert(pos < get_entity_n_overwrittenby(ent));
1054   return ent->overwrittenby[pos];
1055 }
1056
1057 void
1058 set_entity_overwrittenby   (entity *ent, int pos, entity *overwrites) {
1059   assert(ent && is_class_type(get_entity_owner(ent)));
1060   assert(pos < get_entity_n_overwrittenby(ent));
1061   ent->overwrittenby[pos] = overwrites;
1062 }
1063
1064 void    remove_entity_overwrittenby(entity *ent, entity *overwrites) {
1065   int i;
1066   assert(ent  && is_class_type(get_entity_owner(ent)));
1067   for (i = 0; i < (ARR_LEN (ent->overwrittenby)); i++)
1068     if (ent->overwrittenby[i] == overwrites) {
1069       for(; i < (ARR_LEN (ent->overwrittenby))-1; i++)
1070     ent->overwrittenby[i] = ent->overwrittenby[i+1];
1071       ARR_SETLEN(entity*, ent->overwrittenby, ARR_LEN(ent->overwrittenby) - 1);
1072       break;
1073     }
1074 }
1075
1076 /* A link to store intermediate information */
1077 void *
1078 (get_entity_link)(entity *ent) {
1079   return __get_entity_link(ent);
1080 }
1081
1082 void
1083 (set_entity_link)(entity *ent, void *l) {
1084   __set_entity_link(ent, l);
1085 }
1086
1087 ir_graph *
1088 (get_entity_irg)(entity *ent) {
1089   return __get_entity_irg(ent);
1090 }
1091
1092 void
1093 set_entity_irg(entity *ent, ir_graph *irg) {
1094   assert(ent && is_method_type(get_entity_type(ent)));
1095   /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
1096    * Methode selbst nicht mehr aufgerufen werden kann, die Entität
1097    * aber erhalten bleiben soll. */
1098   /* assert(irg); */
1099   assert((irg  && ent->peculiarity == peculiarity_existent) ||
1100          (!irg && ent->peculiarity == peculiarity_description) ||
1101          (!irg && ent->peculiarity == peculiarity_inherited));
1102   ent->irg = irg;
1103 }
1104
1105 int
1106 (is_entity)(void *thing) {
1107   return __is_entity(thing);
1108 }
1109
1110 int is_atomic_entity(entity *ent) {
1111   type* t = get_entity_type(ent);
1112   assert(ent && ent->kind == k_entity);
1113   return (is_primitive_type(t) || is_pointer_type(t) ||
1114       is_enumeration_type(t) || is_method_type(t));
1115 }
1116
1117 int is_compound_entity(entity *ent) {
1118   type* t = get_entity_type(ent);
1119   assert(ent && ent->kind == k_entity);
1120   return (is_class_type(t) || is_struct_type(t) ||
1121       is_array_type(t) || is_union_type(t));
1122 }
1123
1124 /**
1125  * @todo not implemnted!!! */
1126 bool equal_entity(entity *ent1, entity *ent2) {
1127   fprintf(stderr, " calling unimplemented equal entity!!! \n");
1128   return true;
1129 }
1130
1131
1132 unsigned long get_entity_visited(entity *ent) {
1133   assert(ent && ent->kind == k_entity);
1134   return ent->visit;
1135 }
1136 void        set_entity_visited(entity *ent, unsigned long num) {
1137   assert(ent && ent->kind == k_entity);
1138   ent->visit = num;
1139 }
1140 /* Sets visited field in entity to entity_visited. */
1141 void        mark_entity_visited(entity *ent) {
1142   assert(ent && ent->kind == k_entity);
1143   ent->visit = type_visited;
1144 }
1145
1146
1147 bool entity_visited(entity *ent) {
1148   assert(ent && ent->kind == k_entity);
1149   return get_entity_visited(ent) >= type_visited;
1150 }
1151
1152 bool entity_not_visited(entity *ent) {
1153   assert(ent && ent->kind == k_entity);
1154   return get_entity_visited(ent) < type_visited;
1155 }
1156
1157 /* Need two routines because I want to assert the result. */
1158 static entity *resolve_ent_polymorphy2 (type *dynamic_class, entity* static_ent) {
1159   int i, n_overwrittenby;
1160   entity *res = NULL;
1161
1162   if (get_entity_owner(static_ent) == dynamic_class) return static_ent;
1163
1164   n_overwrittenby = get_entity_n_overwrittenby(static_ent);
1165   for (i = 0; i < n_overwrittenby; ++i) {
1166     res = resolve_ent_polymorphy2(dynamic_class, get_entity_overwrittenby(static_ent, i));
1167     if (res) break;
1168   }
1169
1170   return res;
1171 }
1172
1173 /** Resolve polymorphy in the inheritance relation.
1174  *
1175  * Returns the dynamically referenced entity if the static entity and the
1176  * dynamic type are given.
1177  * Search downwards in overwritten tree. */
1178 entity *resolve_ent_polymorphy(type *dynamic_class, entity* static_ent) {
1179   entity *res;
1180   assert(static_ent && static_ent->kind == k_entity);
1181
1182   res = resolve_ent_polymorphy2(dynamic_class, static_ent);
1183   if (!res) {
1184     printf(" Could not find entity "); DDME(static_ent);
1185     printf("  in "); DDMT(dynamic_class);
1186     printf("\n");
1187     dump_entity(static_ent);
1188     dump_type(get_entity_owner(static_ent));
1189     dump_type(dynamic_class);
1190   }
1191   assert(res);
1192   return res;
1193 }
1194
1195
1196
1197 /*******************************************************************/
1198 /** Debug aides                                                   **/
1199 /*******************************************************************/
1200
1201
1202 #if 1 || DEBUG_libfirm
1203 int dump_node_opcode(FILE *F, ir_node *n); /* from irdump.c */
1204
1205
1206
1207 #define X(a)    case a: fprintf(F, #a); break
1208 void    dump_entity_to_file (FILE *F, entity *ent) {
1209   int i, j;
1210   assert(ent && ent->kind == k_entity);
1211   type *owner = get_entity_owner(ent);
1212   type *type  = get_entity_type(ent);
1213   fprintf(F, "entity %s (%ld)\n", get_entity_name(ent), get_entity_nr(ent));
1214   fprintf(F, "  type:  %s (%ld)\n", get_type_name(type),  get_type_nr(type));
1215   fprintf(F, "  owner: %s (%ld)\n", get_type_name(owner), get_type_nr(owner));
1216
1217   if (is_class_type(get_entity_owner(ent))) {
1218     if (get_entity_n_overwrites(ent) > 0) {
1219       fprintf(F, "  overwrites:\n");
1220       for (i = 0; i < get_entity_n_overwrites(ent); ++i) {
1221         entity *ov = get_entity_overwrites(ent, i);
1222       fprintf(F, "    %d: %s of class %s\n", i, get_entity_name(ov), get_type_name(get_entity_owner(ov)));
1223       }
1224     } else {
1225       fprintf(F, "  Does not overwrite other entities. \n");
1226     }
1227     if (get_entity_n_overwrittenby(ent) > 0) {
1228       fprintf(F, "  overwritten by:\n");
1229       for (i = 0; i < get_entity_n_overwrittenby(ent); ++i) {
1230         entity *ov = get_entity_overwrittenby(ent, i);
1231         fprintf(F, "    %d: %s of class %s\n", i, get_entity_name(ov), get_type_name(get_entity_owner(ov)));
1232       }
1233     } else {
1234       fprintf(F, "  Is not overwriten by other entities. \n");
1235     }
1236   }
1237
1238   fprintf(F, "  allocation:  ");
1239   switch (get_entity_allocation(ent)) {
1240     X(allocation_dynamic);
1241     X(allocation_automatic);
1242     X(allocation_static);
1243     X(allocation_parameter);
1244   }
1245
1246   fprintf(F, "\n  visibility:  ");
1247   switch (get_entity_visibility(ent)) {
1248     X(visibility_local);
1249     X(visibility_external_visible);
1250     X(visibility_external_allocated);
1251   }
1252
1253   fprintf(F, "\n  variability: ");
1254   switch (get_entity_variability(ent)) {
1255     X(variability_uninitialized);
1256     X(variability_initialized);
1257     X(variability_part_constant);
1258     X(variability_constant);
1259   }
1260
1261   if (get_entity_variability(ent) != variability_uninitialized) {
1262     if (is_atomic_entity(ent)) {
1263       fprintf(F, "\n  atomic value: ");
1264       dump_node_opcode(F, get_atomic_ent_value(ent));
1265     } else {
1266       fprintf(F, "\n  compound values:");
1267       for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
1268     compound_graph_path *path = get_compound_ent_value_path(ent, i);
1269     entity *ent0 = get_compound_graph_path_node(path, 0);
1270     fprintf(F, "\n    %3d ", get_entity_offset_bits(ent0));
1271     if (get_type_state(type) == layout_fixed)
1272       fprintf(F, "(%3d) ",   get_compound_ent_value_offset_bits(ent, i));
1273     fprintf(F, "%s", get_entity_name(ent0));
1274     for (j = 0; j < get_compound_graph_path_length(path); ++j) {
1275       entity *node = get_compound_graph_path_node(path, j);
1276       fprintf(F, ".%s", get_entity_name(node));
1277       if (is_array_type(get_entity_owner(node)))
1278         fprintf(F, "[%d]", get_compound_graph_path_array_index(path, j));
1279     }
1280     fprintf(F, "\t = ");
1281     dump_node_opcode(F, get_compound_ent_value(ent, i));
1282       }
1283     }
1284   }
1285
1286   fprintf(F, "\n  volatility:  ");
1287   switch (get_entity_volatility(ent)) {
1288     X(volatility_non_volatile);
1289     X(volatility_is_volatile);
1290   }
1291
1292   fprintf(F, "\n  peculiarity: %s", get_peculiarity_string(get_entity_peculiarity(ent)));
1293   fprintf(F, "\n  ld_name: %s", ent->ld_name ? get_entity_ld_name(ent) : "no yet set");
1294   fprintf(F, "\n  offset:  %d", get_entity_offset_bits(ent));
1295   if (is_method_type(get_entity_type(ent))) {
1296     if (get_entity_irg(ent))   /* can be null */ {
1297       fprintf(F, "\n  irg = %ld", get_irg_graph_nr(get_entity_irg(ent)));
1298       if (get_irp_callgraph_state() == irp_callgraph_and_calltree_consistent) {
1299         fprintf(F, "\n    recursion depth %d", get_irg_recursion_depth(get_entity_irg(ent)));
1300         fprintf(F, "\n    loop depth      %d", get_irg_loop_depth(get_entity_irg(ent)));
1301       }
1302     } else {
1303       fprintf(F, "\n  irg = NULL");
1304     }
1305   }
1306   fprintf(F, "\n\n");
1307 }
1308 #undef X
1309
1310 void dump_entity (entity *ent) {
1311   dump_entity_to_file(stdout, ent);
1312 }
1313
1314 #else  /* DEBUG_libfirm */
1315 void dump_entity_to_file (FILE *F, entity *ent) {}
1316 void dump_entity (entity *ent) {}
1317 #endif /* DEBUG_libfirm */