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.
21 # include "entity_t.h"
23 # include "typegmod.h"
25 /* All this is needed to build the constant node for methods: */
26 # include "irprog_t.h"
30 # include "callgraph.h" /* for dumping debug output */
32 /*******************************************************************/
34 /*******************************************************************/
41 /*-----------------------------------------------------------------*/
43 /*-----------------------------------------------------------------*/
45 static void insert_entity_in_owner (entity *ent) {
46 type *owner = ent->owner;
47 switch (get_type_tpop_code(owner)) {
49 add_class_member (owner, ent);
52 add_struct_member (owner, ent);
55 add_union_member (owner, ent);
58 set_array_element_entity(owner, ent);
65 new_entity (type *owner, ident *name, type *type)
70 assert(!id_contains_char(name, ' ') && "entity name should not contain spaces");
72 res = (entity *) xmalloc (sizeof (entity));
74 assert_legal_owner_of_ent(owner);
79 if (get_type_tpop(type) == type_method)
80 res->allocation = allocation_static;
82 res->allocation = allocation_automatic;
84 res->visibility = visibility_local;
86 if (is_method_type(type)) {
89 res->variability = variability_constant;
90 rem = current_ir_graph;
91 current_ir_graph = get_const_code_irg();
92 res->value = new_SymConst(sym, symconst_addr_ent);
93 current_ir_graph = rem;
95 res->variability = variability_uninitialized;
98 res->val_paths = NULL;
100 res->peculiarity = peculiarity_existent;
101 res->volatility = volatility_non_volatile;
102 res->stickyness = stickyness_unsticky;
104 if (is_class_type(owner)) {
105 res->overwrites = NEW_ARR_F(entity *, 0);
106 res->overwrittenby = NEW_ARR_F(entity *, 0);
108 res->overwrites = NULL;
109 res->overwrittenby = NULL;
114 res->nr = get_irp_new_node_nr();
119 /* Remember entity in it's owner. */
120 insert_entity_in_owner (res);
124 new_d_entity (type *owner, ident *name, type *type, dbg_info *db) {
125 entity *res = new_entity(owner, name, type);
126 set_entity_dbg_info(res, db);
130 static void free_entity_attrs(entity *ent) {
132 if (get_type_tpop(get_entity_owner(ent)) == type_class) {
133 DEL_ARR_F(ent->overwrites); ent->overwrites = NULL;
134 DEL_ARR_F(ent->overwrittenby); ent->overwrittenby = NULL;
136 assert(ent->overwrites == NULL);
137 assert(ent->overwrittenby == NULL);
139 /* if (ent->values) DEL_ARR_F(ent->values); *//* @@@ warum nich? */
140 if (ent->val_paths) {
141 if (is_compound_entity(ent))
142 for (i = 0; i < get_compound_ent_n_values(ent); i++)
143 if (ent->val_paths[i]) ;
144 /* free_compound_graph_path(ent->val_paths[i]) ; * @@@ warum nich? */
145 /* Geht nich: wird mehrfach verwendet!!! ==> mehrfach frei gegeben. */
146 /* DEL_ARR_F(ent->val_paths); */
148 ent->val_paths = NULL;
153 copy_entity_own (entity *old, type *new_owner) {
155 assert(old && old->kind == k_entity);
156 assert_legal_owner_of_ent(new_owner);
158 if (old->owner == new_owner) return old;
159 new = (entity *) xmalloc (sizeof (entity));
160 memcpy (new, old, sizeof (entity));
161 new->owner = new_owner;
162 if (is_class_type(new_owner)) {
163 new->overwrites = NEW_ARR_F(entity *, 0);
164 new->overwrittenby = NEW_ARR_F(entity *, 0);
167 new->nr = get_irp_new_node_nr();
170 insert_entity_in_owner (new);
176 copy_entity_name (entity *old, ident *new_name) {
178 assert(old && old->kind == k_entity);
180 if (old->name == new_name) return old;
181 new = (entity *) xmalloc (sizeof (entity));
182 memcpy (new, old, sizeof (entity));
183 new->name = new_name;
185 if (is_class_type(new->owner)) {
186 new->overwrites = DUP_ARR_F(entity *, old->overwrites);
187 new->overwrittenby = DUP_ARR_F(entity *, old->overwrittenby);
190 new->nr = get_irp_new_node_nr();
193 insert_entity_in_owner (new);
200 free_entity (entity *ent) {
201 assert(ent && ent->kind == k_entity);
202 free_entity_attrs(ent);
207 /* Outputs a unique number for this node */
209 get_entity_nr(entity *ent) {
210 assert(ent && ent->kind == k_entity);
219 (get_entity_name)(entity *ent) {
220 return __get_entity_name(ent);
224 (get_entity_ident)(entity *ent) {
225 return get_entity_ident(ent);
229 void set_entitye_ld_name (entity *, char *ld_name);
230 void set_entity_ld_ident (entity *, ident *ld_ident);
234 (get_entity_owner)(entity *ent) {
235 return __get_entity_owner(ent);
239 set_entity_owner (entity *ent, type *owner) {
240 assert(ent && ent->kind == k_entity);
241 assert_legal_owner_of_ent(owner);
245 void /* should this go into type.c? */
246 assert_legal_owner_of_ent(type *owner) {
247 assert(get_type_tpop_code(owner) == tpo_class ||
248 get_type_tpop_code(owner) == tpo_union ||
249 get_type_tpop_code(owner) == tpo_struct ||
250 get_type_tpop_code(owner) == tpo_array); /* Yes, array has an entity
251 -- to select fields! */
255 (get_entity_ld_ident)(entity *ent) {
256 return __get_entity_ld_ident(ent);
260 (set_entity_ld_ident)(entity *ent, ident *ld_ident) {
261 __set_entity_ld_ident(ent, ld_ident);
265 (get_entity_ld_name)(entity *ent) {
266 return __get_entity_ld_name(ent);
270 (get_entity_type)(entity *ent) {
271 return __get_entity_type(ent);
275 (set_entity_type)(entity *ent, type *type) {
276 __set_entity_type(ent, type);
280 (get_entity_allocation)(entity *ent) {
281 return __get_entity_allocation(ent);
285 (set_entity_allocation)(entity *ent, ent_allocation al) {
286 __set_entity_allocation(ent, al);
289 /* return the name of the visibility */
290 const char *get_allocation_name(ent_allocation all)
292 #define X(a) case a: return #a
294 X(allocation_automatic);
295 X(allocation_parameter);
296 X(allocation_dynamic);
297 X(allocation_static);
298 default: return "BAD VALUE";
305 (get_entity_visibility)(entity *ent) {
306 return __get_entity_visibility(ent);
310 set_entity_visibility (entity *ent, ent_visibility vis) {
311 assert(ent && ent->kind == k_entity);
312 if (vis != visibility_local)
313 assert((ent->allocation == allocation_static) ||
314 (ent->allocation == allocation_automatic));
315 /* @@@ Test that the owner type is not local, but how??
316 && get_class_visibility(get_entity_owner(ent)) != local));*/
317 ent->visibility = vis;
320 /* return the name of the visibility */
321 const char *get_visibility_name(ent_visibility vis)
323 #define X(a) case a: return #a
326 X(visibility_external_visible);
327 X(visibility_external_allocated);
328 default: return "BAD VALUE";
334 (get_entity_variability)(entity *ent) {
335 return __get_entity_variability(ent);
339 set_entity_variability (entity *ent, ent_variability var)
341 assert(ent && ent->kind == k_entity);
342 if (var == variability_part_constant)
343 assert(is_class_type(ent->type) || is_struct_type(ent->type));
345 if ((is_compound_type(ent->type)) &&
346 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
347 /* Allocate datastructures for constant values */
348 ent->values = NEW_ARR_F(ir_node *, 0);
349 ent->val_paths = NEW_ARR_F(compound_graph_path *, 0);
351 if ((is_atomic_type(ent->type)) &&
352 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
353 /* Set default constant value. */
354 ent->value = new_rd_Unknown(get_const_code_irg(), get_type_mode(ent->type));
357 if ((is_compound_type(ent->type)) &&
358 (var == variability_uninitialized) && (ent->variability != variability_uninitialized)) {
359 /* Free datastructures for constant values */
360 DEL_ARR_F(ent->values); ent->values = NULL;
361 DEL_ARR_F(ent->val_paths); ent->val_paths = NULL;
363 ent->variability = var;
366 /* return the name of the variablity */
367 const char *get_variability_name(ent_variability var)
369 #define X(a) case a: return #a
371 X(variability_uninitialized);
372 X(variability_initialized);
373 X(variability_part_constant);
374 X(variability_constant);
375 default: return "BAD VALUE";
381 (get_entity_volatility)(entity *ent) {
382 return __get_entity_volatility(ent);
386 (set_entity_volatility)(entity *ent, ent_volatility vol) {
387 __set_entity_volatility(ent, vol);
390 /* return the name of the volatility */
391 const char *get_volatility_name(ent_volatility var)
393 #define X(a) case a: return #a
395 X(volatility_non_volatile);
396 X(volatility_is_volatile);
397 default: return "BAD VALUE";
403 (get_entity_peculiarity)(entity *ent) {
404 return __get_entity_peculiarity(ent);
408 (set_entity_peculiarity)(entity *ent, peculiarity pec) {
409 __set_entity_peculiarity(ent, pec);
412 /* return the name of the peculiarity */
413 const char *get_peculiarity_name(peculiarity var)
415 #define X(a) case a: return #a
417 X(peculiarity_description);
418 X(peculiarity_inherited);
419 X(peculiarity_existent);
420 default: return "BAD VALUE";
425 /* Get the entity's stickyness */
427 (get_entity_stickyness)(entity *ent) {
428 return __get_entity_stickyness(ent);
431 /* Set the entity's stickyness */
433 (set_entity_stickyness)(entity *ent, ent_stickyness stickyness) {
434 __set_entity_stickyness(ent, stickyness);
437 /* Set has no effect for existent entities of type method. */
439 get_atomic_ent_value(entity *ent)
441 assert(ent && is_atomic_entity(ent));
442 assert(ent->variability != variability_uninitialized);
443 return skip_Id (ent->value);
447 set_atomic_ent_value(entity *ent, ir_node *val) {
448 assert(is_atomic_entity(ent) && (ent->variability != variability_uninitialized));
449 if (is_method_type(ent->type) && (ent->peculiarity == peculiarity_existent))
454 /* Returns true if the the node is representable as code on
456 int is_irn_const_expression(ir_node *n) {
460 switch(get_irn_opcode(n)) {
471 if (is_irn_const_expression(get_binop_left(n)))
472 return is_irn_const_expression(get_binop_right(n));
475 return is_irn_const_expression(get_irn_n(n, 0));
484 ir_node *copy_const_value(ir_node *n) {
489 switch(get_irn_opcode(n)) {
491 nn = new_Const(m, get_Const_tarval(n)); break;
494 nn = new_SymConst(get_SymConst_symbol(n), get_SymConst_kind(n));
497 nn = new_Add(copy_const_value(get_Add_left(n)),
498 copy_const_value(get_Add_right(n)), m); break;
500 nn = new_Sub(copy_const_value(get_Sub_left(n)),
501 copy_const_value(get_Sub_right(n)), m); break;
503 nn = new_Mul(copy_const_value(get_Mul_left(n)),
504 copy_const_value(get_Mul_right(n)), m); break;
506 nn = new_And(copy_const_value(get_And_left(n)),
507 copy_const_value(get_And_right(n)), m); break;
509 nn = new_Or(copy_const_value(get_Or_left(n)),
510 copy_const_value(get_Or_right(n)), m); break;
512 nn = new_Eor(copy_const_value(get_Eor_left(n)),
513 copy_const_value(get_Eor_right(n)), m); break;
515 nn = new_Cast(copy_const_value(get_Cast_op(n)), get_Cast_type(n)); break;
517 nn = new_Conv(copy_const_value(get_Conv_op(n)), m); break;
519 nn = new_Unknown(m); break;
522 assert(0 && "opdope invalid or not implemented");
529 compound_graph_path *
530 new_compound_graph_path(type *tp, int length) {
531 compound_graph_path *res;
532 assert(is_type(tp) && is_compound_type(tp));
535 res = (compound_graph_path *) calloc (1, sizeof(compound_graph_path) + (length-1) * sizeof(entity *));
536 res->kind = k_ir_compound_graph_path;
539 res ->arr_indicees = (int *) calloc(length, sizeof(int));
544 free_compound_graph_path (compound_graph_path *gr) {
545 assert(gr && is_compound_graph_path(gr));
547 free(gr ->arr_indicees);
552 is_compound_graph_path(void *thing) {
553 return (get_kind(thing) == k_ir_compound_graph_path);
556 /* checks whether nodes 0..pos are correct (all lie on a path.) */
557 /* @@@ not implemented */
558 int is_proper_compound_graph_path(compound_graph_path *gr, int pos) {
561 type *owner = gr->tp;
562 for (i = 0; i <= pos; i++) {
563 node = get_compound_graph_path_node(gr, i);
564 if (get_entity_owner(node) != owner) return false;
565 owner = get_entity_type(node);
567 if (pos == get_compound_graph_path_length(gr))
568 if (!is_atomic_type(owner)) return false;
573 get_compound_graph_path_length(compound_graph_path *gr) {
574 assert(gr && is_compound_graph_path(gr));
579 get_compound_graph_path_node(compound_graph_path *gr, int pos) {
580 assert(gr && is_compound_graph_path(gr));
581 assert(pos >= 0 && pos < gr->len);
582 return gr->nodes[pos];
586 set_compound_graph_path_node(compound_graph_path *gr, int pos, entity *node) {
587 assert(gr && is_compound_graph_path(gr));
588 assert(pos >= 0 && pos < gr->len);
589 assert(is_entity(node));
590 gr->nodes[pos] = node;
591 assert(is_proper_compound_graph_path(gr, pos));
595 get_compound_graph_path_array_index(compound_graph_path *gr, int pos) {
596 assert(gr && is_compound_graph_path(gr));
597 assert(pos >= 0 && pos < gr->len);
598 return gr->arr_indicees[pos];
602 set_compound_graph_path_array_index(compound_graph_path *gr, int pos, int index) {
603 assert(gr && is_compound_graph_path(gr));
604 assert(pos >= 0 && pos < gr->len);
605 gr->arr_indicees[pos] = index;
608 /* A value of a compound entity is a pair of value and the corresponding path to a member of
611 add_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path) {
612 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
613 ARR_APP1 (ir_node *, ent->values, val);
614 ARR_APP1 (compound_graph_path *, ent->val_paths, path);
618 set_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path, int pos) {
619 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
620 ent->values[pos] = val;
621 ent->val_paths[pos] = path;
625 get_compound_ent_n_values(entity *ent) {
626 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
627 return (ARR_LEN (ent->values));
631 get_compound_ent_value(entity *ent, int pos) {
632 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
633 return ent->values[pos];
636 compound_graph_path *
637 get_compound_ent_value_path(entity *ent, int pos) {
638 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
639 return ent->val_paths[pos];
643 remove_compound_ent_value(entity *ent, entity *value_ent) {
645 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
646 for (i = 0; i < (ARR_LEN (ent->val_paths)); i++) {
647 compound_graph_path *path = ent->val_paths[i];
648 if (path->nodes[path->len-1] == value_ent) {
649 for(; i < (ARR_LEN (ent->val_paths))-1; i++) {
650 ent->val_paths[i] = ent->val_paths[i+1];
651 ent->values[i] = ent->values[i+1];
653 ARR_SETLEN(entity*, ent->val_paths, ARR_LEN(ent->val_paths) - 1);
654 ARR_SETLEN(ir_node*, ent->values, ARR_LEN(ent->values) - 1);
661 add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
662 compound_graph_path *path;
663 type *owner_tp = get_entity_owner(ent);
664 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
665 path = new_compound_graph_path(owner_tp, 1);
666 path->nodes[0] = member;
667 if (is_array_type(owner_tp)) {
671 assert(get_array_n_dimensions(owner_tp) == 1 && has_array_lower_bound(owner_tp, 0));
672 max = get_array_lower_bound_int(owner_tp, 0) -1;
673 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
674 int index = get_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0);
679 path->arr_indicees[0] = max + 1;
681 add_compound_ent_value_w_path(ent, val, path);
684 /* Copies the firm subgraph referenced by val to const_code_irg and adds
685 the node as constant initialization to ent.
686 The subgraph may not contain control flow operations.
688 copy_and_add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
689 ir_graph *rem = current_ir_graph;
691 assert(get_entity_variability(ent) != variability_uninitialized);
692 current_ir_graph = get_const_code_irg();
694 val = copy_const_value(val);
695 add_compound_ent_value(ent, val, member);
696 current_ir_graph = rem;
699 /* Copies the value i of the entity to current_block in current_ir_graph.
701 copy_compound_ent_value(entity *ent, int pos) {
702 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
703 return copy_const_value(ent->values[pos+1]);
707 get_compound_ent_value_member(entity *ent, int pos) {
708 compound_graph_path *path;
709 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
710 path = get_compound_ent_value_path(ent, pos);
712 return get_compound_graph_path_node(path, get_compound_graph_path_length(path)-1);
716 set_compound_ent_value(entity *ent, ir_node *val, entity *member, int pos) {
717 compound_graph_path *path;
718 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
719 path = get_compound_ent_value_path(ent, pos);
720 set_compound_graph_path_node(path, 0, member);
721 set_compound_ent_value_w_path(ent, val, path, pos);
725 set_array_entity_values(entity *ent, tarval **values, int num_vals) {
727 ir_graph *rem = current_ir_graph;
728 type *arrtp = get_entity_type(ent);
731 assert(is_array_type(arrtp));
732 assert(get_array_n_dimensions(arrtp) == 1);
733 /* One bound is sufficient, the nunmber of constant fields makes the
735 assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
736 assert(get_entity_variability(ent) != variability_uninitialized);
737 current_ir_graph = get_const_code_irg();
739 for (i = 0; i < num_vals; i++) {
740 val = new_Const(get_tarval_mode (values[i]), values[i]);
741 add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
742 set_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0, i);
744 current_ir_graph = rem;
747 int get_compound_ent_value_offset_bits(entity *ent, int pos) {
748 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
750 compound_graph_path *path = get_compound_ent_value_path(ent, pos);
751 int i, path_len = get_compound_graph_path_length(path);
754 for (i = 0; i < path_len; ++i) {
755 entity *node = get_compound_graph_path_node(path, i);
756 type *node_tp = get_entity_type(node);
757 type *owner_tp = get_entity_owner(node);
758 if (is_array_type(owner_tp)) {
759 int size = get_mode_size_bits (get_type_mode(node_tp));
760 int align = get_mode_align_bits(get_type_mode(node_tp));
764 assert(size % align == 0);
765 /* ansonsten aufrunden */
767 offset += size * get_compound_graph_path_array_index(path, i);
769 offset += get_entity_offset_bits(node);
775 int get_compound_ent_value_offset_bytes(entity *ent, int pos) {
776 int offset = get_compound_ent_value_offset_bits(ent, pos);
777 assert(offset % 8 == 0);
782 static void init_index(type *arr) {
786 assert(get_array_n_dimensions(arr) == 1);
788 if (has_array_lower_bound(arr, dim))
789 init = get_array_lower_bound_int(arr, 0) -1;
791 init = get_array_upper_bound_int(arr, 0) +1;
793 set_entity_link(get_array_element_entity(arr), (void *)init);
797 static int get_next_index(entity *elem_ent) {
798 type *arr = get_entity_owner(elem_ent);
802 assert(get_array_n_dimensions(arr) == 1);
804 if (has_array_lower_bound(arr, dim)) {
805 next = (int)get_entity_link(elem_ent) +1;
806 if (has_array_upper_bound(arr, dim)) {
807 int upper = get_array_upper_bound_int(arr, dim);
808 if (next == upper) next = get_array_lower_bound_int(arr, dim);
811 next = (int)get_entity_link(elem_ent) -1;
812 if (has_array_lower_bound(arr, dim)) {
813 int upper = get_array_upper_bound_int(arr, dim);
814 if (next == upper) next = get_array_upper_bound_int(arr, dim);
818 set_entity_link(elem_ent, (void *)next);
822 /* Compute the array indicees in compound graph paths of initialized entities.
824 * All arrays must have fixed lower and upper bounds. One array can
825 * have an open bound. If there are several open bounds, we do
826 * nothing. There must be initializer elements for all array
827 * elements. Uses the link field in the array element entities. The
828 * array bounds must be representable as ints.
830 * (If the bounds are not representable as ints we have to represent
831 * the indicees as firm nodes. But the still we must be able to
832 * evaluate the index against the upper bound.)
834 void compute_compound_ent_array_indicees(entity *ent) {
835 type *tp = get_entity_type(ent);
837 entity *unknown_bound_entity = NULL;
839 if (!is_compound_type(tp) ||
840 (ent->variability == variability_uninitialized)) return ;
842 n_vals = get_compound_ent_n_values(ent);
843 if (n_vals == 0) return;
845 /* We can not compute the indicees if there is more than one array
846 with an unknown bound. For this remember the first entity that
847 represents such an array. It could be ent. */
848 if (is_array_type(tp)) {
849 assert(get_array_n_dimensions(tp) == 1 && "other not implemented");
851 if (!has_array_lower_bound(tp, dim) || !has_array_upper_bound(tp, dim))
852 unknown_bound_entity = ent;
855 /* Initialize the entity links to lower bound -1 and test all path elements
857 for (i = 0; i < n_vals; ++i) {
858 compound_graph_path *path = get_compound_ent_value_path(ent, i);
859 int j, path_len = get_compound_graph_path_length(path);
860 for (j = 0; j < path_len; ++j) {
861 entity *node = get_compound_graph_path_node(path, j);
862 type *elem_tp = get_entity_type(node);
864 if (is_array_type(elem_tp)) {
865 assert(get_array_n_dimensions(elem_tp) == 1 && "other not implemented");
867 if (!has_array_lower_bound(elem_tp, dim) || !has_array_upper_bound(elem_tp, dim)) {
868 if (!unknown_bound_entity) unknown_bound_entity = node;
869 if (node != unknown_bound_entity) return;
877 /* Finally compute the indicees ... */
878 for (i = 0; i < n_vals; ++i) {
879 compound_graph_path *path = get_compound_ent_value_path(ent, i);
880 int j, path_len = get_compound_graph_path_length(path);
881 for (j = 0; j < path_len; ++j) {
882 entity *node = get_compound_graph_path_node(path, j);
883 type *owner_tp = get_entity_owner(node);
884 if (is_array_type(owner_tp))
885 set_compound_graph_path_array_index (path, j, get_next_index(node));
892 static int *resize (int *buf, int new_size) {
893 int *new_buf = (int *)calloc(new_size, 4);
894 memcpy(new_buf, buf, new_size>1);
899 /* We sort the elements by placing them at their bit offset in an
900 array where each entry represents one bit called permutation. In
901 fact, we do not place the values themselves, as we would have to
902 copy two things, the value and the path. We only remember the
903 position in the old order. Each value should have a distinct
904 position in the permutation.
906 A second iteration now permutes the actual elements into two
908 void sort_compound_ent_values(entity *ent) {
909 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
911 type *tp = get_entity_type(ent);
912 int i, n_vals = get_compound_ent_n_values(ent);
913 int tp_size = get_type_size_bits(tp);
917 if (!is_compound_type(tp) ||
918 (ent->variability == variability_uninitialized) ||
919 (get_type_state(tp) != layout_fixed) ||
920 (n_vals == 0) ) return;
922 /* estimated upper bound for size. Better: use flexible array ... */
923 size = ((tp_size > (n_vals * 32)) ? tp_size : (n_vals * 32)) * 4;
924 permutation = (int *)calloc(size, 4);
925 for (i = 0; i < n_vals; ++i) {
926 int pos = get_compound_ent_value_offset_bits(ent, i);
927 while (pos >= size) {
929 permutation = resize(permutation, size);
932 assert(permutation[pos] == 0 && "two values with the same offset");
933 permutation[pos] = i + 1; /* We initialized with 0, so we can not distinguish entry 0.
934 So inc all entries by one. */
935 //fprintf(stderr, "i: %d, pos: %d \n", i, pos);
939 ir_node **my_values = NEW_ARR_F(ir_node *, n_vals);
940 compound_graph_path **my_paths = NEW_ARR_F(compound_graph_path *, n_vals);
941 for (i = 0; i < size; ++i) {
942 int pos = permutation[i];
944 //fprintf(stderr, "pos: %d i: %d next %d \n", i, pos, next);
945 assert(next < n_vals);
946 pos--; /* We increased the pos by one */
947 my_values[next] = get_compound_ent_value (ent, pos);
948 my_paths [next] = get_compound_ent_value_path(ent, pos);
954 DEL_ARR_F(ent->values);
955 ent->values = my_values;
956 DEL_ARR_F(ent->val_paths);
957 ent->val_paths = my_paths;
961 (get_entity_offset_bytes)(entity *ent) {
962 return __get_entity_offset_bytes(ent);
966 (get_entity_offset_bits)(entity *ent) {
967 return __get_entity_offset_bits(ent);
971 (set_entity_offset_bytes)(entity *ent, int offset) {
972 __set_entity_offset_bytes(ent, offset);
976 (set_entity_offset_bits)(entity *ent, int offset) {
977 __set_entity_offset_bits(ent, offset);
981 add_entity_overwrites(entity *ent, entity *overwritten) {
982 assert(ent && is_class_type(get_entity_owner(ent)));
983 ARR_APP1(entity *, ent->overwrites, overwritten);
984 ARR_APP1(entity *, overwritten->overwrittenby, ent);
988 get_entity_n_overwrites(entity *ent) {
989 assert(ent && is_class_type(get_entity_owner(ent)));
990 return (ARR_LEN(ent->overwrites));
994 get_entity_overwrites_index(entity *ent, entity *overwritten) {
996 assert(ent && is_class_type(get_entity_owner(ent)));
997 for (i = 0; i < get_entity_n_overwrites(ent); i++)
998 if (get_entity_overwrites(ent, i) == overwritten)
1004 get_entity_overwrites (entity *ent, int pos) {
1005 assert(ent && is_class_type(get_entity_owner(ent)));
1006 assert(pos < get_entity_n_overwrites(ent));
1007 return ent->overwrites[pos];
1011 set_entity_overwrites (entity *ent, int pos, entity *overwritten) {
1012 assert(ent && is_class_type(get_entity_owner(ent)));
1013 assert(pos < get_entity_n_overwrites(ent));
1014 ent->overwrites[pos] = overwritten;
1018 remove_entity_overwrites(entity *ent, entity *overwritten) {
1020 assert(ent && is_class_type(get_entity_owner(ent)));
1021 for (i = 0; i < (ARR_LEN (ent->overwrites)); i++)
1022 if (ent->overwrites[i] == overwritten) {
1023 for(; i < (ARR_LEN (ent->overwrites))-1; i++)
1024 ent->overwrites[i] = ent->overwrites[i+1];
1025 ARR_SETLEN(entity*, ent->overwrites, ARR_LEN(ent->overwrites) - 1);
1031 add_entity_overwrittenby (entity *ent, entity *overwrites) {
1032 assert(ent && is_class_type(get_entity_owner(ent)));
1033 add_entity_overwrites(overwrites, ent);
1037 get_entity_n_overwrittenby (entity *ent) {
1038 assert(ent && is_class_type(get_entity_owner(ent)));
1039 return (ARR_LEN (ent->overwrittenby));
1043 get_entity_overwrittenby_index(entity *ent, entity *overwrites) {
1045 assert(ent && is_class_type(get_entity_owner(ent)));
1046 for (i = 0; i < get_entity_n_overwrittenby(ent); i++)
1047 if (get_entity_overwrittenby(ent, i) == overwrites)
1053 get_entity_overwrittenby (entity *ent, int pos) {
1054 assert(ent && is_class_type(get_entity_owner(ent)));
1055 assert(pos < get_entity_n_overwrittenby(ent));
1056 return ent->overwrittenby[pos];
1060 set_entity_overwrittenby (entity *ent, int pos, entity *overwrites) {
1061 assert(ent && is_class_type(get_entity_owner(ent)));
1062 assert(pos < get_entity_n_overwrittenby(ent));
1063 ent->overwrittenby[pos] = overwrites;
1066 void remove_entity_overwrittenby(entity *ent, entity *overwrites) {
1068 assert(ent && is_class_type(get_entity_owner(ent)));
1069 for (i = 0; i < (ARR_LEN (ent->overwrittenby)); i++)
1070 if (ent->overwrittenby[i] == overwrites) {
1071 for(; i < (ARR_LEN (ent->overwrittenby))-1; i++)
1072 ent->overwrittenby[i] = ent->overwrittenby[i+1];
1073 ARR_SETLEN(entity*, ent->overwrittenby, ARR_LEN(ent->overwrittenby) - 1);
1078 /* A link to store intermediate information */
1080 (get_entity_link)(entity *ent) {
1081 return __get_entity_link(ent);
1085 (set_entity_link)(entity *ent, void *l) {
1086 __set_entity_link(ent, l);
1090 (get_entity_irg)(entity *ent) {
1091 return __get_entity_irg(ent);
1095 set_entity_irg(entity *ent, ir_graph *irg) {
1096 assert(ent && is_method_type(get_entity_type(ent)));
1097 /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
1098 * Methode selbst nicht mehr aufgerufen werden kann, die Entität
1099 * aber erhalten bleiben soll. */
1101 assert((irg && ent->peculiarity == peculiarity_existent) ||
1102 (!irg && ent->peculiarity == peculiarity_description) ||
1103 (!irg && ent->peculiarity == peculiarity_inherited));
1108 (is_entity)(void *thing) {
1109 return __is_entity(thing);
1112 int is_atomic_entity(entity *ent) {
1113 type* t = get_entity_type(ent);
1114 assert(ent && ent->kind == k_entity);
1115 return (is_primitive_type(t) || is_pointer_type(t) ||
1116 is_enumeration_type(t) || is_method_type(t));
1119 int is_compound_entity(entity *ent) {
1120 type* t = get_entity_type(ent);
1121 assert(ent && ent->kind == k_entity);
1122 return (is_class_type(t) || is_struct_type(t) ||
1123 is_array_type(t) || is_union_type(t));
1127 * @todo not implemnted!!! */
1128 bool equal_entity(entity *ent1, entity *ent2) {
1129 fprintf(stderr, " calling unimplemented equal entity!!! \n");
1134 unsigned long get_entity_visited(entity *ent) {
1135 assert(ent && ent->kind == k_entity);
1138 void set_entity_visited(entity *ent, unsigned long num) {
1139 assert(ent && ent->kind == k_entity);
1142 /* Sets visited field in entity to entity_visited. */
1143 void mark_entity_visited(entity *ent) {
1144 assert(ent && ent->kind == k_entity);
1145 ent->visit = type_visited;
1149 bool entity_visited(entity *ent) {
1150 assert(ent && ent->kind == k_entity);
1151 return get_entity_visited(ent) >= type_visited;
1154 bool entity_not_visited(entity *ent) {
1155 assert(ent && ent->kind == k_entity);
1156 return get_entity_visited(ent) < type_visited;
1159 /* Need two routines because I want to assert the result. */
1160 static entity *resolve_ent_polymorphy2 (type *dynamic_class, entity* static_ent) {
1161 int i, n_overwrittenby;
1164 if (get_entity_owner(static_ent) == dynamic_class) return static_ent;
1166 n_overwrittenby = get_entity_n_overwrittenby(static_ent);
1167 for (i = 0; i < n_overwrittenby; ++i) {
1168 res = resolve_ent_polymorphy2(dynamic_class, get_entity_overwrittenby(static_ent, i));
1175 /** Resolve polymorphy in the inheritance relation.
1177 * Returns the dynamically referenced entity if the static entity and the
1178 * dynamic type are given.
1179 * Search downwards in overwritten tree. */
1180 entity *resolve_ent_polymorphy(type *dynamic_class, entity* static_ent) {
1182 assert(static_ent && static_ent->kind == k_entity);
1184 res = resolve_ent_polymorphy2(dynamic_class, static_ent);
1186 printf(" Could not find entity "); DDME(static_ent);
1187 printf(" in "); DDMT(dynamic_class);
1189 dump_entity(static_ent);
1190 dump_type(get_entity_owner(static_ent));
1191 dump_type(dynamic_class);
1199 /*******************************************************************/
1201 /*******************************************************************/
1204 #if 1 || DEBUG_libfirm
1205 int dump_node_opcode(FILE *F, ir_node *n); /* from irdump.c */
1209 #define X(a) case a: fprintf(F, #a); break
1210 void dump_entity_to_file (FILE *F, entity *ent) {
1212 assert(ent && ent->kind == k_entity);
1213 type *owner = get_entity_owner(ent);
1214 type *type = get_entity_type(ent);
1215 fprintf(F, "entity %s (%ld)\n", get_entity_name(ent), get_entity_nr(ent));
1216 fprintf(F, " type: %s (%ld)\n", get_type_name(type), get_type_nr(type));
1217 fprintf(F, " owner: %s (%ld)\n", get_type_name(owner), get_type_nr(owner));
1219 if (is_class_type(get_entity_owner(ent))) {
1220 if (get_entity_n_overwrites(ent) > 0) {
1221 fprintf(F, " overwrites:\n");
1222 for (i = 0; i < get_entity_n_overwrites(ent); ++i) {
1223 entity *ov = get_entity_overwrites(ent, i);
1224 fprintf(F, " %d: %s of class %s\n", i, get_entity_name(ov), get_type_name(get_entity_owner(ov)));
1227 fprintf(F, " Does not overwrite other entities. \n");
1229 if (get_entity_n_overwrittenby(ent) > 0) {
1230 fprintf(F, " overwritten by:\n");
1231 for (i = 0; i < get_entity_n_overwrittenby(ent); ++i) {
1232 entity *ov = get_entity_overwrittenby(ent, i);
1233 fprintf(F, " %d: %s of class %s\n", i, get_entity_name(ov), get_type_name(get_entity_owner(ov)));
1236 fprintf(F, " Is not overwriten by other entities. \n");
1240 fprintf(F, " allocation: ");
1241 switch (get_entity_allocation(ent)) {
1242 X(allocation_dynamic);
1243 X(allocation_automatic);
1244 X(allocation_static);
1245 X(allocation_parameter);
1248 fprintf(F, "\n visibility: ");
1249 switch (get_entity_visibility(ent)) {
1250 X(visibility_local);
1251 X(visibility_external_visible);
1252 X(visibility_external_allocated);
1255 fprintf(F, "\n variability: ");
1256 switch (get_entity_variability(ent)) {
1257 X(variability_uninitialized);
1258 X(variability_initialized);
1259 X(variability_part_constant);
1260 X(variability_constant);
1263 if (get_entity_variability(ent) != variability_uninitialized) {
1264 if (is_atomic_entity(ent)) {
1265 fprintf(F, "\n atomic value: ");
1266 dump_node_opcode(F, get_atomic_ent_value(ent));
1268 fprintf(F, "\n compound values:");
1269 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
1270 compound_graph_path *path = get_compound_ent_value_path(ent, i);
1271 entity *ent0 = get_compound_graph_path_node(path, 0);
1272 fprintf(F, "\n %3d ", get_entity_offset_bits(ent0));
1273 if (get_type_state(type) == layout_fixed)
1274 fprintf(F, "(%3d) ", get_compound_ent_value_offset_bits(ent, i));
1275 fprintf(F, "%s", get_entity_name(ent0));
1276 for (j = 0; j < get_compound_graph_path_length(path); ++j) {
1277 entity *node = get_compound_graph_path_node(path, j);
1278 fprintf(F, ".%s", get_entity_name(node));
1279 if (is_array_type(get_entity_owner(node)))
1280 fprintf(F, "[%d]", get_compound_graph_path_array_index(path, j));
1282 fprintf(F, "\t = ");
1283 dump_node_opcode(F, get_compound_ent_value(ent, i));
1288 fprintf(F, "\n volatility: ");
1289 switch (get_entity_volatility(ent)) {
1290 X(volatility_non_volatile);
1291 X(volatility_is_volatile);
1294 fprintf(F, "\n peculiarity: %s", get_peculiarity_string(get_entity_peculiarity(ent)));
1295 fprintf(F, "\n ld_name: %s", ent->ld_name ? get_entity_ld_name(ent) : "no yet set");
1296 fprintf(F, "\n offset: %d", get_entity_offset_bits(ent));
1297 if (is_method_type(get_entity_type(ent))) {
1298 if (get_entity_irg(ent)) /* can be null */ {
1299 fprintf(F, "\n irg = %ld", get_irg_graph_nr(get_entity_irg(ent)));
1300 if (get_irp_callgraph_state() == irp_callgraph_and_calltree_consistent) {
1301 fprintf(F, "\n recursion depth %d", get_irg_recursion_depth(get_entity_irg(ent)));
1302 fprintf(F, "\n loop depth %d", get_irg_loop_depth(get_entity_irg(ent)));
1305 fprintf(F, "\n irg = NULL");
1312 void dump_entity (entity *ent) {
1313 dump_entity_to_file(stdout, ent);
1316 #else /* DEBUG_libfirm */
1317 void dump_entity_to_file (FILE *F, entity *ent) {}
1318 void dump_entity (entity *ent) {}
1319 #endif /* DEBUG_libfirm */