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
9 * Copyright: (c) 1998-2003 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
13 #include "firm_common_t.h"
19 # include "entity_t.h"
21 # include "typegmod.h"
24 /* All this is needed to build the constant node for methods: */
25 # include "irprog_t.h"
30 # include "irdump.h" /* for output if errors occur. */
33 # include "callgraph.h" /* for dumping debug output */
35 /*******************************************************************/
37 /*******************************************************************/
39 entity *unknown_entity = NULL; entity *get_unknown_entity(void) { return unknown_entity; }
40 #define UNKNOWN_ENTITY_NAME "unknown_entity"
42 static INLINE entity *
43 new_rd_entity (dbg_info *db, type *owner, ident *name, type *type);
48 assert(unknown_type && "Call init_type before init_entity!");
49 new_rd_entity(NULL, unknown_type, new_id_from_str(UNKNOWN_ENTITY_NAME), unknown_type);
53 /*-----------------------------------------------------------------*/
55 /*-----------------------------------------------------------------*/
57 static INLINE void insert_entity_in_owner (entity *ent) {
58 type *owner = ent->owner;
59 switch (get_type_tpop_code(owner)) {
61 add_class_member (owner, ent);
64 add_struct_member (owner, ent);
67 add_union_member (owner, ent);
70 set_array_element_entity(owner, ent);
76 static INLINE entity *
77 new_rd_entity (dbg_info *db, type *owner, ident *name, type *type)
82 assert(!id_contains_char(name, ' ') && "entity name should not contain spaces");
84 res = (entity *) xmalloc (sizeof (entity));
85 memset(res, 0, sizeof(res));
91 if (get_type_tpop(type) == type_method)
92 res->allocation = allocation_static;
94 res->allocation = allocation_automatic;
96 res->visibility = visibility_local;
98 if (is_method_type(type)) {
101 res->variability = variability_constant;
102 rem = current_ir_graph;
103 current_ir_graph = get_const_code_irg();
104 res->value = new_SymConst(sym, symconst_addr_ent);
105 current_ir_graph = rem;
107 res->variability = variability_uninitialized;
110 res->val_paths = NULL;
112 res->peculiarity = peculiarity_existent;
113 res->volatility = volatility_non_volatile;
114 res->stickyness = stickyness_unsticky;
116 if (is_class_type(owner)) {
117 res->overwrites = NEW_ARR_F(entity *, 0);
118 res->overwrittenby = NEW_ARR_F(entity *, 0);
120 res->overwrites = NULL;
121 res->overwrittenby = NULL;
125 //res->accesses = NULL;
128 res->nr = get_irp_new_node_nr();
129 res->c_name = (char *)get_id_str (name);
130 #endif /* DEBUG_libfirm */
133 set_entity_dbg_info(res, db);
139 new_d_entity (type *owner, ident *name, type *type, dbg_info *db) {
140 assert_legal_owner_of_ent(owner);
141 entity *res = new_rd_entity(db, owner, name, type);
142 /* Remember entity in it's owner. */
143 insert_entity_in_owner (res);
149 new_entity (type *owner, ident *name, type *type) {
150 return new_d_entity(owner, name, type, NULL);
156 static void free_entity_attrs(entity *ent) {
158 if (get_type_tpop(get_entity_owner(ent)) == type_class) {
159 DEL_ARR_F(ent->overwrites); ent->overwrites = NULL;
160 DEL_ARR_F(ent->overwrittenby); ent->overwrittenby = NULL;
162 assert(ent->overwrites == NULL);
163 assert(ent->overwrittenby == NULL);
165 /* if (ent->values) DEL_ARR_F(ent->values); *//* @@@ warum nich? */
166 if (ent->val_paths) {
167 if (is_compound_entity(ent))
168 for (i = 0; i < get_compound_ent_n_values(ent); i++)
169 if (ent->val_paths[i]) ;
170 /* free_compound_graph_path(ent->val_paths[i]) ; * @@@ warum nich? */
171 /* Geht nich: wird mehrfach verwendet!!! ==> mehrfach frei gegeben. */
172 /* DEL_ARR_F(ent->val_paths); */
174 ent->val_paths = NULL;
179 copy_entity_own (entity *old, type *new_owner) {
181 assert(old && old->kind == k_entity);
182 assert_legal_owner_of_ent(new_owner);
184 if (old->owner == new_owner) return old;
185 new = (entity *) xmalloc (sizeof (entity));
186 memcpy (new, old, sizeof (entity));
187 new->owner = new_owner;
188 if (is_class_type(new_owner)) {
189 new->overwrites = NEW_ARR_F(entity *, 0);
190 new->overwrittenby = NEW_ARR_F(entity *, 0);
193 new->nr = get_irp_new_node_nr();
196 insert_entity_in_owner (new);
202 copy_entity_name (entity *old, ident *new_name) {
204 assert(old && old->kind == k_entity);
206 if (old->name == new_name) return old;
207 new = (entity *) xmalloc (sizeof (entity));
208 memcpy (new, old, sizeof (entity));
209 new->name = new_name;
211 if (is_class_type(new->owner)) {
212 new->overwrites = DUP_ARR_F(entity *, old->overwrites);
213 new->overwrittenby = DUP_ARR_F(entity *, old->overwrittenby);
216 new->nr = get_irp_new_node_nr();
217 new->c_name = (char *)get_id_str (new->name);
220 insert_entity_in_owner (new);
227 free_entity (entity *ent) {
228 assert(ent && ent->kind == k_entity);
229 free_entity_attrs(ent);
234 /* Outputs a unique number for this node */
236 get_entity_nr(entity *ent) {
237 assert(ent && ent->kind == k_entity);
246 (get_entity_name)(const entity *ent) {
247 return __get_entity_name(ent);
251 (get_entity_ident)(const entity *ent) {
252 return get_entity_ident(ent);
256 void set_entitye_ld_name (entity *, char *ld_name);
257 void set_entity_ld_ident (entity *, ident *ld_ident);
261 (get_entity_owner)(entity *ent) {
262 return __get_entity_owner(ent);
266 set_entity_owner (entity *ent, type *owner) {
267 assert(ent && ent->kind == k_entity);
268 assert_legal_owner_of_ent(owner);
272 void /* should this go into type.c? */
273 assert_legal_owner_of_ent(type *owner) {
274 assert(get_type_tpop_code(owner) == tpo_class ||
275 get_type_tpop_code(owner) == tpo_union ||
276 get_type_tpop_code(owner) == tpo_struct ||
277 get_type_tpop_code(owner) == tpo_array); /* Yes, array has an entity
278 -- to select fields! */
282 (get_entity_ld_ident)(entity *ent) {
283 return __get_entity_ld_ident(ent);
287 (set_entity_ld_ident)(entity *ent, ident *ld_ident) {
288 __set_entity_ld_ident(ent, ld_ident);
292 (get_entity_ld_name)(entity *ent) {
293 return __get_entity_ld_name(ent);
297 (get_entity_type)(entity *ent) {
298 return __get_entity_type(ent);
302 (set_entity_type)(entity *ent, type *type) {
303 __set_entity_type(ent, type);
307 (get_entity_allocation)(const entity *ent) {
308 return __get_entity_allocation(ent);
312 (set_entity_allocation)(entity *ent, ent_allocation al) {
313 __set_entity_allocation(ent, al);
316 /* return the name of the visibility */
317 const char *get_allocation_name(ent_allocation all)
319 #define X(a) case a: return #a
321 X(allocation_automatic);
322 X(allocation_parameter);
323 X(allocation_dynamic);
324 X(allocation_static);
325 default: return "BAD VALUE";
332 (get_entity_visibility)(const entity *ent) {
333 return __get_entity_visibility(ent);
337 set_entity_visibility (entity *ent, ent_visibility vis) {
338 assert(ent && ent->kind == k_entity);
339 if (vis != visibility_local)
340 assert((ent->allocation == allocation_static) ||
341 (ent->allocation == allocation_automatic));
342 /* @@@ Test that the owner type is not local, but how??
343 && get_class_visibility(get_entity_owner(ent)) != local));*/
344 ent->visibility = vis;
347 /* return the name of the visibility */
348 const char *get_visibility_name(ent_visibility vis)
350 #define X(a) case a: return #a
353 X(visibility_external_visible);
354 X(visibility_external_allocated);
355 default: return "BAD VALUE";
361 (get_entity_variability)(const entity *ent) {
362 return __get_entity_variability(ent);
366 set_entity_variability (entity *ent, ent_variability var)
368 assert(ent && ent->kind == k_entity);
369 if (var == variability_part_constant)
370 assert(is_class_type(ent->type) || is_struct_type(ent->type));
372 if ((is_compound_type(ent->type)) &&
373 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
374 /* Allocate datastructures for constant values */
375 ent->values = NEW_ARR_F(ir_node *, 0);
376 ent->val_paths = NEW_ARR_F(compound_graph_path *, 0);
378 if ((is_atomic_type(ent->type)) &&
379 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
380 /* Set default constant value. */
381 ent->value = new_rd_Unknown(get_const_code_irg(), get_type_mode(ent->type));
384 if ((is_compound_type(ent->type)) &&
385 (var == variability_uninitialized) && (ent->variability != variability_uninitialized)) {
386 /* Free datastructures for constant values */
387 DEL_ARR_F(ent->values); ent->values = NULL;
388 DEL_ARR_F(ent->val_paths); ent->val_paths = NULL;
390 ent->variability = var;
393 /* return the name of the variablity */
394 const char *get_variability_name(ent_variability var)
396 #define X(a) case a: return #a
398 X(variability_uninitialized);
399 X(variability_initialized);
400 X(variability_part_constant);
401 X(variability_constant);
402 default: return "BAD VALUE";
408 (get_entity_volatility)(const entity *ent) {
409 return __get_entity_volatility(ent);
413 (set_entity_volatility)(entity *ent, ent_volatility vol) {
414 __set_entity_volatility(ent, vol);
417 /* return the name of the volatility */
418 const char *get_volatility_name(ent_volatility var)
420 #define X(a) case a: return #a
422 X(volatility_non_volatile);
423 X(volatility_is_volatile);
424 default: return "BAD VALUE";
430 (get_entity_peculiarity)(const entity *ent) {
431 return __get_entity_peculiarity(ent);
435 (set_entity_peculiarity)(entity *ent, peculiarity pec) {
436 __set_entity_peculiarity(ent, pec);
439 /* return the name of the peculiarity */
440 const char *get_peculiarity_name(peculiarity var)
442 #define X(a) case a: return #a
444 X(peculiarity_description);
445 X(peculiarity_inherited);
446 X(peculiarity_existent);
447 default: return "BAD VALUE";
452 /* Get the entity's stickyness */
454 (get_entity_stickyness)(const entity *ent) {
455 return __get_entity_stickyness(ent);
458 /* Set the entity's stickyness */
460 (set_entity_stickyness)(entity *ent, ent_stickyness stickyness) {
461 __set_entity_stickyness(ent, stickyness);
464 /* Set has no effect for existent entities of type method. */
466 get_atomic_ent_value(entity *ent)
468 assert(ent && is_atomic_entity(ent));
469 assert(ent->variability != variability_uninitialized);
470 return skip_Id (ent->value);
474 set_atomic_ent_value(entity *ent, ir_node *val) {
475 assert(is_atomic_entity(ent) && (ent->variability != variability_uninitialized));
476 if (is_method_type(ent->type) && (ent->peculiarity == peculiarity_existent))
481 /* Returns true if the the node is representable as code on
483 int is_irn_const_expression(ir_node *n) {
486 /* we are in dange iff an exception will arise. TODO: be more precisely,
487 * for instance Div. will NOT rise if divisor != 0
489 if (is_binop(n) && !is_fragile_op(n))
490 return is_irn_const_expression(get_binop_left(n)) && is_irn_const_expression(get_binop_right(n));
493 switch(get_irn_opcode(n)) {
500 return is_irn_const_expression(get_irn_n(n, 0));
509 ir_node *copy_const_value(ir_node *n) {
513 /* @@@ GL I think we should implement this using the routines from irgopt for
514 dead node elimination/inlineing. */
517 switch(get_irn_opcode(n)) {
519 nn = new_Const(m, get_Const_tarval(n)); set_Const_type(nn, get_Const_type(n));
520 //nn = new_rd_Const_type(get_irn_dbg_info(n), current_ir_graph, get_cur_block(),
521 // m, get_Const_tarval(n), get_Const_type(n));
524 nn = new_d_SymConst_type(NULL, get_SymConst_symbol(n), get_SymConst_kind(n),
525 get_SymConst_value_type(n));
528 nn = new_Add(copy_const_value(get_Add_left(n)),
529 copy_const_value(get_Add_right(n)), m); break;
531 nn = new_Sub(copy_const_value(get_Sub_left(n)),
532 copy_const_value(get_Sub_right(n)), m); break;
534 nn = new_Mul(copy_const_value(get_Mul_left(n)),
535 copy_const_value(get_Mul_right(n)), m); break;
537 nn = new_And(copy_const_value(get_And_left(n)),
538 copy_const_value(get_And_right(n)), m); break;
540 nn = new_Or(copy_const_value(get_Or_left(n)),
541 copy_const_value(get_Or_right(n)), m); break;
543 nn = new_Eor(copy_const_value(get_Eor_left(n)),
544 copy_const_value(get_Eor_right(n)), m); break;
546 nn = new_Cast(copy_const_value(get_Cast_op(n)), get_Cast_type(n)); break;
548 nn = new_Conv(copy_const_value(get_Conv_op(n)), m); break;
550 nn = new_Unknown(m); break;
553 assert(0 && "opcode invalid or not implemented");
560 compound_graph_path *
561 new_compound_graph_path(type *tp, int length) {
562 compound_graph_path *res;
563 assert(is_type(tp) && is_compound_type(tp));
566 res = (compound_graph_path *) calloc (1, sizeof(compound_graph_path) + (length-1) * sizeof(entity *));
567 res->kind = k_ir_compound_graph_path;
570 res ->arr_indicees = (int *) calloc(length, sizeof(int));
575 free_compound_graph_path (compound_graph_path *gr) {
576 assert(gr && is_compound_graph_path(gr));
578 free(gr ->arr_indicees);
583 is_compound_graph_path(void *thing) {
584 return (get_kind(thing) == k_ir_compound_graph_path);
587 /* checks whether nodes 0..pos are correct (all lie on a path.) */
588 /* @@@ not implemented */
589 int is_proper_compound_graph_path(compound_graph_path *gr, int pos) {
592 type *owner = gr->tp;
593 for (i = 0; i <= pos; i++) {
594 node = get_compound_graph_path_node(gr, i);
595 if (get_entity_owner(node) != owner) return false;
596 owner = get_entity_type(node);
598 if (pos == get_compound_graph_path_length(gr))
599 if (!is_atomic_type(owner)) return false;
604 get_compound_graph_path_length(compound_graph_path *gr) {
605 assert(gr && is_compound_graph_path(gr));
610 get_compound_graph_path_node(compound_graph_path *gr, int pos) {
611 assert(gr && is_compound_graph_path(gr));
612 assert(pos >= 0 && pos < gr->len);
613 return gr->nodes[pos];
617 set_compound_graph_path_node(compound_graph_path *gr, int pos, entity *node) {
618 assert(gr && is_compound_graph_path(gr));
619 assert(pos >= 0 && pos < gr->len);
620 assert(is_entity(node));
621 gr->nodes[pos] = node;
622 assert(is_proper_compound_graph_path(gr, pos));
626 get_compound_graph_path_array_index(compound_graph_path *gr, int pos) {
627 assert(gr && is_compound_graph_path(gr));
628 assert(pos >= 0 && pos < gr->len);
629 return gr->arr_indicees[pos];
633 set_compound_graph_path_array_index(compound_graph_path *gr, int pos, int index) {
634 assert(gr && is_compound_graph_path(gr));
635 assert(pos >= 0 && pos < gr->len);
636 gr->arr_indicees[pos] = index;
639 /* A value of a compound entity is a pair of value and the corresponding path to a member of
642 add_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path) {
643 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
644 ARR_APP1 (ir_node *, ent->values, val);
645 ARR_APP1 (compound_graph_path *, ent->val_paths, path);
649 set_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path, int pos) {
650 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
651 ent->values[pos] = val;
652 ent->val_paths[pos] = path;
656 get_compound_ent_n_values(entity *ent) {
657 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
658 return (ARR_LEN (ent->values));
662 get_compound_ent_value(entity *ent, int pos) {
663 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
664 return ent->values[pos];
667 compound_graph_path *
668 get_compound_ent_value_path(entity *ent, int pos) {
669 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
670 return ent->val_paths[pos];
674 remove_compound_ent_value(entity *ent, entity *value_ent) {
676 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
677 for (i = 0; i < (ARR_LEN (ent->val_paths)); i++) {
678 compound_graph_path *path = ent->val_paths[i];
679 if (path->nodes[path->len-1] == value_ent) {
680 for(; i < (ARR_LEN (ent->val_paths))-1; i++) {
681 ent->val_paths[i] = ent->val_paths[i+1];
682 ent->values[i] = ent->values[i+1];
684 ARR_SETLEN(entity*, ent->val_paths, ARR_LEN(ent->val_paths) - 1);
685 ARR_SETLEN(ir_node*, ent->values, ARR_LEN(ent->values) - 1);
692 add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
693 compound_graph_path *path;
694 type *owner_tp = get_entity_owner(ent);
695 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
696 path = new_compound_graph_path(owner_tp, 1);
697 path->nodes[0] = member;
698 if (is_array_type(owner_tp)) {
702 assert(get_array_n_dimensions(owner_tp) == 1 && has_array_lower_bound(owner_tp, 0));
703 max = get_array_lower_bound_int(owner_tp, 0) -1;
704 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
705 int index = get_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0);
710 path->arr_indicees[0] = max + 1;
712 add_compound_ent_value_w_path(ent, val, path);
715 /* Copies the firm subgraph referenced by val to const_code_irg and adds
716 the node as constant initialization to ent.
717 The subgraph may not contain control flow operations.
719 copy_and_add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
720 ir_graph *rem = current_ir_graph;
722 assert(get_entity_variability(ent) != variability_uninitialized);
723 current_ir_graph = get_const_code_irg();
725 val = copy_const_value(val);
726 add_compound_ent_value(ent, val, member);
727 current_ir_graph = rem;
730 /* Copies the value i of the entity to current_block in current_ir_graph.
732 copy_compound_ent_value(entity *ent, int pos) {
733 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
734 return copy_const_value(ent->values[pos+1]);
738 get_compound_ent_value_member(entity *ent, int pos) {
739 compound_graph_path *path;
740 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
741 path = get_compound_ent_value_path(ent, pos);
743 return get_compound_graph_path_node(path, get_compound_graph_path_length(path)-1);
747 set_compound_ent_value(entity *ent, ir_node *val, entity *member, int pos) {
748 compound_graph_path *path;
749 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
750 path = get_compound_ent_value_path(ent, pos);
751 set_compound_graph_path_node(path, 0, member);
752 set_compound_ent_value_w_path(ent, val, path, pos);
756 set_array_entity_values(entity *ent, tarval **values, int num_vals) {
758 ir_graph *rem = current_ir_graph;
759 type *arrtp = get_entity_type(ent);
761 type *elttp = get_array_element_type(arrtp);
763 assert(is_array_type(arrtp));
764 assert(get_array_n_dimensions(arrtp) == 1);
765 /* One bound is sufficient, the nunmber of constant fields makes the
767 assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
768 assert(get_entity_variability(ent) != variability_uninitialized);
769 current_ir_graph = get_const_code_irg();
771 for (i = 0; i < num_vals; i++) {
772 val = new_Const_type(values[i], elttp);
773 add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
774 set_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0, i);
776 current_ir_graph = rem;
779 int get_compound_ent_value_offset_bits(entity *ent, int pos) {
780 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
782 compound_graph_path *path = get_compound_ent_value_path(ent, pos);
783 int i, path_len = get_compound_graph_path_length(path);
786 for (i = 0; i < path_len; ++i) {
787 entity *node = get_compound_graph_path_node(path, i);
788 type *node_tp = get_entity_type(node);
789 type *owner_tp = get_entity_owner(node);
790 if (is_array_type(owner_tp)) {
791 int size = get_type_size_bits(node_tp);
792 int align = get_type_alignment_bits(node_tp);
796 assert(size % align == 0);
797 /* ansonsten aufrunden */
799 offset += size * get_compound_graph_path_array_index(path, i);
801 offset += get_entity_offset_bits(node);
807 int get_compound_ent_value_offset_bytes(entity *ent, int pos) {
808 int offset = get_compound_ent_value_offset_bits(ent, pos);
809 assert(offset % 8 == 0);
814 static void init_index(type *arr) {
818 assert(get_array_n_dimensions(arr) == 1);
820 if (has_array_lower_bound(arr, dim))
821 init = get_array_lower_bound_int(arr, 0) -1;
823 init = get_array_upper_bound_int(arr, 0) +1;
825 set_entity_link(get_array_element_entity(arr), (void *)init);
829 static int get_next_index(entity *elem_ent) {
830 type *arr = get_entity_owner(elem_ent);
834 assert(get_array_n_dimensions(arr) == 1);
836 if (has_array_lower_bound(arr, dim)) {
837 next = (int)get_entity_link(elem_ent) +1;
838 if (has_array_upper_bound(arr, dim)) {
839 int upper = get_array_upper_bound_int(arr, dim);
840 if (next == upper) next = get_array_lower_bound_int(arr, dim);
843 next = (int)get_entity_link(elem_ent) -1;
844 if (has_array_lower_bound(arr, dim)) {
845 int upper = get_array_upper_bound_int(arr, dim);
846 if (next == upper) next = get_array_upper_bound_int(arr, dim);
850 set_entity_link(elem_ent, (void *)next);
854 /* Compute the array indicees in compound graph paths of initialized entities.
856 * All arrays must have fixed lower and upper bounds. One array can
857 * have an open bound. If there are several open bounds, we do
858 * nothing. There must be initializer elements for all array
859 * elements. Uses the link field in the array element entities. The
860 * array bounds must be representable as ints.
862 * (If the bounds are not representable as ints we have to represent
863 * the indicees as firm nodes. But the still we must be able to
864 * evaluate the index against the upper bound.)
866 void compute_compound_ent_array_indicees(entity *ent) {
867 type *tp = get_entity_type(ent);
869 entity *unknown_bound_entity = NULL;
871 if (!is_compound_type(tp) ||
872 (ent->variability == variability_uninitialized)) return ;
874 n_vals = get_compound_ent_n_values(ent);
875 if (n_vals == 0) return;
877 /* We can not compute the indicees if there is more than one array
878 with an unknown bound. For this remember the first entity that
879 represents such an array. It could be ent. */
880 if (is_array_type(tp)) {
881 assert(get_array_n_dimensions(tp) == 1 && "other not implemented");
883 if (!has_array_lower_bound(tp, dim) || !has_array_upper_bound(tp, dim))
884 unknown_bound_entity = ent;
887 /* Initialize the entity links to lower bound -1 and test all path elements
889 for (i = 0; i < n_vals; ++i) {
890 compound_graph_path *path = get_compound_ent_value_path(ent, i);
891 int j, path_len = get_compound_graph_path_length(path);
892 for (j = 0; j < path_len; ++j) {
893 entity *node = get_compound_graph_path_node(path, j);
894 type *elem_tp = get_entity_type(node);
896 if (is_array_type(elem_tp)) {
897 assert(get_array_n_dimensions(elem_tp) == 1 && "other not implemented");
899 if (!has_array_lower_bound(elem_tp, dim) || !has_array_upper_bound(elem_tp, dim)) {
900 if (!unknown_bound_entity) unknown_bound_entity = node;
901 if (node != unknown_bound_entity) return;
909 /* Finally compute the indicees ... */
910 for (i = 0; i < n_vals; ++i) {
911 compound_graph_path *path = get_compound_ent_value_path(ent, i);
912 int j, path_len = get_compound_graph_path_length(path);
913 for (j = 0; j < path_len; ++j) {
914 entity *node = get_compound_graph_path_node(path, j);
915 type *owner_tp = get_entity_owner(node);
916 if (is_array_type(owner_tp))
917 set_compound_graph_path_array_index (path, j, get_next_index(node));
924 static int *resize (int *buf, int new_size) {
925 int *new_buf = (int *)calloc(new_size, 4);
926 memcpy(new_buf, buf, new_size>1);
931 /* We sort the elements by placing them at their bit offset in an
932 array where each entry represents one bit called permutation. In
933 fact, we do not place the values themselves, as we would have to
934 copy two things, the value and the path. We only remember the
935 position in the old order. Each value should have a distinct
936 position in the permutation.
938 A second iteration now permutes the actual elements into two
940 void sort_compound_ent_values(entity *ent) {
941 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
943 type *tp = get_entity_type(ent);
944 int i, n_vals = get_compound_ent_n_values(ent);
945 int tp_size = get_type_size_bits(tp);
949 if (!is_compound_type(tp) ||
950 (ent->variability == variability_uninitialized) ||
951 (get_type_state(tp) != layout_fixed) ||
952 (n_vals == 0) ) return;
954 /* estimated upper bound for size. Better: use flexible array ... */
955 size = ((tp_size > (n_vals * 32)) ? tp_size : (n_vals * 32)) * 4;
956 permutation = (int *)calloc(size, 4);
957 for (i = 0; i < n_vals; ++i) {
958 int pos = get_compound_ent_value_offset_bits(ent, i);
959 while (pos >= size) {
961 permutation = resize(permutation, size);
964 assert(permutation[pos] == 0 && "two values with the same offset");
965 permutation[pos] = i + 1; /* We initialized with 0, so we can not distinguish entry 0.
966 So inc all entries by one. */
967 //fprintf(stderr, "i: %d, pos: %d \n", i, pos);
971 ir_node **my_values = NEW_ARR_F(ir_node *, n_vals);
972 compound_graph_path **my_paths = NEW_ARR_F(compound_graph_path *, n_vals);
973 for (i = 0; i < size; ++i) {
974 int pos = permutation[i];
976 //fprintf(stderr, "pos: %d i: %d next %d \n", i, pos, next);
977 assert(next < n_vals);
978 pos--; /* We increased the pos by one */
979 my_values[next] = get_compound_ent_value (ent, pos);
980 my_paths [next] = get_compound_ent_value_path(ent, pos);
986 DEL_ARR_F(ent->values);
987 ent->values = my_values;
988 DEL_ARR_F(ent->val_paths);
989 ent->val_paths = my_paths;
993 (get_entity_offset_bytes)(const entity *ent) {
994 return __get_entity_offset_bytes(ent);
998 (get_entity_offset_bits)(const entity *ent) {
999 return __get_entity_offset_bits(ent);
1003 (set_entity_offset_bytes)(entity *ent, int offset) {
1004 __set_entity_offset_bytes(ent, offset);
1008 (set_entity_offset_bits)(entity *ent, int offset) {
1009 __set_entity_offset_bits(ent, offset);
1013 add_entity_overwrites(entity *ent, entity *overwritten) {
1014 assert(ent && is_class_type(get_entity_owner(ent)));
1015 ARR_APP1(entity *, ent->overwrites, overwritten);
1016 ARR_APP1(entity *, overwritten->overwrittenby, ent);
1020 get_entity_n_overwrites(entity *ent) {
1021 assert(ent && is_class_type(get_entity_owner(ent)));
1022 return (ARR_LEN(ent->overwrites));
1026 get_entity_overwrites_index(entity *ent, entity *overwritten) {
1028 assert(ent && is_class_type(get_entity_owner(ent)));
1029 for (i = 0; i < get_entity_n_overwrites(ent); i++)
1030 if (get_entity_overwrites(ent, i) == overwritten)
1036 get_entity_overwrites (entity *ent, int pos) {
1037 assert(ent && is_class_type(get_entity_owner(ent)));
1038 assert(pos < get_entity_n_overwrites(ent));
1039 return ent->overwrites[pos];
1043 set_entity_overwrites (entity *ent, int pos, entity *overwritten) {
1044 assert(ent && is_class_type(get_entity_owner(ent)));
1045 assert(pos < get_entity_n_overwrites(ent));
1046 ent->overwrites[pos] = overwritten;
1050 remove_entity_overwrites(entity *ent, entity *overwritten) {
1052 assert(ent && is_class_type(get_entity_owner(ent)));
1053 for (i = 0; i < (ARR_LEN (ent->overwrites)); i++)
1054 if (ent->overwrites[i] == overwritten) {
1055 for(; i < (ARR_LEN (ent->overwrites))-1; i++)
1056 ent->overwrites[i] = ent->overwrites[i+1];
1057 ARR_SETLEN(entity*, ent->overwrites, ARR_LEN(ent->overwrites) - 1);
1063 add_entity_overwrittenby (entity *ent, entity *overwrites) {
1064 assert(ent && is_class_type(get_entity_owner(ent)));
1065 add_entity_overwrites(overwrites, ent);
1069 get_entity_n_overwrittenby (entity *ent) {
1070 assert(ent && is_class_type(get_entity_owner(ent)));
1071 return (ARR_LEN (ent->overwrittenby));
1075 get_entity_overwrittenby_index(entity *ent, entity *overwrites) {
1077 assert(ent && is_class_type(get_entity_owner(ent)));
1078 for (i = 0; i < get_entity_n_overwrittenby(ent); i++)
1079 if (get_entity_overwrittenby(ent, i) == overwrites)
1085 get_entity_overwrittenby (entity *ent, int pos) {
1086 assert(ent && is_class_type(get_entity_owner(ent)));
1087 assert(pos < get_entity_n_overwrittenby(ent));
1088 return ent->overwrittenby[pos];
1092 set_entity_overwrittenby (entity *ent, int pos, entity *overwrites) {
1093 assert(ent && is_class_type(get_entity_owner(ent)));
1094 assert(pos < get_entity_n_overwrittenby(ent));
1095 ent->overwrittenby[pos] = overwrites;
1098 void remove_entity_overwrittenby(entity *ent, entity *overwrites) {
1100 assert(ent && is_class_type(get_entity_owner(ent)));
1101 for (i = 0; i < (ARR_LEN (ent->overwrittenby)); i++)
1102 if (ent->overwrittenby[i] == overwrites) {
1103 for(; i < (ARR_LEN (ent->overwrittenby))-1; i++)
1104 ent->overwrittenby[i] = ent->overwrittenby[i+1];
1105 ARR_SETLEN(entity*, ent->overwrittenby, ARR_LEN(ent->overwrittenby) - 1);
1110 /* A link to store intermediate information */
1112 (get_entity_link)(const entity *ent) {
1113 return __get_entity_link(ent);
1117 (set_entity_link)(entity *ent, void *l) {
1118 __set_entity_link(ent, l);
1122 (get_entity_irg)(const entity *ent) {
1123 return __get_entity_irg(ent);
1127 set_entity_irg(entity *ent, ir_graph *irg) {
1128 assert(ent && is_method_type(get_entity_type(ent)));
1129 /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
1130 * Methode selbst nicht mehr aufgerufen werden kann, die Entität
1131 * aber erhalten bleiben soll. */
1133 assert((irg && ent->peculiarity == peculiarity_existent) ||
1134 (!irg && ent->peculiarity == peculiarity_description) ||
1135 (!irg && ent->peculiarity == peculiarity_inherited));
1140 (is_entity)(const void *thing) {
1141 return __is_entity(thing);
1144 int is_atomic_entity(entity *ent) {
1145 type* t = get_entity_type(ent);
1146 assert(ent && ent->kind == k_entity);
1147 return (is_primitive_type(t) || is_pointer_type(t) ||
1148 is_enumeration_type(t) || is_method_type(t));
1151 int is_compound_entity(entity *ent) {
1152 type* t = get_entity_type(ent);
1153 assert(ent && ent->kind == k_entity);
1154 return (is_class_type(t) || is_struct_type(t) ||
1155 is_array_type(t) || is_union_type(t));
1159 * @todo not implemnted!!! */
1160 bool equal_entity(entity *ent1, entity *ent2) {
1161 fprintf(stderr, " calling unimplemented equal entity!!! \n");
1166 unsigned long get_entity_visited(entity *ent) {
1167 assert(ent && ent->kind == k_entity);
1170 void set_entity_visited(entity *ent, unsigned long num) {
1171 assert(ent && ent->kind == k_entity);
1174 /* Sets visited field in entity to entity_visited. */
1175 void mark_entity_visited(entity *ent) {
1176 assert(ent && ent->kind == k_entity);
1177 ent->visit = type_visited;
1181 bool entity_visited(entity *ent) {
1182 assert(ent && ent->kind == k_entity);
1183 return get_entity_visited(ent) >= type_visited;
1186 bool entity_not_visited(entity *ent) {
1187 assert(ent && ent->kind == k_entity);
1188 return get_entity_visited(ent) < type_visited;
1191 /* Need two routines because I want to assert the result. */
1192 static entity *resolve_ent_polymorphy2 (type *dynamic_class, entity* static_ent) {
1193 int i, n_overwrittenby;
1196 if (get_entity_owner(static_ent) == dynamic_class) return static_ent;
1198 n_overwrittenby = get_entity_n_overwrittenby(static_ent);
1199 for (i = 0; i < n_overwrittenby; ++i) {
1200 res = resolve_ent_polymorphy2(dynamic_class, get_entity_overwrittenby(static_ent, i));
1207 /** Resolve polymorphy in the inheritance relation.
1209 * Returns the dynamically referenced entity if the static entity and the
1210 * dynamic type are given.
1211 * Search downwards in overwritten tree. */
1212 entity *resolve_ent_polymorphy(type *dynamic_class, entity* static_ent) {
1214 assert(static_ent && static_ent->kind == k_entity);
1216 res = resolve_ent_polymorphy2(dynamic_class, static_ent);
1219 printf(" Could not find entity "); DDME(static_ent);
1220 printf(" in "); DDMT(dynamic_class);
1222 dump_entity(static_ent);
1223 dump_type(get_entity_owner(static_ent));
1224 dump_type(dynamic_class);