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);
350 if ((is_compound_type(ent->type)) &&
351 (var == variability_uninitialized) && (ent->variability != variability_uninitialized)) {
352 /* Free datastructures for constant values */
353 DEL_ARR_F(ent->values); ent->values = NULL;
354 DEL_ARR_F(ent->val_paths); ent->val_paths = NULL;
356 ent->variability = var;
359 /* return the name of the variablity */
360 const char *get_variability_name(ent_variability var)
362 #define X(a) case a: return #a
364 X(variability_uninitialized);
365 X(variability_initialized);
366 X(variability_part_constant);
367 X(variability_constant);
368 default: return "BAD VALUE";
374 (get_entity_volatility)(entity *ent) {
375 return __get_entity_volatility(ent);
379 (set_entity_volatility)(entity *ent, ent_volatility vol) {
380 __set_entity_volatility(ent, vol);
383 /* return the name of the volatility */
384 const char *get_volatility_name(ent_volatility var)
386 #define X(a) case a: return #a
388 X(volatility_non_volatile);
389 X(volatility_is_volatile);
390 default: return "BAD VALUE";
396 (get_entity_peculiarity)(entity *ent) {
397 return __get_entity_peculiarity(ent);
401 (set_entity_peculiarity)(entity *ent, peculiarity pec) {
402 __set_entity_peculiarity(ent, pec);
405 /* return the name of the peculiarity */
406 const char *get_peculiarity_name(peculiarity var)
408 #define X(a) case a: return #a
410 X(peculiarity_description);
411 X(peculiarity_inherited);
412 X(peculiarity_existent);
413 default: return "BAD VALUE";
418 /* Get the entity's stickyness */
420 (get_entity_stickyness)(entity *ent) {
421 return __get_entity_stickyness(ent);
424 /* Set the entity's stickyness */
426 (set_entity_stickyness)(entity *ent, ent_stickyness stickyness) {
427 __set_entity_stickyness(ent, stickyness);
430 /* Set has no effect for existent entities of type method. */
432 get_atomic_ent_value(entity *ent)
434 assert(ent && is_atomic_entity(ent));
435 assert(ent->variability != variability_uninitialized);
436 return skip_Id (ent->value);
440 set_atomic_ent_value(entity *ent, ir_node *val) {
441 assert(is_atomic_entity(ent) && (ent->variability != variability_uninitialized));
442 if (is_method_type(ent->type) && (ent->peculiarity == peculiarity_existent))
447 /* Returns true if the the node is representable as code on
449 int is_irn_const_expression(ir_node *n) {
453 switch(get_irn_opcode(n)) {
464 if (is_irn_const_expression(get_binop_left(n)))
465 return is_irn_const_expression(get_binop_right(n));
468 return is_irn_const_expression(get_irn_n(n, 0));
477 ir_node *copy_const_value(ir_node *n) {
482 switch(get_irn_opcode(n)) {
484 nn = new_Const(m, get_Const_tarval(n)); break;
487 nn = new_SymConst(get_SymConst_symbol(n), get_SymConst_kind(n));
490 nn = new_Add(copy_const_value(get_Add_left(n)),
491 copy_const_value(get_Add_right(n)), m); break;
493 nn = new_Sub(copy_const_value(get_Sub_left(n)),
494 copy_const_value(get_Sub_right(n)), m); break;
496 nn = new_Mul(copy_const_value(get_Mul_left(n)),
497 copy_const_value(get_Mul_right(n)), m); break;
499 nn = new_And(copy_const_value(get_And_left(n)),
500 copy_const_value(get_And_right(n)), m); break;
502 nn = new_Or(copy_const_value(get_Or_left(n)),
503 copy_const_value(get_Or_right(n)), m); break;
505 nn = new_Eor(copy_const_value(get_Eor_left(n)),
506 copy_const_value(get_Eor_right(n)), m); break;
508 nn = new_Cast(copy_const_value(get_Cast_op(n)), get_Cast_type(n)); break;
510 nn = new_Conv(copy_const_value(get_Conv_op(n)), m); break;
512 nn = new_Unknown(m); break;
515 assert(0 && "opdope invalid or not implemented");
522 compound_graph_path *
523 new_compound_graph_path(type *tp, int length) {
524 compound_graph_path *res;
525 assert(is_type(tp) && is_compound_type(tp));
528 res = (compound_graph_path *) calloc (1, sizeof(compound_graph_path) + (length-1) * sizeof(entity *));
529 res->kind = k_ir_compound_graph_path;
532 res ->arr_indicees = (int *) calloc(length, sizeof(int));
537 free_compound_graph_path (compound_graph_path *gr) {
538 assert(gr && is_compound_graph_path(gr));
540 free(gr ->arr_indicees);
545 is_compound_graph_path(void *thing) {
546 return (get_kind(thing) == k_ir_compound_graph_path);
549 /* checks whether nodes 0..pos are correct (all lie on a path.) */
550 /* @@@ not implemented */
551 int is_proper_compound_graph_path(compound_graph_path *gr, int pos) {
554 type *owner = gr->tp;
555 for (i = 0; i <= pos; i++) {
556 node = get_compound_graph_path_node(gr, i);
557 if (get_entity_owner(node) != owner) return false;
558 owner = get_entity_type(node);
560 if (pos == get_compound_graph_path_length(gr))
561 if (!is_atomic_type(owner)) return false;
566 get_compound_graph_path_length(compound_graph_path *gr) {
567 assert(gr && is_compound_graph_path(gr));
572 get_compound_graph_path_node(compound_graph_path *gr, int pos) {
573 assert(gr && is_compound_graph_path(gr));
574 assert(pos >= 0 && pos < gr->len);
575 return gr->nodes[pos];
579 set_compound_graph_path_node(compound_graph_path *gr, int pos, entity *node) {
580 assert(gr && is_compound_graph_path(gr));
581 assert(pos >= 0 && pos < gr->len);
582 assert(is_entity(node));
583 gr->nodes[pos] = node;
584 assert(is_proper_compound_graph_path(gr, pos));
588 get_compound_graph_path_array_index(compound_graph_path *gr, int pos) {
589 assert(gr && is_compound_graph_path(gr));
590 assert(pos >= 0 && pos < gr->len);
591 return gr->arr_indicees[pos];
595 set_compound_graph_path_array_index(compound_graph_path *gr, int pos, int index) {
596 assert(gr && is_compound_graph_path(gr));
597 assert(pos >= 0 && pos < gr->len);
598 gr->arr_indicees[pos] = index;
601 /* A value of a compound entity is a pair of value and the corresponding path to a member of
604 add_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path) {
605 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
606 ARR_APP1 (ir_node *, ent->values, val);
607 ARR_APP1 (compound_graph_path *, ent->val_paths, path);
611 set_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path, int pos) {
612 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
613 ent->values[pos] = val;
614 ent->val_paths[pos] = path;
618 get_compound_ent_n_values(entity *ent) {
619 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
620 return (ARR_LEN (ent->values));
624 get_compound_ent_value(entity *ent, int pos) {
625 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
626 return ent->values[pos];
629 compound_graph_path *
630 get_compound_ent_value_path(entity *ent, int pos) {
631 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
632 return ent->val_paths[pos];
636 remove_compound_ent_value(entity *ent, entity *value_ent) {
638 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
639 for (i = 0; i < (ARR_LEN (ent->val_paths)); i++) {
640 compound_graph_path *path = ent->val_paths[i];
641 if (path->nodes[path->len-1] == value_ent) {
642 for(; i < (ARR_LEN (ent->val_paths))-1; i++) {
643 ent->val_paths[i] = ent->val_paths[i+1];
644 ent->values[i] = ent->values[i+1];
646 ARR_SETLEN(entity*, ent->val_paths, ARR_LEN(ent->val_paths) - 1);
647 ARR_SETLEN(ir_node*, ent->values, ARR_LEN(ent->values) - 1);
654 add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
655 compound_graph_path *path;
656 type *owner_tp = get_entity_owner(ent);
657 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
658 path = new_compound_graph_path(owner_tp, 1);
659 path->nodes[0] = member;
660 if (is_array_type(owner_tp)) {
664 assert(get_array_n_dimensions(owner_tp) == 1 && has_array_lower_bound(owner_tp, 0));
665 max = get_array_lower_bound_int(owner_tp, 0) -1;
666 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
667 int index = get_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0);
672 path->arr_indicees[0] = max + 1;
674 add_compound_ent_value_w_path(ent, val, path);
677 /* Copies the firm subgraph referenced by val to const_code_irg and adds
678 the node as constant initialization to ent.
679 The subgraph may not contain control flow operations.
681 copy_and_add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
682 ir_graph *rem = current_ir_graph;
684 assert(get_entity_variability(ent) != variability_uninitialized);
685 current_ir_graph = get_const_code_irg();
687 val = copy_const_value(val);
688 add_compound_ent_value(ent, val, member);
689 current_ir_graph = rem;
692 /* Copies the value i of the entity to current_block in current_ir_graph.
694 copy_compound_ent_value(entity *ent, int pos) {
695 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
696 return copy_const_value(ent->values[pos+1]);
700 get_compound_ent_value_member(entity *ent, int pos) {
701 compound_graph_path *path;
702 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
703 path = get_compound_ent_value_path(ent, pos);
705 return get_compound_graph_path_node(path, get_compound_graph_path_length(path)-1);
709 set_compound_ent_value(entity *ent, ir_node *val, entity *member, int pos) {
710 compound_graph_path *path;
711 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
712 path = get_compound_ent_value_path(ent, pos);
713 set_compound_graph_path_node(path, 0, member);
714 set_compound_ent_value_w_path(ent, val, path, pos);
718 set_array_entity_values(entity *ent, tarval **values, int num_vals) {
720 ir_graph *rem = current_ir_graph;
721 type *arrtp = get_entity_type(ent);
724 assert(is_array_type(arrtp));
725 assert(get_array_n_dimensions(arrtp) == 1);
726 /* One bound is sufficient, the nunmber of constant fields makes the
728 assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
729 assert(get_entity_variability(ent) != variability_uninitialized);
730 current_ir_graph = get_const_code_irg();
732 for (i = 0; i < num_vals; i++) {
733 val = new_Const(get_tarval_mode (values[i]), values[i]);
734 add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
735 set_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0, i);
737 current_ir_graph = rem;
740 int get_compound_ent_value_offset_bits(entity *ent, int pos) {
741 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
743 compound_graph_path *path = get_compound_ent_value_path(ent, pos);
744 int i, path_len = get_compound_graph_path_length(path);
747 for (i = 0; i < path_len; ++i) {
748 entity *node = get_compound_graph_path_node(path, i);
749 type *node_tp = get_entity_type(node);
750 type *owner_tp = get_entity_owner(node);
751 if (is_array_type(owner_tp)) {
752 int size = get_mode_size_bits (get_type_mode(node_tp));
753 int align = get_mode_align_bits(get_type_mode(node_tp));
757 assert(size % align == 0);
758 /* ansonsten aufrunden */
760 offset += size * get_compound_graph_path_array_index(path, i);
762 offset += get_entity_offset_bits(node);
768 int get_compound_ent_value_offset_bytes(entity *ent, int pos) {
769 int offset = get_compound_ent_value_offset_bits(ent, pos);
770 assert(offset % 8 == 0);
775 static void init_index(type *arr) {
779 assert(get_array_n_dimensions(arr) == 1);
781 if (has_array_lower_bound(arr, dim))
782 init = get_array_lower_bound_int(arr, 0) -1;
784 init = get_array_upper_bound_int(arr, 0) +1;
786 set_entity_link(get_array_element_entity(arr), (void *)init);
790 static int get_next_index(entity *elem_ent) {
791 type *arr = get_entity_owner(elem_ent);
795 assert(get_array_n_dimensions(arr) == 1);
797 if (has_array_lower_bound(arr, dim)) {
798 next = (int)get_entity_link(elem_ent) +1;
799 if (has_array_upper_bound(arr, dim)) {
800 int upper = get_array_upper_bound_int(arr, dim);
801 if (next == upper) next = get_array_lower_bound_int(arr, dim);
804 next = (int)get_entity_link(elem_ent) -1;
805 if (has_array_lower_bound(arr, dim)) {
806 int upper = get_array_upper_bound_int(arr, dim);
807 if (next == upper) next = get_array_upper_bound_int(arr, dim);
811 set_entity_link(elem_ent, (void *)next);
815 /* Compute the array indicees in compound graph paths of initialized entities.
817 * All arrays must have fixed lower and upper bounds. One array can
818 * have an open bound. If there are several open bounds, we do
819 * nothing. There must be initializer elements for all array
820 * elements. Uses the link field in the array element entities. The
821 * array bounds must be representable as ints.
823 * (If the bounds are not representable as ints we have to represent
824 * the indicees as firm nodes. But the still we must be able to
825 * evaluate the index against the upper bound.)
827 void compute_compound_ent_array_indicees(entity *ent) {
828 type *tp = get_entity_type(ent);
830 entity *unknown_bound_entity = NULL;
832 if (!is_compound_type(tp) ||
833 (ent->variability == variability_uninitialized)) return ;
835 n_vals = get_compound_ent_n_values(ent);
836 if (n_vals == 0) return;
838 /* We can not compute the indicees if there is more than one array
839 with an unknown bound. For this remember the first entity that
840 represents such an array. It could be ent. */
841 if (is_array_type(tp)) {
842 assert(get_array_n_dimensions(tp) == 1 && "other not implemented");
844 if (!has_array_lower_bound(tp, dim) || !has_array_upper_bound(tp, dim))
845 unknown_bound_entity = ent;
848 /* Initialize the entity links to lower bound -1 and test all path elements
850 for (i = 0; i < n_vals; ++i) {
851 compound_graph_path *path = get_compound_ent_value_path(ent, i);
852 int j, path_len = get_compound_graph_path_length(path);
853 for (j = 0; j < path_len; ++j) {
854 entity *node = get_compound_graph_path_node(path, j);
855 type *elem_tp = get_entity_type(node);
857 if (is_array_type(elem_tp)) {
858 assert(get_array_n_dimensions(elem_tp) == 1 && "other not implemented");
860 if (!has_array_lower_bound(elem_tp, dim) || !has_array_upper_bound(elem_tp, dim)) {
861 if (!unknown_bound_entity) unknown_bound_entity = node;
862 if (node != unknown_bound_entity) return;
870 /* Finally compute the indicees ... */
871 for (i = 0; i < n_vals; ++i) {
872 compound_graph_path *path = get_compound_ent_value_path(ent, i);
873 int j, path_len = get_compound_graph_path_length(path);
874 for (j = 0; j < path_len; ++j) {
875 entity *node = get_compound_graph_path_node(path, j);
876 type *owner_tp = get_entity_owner(node);
877 if (is_array_type(owner_tp))
878 set_compound_graph_path_array_index (path, j, get_next_index(node));
885 static int *resize (int *buf, int new_size) {
886 int *new_buf = (int *)calloc(new_size, 4);
887 memcpy(new_buf, buf, new_size>1);
892 /* We sort the elements by placing them at their bit offset in an
893 array where each entry represents one bit called permutation. In
894 fact, we do not place the values themselves, as we would have to
895 copy two things, the value and the path. We only remember the
896 position in the old order. Each value should have a distinct
897 position in the permutation.
899 A second iteration now permutes the actual elements into two
901 void sort_compound_ent_values(entity *ent) {
902 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
904 type *tp = get_entity_type(ent);
905 int i, n_vals = get_compound_ent_n_values(ent);
906 int tp_size = get_type_size_bits(tp);
910 if (!is_compound_type(tp) ||
911 (ent->variability == variability_uninitialized) ||
912 (get_type_state(tp) != layout_fixed) ||
913 (n_vals == 0) ) return;
915 /* estimated upper bound for size. Better: use flexible array ... */
916 size = ((tp_size > (n_vals * 32)) ? tp_size : (n_vals * 32)) * 4;
917 permutation = (int *)calloc(size, 4);
918 for (i = 0; i < n_vals; ++i) {
919 int pos = get_compound_ent_value_offset_bits(ent, i);
920 while (pos >= size) {
922 permutation = resize(permutation, size);
925 assert(permutation[pos] == 0 && "two values with the same offset");
926 permutation[pos] = i + 1; /* We initialized with 0, so we can not distinguish entry 0.
927 So inc all entries by one. */
928 //fprintf(stderr, "i: %d, pos: %d \n", i, pos);
932 ir_node **my_values = NEW_ARR_F(ir_node *, n_vals);
933 compound_graph_path **my_paths = NEW_ARR_F(compound_graph_path *, n_vals);
934 for (i = 0; i < size; ++i) {
935 int pos = permutation[i];
937 //fprintf(stderr, "pos: %d i: %d next %d \n", i, pos, next);
938 assert(next < n_vals);
939 pos--; /* We increased the pos by one */
940 my_values[next] = get_compound_ent_value (ent, pos);
941 my_paths [next] = get_compound_ent_value_path(ent, pos);
947 DEL_ARR_F(ent->values);
948 ent->values = my_values;
949 DEL_ARR_F(ent->val_paths);
950 ent->val_paths = my_paths;
954 (get_entity_offset_bytes)(entity *ent) {
955 return __get_entity_offset_bytes(ent);
959 (get_entity_offset_bits)(entity *ent) {
960 return __get_entity_offset_bits(ent);
964 (set_entity_offset_bytes)(entity *ent, int offset) {
965 __set_entity_offset_bytes(ent, offset);
969 (set_entity_offset_bits)(entity *ent, int offset) {
970 __set_entity_offset_bits(ent, offset);
974 add_entity_overwrites(entity *ent, entity *overwritten) {
975 assert(ent && is_class_type(get_entity_owner(ent)));
976 ARR_APP1(entity *, ent->overwrites, overwritten);
977 ARR_APP1(entity *, overwritten->overwrittenby, ent);
981 get_entity_n_overwrites(entity *ent) {
982 assert(ent && is_class_type(get_entity_owner(ent)));
983 return (ARR_LEN(ent->overwrites));
987 get_entity_overwrites_index(entity *ent, entity *overwritten) {
989 assert(ent && is_class_type(get_entity_owner(ent)));
990 for (i = 0; i < get_entity_n_overwrites(ent); i++)
991 if (get_entity_overwrites(ent, i) == overwritten)
997 get_entity_overwrites (entity *ent, int pos) {
998 assert(ent && is_class_type(get_entity_owner(ent)));
999 assert(pos < get_entity_n_overwrites(ent));
1000 return ent->overwrites[pos];
1004 set_entity_overwrites (entity *ent, int pos, entity *overwritten) {
1005 assert(ent && is_class_type(get_entity_owner(ent)));
1006 assert(pos < get_entity_n_overwrites(ent));
1007 ent->overwrites[pos] = overwritten;
1011 remove_entity_overwrites(entity *ent, entity *overwritten) {
1013 assert(ent && is_class_type(get_entity_owner(ent)));
1014 for (i = 0; i < (ARR_LEN (ent->overwrites)); i++)
1015 if (ent->overwrites[i] == overwritten) {
1016 for(; i < (ARR_LEN (ent->overwrites))-1; i++)
1017 ent->overwrites[i] = ent->overwrites[i+1];
1018 ARR_SETLEN(entity*, ent->overwrites, ARR_LEN(ent->overwrites) - 1);
1024 add_entity_overwrittenby (entity *ent, entity *overwrites) {
1025 assert(ent && is_class_type(get_entity_owner(ent)));
1026 add_entity_overwrites(overwrites, ent);
1030 get_entity_n_overwrittenby (entity *ent) {
1031 assert(ent && is_class_type(get_entity_owner(ent)));
1032 return (ARR_LEN (ent->overwrittenby));
1036 get_entity_overwrittenby_index(entity *ent, entity *overwrites) {
1038 assert(ent && is_class_type(get_entity_owner(ent)));
1039 for (i = 0; i < get_entity_n_overwrittenby(ent); i++)
1040 if (get_entity_overwrittenby(ent, i) == overwrites)
1046 get_entity_overwrittenby (entity *ent, int pos) {
1047 assert(ent && is_class_type(get_entity_owner(ent)));
1048 assert(pos < get_entity_n_overwrittenby(ent));
1049 return ent->overwrittenby[pos];
1053 set_entity_overwrittenby (entity *ent, int pos, entity *overwrites) {
1054 assert(ent && is_class_type(get_entity_owner(ent)));
1055 assert(pos < get_entity_n_overwrittenby(ent));
1056 ent->overwrittenby[pos] = overwrites;
1059 void remove_entity_overwrittenby(entity *ent, entity *overwrites) {
1061 assert(ent && is_class_type(get_entity_owner(ent)));
1062 for (i = 0; i < (ARR_LEN (ent->overwrittenby)); i++)
1063 if (ent->overwrittenby[i] == overwrites) {
1064 for(; i < (ARR_LEN (ent->overwrittenby))-1; i++)
1065 ent->overwrittenby[i] = ent->overwrittenby[i+1];
1066 ARR_SETLEN(entity*, ent->overwrittenby, ARR_LEN(ent->overwrittenby) - 1);
1071 /* A link to store intermediate information */
1073 (get_entity_link)(entity *ent) {
1074 return __get_entity_link(ent);
1078 (set_entity_link)(entity *ent, void *l) {
1079 __set_entity_link(ent, l);
1083 (get_entity_irg)(entity *ent) {
1084 return __get_entity_irg(ent);
1088 set_entity_irg(entity *ent, ir_graph *irg) {
1089 assert(ent && is_method_type(get_entity_type(ent)));
1090 /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
1091 * Methode selbst nicht mehr aufgerufen werden kann, die Entität
1092 * aber erhalten bleiben soll. */
1094 assert((irg && ent->peculiarity == peculiarity_existent) ||
1095 (!irg && ent->peculiarity == peculiarity_description) ||
1096 (!irg && ent->peculiarity == peculiarity_inherited));
1101 (is_entity)(void *thing) {
1102 return __is_entity(thing);
1105 int is_atomic_entity(entity *ent) {
1106 type* t = get_entity_type(ent);
1107 assert(ent && ent->kind == k_entity);
1108 return (is_primitive_type(t) || is_pointer_type(t) ||
1109 is_enumeration_type(t) || is_method_type(t));
1112 int is_compound_entity(entity *ent) {
1113 type* t = get_entity_type(ent);
1114 assert(ent && ent->kind == k_entity);
1115 return (is_class_type(t) || is_struct_type(t) ||
1116 is_array_type(t) || is_union_type(t));
1120 * @todo not implemnted!!! */
1121 bool equal_entity(entity *ent1, entity *ent2) {
1122 fprintf(stderr, " calling unimplemented equal entity!!! \n");
1127 unsigned long get_entity_visited(entity *ent) {
1128 assert(ent && ent->kind == k_entity);
1131 void set_entity_visited(entity *ent, unsigned long num) {
1132 assert(ent && ent->kind == k_entity);
1135 /* Sets visited field in entity to entity_visited. */
1136 void mark_entity_visited(entity *ent) {
1137 assert(ent && ent->kind == k_entity);
1138 ent->visit = type_visited;
1142 bool entity_visited(entity *ent) {
1143 assert(ent && ent->kind == k_entity);
1144 return get_entity_visited(ent) >= type_visited;
1147 bool entity_not_visited(entity *ent) {
1148 assert(ent && ent->kind == k_entity);
1149 return get_entity_visited(ent) < type_visited;
1152 /* Need two routines because I want to assert the result. */
1153 static entity *resolve_ent_polymorphy2 (type *dynamic_class, entity* static_ent) {
1154 int i, n_overwrittenby;
1157 if (get_entity_owner(static_ent) == dynamic_class) return static_ent;
1159 n_overwrittenby = get_entity_n_overwrittenby(static_ent);
1160 for (i = 0; i < n_overwrittenby; ++i) {
1161 res = resolve_ent_polymorphy2(dynamic_class, get_entity_overwrittenby(static_ent, i));
1168 /** Resolve polymorphy in the inheritance relation.
1170 * Returns the dynamically referenced entity if the static entity and the
1171 * dynamic type are given.
1172 * Search downwards in overwritten tree. */
1173 entity *resolve_ent_polymorphy(type *dynamic_class, entity* static_ent) {
1175 assert(static_ent && static_ent->kind == k_entity);
1177 res = resolve_ent_polymorphy2(dynamic_class, static_ent);
1179 printf(" Could not find entity "); DDME(static_ent);
1180 printf(" in "); DDMT(dynamic_class);
1182 dump_entity(static_ent);
1183 dump_type(get_entity_owner(static_ent));
1184 dump_type(dynamic_class);
1192 /*******************************************************************/
1194 /*******************************************************************/
1197 #if 1 || DEBUG_libfirm
1198 int dump_node_opcode(FILE *F, ir_node *n); /* from irdump.c */
1200 #define X(a) case a: printf(#a); break
1201 void dump_entity (entity *ent) {
1203 type *owner = get_entity_owner(ent);
1204 type *type = get_entity_type(ent);
1205 assert(ent && ent->kind == k_entity);
1206 printf("entity %s (%ld)\n", get_entity_name(ent), get_entity_nr(ent));
1207 printf(" type: %s (%ld)\n", get_type_name(type), get_type_nr(type));
1208 printf(" owner: %s (%ld)\n", get_type_name(owner), get_type_nr(owner));
1210 if (get_entity_n_overwrites(ent) > 0) {
1211 printf(" overwrites:\n");
1212 for (i = 0; i < get_entity_n_overwrites(ent); ++i) {
1213 entity *ov = get_entity_overwrites(ent, i);
1214 printf(" %d: %s of class %s\n", i, get_entity_name(ov), get_type_name(get_entity_owner(ov)));
1217 printf(" Does not overwrite other entities. \n");
1219 if (get_entity_n_overwrittenby(ent) > 0) {
1220 printf(" overwritten by:\n");
1221 for (i = 0; i < get_entity_n_overwrittenby(ent); ++i) {
1222 entity *ov = get_entity_overwrittenby(ent, i);
1223 printf(" %d: %s of class %s\n", i, get_entity_name(ov), get_type_name(get_entity_owner(ov)));
1226 printf(" Is not overwriten by other entities. \n");
1229 printf(" allocation: ");
1230 switch (get_entity_allocation(ent)) {
1231 X(allocation_dynamic);
1232 X(allocation_automatic);
1233 X(allocation_static);
1234 X(allocation_parameter);
1237 printf("\n visibility: ");
1238 switch (get_entity_visibility(ent)) {
1239 X(visibility_local);
1240 X(visibility_external_visible);
1241 X(visibility_external_allocated);
1244 printf("\n variability: ");
1245 switch (get_entity_variability(ent)) {
1246 X(variability_uninitialized);
1247 X(variability_initialized);
1248 X(variability_part_constant);
1249 X(variability_constant);
1252 if (get_entity_variability(ent) != variability_uninitialized) {
1253 if (is_atomic_entity(ent)) {
1254 printf("\n atomic value: ");
1255 dump_node_opcode(stdout, get_atomic_ent_value(ent));
1257 printf("\n compound values:");
1258 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
1259 compound_graph_path *path = get_compound_ent_value_path(ent, i);
1260 entity *ent0 = get_compound_graph_path_node(path, 0);
1261 printf("\n %3d ", get_entity_offset_bits(ent0));
1262 if (get_type_state(type) == layout_fixed)
1263 printf("(%3d) ", get_compound_ent_value_offset_bits(ent, i));
1264 printf("%s", get_entity_name(ent0));
1265 for (j = 0; j < get_compound_graph_path_length(path); ++j) {
1266 entity *node = get_compound_graph_path_node(path, j);
1267 printf(".%s", get_entity_name(node));
1268 if (is_array_type(get_entity_owner(node)))
1269 printf("[%d]", get_compound_graph_path_array_index(path, j));
1272 dump_node_opcode(stdout, get_compound_ent_value(ent, i));
1277 printf("\n volatility: ");
1278 switch (get_entity_volatility(ent)) {
1279 X(volatility_non_volatile);
1280 X(volatility_is_volatile);
1283 printf("\n peculiarity: %s", get_peculiarity_string(get_entity_peculiarity(ent)));
1284 printf("\n ld_name: %s", ent->ld_name ? get_entity_ld_name(ent) : "no yet set");
1285 printf("\n offset: %d", get_entity_offset_bits(ent));
1286 if (is_method_type(get_entity_type(ent))) {
1287 if (get_entity_irg(ent)) /* can be null */
1288 { printf("\n irg = %ld", get_irg_graph_nr(get_entity_irg(ent))); }
1290 { printf("\n irg = NULL"); }
1295 #else /* DEBUG_libfirm */
1296 void dump_entity (entity *ent) {}
1297 #endif /* DEBUG_libfirm */