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 /*******************************************************************/
32 /*******************************************************************/
39 /*-----------------------------------------------------------------*/
41 /*-----------------------------------------------------------------*/
43 static void insert_entity_in_owner (entity *ent) {
44 type *owner = ent->owner;
45 switch (get_type_tpop_code(owner)) {
47 add_class_member (owner, ent);
50 add_struct_member (owner, ent);
53 add_union_member (owner, ent);
56 set_array_element_entity(owner, ent);
63 new_entity (type *owner, ident *name, type *type)
68 assert(!id_contains_char(name, ' ') && "entity name should not contain spaces");
70 res = (entity *) xmalloc (sizeof (entity));
72 assert_legal_owner_of_ent(owner);
77 if (get_type_tpop(type) == type_method)
78 res->allocation = allocation_static;
80 res->allocation = allocation_automatic;
82 res->visibility = visibility_local;
84 if (is_method_type(type)) {
87 res->variability = variability_constant;
88 rem = current_ir_graph;
89 current_ir_graph = get_const_code_irg();
90 res->value = new_SymConst(sym, symconst_addr_ent);
91 current_ir_graph = rem;
93 res->variability = variability_uninitialized;
96 res->val_paths = NULL;
98 res->peculiarity = peculiarity_existent;
99 res->volatility = volatility_non_volatile;
100 res->stickyness = stickyness_unsticky;
102 if (is_class_type(owner)) {
103 res->overwrites = NEW_ARR_F(entity *, 0);
104 res->overwrittenby = NEW_ARR_F(entity *, 0);
106 res->overwrites = NULL;
107 res->overwrittenby = NULL;
112 res->nr = get_irp_new_node_nr();
117 /* Remember entity in it's owner. */
118 insert_entity_in_owner (res);
122 new_d_entity (type *owner, ident *name, type *type, dbg_info *db) {
123 entity *res = new_entity(owner, name, type);
124 set_entity_dbg_info(res, db);
128 static void free_entity_attrs(entity *ent) {
130 if (get_type_tpop(get_entity_owner(ent)) == type_class) {
131 DEL_ARR_F(ent->overwrites); ent->overwrites = NULL;
132 DEL_ARR_F(ent->overwrittenby); ent->overwrittenby = NULL;
134 assert(ent->overwrites == NULL);
135 assert(ent->overwrittenby == NULL);
137 /* if (ent->values) DEL_ARR_F(ent->values); *//* @@@ warum nich? */
138 if (ent->val_paths) {
139 if (is_compound_entity(ent))
140 for (i = 0; i < get_compound_ent_n_values(ent); i++)
141 if (ent->val_paths[i]) ;
142 /* free_compound_graph_path(ent->val_paths[i]) ; * @@@ warum nich? */
143 /* Geht nich: wird mehrfach verwendet!!! ==> mehrfach frei gegeben. */
144 /* DEL_ARR_F(ent->val_paths); */
146 ent->val_paths = NULL;
151 copy_entity_own (entity *old, type *new_owner) {
153 assert(old && old->kind == k_entity);
154 assert_legal_owner_of_ent(new_owner);
156 if (old->owner == new_owner) return old;
157 new = (entity *) xmalloc (sizeof (entity));
158 memcpy (new, old, sizeof (entity));
159 new->owner = new_owner;
160 if (is_class_type(new_owner)) {
161 new->overwrites = NEW_ARR_F(entity *, 0);
162 new->overwrittenby = NEW_ARR_F(entity *, 0);
165 new->nr = get_irp_new_node_nr();
168 insert_entity_in_owner (new);
174 copy_entity_name (entity *old, ident *new_name) {
176 assert(old && old->kind == k_entity);
178 if (old->name == new_name) return old;
179 new = (entity *) xmalloc (sizeof (entity));
180 memcpy (new, old, sizeof (entity));
181 new->name = new_name;
183 if (is_class_type(new->owner)) {
184 new->overwrites = DUP_ARR_F(entity *, old->overwrites);
185 new->overwrittenby = DUP_ARR_F(entity *, old->overwrittenby);
188 new->nr = get_irp_new_node_nr();
191 insert_entity_in_owner (new);
198 free_entity (entity *ent) {
199 assert(ent && ent->kind == k_entity);
200 free_entity_attrs(ent);
205 /* Outputs a unique number for this node */
207 get_entity_nr(entity *ent) {
208 assert(ent && ent->kind == k_entity);
217 (get_entity_name)(entity *ent) {
218 return __get_entity_name(ent);
222 (get_entity_ident)(entity *ent) {
223 return get_entity_ident(ent);
227 void set_entitye_ld_name (entity *, char *ld_name);
228 void set_entity_ld_ident (entity *, ident *ld_ident);
232 (get_entity_owner)(entity *ent) {
233 return __get_entity_owner(ent);
237 set_entity_owner (entity *ent, type *owner) {
238 assert(ent && ent->kind == k_entity);
239 assert_legal_owner_of_ent(owner);
243 void /* should this go into type.c? */
244 assert_legal_owner_of_ent(type *owner) {
245 assert(get_type_tpop_code(owner) == tpo_class ||
246 get_type_tpop_code(owner) == tpo_union ||
247 get_type_tpop_code(owner) == tpo_struct ||
248 get_type_tpop_code(owner) == tpo_array); /* Yes, array has an entity
249 -- to select fields! */
253 (get_entity_ld_ident)(entity *ent) {
254 return __get_entity_ld_ident(ent);
258 (set_entity_ld_ident)(entity *ent, ident *ld_ident) {
259 __set_entity_ld_ident(ent, ld_ident);
263 (get_entity_ld_name)(entity *ent) {
264 return __get_entity_ld_name(ent);
268 (get_entity_type)(entity *ent) {
269 return __get_entity_type(ent);
273 (set_entity_type)(entity *ent, type *type) {
274 __set_entity_type(ent, type);
278 (get_entity_allocation)(entity *ent) {
279 return __get_entity_allocation(ent);
283 (set_entity_allocation)(entity *ent, ent_allocation al) {
284 __set_entity_allocation(ent, al);
287 /* return the name of the visibility */
288 const char *get_allocation_name(ent_allocation all)
290 #define X(a) case a: return #a
292 X(allocation_automatic);
293 X(allocation_parameter);
294 X(allocation_dynamic);
295 X(allocation_static);
296 default: return "BAD VALUE";
303 (get_entity_visibility)(entity *ent) {
304 return __get_entity_visibility(ent);
308 set_entity_visibility (entity *ent, ent_visibility vis) {
309 assert(ent && ent->kind == k_entity);
310 if (vis != visibility_local)
311 assert((ent->allocation == allocation_static) ||
312 (ent->allocation == allocation_automatic));
313 /* @@@ Test that the owner type is not local, but how??
314 && get_class_visibility(get_entity_owner(ent)) != local));*/
315 ent->visibility = vis;
318 /* return the name of the visibility */
319 const char *get_visibility_name(ent_visibility vis)
321 #define X(a) case a: return #a
324 X(visibility_external_visible);
325 X(visibility_external_allocated);
326 default: return "BAD VALUE";
332 (get_entity_variability)(entity *ent) {
333 return __get_entity_variability(ent);
337 set_entity_variability (entity *ent, ent_variability var)
339 assert(ent && ent->kind == k_entity);
340 if (var == variability_part_constant)
341 assert(is_class_type(ent->type) || is_struct_type(ent->type));
343 if ((is_compound_type(ent->type)) &&
344 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
345 /* Allocate datastructures for constant values */
346 ent->values = NEW_ARR_F(ir_node *, 0);
347 ent->val_paths = NEW_ARR_F(compound_graph_path *, 0);
349 if ((is_atomic_type(ent->type)) &&
350 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
351 /* Set default constant value. */
352 ent->value = new_rd_Unknown(get_const_code_irg(), get_type_mode(ent->type));
355 if ((is_compound_type(ent->type)) &&
356 (var == variability_uninitialized) && (ent->variability != variability_uninitialized)) {
357 /* Free datastructures for constant values */
358 DEL_ARR_F(ent->values); ent->values = NULL;
359 DEL_ARR_F(ent->val_paths); ent->val_paths = NULL;
361 ent->variability = var;
364 /* return the name of the variablity */
365 const char *get_variability_name(ent_variability var)
367 #define X(a) case a: return #a
369 X(variability_uninitialized);
370 X(variability_initialized);
371 X(variability_part_constant);
372 X(variability_constant);
373 default: return "BAD VALUE";
379 (get_entity_volatility)(entity *ent) {
380 return __get_entity_volatility(ent);
384 (set_entity_volatility)(entity *ent, ent_volatility vol) {
385 __set_entity_volatility(ent, vol);
388 /* return the name of the volatility */
389 const char *get_volatility_name(ent_volatility var)
391 #define X(a) case a: return #a
393 X(volatility_non_volatile);
394 X(volatility_is_volatile);
395 default: return "BAD VALUE";
401 (get_entity_peculiarity)(entity *ent) {
402 return __get_entity_peculiarity(ent);
406 (set_entity_peculiarity)(entity *ent, peculiarity pec) {
407 __set_entity_peculiarity(ent, pec);
410 /* return the name of the peculiarity */
411 const char *get_peculiarity_name(peculiarity var)
413 #define X(a) case a: return #a
415 X(peculiarity_description);
416 X(peculiarity_inherited);
417 X(peculiarity_existent);
418 default: return "BAD VALUE";
423 /* Get the entity's stickyness */
425 (get_entity_stickyness)(entity *ent) {
426 return __get_entity_stickyness(ent);
429 /* Set the entity's stickyness */
431 (set_entity_stickyness)(entity *ent, ent_stickyness stickyness) {
432 __set_entity_stickyness(ent, stickyness);
435 /* Set has no effect for existent entities of type method. */
437 get_atomic_ent_value(entity *ent)
439 assert(ent && is_atomic_entity(ent));
440 assert(ent->variability != variability_uninitialized);
441 return skip_Id (ent->value);
445 set_atomic_ent_value(entity *ent, ir_node *val) {
446 assert(is_atomic_entity(ent) && (ent->variability != variability_uninitialized));
447 if (is_method_type(ent->type) && (ent->peculiarity == peculiarity_existent))
452 /* Returns true if the the node is representable as code on
454 int is_irn_const_expression(ir_node *n) {
458 switch(get_irn_opcode(n)) {
469 if (is_irn_const_expression(get_binop_left(n)))
470 return is_irn_const_expression(get_binop_right(n));
473 return is_irn_const_expression(get_irn_n(n, 0));
482 ir_node *copy_const_value(ir_node *n) {
487 switch(get_irn_opcode(n)) {
489 nn = new_Const(m, get_Const_tarval(n)); break;
492 nn = new_SymConst(get_SymConst_symbol(n), get_SymConst_kind(n));
495 nn = new_Add(copy_const_value(get_Add_left(n)),
496 copy_const_value(get_Add_right(n)), m); break;
498 nn = new_Sub(copy_const_value(get_Sub_left(n)),
499 copy_const_value(get_Sub_right(n)), m); break;
501 nn = new_Mul(copy_const_value(get_Mul_left(n)),
502 copy_const_value(get_Mul_right(n)), m); break;
504 nn = new_And(copy_const_value(get_And_left(n)),
505 copy_const_value(get_And_right(n)), m); break;
507 nn = new_Or(copy_const_value(get_Or_left(n)),
508 copy_const_value(get_Or_right(n)), m); break;
510 nn = new_Eor(copy_const_value(get_Eor_left(n)),
511 copy_const_value(get_Eor_right(n)), m); break;
513 nn = new_Cast(copy_const_value(get_Cast_op(n)), get_Cast_type(n)); break;
515 nn = new_Conv(copy_const_value(get_Conv_op(n)), m); break;
517 nn = new_Unknown(m); break;
520 assert(0 && "opdope invalid or not implemented");
527 compound_graph_path *
528 new_compound_graph_path(type *tp, int length) {
529 compound_graph_path *res;
530 assert(is_type(tp) && is_compound_type(tp));
533 res = (compound_graph_path *) calloc (1, sizeof(compound_graph_path) + (length-1) * sizeof(entity *));
534 res->kind = k_ir_compound_graph_path;
537 res ->arr_indicees = (int *) calloc(length, sizeof(int));
542 free_compound_graph_path (compound_graph_path *gr) {
543 assert(gr && is_compound_graph_path(gr));
545 free(gr ->arr_indicees);
550 is_compound_graph_path(void *thing) {
551 return (get_kind(thing) == k_ir_compound_graph_path);
554 /* checks whether nodes 0..pos are correct (all lie on a path.) */
555 /* @@@ not implemented */
556 int is_proper_compound_graph_path(compound_graph_path *gr, int pos) {
559 type *owner = gr->tp;
560 for (i = 0; i <= pos; i++) {
561 node = get_compound_graph_path_node(gr, i);
562 if (get_entity_owner(node) != owner) return false;
563 owner = get_entity_type(node);
565 if (pos == get_compound_graph_path_length(gr))
566 if (!is_atomic_type(owner)) return false;
571 get_compound_graph_path_length(compound_graph_path *gr) {
572 assert(gr && is_compound_graph_path(gr));
577 get_compound_graph_path_node(compound_graph_path *gr, int pos) {
578 assert(gr && is_compound_graph_path(gr));
579 assert(pos >= 0 && pos < gr->len);
580 return gr->nodes[pos];
584 set_compound_graph_path_node(compound_graph_path *gr, int pos, entity *node) {
585 assert(gr && is_compound_graph_path(gr));
586 assert(pos >= 0 && pos < gr->len);
587 assert(is_entity(node));
588 gr->nodes[pos] = node;
589 assert(is_proper_compound_graph_path(gr, pos));
593 get_compound_graph_path_array_index(compound_graph_path *gr, int pos) {
594 assert(gr && is_compound_graph_path(gr));
595 assert(pos >= 0 && pos < gr->len);
596 return gr->arr_indicees[pos];
600 set_compound_graph_path_array_index(compound_graph_path *gr, int pos, int index) {
601 assert(gr && is_compound_graph_path(gr));
602 assert(pos >= 0 && pos < gr->len);
603 gr->arr_indicees[pos] = index;
606 /* A value of a compound entity is a pair of value and the corresponding path to a member of
609 add_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path) {
610 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
611 ARR_APP1 (ir_node *, ent->values, val);
612 ARR_APP1 (compound_graph_path *, ent->val_paths, path);
616 set_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path, int pos) {
617 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
618 ent->values[pos] = val;
619 ent->val_paths[pos] = path;
623 get_compound_ent_n_values(entity *ent) {
624 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
625 return (ARR_LEN (ent->values));
629 get_compound_ent_value(entity *ent, int pos) {
630 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
631 return ent->values[pos];
634 compound_graph_path *
635 get_compound_ent_value_path(entity *ent, int pos) {
636 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
637 return ent->val_paths[pos];
641 remove_compound_ent_value(entity *ent, entity *value_ent) {
643 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
644 for (i = 0; i < (ARR_LEN (ent->val_paths)); i++) {
645 compound_graph_path *path = ent->val_paths[i];
646 if (path->nodes[path->len-1] == value_ent) {
647 for(; i < (ARR_LEN (ent->val_paths))-1; i++) {
648 ent->val_paths[i] = ent->val_paths[i+1];
649 ent->values[i] = ent->values[i+1];
651 ARR_SETLEN(entity*, ent->val_paths, ARR_LEN(ent->val_paths) - 1);
652 ARR_SETLEN(ir_node*, ent->values, ARR_LEN(ent->values) - 1);
659 add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
660 compound_graph_path *path;
661 type *owner_tp = get_entity_owner(ent);
662 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
663 path = new_compound_graph_path(owner_tp, 1);
664 path->nodes[0] = member;
665 if (is_array_type(owner_tp)) {
669 assert(get_array_n_dimensions(owner_tp) == 1 && has_array_lower_bound(owner_tp, 0));
670 max = get_array_lower_bound_int(owner_tp, 0) -1;
671 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
672 int index = get_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0);
677 path->arr_indicees[0] = max + 1;
679 add_compound_ent_value_w_path(ent, val, path);
682 /* Copies the firm subgraph referenced by val to const_code_irg and adds
683 the node as constant initialization to ent.
684 The subgraph may not contain control flow operations.
686 copy_and_add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
687 ir_graph *rem = current_ir_graph;
689 assert(get_entity_variability(ent) != variability_uninitialized);
690 current_ir_graph = get_const_code_irg();
692 val = copy_const_value(val);
693 add_compound_ent_value(ent, val, member);
694 current_ir_graph = rem;
697 /* Copies the value i of the entity to current_block in current_ir_graph.
699 copy_compound_ent_value(entity *ent, int pos) {
700 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
701 return copy_const_value(ent->values[pos+1]);
705 get_compound_ent_value_member(entity *ent, int pos) {
706 compound_graph_path *path;
707 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
708 path = get_compound_ent_value_path(ent, pos);
710 return get_compound_graph_path_node(path, get_compound_graph_path_length(path)-1);
714 set_compound_ent_value(entity *ent, ir_node *val, entity *member, int pos) {
715 compound_graph_path *path;
716 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
717 path = get_compound_ent_value_path(ent, pos);
718 set_compound_graph_path_node(path, 0, member);
719 set_compound_ent_value_w_path(ent, val, path, pos);
723 set_array_entity_values(entity *ent, tarval **values, int num_vals) {
725 ir_graph *rem = current_ir_graph;
726 type *arrtp = get_entity_type(ent);
729 assert(is_array_type(arrtp));
730 assert(get_array_n_dimensions(arrtp) == 1);
731 /* One bound is sufficient, the nunmber of constant fields makes the
733 assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
734 assert(get_entity_variability(ent) != variability_uninitialized);
735 current_ir_graph = get_const_code_irg();
737 for (i = 0; i < num_vals; i++) {
738 val = new_Const(get_tarval_mode (values[i]), values[i]);
739 add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
740 set_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0, i);
742 current_ir_graph = rem;
745 int get_compound_ent_value_offset_bits(entity *ent, int pos) {
746 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
748 compound_graph_path *path = get_compound_ent_value_path(ent, pos);
749 int i, path_len = get_compound_graph_path_length(path);
752 for (i = 0; i < path_len; ++i) {
753 entity *node = get_compound_graph_path_node(path, i);
754 type *node_tp = get_entity_type(node);
755 type *owner_tp = get_entity_owner(node);
756 if (is_array_type(owner_tp)) {
757 int size = get_mode_size_bits (get_type_mode(node_tp));
758 int align = get_mode_align_bits(get_type_mode(node_tp));
762 assert(size % align == 0);
763 /* ansonsten aufrunden */
765 offset += size * get_compound_graph_path_array_index(path, i);
767 offset += get_entity_offset_bits(node);
773 int get_compound_ent_value_offset_bytes(entity *ent, int pos) {
774 int offset = get_compound_ent_value_offset_bits(ent, pos);
775 assert(offset % 8 == 0);
780 static void init_index(type *arr) {
784 assert(get_array_n_dimensions(arr) == 1);
786 if (has_array_lower_bound(arr, dim))
787 init = get_array_lower_bound_int(arr, 0) -1;
789 init = get_array_upper_bound_int(arr, 0) +1;
791 set_entity_link(get_array_element_entity(arr), (void *)init);
795 static int get_next_index(entity *elem_ent) {
796 type *arr = get_entity_owner(elem_ent);
800 assert(get_array_n_dimensions(arr) == 1);
802 if (has_array_lower_bound(arr, dim)) {
803 next = (int)get_entity_link(elem_ent) +1;
804 if (has_array_upper_bound(arr, dim)) {
805 int upper = get_array_upper_bound_int(arr, dim);
806 if (next == upper) next = get_array_lower_bound_int(arr, dim);
809 next = (int)get_entity_link(elem_ent) -1;
810 if (has_array_lower_bound(arr, dim)) {
811 int upper = get_array_upper_bound_int(arr, dim);
812 if (next == upper) next = get_array_upper_bound_int(arr, dim);
816 set_entity_link(elem_ent, (void *)next);
820 /* Compute the array indicees in compound graph paths of initialized entities.
822 * All arrays must have fixed lower and upper bounds. One array can
823 * have an open bound. If there are several open bounds, we do
824 * nothing. There must be initializer elements for all array
825 * elements. Uses the link field in the array element entities. The
826 * array bounds must be representable as ints.
828 * (If the bounds are not representable as ints we have to represent
829 * the indicees as firm nodes. But the still we must be able to
830 * evaluate the index against the upper bound.)
832 void compute_compound_ent_array_indicees(entity *ent) {
833 type *tp = get_entity_type(ent);
835 entity *unknown_bound_entity = NULL;
837 if (!is_compound_type(tp) ||
838 (ent->variability == variability_uninitialized)) return ;
840 n_vals = get_compound_ent_n_values(ent);
841 if (n_vals == 0) return;
843 /* We can not compute the indicees if there is more than one array
844 with an unknown bound. For this remember the first entity that
845 represents such an array. It could be ent. */
846 if (is_array_type(tp)) {
847 assert(get_array_n_dimensions(tp) == 1 && "other not implemented");
849 if (!has_array_lower_bound(tp, dim) || !has_array_upper_bound(tp, dim))
850 unknown_bound_entity = ent;
853 /* Initialize the entity links to lower bound -1 and test all path elements
855 for (i = 0; i < n_vals; ++i) {
856 compound_graph_path *path = get_compound_ent_value_path(ent, i);
857 int j, path_len = get_compound_graph_path_length(path);
858 for (j = 0; j < path_len; ++j) {
859 entity *node = get_compound_graph_path_node(path, j);
860 type *elem_tp = get_entity_type(node);
862 if (is_array_type(elem_tp)) {
863 assert(get_array_n_dimensions(elem_tp) == 1 && "other not implemented");
865 if (!has_array_lower_bound(elem_tp, dim) || !has_array_upper_bound(elem_tp, dim)) {
866 if (!unknown_bound_entity) unknown_bound_entity = node;
867 if (node != unknown_bound_entity) return;
875 /* Finally compute the indicees ... */
876 for (i = 0; i < n_vals; ++i) {
877 compound_graph_path *path = get_compound_ent_value_path(ent, i);
878 int j, path_len = get_compound_graph_path_length(path);
879 for (j = 0; j < path_len; ++j) {
880 entity *node = get_compound_graph_path_node(path, j);
881 type *owner_tp = get_entity_owner(node);
882 if (is_array_type(owner_tp))
883 set_compound_graph_path_array_index (path, j, get_next_index(node));
890 static int *resize (int *buf, int new_size) {
891 int *new_buf = (int *)calloc(new_size, 4);
892 memcpy(new_buf, buf, new_size>1);
897 /* We sort the elements by placing them at their bit offset in an
898 array where each entry represents one bit called permutation. In
899 fact, we do not place the values themselves, as we would have to
900 copy two things, the value and the path. We only remember the
901 position in the old order. Each value should have a distinct
902 position in the permutation.
904 A second iteration now permutes the actual elements into two
906 void sort_compound_ent_values(entity *ent) {
907 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
909 type *tp = get_entity_type(ent);
910 int i, n_vals = get_compound_ent_n_values(ent);
911 int tp_size = get_type_size_bits(tp);
915 if (!is_compound_type(tp) ||
916 (ent->variability == variability_uninitialized) ||
917 (get_type_state(tp) != layout_fixed) ||
918 (n_vals == 0) ) return;
920 /* estimated upper bound for size. Better: use flexible array ... */
921 size = ((tp_size > (n_vals * 32)) ? tp_size : (n_vals * 32)) * 4;
922 permutation = (int *)calloc(size, 4);
923 for (i = 0; i < n_vals; ++i) {
924 int pos = get_compound_ent_value_offset_bits(ent, i);
925 while (pos >= size) {
927 permutation = resize(permutation, size);
930 assert(permutation[pos] == 0 && "two values with the same offset");
931 permutation[pos] = i + 1; /* We initialized with 0, so we can not distinguish entry 0.
932 So inc all entries by one. */
933 //fprintf(stderr, "i: %d, pos: %d \n", i, pos);
937 ir_node **my_values = NEW_ARR_F(ir_node *, n_vals);
938 compound_graph_path **my_paths = NEW_ARR_F(compound_graph_path *, n_vals);
939 for (i = 0; i < size; ++i) {
940 int pos = permutation[i];
942 //fprintf(stderr, "pos: %d i: %d next %d \n", i, pos, next);
943 assert(next < n_vals);
944 pos--; /* We increased the pos by one */
945 my_values[next] = get_compound_ent_value (ent, pos);
946 my_paths [next] = get_compound_ent_value_path(ent, pos);
952 DEL_ARR_F(ent->values);
953 ent->values = my_values;
954 DEL_ARR_F(ent->val_paths);
955 ent->val_paths = my_paths;
959 (get_entity_offset_bytes)(entity *ent) {
960 return __get_entity_offset_bytes(ent);
964 (get_entity_offset_bits)(entity *ent) {
965 return __get_entity_offset_bits(ent);
969 (set_entity_offset_bytes)(entity *ent, int offset) {
970 __set_entity_offset_bytes(ent, offset);
974 (set_entity_offset_bits)(entity *ent, int offset) {
975 __set_entity_offset_bits(ent, offset);
979 add_entity_overwrites(entity *ent, entity *overwritten) {
980 assert(ent && is_class_type(get_entity_owner(ent)));
981 ARR_APP1(entity *, ent->overwrites, overwritten);
982 ARR_APP1(entity *, overwritten->overwrittenby, ent);
986 get_entity_n_overwrites(entity *ent) {
987 assert(ent && is_class_type(get_entity_owner(ent)));
988 return (ARR_LEN(ent->overwrites));
992 get_entity_overwrites_index(entity *ent, entity *overwritten) {
994 assert(ent && is_class_type(get_entity_owner(ent)));
995 for (i = 0; i < get_entity_n_overwrites(ent); i++)
996 if (get_entity_overwrites(ent, i) == overwritten)
1002 get_entity_overwrites (entity *ent, int pos) {
1003 assert(ent && is_class_type(get_entity_owner(ent)));
1004 assert(pos < get_entity_n_overwrites(ent));
1005 return ent->overwrites[pos];
1009 set_entity_overwrites (entity *ent, int pos, entity *overwritten) {
1010 assert(ent && is_class_type(get_entity_owner(ent)));
1011 assert(pos < get_entity_n_overwrites(ent));
1012 ent->overwrites[pos] = overwritten;
1016 remove_entity_overwrites(entity *ent, entity *overwritten) {
1018 assert(ent && is_class_type(get_entity_owner(ent)));
1019 for (i = 0; i < (ARR_LEN (ent->overwrites)); i++)
1020 if (ent->overwrites[i] == overwritten) {
1021 for(; i < (ARR_LEN (ent->overwrites))-1; i++)
1022 ent->overwrites[i] = ent->overwrites[i+1];
1023 ARR_SETLEN(entity*, ent->overwrites, ARR_LEN(ent->overwrites) - 1);
1029 add_entity_overwrittenby (entity *ent, entity *overwrites) {
1030 assert(ent && is_class_type(get_entity_owner(ent)));
1031 add_entity_overwrites(overwrites, ent);
1035 get_entity_n_overwrittenby (entity *ent) {
1036 assert(ent && is_class_type(get_entity_owner(ent)));
1037 return (ARR_LEN (ent->overwrittenby));
1041 get_entity_overwrittenby_index(entity *ent, entity *overwrites) {
1043 assert(ent && is_class_type(get_entity_owner(ent)));
1044 for (i = 0; i < get_entity_n_overwrittenby(ent); i++)
1045 if (get_entity_overwrittenby(ent, i) == overwrites)
1051 get_entity_overwrittenby (entity *ent, int pos) {
1052 assert(ent && is_class_type(get_entity_owner(ent)));
1053 assert(pos < get_entity_n_overwrittenby(ent));
1054 return ent->overwrittenby[pos];
1058 set_entity_overwrittenby (entity *ent, int pos, entity *overwrites) {
1059 assert(ent && is_class_type(get_entity_owner(ent)));
1060 assert(pos < get_entity_n_overwrittenby(ent));
1061 ent->overwrittenby[pos] = overwrites;
1064 void remove_entity_overwrittenby(entity *ent, entity *overwrites) {
1066 assert(ent && is_class_type(get_entity_owner(ent)));
1067 for (i = 0; i < (ARR_LEN (ent->overwrittenby)); i++)
1068 if (ent->overwrittenby[i] == overwrites) {
1069 for(; i < (ARR_LEN (ent->overwrittenby))-1; i++)
1070 ent->overwrittenby[i] = ent->overwrittenby[i+1];
1071 ARR_SETLEN(entity*, ent->overwrittenby, ARR_LEN(ent->overwrittenby) - 1);
1076 /* A link to store intermediate information */
1078 (get_entity_link)(entity *ent) {
1079 return __get_entity_link(ent);
1083 (set_entity_link)(entity *ent, void *l) {
1084 __set_entity_link(ent, l);
1088 (get_entity_irg)(entity *ent) {
1089 return __get_entity_irg(ent);
1093 set_entity_irg(entity *ent, ir_graph *irg) {
1094 assert(ent && is_method_type(get_entity_type(ent)));
1095 /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
1096 * Methode selbst nicht mehr aufgerufen werden kann, die Entität
1097 * aber erhalten bleiben soll. */
1099 assert((irg && ent->peculiarity == peculiarity_existent) ||
1100 (!irg && ent->peculiarity == peculiarity_description) ||
1101 (!irg && ent->peculiarity == peculiarity_inherited));
1106 (is_entity)(void *thing) {
1107 return __is_entity(thing);
1110 int is_atomic_entity(entity *ent) {
1111 type* t = get_entity_type(ent);
1112 assert(ent && ent->kind == k_entity);
1113 return (is_primitive_type(t) || is_pointer_type(t) ||
1114 is_enumeration_type(t) || is_method_type(t));
1117 int is_compound_entity(entity *ent) {
1118 type* t = get_entity_type(ent);
1119 assert(ent && ent->kind == k_entity);
1120 return (is_class_type(t) || is_struct_type(t) ||
1121 is_array_type(t) || is_union_type(t));
1125 * @todo not implemnted!!! */
1126 bool equal_entity(entity *ent1, entity *ent2) {
1127 fprintf(stderr, " calling unimplemented equal entity!!! \n");
1132 unsigned long get_entity_visited(entity *ent) {
1133 assert(ent && ent->kind == k_entity);
1136 void set_entity_visited(entity *ent, unsigned long num) {
1137 assert(ent && ent->kind == k_entity);
1140 /* Sets visited field in entity to entity_visited. */
1141 void mark_entity_visited(entity *ent) {
1142 assert(ent && ent->kind == k_entity);
1143 ent->visit = type_visited;
1147 bool entity_visited(entity *ent) {
1148 assert(ent && ent->kind == k_entity);
1149 return get_entity_visited(ent) >= type_visited;
1152 bool entity_not_visited(entity *ent) {
1153 assert(ent && ent->kind == k_entity);
1154 return get_entity_visited(ent) < type_visited;
1157 /* Need two routines because I want to assert the result. */
1158 static entity *resolve_ent_polymorphy2 (type *dynamic_class, entity* static_ent) {
1159 int i, n_overwrittenby;
1162 if (get_entity_owner(static_ent) == dynamic_class) return static_ent;
1164 n_overwrittenby = get_entity_n_overwrittenby(static_ent);
1165 for (i = 0; i < n_overwrittenby; ++i) {
1166 res = resolve_ent_polymorphy2(dynamic_class, get_entity_overwrittenby(static_ent, i));
1173 /** Resolve polymorphy in the inheritance relation.
1175 * Returns the dynamically referenced entity if the static entity and the
1176 * dynamic type are given.
1177 * Search downwards in overwritten tree. */
1178 entity *resolve_ent_polymorphy(type *dynamic_class, entity* static_ent) {
1180 assert(static_ent && static_ent->kind == k_entity);
1182 res = resolve_ent_polymorphy2(dynamic_class, static_ent);
1184 printf(" Could not find entity "); DDME(static_ent);
1185 printf(" in "); DDMT(dynamic_class);
1187 dump_entity(static_ent);
1188 dump_type(get_entity_owner(static_ent));
1189 dump_type(dynamic_class);
1197 /*******************************************************************/
1199 /*******************************************************************/
1202 #if 1 || DEBUG_libfirm
1203 int dump_node_opcode(FILE *F, ir_node *n); /* from irdump.c */
1207 #define X(a) case a: fprintf(F, #a); break
1208 void dump_entity_to_file (FILE *F, entity *ent) {
1210 assert(ent && ent->kind == k_entity);
1211 type *owner = get_entity_owner(ent);
1212 type *type = get_entity_type(ent);
1213 fprintf(F, "entity %s (%ld)\n", get_entity_name(ent), get_entity_nr(ent));
1214 fprintf(F, " type: %s (%ld)\n", get_type_name(type), get_type_nr(type));
1215 fprintf(F, " owner: %s (%ld)\n", get_type_name(owner), get_type_nr(owner));
1217 if (is_class_type(get_entity_owner(ent))) {
1218 if (get_entity_n_overwrites(ent) > 0) {
1219 fprintf(F, " overwrites:\n");
1220 for (i = 0; i < get_entity_n_overwrites(ent); ++i) {
1221 entity *ov = get_entity_overwrites(ent, i);
1222 fprintf(F, " %d: %s of class %s\n", i, get_entity_name(ov), get_type_name(get_entity_owner(ov)));
1225 fprintf(F, " Does not overwrite other entities. \n");
1227 if (get_entity_n_overwrittenby(ent) > 0) {
1228 fprintf(F, " overwritten by:\n");
1229 for (i = 0; i < get_entity_n_overwrittenby(ent); ++i) {
1230 entity *ov = get_entity_overwrittenby(ent, i);
1231 fprintf(F, " %d: %s of class %s\n", i, get_entity_name(ov), get_type_name(get_entity_owner(ov)));
1234 fprintf(F, " Is not overwriten by other entities. \n");
1238 fprintf(F, " allocation: ");
1239 switch (get_entity_allocation(ent)) {
1240 X(allocation_dynamic);
1241 X(allocation_automatic);
1242 X(allocation_static);
1243 X(allocation_parameter);
1246 fprintf(F, "\n visibility: ");
1247 switch (get_entity_visibility(ent)) {
1248 X(visibility_local);
1249 X(visibility_external_visible);
1250 X(visibility_external_allocated);
1253 fprintf(F, "\n variability: ");
1254 switch (get_entity_variability(ent)) {
1255 X(variability_uninitialized);
1256 X(variability_initialized);
1257 X(variability_part_constant);
1258 X(variability_constant);
1261 if (get_entity_variability(ent) != variability_uninitialized) {
1262 if (is_atomic_entity(ent)) {
1263 fprintf(F, "\n atomic value: ");
1264 dump_node_opcode(F, get_atomic_ent_value(ent));
1266 fprintf(F, "\n compound values:");
1267 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
1268 compound_graph_path *path = get_compound_ent_value_path(ent, i);
1269 entity *ent0 = get_compound_graph_path_node(path, 0);
1270 fprintf(F, "\n %3d ", get_entity_offset_bits(ent0));
1271 if (get_type_state(type) == layout_fixed)
1272 fprintf(F, "(%3d) ", get_compound_ent_value_offset_bits(ent, i));
1273 fprintf(F, "%s", get_entity_name(ent0));
1274 for (j = 0; j < get_compound_graph_path_length(path); ++j) {
1275 entity *node = get_compound_graph_path_node(path, j);
1276 fprintf(F, ".%s", get_entity_name(node));
1277 if (is_array_type(get_entity_owner(node)))
1278 fprintf(F, "[%d]", get_compound_graph_path_array_index(path, j));
1280 fprintf(F, "\t = ");
1281 dump_node_opcode(F, get_compound_ent_value(ent, i));
1286 fprintf(F, "\n volatility: ");
1287 switch (get_entity_volatility(ent)) {
1288 X(volatility_non_volatile);
1289 X(volatility_is_volatile);
1292 fprintf(F, "\n peculiarity: %s", get_peculiarity_string(get_entity_peculiarity(ent)));
1293 fprintf(F, "\n ld_name: %s", ent->ld_name ? get_entity_ld_name(ent) : "no yet set");
1294 fprintf(F, "\n offset: %d", get_entity_offset_bits(ent));
1295 if (is_method_type(get_entity_type(ent))) {
1296 if (get_entity_irg(ent)) /* can be null */ {
1297 fprintf(F, "\n irg = %ld", get_irg_graph_nr(get_entity_irg(ent)));
1298 if (get_irp_callgraph_state() == irp_callgraph_and_calltree_consistent) {
1299 fprintf(F, "\n recursion depth %d", get_irg_recursion_depth(get_entity_irg(ent)));
1300 fprintf(F, "\n loop depth %d", get_irg_loop_depth(get_entity_irg(ent)));
1303 fprintf(F, "\n irg = NULL");
1310 void dump_entity (entity *ent) {
1311 dump_entity_to_file(stdout, ent);
1314 #else /* DEBUG_libfirm */
1315 void dump_entity_to_file (FILE *F, entity *ent) {}
1316 void dump_entity (entity *ent) {}
1317 #endif /* DEBUG_libfirm */