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