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.
24 #include "firm_common_t.h"
27 # include "entity_t.h"
29 # include "typegmod.h"
32 /* All this is needed to build the constant node for methods: */
33 # include "irprog_t.h"
38 # include "irdump.h" /* for output if errors occur. */
41 # include "callgraph.h" /* for dumping debug output */
43 /*******************************************************************/
45 /*******************************************************************/
47 entity *unknown_entity = NULL; entity *get_unknown_entity(void) { return unknown_entity; }
48 #define UNKNOWN_ENTITY_NAME "unknown_entity"
50 static INLINE entity *
51 new_rd_entity (dbg_info *db, type *owner, ident *name, type *type);
58 assert(unknown_type && "Call init_type before init_entity!");
59 assert(!unknown_entity && "Call init_entity only once!");
60 unknown_entity = new_rd_entity(NULL, unknown_type, new_id_from_str(UNKNOWN_ENTITY_NAME), unknown_type);
61 set_entity_visibility(unknown_entity, visibility_external_allocated);
62 set_entity_ld_ident(unknown_entity, get_entity_ident(unknown_entity));
64 sym.entity_p = unknown_entity;
65 current_ir_graph = get_const_code_irg();
66 unknown_entity->value = new_SymConst(sym, symconst_addr_ent);
70 /*-----------------------------------------------------------------*/
72 /*-----------------------------------------------------------------*/
74 static INLINE void insert_entity_in_owner (entity *ent) {
75 type *owner = ent->owner;
76 switch (get_type_tpop_code(owner)) {
78 add_class_member (owner, ent);
81 add_struct_member (owner, ent);
84 add_union_member (owner, ent);
87 set_array_element_entity(owner, ent);
93 static INLINE entity *
94 new_rd_entity (dbg_info *db, type *owner, ident *name, type *type)
99 assert(!id_contains_char(name, ' ') && "entity name should not contain spaces");
101 res = xmalloc(sizeof(*res));
102 memset(res, 0, sizeof(*res));
103 res->kind = k_entity;
108 if (get_type_tpop(type) == type_method)
109 res->allocation = allocation_static;
111 res->allocation = allocation_automatic;
113 res->visibility = visibility_local;
115 if (is_method_type(type)) {
118 res->variability = variability_constant;
119 rem = current_ir_graph;
120 current_ir_graph = get_const_code_irg();
121 res->value = new_SymConst(sym, symconst_addr_ent);
122 current_ir_graph = rem;
124 res->variability = variability_uninitialized;
127 res->val_paths = NULL;
129 res->peculiarity = peculiarity_existent;
130 res->volatility = volatility_non_volatile;
131 res->stickyness = stickyness_unsticky;
133 if (is_class_type(owner)) {
134 res->overwrites = NEW_ARR_F(entity *, 0);
135 res->overwrittenby = NEW_ARR_F(entity *, 0);
137 res->overwrites = NULL;
138 res->overwrittenby = NULL;
142 //res->accesses = NULL;
145 res->nr = get_irp_new_node_nr();
146 res->c_name = (char *)get_id_str (name);
147 #endif /* DEBUG_libfirm */
150 set_entity_dbg_info(res, db);
156 new_d_entity (type *owner, ident *name, type *type, dbg_info *db) {
159 assert_legal_owner_of_ent(owner);
160 res = new_rd_entity(db, owner, name, type);
161 /* Remember entity in it's owner. */
162 insert_entity_in_owner (res);
168 new_entity (type *owner, ident *name, type *type) {
169 return new_d_entity(owner, name, type, NULL);
175 static void free_entity_attrs(entity *ent) {
177 if (get_type_tpop(get_entity_owner(ent)) == type_class) {
178 DEL_ARR_F(ent->overwrites); ent->overwrites = NULL;
179 DEL_ARR_F(ent->overwrittenby); ent->overwrittenby = NULL;
181 assert(ent->overwrites == NULL);
182 assert(ent->overwrittenby == NULL);
184 /* if (ent->values) DEL_ARR_F(ent->values); *//* @@@ warum nich? */
185 if (ent->val_paths) {
186 if (is_compound_entity(ent))
187 for (i = 0; i < get_compound_ent_n_values(ent); i++)
188 if (ent->val_paths[i]) ;
189 /* free_compound_graph_path(ent->val_paths[i]) ; * @@@ warum nich? */
190 /* Geht nich: wird mehrfach verwendet!!! ==> mehrfach frei gegeben. */
191 /* DEL_ARR_F(ent->val_paths); */
193 ent->val_paths = NULL;
198 copy_entity_own (entity *old, type *new_owner) {
200 assert(old && old->kind == k_entity);
201 assert_legal_owner_of_ent(new_owner);
203 if (old->owner == new_owner) return old;
204 newe = xmalloc(sizeof(*newe));
205 memcpy (newe, old, sizeof(*newe));
206 newe->owner = new_owner;
207 if (is_class_type(new_owner)) {
208 newe->overwrites = NEW_ARR_F(entity *, 0);
209 newe->overwrittenby = NEW_ARR_F(entity *, 0);
212 newe->nr = get_irp_new_node_nr();
215 insert_entity_in_owner (newe);
221 copy_entity_name (entity *old, ident *new_name) {
223 assert(old && old->kind == k_entity);
225 if (old->name == new_name) return old;
226 newe = xmalloc(sizeof(*newe));
227 memcpy(newe, old, sizeof(*newe));
228 newe->name = new_name;
229 newe->ld_name = NULL;
230 if (is_class_type(newe->owner)) {
231 newe->overwrites = DUP_ARR_F(entity *, old->overwrites);
232 newe->overwrittenby = DUP_ARR_F(entity *, old->overwrittenby);
235 newe->nr = get_irp_new_node_nr();
236 newe->c_name = (char *)get_id_str (newe->name);
239 insert_entity_in_owner (newe);
246 free_entity (entity *ent) {
247 assert(ent && ent->kind == k_entity);
248 free_entity_attrs(ent);
253 /* Outputs a unique number for this node */
255 get_entity_nr(entity *ent) {
256 assert(ent && ent->kind == k_entity);
265 (get_entity_name)(const entity *ent) {
266 return __get_entity_name(ent);
270 (get_entity_ident)(const entity *ent) {
271 return get_entity_ident(ent);
275 void set_entitye_ld_name (entity *, char *ld_name);
276 void set_entity_ld_ident (entity *, ident *ld_ident);
280 (get_entity_owner)(entity *ent) {
281 return __get_entity_owner(ent);
285 set_entity_owner (entity *ent, type *owner) {
286 assert(ent && ent->kind == k_entity);
287 assert_legal_owner_of_ent(owner);
291 void /* should this go into type.c? */
292 assert_legal_owner_of_ent(type *owner) {
293 assert(get_type_tpop_code(owner) == tpo_class ||
294 get_type_tpop_code(owner) == tpo_union ||
295 get_type_tpop_code(owner) == tpo_struct ||
296 get_type_tpop_code(owner) == tpo_array); /* Yes, array has an entity
297 -- to select fields! */
301 (get_entity_ld_ident)(entity *ent) {
302 return __get_entity_ld_ident(ent);
306 (set_entity_ld_ident)(entity *ent, ident *ld_ident) {
307 __set_entity_ld_ident(ent, ld_ident);
311 (get_entity_ld_name)(entity *ent) {
312 return __get_entity_ld_name(ent);
316 (get_entity_type)(entity *ent) {
317 return __get_entity_type(ent);
321 (set_entity_type)(entity *ent, type *type) {
322 __set_entity_type(ent, type);
326 (get_entity_allocation)(const entity *ent) {
327 return __get_entity_allocation(ent);
331 (set_entity_allocation)(entity *ent, ent_allocation al) {
332 __set_entity_allocation(ent, al);
335 /* return the name of the visibility */
336 const char *get_allocation_name(ent_allocation all)
338 #define X(a) case a: return #a
340 X(allocation_automatic);
341 X(allocation_parameter);
342 X(allocation_dynamic);
343 X(allocation_static);
344 default: return "BAD VALUE";
351 (get_entity_visibility)(const entity *ent) {
352 return __get_entity_visibility(ent);
356 set_entity_visibility (entity *ent, ent_visibility vis) {
357 assert(ent && ent->kind == k_entity);
358 if (vis != visibility_local)
359 assert((ent->allocation == allocation_static) ||
360 (ent->allocation == allocation_automatic));
361 /* @@@ Test that the owner type is not local, but how??
362 && get_class_visibility(get_entity_owner(ent)) != local));*/
363 ent->visibility = vis;
366 /* return the name of the visibility */
367 const char *get_visibility_name(ent_visibility vis)
369 #define X(a) case a: return #a
372 X(visibility_external_visible);
373 X(visibility_external_allocated);
374 default: return "BAD VALUE";
380 (get_entity_variability)(const entity *ent) {
381 return __get_entity_variability(ent);
385 set_entity_variability (entity *ent, ent_variability var)
387 assert(ent && ent->kind == k_entity);
388 if (var == variability_part_constant)
389 assert(is_class_type(ent->type) || is_struct_type(ent->type));
391 if ((is_compound_type(ent->type)) &&
392 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
393 /* Allocate datastructures for constant values */
394 ent->values = NEW_ARR_F(ir_node *, 0);
395 ent->val_paths = NEW_ARR_F(compound_graph_path *, 0);
397 if ((is_atomic_type(ent->type)) &&
398 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
399 /* Set default constant value. */
400 ent->value = new_rd_Unknown(get_const_code_irg(), get_type_mode(ent->type));
403 if ((is_compound_type(ent->type)) &&
404 (var == variability_uninitialized) && (ent->variability != variability_uninitialized)) {
405 /* Free datastructures for constant values */
406 DEL_ARR_F(ent->values); ent->values = NULL;
407 DEL_ARR_F(ent->val_paths); ent->val_paths = NULL;
409 ent->variability = var;
412 /* return the name of the variablity */
413 const char *get_variability_name(ent_variability var)
415 #define X(a) case a: return #a
417 X(variability_uninitialized);
418 X(variability_initialized);
419 X(variability_part_constant);
420 X(variability_constant);
421 default: return "BAD VALUE";
427 (get_entity_volatility)(const entity *ent) {
428 return __get_entity_volatility(ent);
432 (set_entity_volatility)(entity *ent, ent_volatility vol) {
433 __set_entity_volatility(ent, vol);
436 /* return the name of the volatility */
437 const char *get_volatility_name(ent_volatility var)
439 #define X(a) case a: return #a
441 X(volatility_non_volatile);
442 X(volatility_is_volatile);
443 default: return "BAD VALUE";
449 (get_entity_peculiarity)(const entity *ent) {
450 return __get_entity_peculiarity(ent);
454 (set_entity_peculiarity)(entity *ent, peculiarity pec) {
455 __set_entity_peculiarity(ent, pec);
458 /* return the name of the peculiarity */
459 const char *get_peculiarity_name(peculiarity var)
461 #define X(a) case a: return #a
463 X(peculiarity_description);
464 X(peculiarity_inherited);
465 X(peculiarity_existent);
466 default: return "BAD VALUE";
471 /* Get the entity's stickyness */
473 (get_entity_stickyness)(const entity *ent) {
474 return __get_entity_stickyness(ent);
477 /* Set the entity's stickyness */
479 (set_entity_stickyness)(entity *ent, ent_stickyness stickyness) {
480 __set_entity_stickyness(ent, stickyness);
483 /* Set has no effect for existent entities of type method. */
485 get_atomic_ent_value(entity *ent)
487 assert(ent && is_atomic_entity(ent));
488 assert(ent->variability != variability_uninitialized);
489 return skip_Id (ent->value);
493 set_atomic_ent_value(entity *ent, ir_node *val) {
494 assert(is_atomic_entity(ent) && (ent->variability != variability_uninitialized));
495 if (is_method_type(ent->type) && (ent->peculiarity == peculiarity_existent))
500 /* Returns true if the the node is representable as code on
502 int is_irn_const_expression(ir_node *n) {
505 /* we are in dange iff an exception will arise. TODO: be more precisely,
506 * for instance Div. will NOT rise if divisor != 0
508 if (is_binop(n) && !is_fragile_op(n))
509 return is_irn_const_expression(get_binop_left(n)) && is_irn_const_expression(get_binop_right(n));
512 switch(get_irn_opcode(n)) {
519 return is_irn_const_expression(get_irn_n(n, 0));
528 ir_node *copy_const_value(ir_node *n) {
532 /* @@@ GL I think we should implement this using the routines from irgopt for
533 dead node elimination/inlineing. */
536 switch(get_irn_opcode(n)) {
538 nn = new_Const(m, get_Const_tarval(n)); set_Const_type(nn, get_Const_type(n));
539 //nn = new_rd_Const_type(get_irn_dbg_info(n), current_ir_graph, get_cur_block(),
540 // m, get_Const_tarval(n), get_Const_type(n));
543 nn = new_d_SymConst_type(NULL, get_SymConst_symbol(n), get_SymConst_kind(n),
544 get_SymConst_value_type(n));
547 nn = new_Add(copy_const_value(get_Add_left(n)),
548 copy_const_value(get_Add_right(n)), m); break;
550 nn = new_Sub(copy_const_value(get_Sub_left(n)),
551 copy_const_value(get_Sub_right(n)), m); break;
553 nn = new_Mul(copy_const_value(get_Mul_left(n)),
554 copy_const_value(get_Mul_right(n)), m); break;
556 nn = new_And(copy_const_value(get_And_left(n)),
557 copy_const_value(get_And_right(n)), m); break;
559 nn = new_Or(copy_const_value(get_Or_left(n)),
560 copy_const_value(get_Or_right(n)), m); break;
562 nn = new_Eor(copy_const_value(get_Eor_left(n)),
563 copy_const_value(get_Eor_right(n)), m); break;
565 nn = new_Cast(copy_const_value(get_Cast_op(n)), get_Cast_type(n)); break;
567 nn = new_Conv(copy_const_value(get_Conv_op(n)), m); break;
569 nn = new_Unknown(m); break;
572 assert(0 && "opcode invalid or not implemented");
579 compound_graph_path *
580 new_compound_graph_path(type *tp, int length) {
581 compound_graph_path *res;
582 assert(is_type(tp) && is_compound_type(tp));
585 res = calloc (1, sizeof(*res) + (length-1) * sizeof(res->nodes[0]));
586 res->kind = k_ir_compound_graph_path;
589 res ->arr_indicees = calloc(length, sizeof(*res ->arr_indicees));
594 free_compound_graph_path (compound_graph_path *gr) {
595 assert(gr && is_compound_graph_path(gr));
597 free(gr ->arr_indicees);
602 is_compound_graph_path(void *thing) {
603 return (get_kind(thing) == k_ir_compound_graph_path);
606 /* checks whether nodes 0..pos are correct (all lie on a path.) */
607 /* @@@ not implemented */
608 int is_proper_compound_graph_path(compound_graph_path *gr, int pos) {
611 type *owner = gr->tp;
612 for (i = 0; i <= pos; i++) {
613 node = get_compound_graph_path_node(gr, i);
614 if (get_entity_owner(node) != owner) return false;
615 owner = get_entity_type(node);
617 if (pos == get_compound_graph_path_length(gr))
618 if (!is_atomic_type(owner)) return false;
623 get_compound_graph_path_length(compound_graph_path *gr) {
624 assert(gr && is_compound_graph_path(gr));
629 get_compound_graph_path_node(compound_graph_path *gr, int pos) {
630 assert(gr && is_compound_graph_path(gr));
631 assert(pos >= 0 && pos < gr->len);
632 return gr->nodes[pos];
636 set_compound_graph_path_node(compound_graph_path *gr, int pos, entity *node) {
637 assert(gr && is_compound_graph_path(gr));
638 assert(pos >= 0 && pos < gr->len);
639 assert(is_entity(node));
640 gr->nodes[pos] = node;
641 assert(is_proper_compound_graph_path(gr, pos));
645 get_compound_graph_path_array_index(compound_graph_path *gr, int pos) {
646 assert(gr && is_compound_graph_path(gr));
647 assert(pos >= 0 && pos < gr->len);
648 return gr->arr_indicees[pos];
652 set_compound_graph_path_array_index(compound_graph_path *gr, int pos, int index) {
653 assert(gr && is_compound_graph_path(gr));
654 assert(pos >= 0 && pos < gr->len);
655 gr->arr_indicees[pos] = index;
658 /* A value of a compound entity is a pair of value and the corresponding path to a member of
661 add_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path) {
662 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
663 ARR_APP1 (ir_node *, ent->values, val);
664 ARR_APP1 (compound_graph_path *, ent->val_paths, path);
668 set_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path, int pos) {
669 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
670 ent->values[pos] = val;
671 ent->val_paths[pos] = path;
675 get_compound_ent_n_values(entity *ent) {
676 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
677 return (ARR_LEN (ent->values));
681 get_compound_ent_value(entity *ent, int pos) {
682 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
683 return ent->values[pos];
686 compound_graph_path *
687 get_compound_ent_value_path(entity *ent, int pos) {
688 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
689 return ent->val_paths[pos];
693 remove_compound_ent_value(entity *ent, entity *value_ent) {
695 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
696 for (i = 0; i < (ARR_LEN (ent->val_paths)); i++) {
697 compound_graph_path *path = ent->val_paths[i];
698 if (path->nodes[path->len-1] == value_ent) {
699 for(; i < (ARR_LEN (ent->val_paths))-1; i++) {
700 ent->val_paths[i] = ent->val_paths[i+1];
701 ent->values[i] = ent->values[i+1];
703 ARR_SETLEN(entity*, ent->val_paths, ARR_LEN(ent->val_paths) - 1);
704 ARR_SETLEN(ir_node*, ent->values, ARR_LEN(ent->values) - 1);
711 add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
712 compound_graph_path *path;
713 type *owner_tp = get_entity_owner(ent);
714 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
715 path = new_compound_graph_path(owner_tp, 1);
716 path->nodes[0] = member;
717 if (is_array_type(owner_tp)) {
721 assert(get_array_n_dimensions(owner_tp) == 1 && has_array_lower_bound(owner_tp, 0));
722 max = get_array_lower_bound_int(owner_tp, 0) -1;
723 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
724 int index = get_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0);
729 path->arr_indicees[0] = max + 1;
731 add_compound_ent_value_w_path(ent, val, path);
734 /* Copies the firm subgraph referenced by val to const_code_irg and adds
735 the node as constant initialization to ent.
736 The subgraph may not contain control flow operations.
738 copy_and_add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
739 ir_graph *rem = current_ir_graph;
741 assert(get_entity_variability(ent) != variability_uninitialized);
742 current_ir_graph = get_const_code_irg();
744 val = copy_const_value(val);
745 add_compound_ent_value(ent, val, member);
746 current_ir_graph = rem;
749 /* Copies the value i of the entity to current_block in current_ir_graph.
751 copy_compound_ent_value(entity *ent, int pos) {
752 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
753 return copy_const_value(ent->values[pos+1]);
757 get_compound_ent_value_member(entity *ent, int pos) {
758 compound_graph_path *path;
759 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
760 path = get_compound_ent_value_path(ent, pos);
762 return get_compound_graph_path_node(path, get_compound_graph_path_length(path)-1);
766 set_compound_ent_value(entity *ent, ir_node *val, entity *member, int pos) {
767 compound_graph_path *path;
768 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
769 path = get_compound_ent_value_path(ent, pos);
770 set_compound_graph_path_node(path, 0, member);
771 set_compound_ent_value_w_path(ent, val, path, pos);
775 set_array_entity_values(entity *ent, tarval **values, int num_vals) {
777 ir_graph *rem = current_ir_graph;
778 type *arrtp = get_entity_type(ent);
780 type *elttp = get_array_element_type(arrtp);
782 assert(is_array_type(arrtp));
783 assert(get_array_n_dimensions(arrtp) == 1);
784 /* One bound is sufficient, the nunmber of constant fields makes the
786 assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
787 assert(get_entity_variability(ent) != variability_uninitialized);
788 current_ir_graph = get_const_code_irg();
790 for (i = 0; i < num_vals; i++) {
791 val = new_Const_type(values[i], elttp);
792 add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
793 set_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0, i);
795 current_ir_graph = rem;
798 int get_compound_ent_value_offset_bits(entity *ent, int pos) {
799 compound_graph_path *path;
803 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
805 path = get_compound_ent_value_path(ent, pos);
806 path_len = get_compound_graph_path_length(path);
808 for (i = 0; i < path_len; ++i) {
809 entity *node = get_compound_graph_path_node(path, i);
810 type *node_tp = get_entity_type(node);
811 type *owner_tp = get_entity_owner(node);
812 if (is_array_type(owner_tp)) {
813 int size = get_type_size_bits(node_tp);
814 int align = get_type_alignment_bits(node_tp);
818 assert(size % align == 0);
819 /* ansonsten aufrunden */
821 offset += size * get_compound_graph_path_array_index(path, i);
823 offset += get_entity_offset_bits(node);
829 int get_compound_ent_value_offset_bytes(entity *ent, int pos) {
830 int offset = get_compound_ent_value_offset_bits(ent, pos);
831 assert(offset % 8 == 0);
836 static void init_index(type *arr) {
840 assert(get_array_n_dimensions(arr) == 1);
842 if (has_array_lower_bound(arr, dim))
843 init = get_array_lower_bound_int(arr, 0) -1;
845 init = get_array_upper_bound_int(arr, 0) +1;
847 set_entity_link(get_array_element_entity(arr), (void *)init);
851 static int get_next_index(entity *elem_ent) {
852 type *arr = get_entity_owner(elem_ent);
856 assert(get_array_n_dimensions(arr) == 1);
858 if (has_array_lower_bound(arr, dim)) {
859 next = (int)get_entity_link(elem_ent) +1;
860 if (has_array_upper_bound(arr, dim)) {
861 int upper = get_array_upper_bound_int(arr, dim);
862 if (next == upper) next = get_array_lower_bound_int(arr, dim);
865 next = (int)get_entity_link(elem_ent) -1;
866 if (has_array_lower_bound(arr, dim)) {
867 int upper = get_array_upper_bound_int(arr, dim);
868 if (next == upper) next = get_array_upper_bound_int(arr, dim);
872 set_entity_link(elem_ent, (void *)next);
876 /* Compute the array indicees in compound graph paths of initialized entities.
878 * All arrays must have fixed lower and upper bounds. One array can
879 * have an open bound. If there are several open bounds, we do
880 * nothing. There must be initializer elements for all array
881 * elements. Uses the link field in the array element entities. The
882 * array bounds must be representable as ints.
884 * (If the bounds are not representable as ints we have to represent
885 * the indicees as firm nodes. But the still we must be able to
886 * evaluate the index against the upper bound.)
888 void compute_compound_ent_array_indicees(entity *ent) {
889 type *tp = get_entity_type(ent);
891 entity *unknown_bound_entity = NULL;
893 if (!is_compound_type(tp) ||
894 (ent->variability == variability_uninitialized)) return ;
896 n_vals = get_compound_ent_n_values(ent);
897 if (n_vals == 0) return;
899 /* We can not compute the indexes if there is more than one array
900 with an unknown bound. For this remember the first entity that
901 represents such an array. It could be ent. */
902 if (is_array_type(tp)) {
905 assert(get_array_n_dimensions(tp) == 1 && "other not implemented");
906 if (!has_array_lower_bound(tp, dim) || !has_array_upper_bound(tp, dim))
907 unknown_bound_entity = ent;
910 /* Initialize the entity links to lower bound -1 and test all path elements
912 for (i = 0; i < n_vals; ++i) {
913 compound_graph_path *path = get_compound_ent_value_path(ent, i);
914 int j, path_len = get_compound_graph_path_length(path);
915 for (j = 0; j < path_len; ++j) {
916 entity *node = get_compound_graph_path_node(path, j);
917 type *elem_tp = get_entity_type(node);
919 if (is_array_type(elem_tp)) {
921 assert(get_array_n_dimensions(elem_tp) == 1 && "other not implemented");
922 if (!has_array_lower_bound(elem_tp, dim) || !has_array_upper_bound(elem_tp, dim)) {
923 if (!unknown_bound_entity) unknown_bound_entity = node;
924 if (node != unknown_bound_entity) return;
932 /* Finally compute the indexes ... */
933 for (i = 0; i < n_vals; ++i) {
934 compound_graph_path *path = get_compound_ent_value_path(ent, i);
935 int j, path_len = get_compound_graph_path_length(path);
936 for (j = 0; j < path_len; ++j) {
937 entity *node = get_compound_graph_path_node(path, j);
938 type *owner_tp = get_entity_owner(node);
939 if (is_array_type(owner_tp))
940 set_compound_graph_path_array_index (path, j, get_next_index(node));
946 /* FIXME MMB: the memcpy is very strange */
947 static int *resize (int *buf, int new_size) {
948 int *new_buf = (int *)calloc(new_size, sizeof(*new_buf));
949 memcpy(new_buf, buf, new_size>1);
954 /* We sort the elements by placing them at their bit offset in an
955 array where each entry represents one bit called permutation. In
956 fact, we do not place the values themselves, as we would have to
957 copy two things, the value and the path. We only remember the
958 position in the old order. Each value should have a distinct
959 position in the permutation.
961 A second iteration now permutes the actual elements into two
963 void sort_compound_ent_values(entity *ent) {
972 compound_graph_path **my_paths;
974 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
976 tp = get_entity_type(ent);
977 n_vals = get_compound_ent_n_values(ent);
978 tp_size = get_type_size_bits(tp);
980 if (!is_compound_type(tp) ||
981 (ent->variability == variability_uninitialized) ||
982 (get_type_state(tp) != layout_fixed) ||
983 (n_vals == 0) ) return;
985 /* estimated upper bound for size. Better: use flexible array ... */
986 size = ((tp_size > (n_vals * 32)) ? tp_size : (n_vals * 32)) * 4;
987 permutation = calloc(size, sizeof(*permutation));
988 for (i = 0; i < n_vals; ++i) {
989 int pos = get_compound_ent_value_offset_bits(ent, i);
990 while (pos >= size) {
992 permutation = resize(permutation, size);
995 assert(permutation[pos] == 0 && "two values with the same offset");
996 permutation[pos] = i + 1; /* We initialized with 0, so we can not distinguish entry 0.
997 So inc all entries by one. */
998 //fprintf(stderr, "i: %d, pos: %d \n", i, pos);
1002 my_values = NEW_ARR_F(ir_node *, n_vals);
1003 my_paths = NEW_ARR_F(compound_graph_path *, n_vals);
1004 for (i = 0; i < size; ++i) {
1005 int pos = permutation[i];
1007 //fprintf(stderr, "pos: %d i: %d next %d \n", i, pos, next);
1008 assert(next < n_vals);
1009 pos--; /* We increased the pos by one */
1010 my_values[next] = get_compound_ent_value (ent, pos);
1011 my_paths [next] = get_compound_ent_value_path(ent, pos);
1017 DEL_ARR_F(ent->values);
1018 ent->values = my_values;
1019 DEL_ARR_F(ent->val_paths);
1020 ent->val_paths = my_paths;
1024 (get_entity_offset_bytes)(const entity *ent) {
1025 return __get_entity_offset_bytes(ent);
1029 (get_entity_offset_bits)(const entity *ent) {
1030 return __get_entity_offset_bits(ent);
1034 (set_entity_offset_bytes)(entity *ent, int offset) {
1035 __set_entity_offset_bytes(ent, offset);
1039 (set_entity_offset_bits)(entity *ent, int offset) {
1040 __set_entity_offset_bits(ent, offset);
1044 add_entity_overwrites(entity *ent, entity *overwritten) {
1045 assert(ent && is_class_type(get_entity_owner(ent)));
1046 ARR_APP1(entity *, ent->overwrites, overwritten);
1047 ARR_APP1(entity *, overwritten->overwrittenby, ent);
1051 get_entity_n_overwrites(entity *ent) {
1052 assert(ent && is_class_type(get_entity_owner(ent)));
1053 return (ARR_LEN(ent->overwrites));
1057 get_entity_overwrites_index(entity *ent, entity *overwritten) {
1059 assert(ent && is_class_type(get_entity_owner(ent)));
1060 for (i = 0; i < get_entity_n_overwrites(ent); i++)
1061 if (get_entity_overwrites(ent, i) == overwritten)
1067 get_entity_overwrites (entity *ent, int pos) {
1068 assert(ent && is_class_type(get_entity_owner(ent)));
1069 assert(pos < get_entity_n_overwrites(ent));
1070 return ent->overwrites[pos];
1074 set_entity_overwrites (entity *ent, int pos, entity *overwritten) {
1075 assert(ent && is_class_type(get_entity_owner(ent)));
1076 assert(pos < get_entity_n_overwrites(ent));
1077 ent->overwrites[pos] = overwritten;
1081 remove_entity_overwrites(entity *ent, entity *overwritten) {
1083 assert(ent && is_class_type(get_entity_owner(ent)));
1084 for (i = 0; i < (ARR_LEN (ent->overwrites)); i++)
1085 if (ent->overwrites[i] == overwritten) {
1086 for(; i < (ARR_LEN (ent->overwrites))-1; i++)
1087 ent->overwrites[i] = ent->overwrites[i+1];
1088 ARR_SETLEN(entity*, ent->overwrites, ARR_LEN(ent->overwrites) - 1);
1094 add_entity_overwrittenby (entity *ent, entity *overwrites) {
1095 assert(ent && is_class_type(get_entity_owner(ent)));
1096 add_entity_overwrites(overwrites, ent);
1100 get_entity_n_overwrittenby (entity *ent) {
1101 assert(ent && is_class_type(get_entity_owner(ent)));
1102 return (ARR_LEN (ent->overwrittenby));
1106 get_entity_overwrittenby_index(entity *ent, entity *overwrites) {
1108 assert(ent && is_class_type(get_entity_owner(ent)));
1109 for (i = 0; i < get_entity_n_overwrittenby(ent); i++)
1110 if (get_entity_overwrittenby(ent, i) == overwrites)
1116 get_entity_overwrittenby (entity *ent, int pos) {
1117 assert(ent && is_class_type(get_entity_owner(ent)));
1118 assert(pos < get_entity_n_overwrittenby(ent));
1119 return ent->overwrittenby[pos];
1123 set_entity_overwrittenby (entity *ent, int pos, entity *overwrites) {
1124 assert(ent && is_class_type(get_entity_owner(ent)));
1125 assert(pos < get_entity_n_overwrittenby(ent));
1126 ent->overwrittenby[pos] = overwrites;
1129 void remove_entity_overwrittenby(entity *ent, entity *overwrites) {
1131 assert(ent && is_class_type(get_entity_owner(ent)));
1132 for (i = 0; i < (ARR_LEN (ent->overwrittenby)); i++)
1133 if (ent->overwrittenby[i] == overwrites) {
1134 for(; i < (ARR_LEN (ent->overwrittenby))-1; i++)
1135 ent->overwrittenby[i] = ent->overwrittenby[i+1];
1136 ARR_SETLEN(entity*, ent->overwrittenby, ARR_LEN(ent->overwrittenby) - 1);
1141 /* A link to store intermediate information */
1143 (get_entity_link)(const entity *ent) {
1144 return __get_entity_link(ent);
1148 (set_entity_link)(entity *ent, void *l) {
1149 __set_entity_link(ent, l);
1153 (get_entity_irg)(const entity *ent) {
1154 return __get_entity_irg(ent);
1158 set_entity_irg(entity *ent, ir_graph *irg) {
1159 assert(ent && is_method_type(get_entity_type(ent)));
1160 /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
1161 * Methode selbst nicht mehr aufgerufen werden kann, die Entität
1162 * aber erhalten bleiben soll? Wandle die Entitaet in description oder
1165 assert((irg && ent->peculiarity == peculiarity_existent) ||
1166 (!irg && (ent->peculiarity == peculiarity_existent)
1167 && (ent -> visibility == visibility_external_allocated)) ||
1168 (!irg && ent->peculiarity == peculiarity_description) ||
1169 (!irg && ent->peculiarity == peculiarity_inherited));
1174 (is_entity)(const void *thing) {
1175 return __is_entity(thing);
1178 int is_atomic_entity(entity *ent) {
1179 type* t = get_entity_type(ent);
1180 assert(ent && ent->kind == k_entity);
1181 return (is_primitive_type(t) || is_pointer_type(t) ||
1182 is_enumeration_type(t) || is_method_type(t));
1185 int is_compound_entity(entity *ent) {
1186 type* t = get_entity_type(ent);
1187 assert(ent && ent->kind == k_entity);
1188 return (is_class_type(t) || is_struct_type(t) ||
1189 is_array_type(t) || is_union_type(t));
1193 * @todo not implemnted!!! */
1194 bool equal_entity(entity *ent1, entity *ent2) {
1195 fprintf(stderr, " calling unimplemented equal entity!!! \n");
1200 unsigned long get_entity_visited(entity *ent) {
1201 assert(ent && ent->kind == k_entity);
1204 void set_entity_visited(entity *ent, unsigned long num) {
1205 assert(ent && ent->kind == k_entity);
1208 /* Sets visited field in entity to entity_visited. */
1209 void mark_entity_visited(entity *ent) {
1210 assert(ent && ent->kind == k_entity);
1211 ent->visit = type_visited;
1215 bool entity_visited(entity *ent) {
1216 assert(ent && ent->kind == k_entity);
1217 return get_entity_visited(ent) >= type_visited;
1220 bool entity_not_visited(entity *ent) {
1221 assert(ent && ent->kind == k_entity);
1222 return get_entity_visited(ent) < type_visited;
1225 /* Need two routines because I want to assert the result. */
1226 static entity *resolve_ent_polymorphy2 (type *dynamic_class, entity* static_ent) {
1227 int i, n_overwrittenby;
1230 if (get_entity_owner(static_ent) == dynamic_class) return static_ent;
1232 n_overwrittenby = get_entity_n_overwrittenby(static_ent);
1233 for (i = 0; i < n_overwrittenby; ++i) {
1234 res = resolve_ent_polymorphy2(dynamic_class, get_entity_overwrittenby(static_ent, i));
1241 /** Resolve polymorphy in the inheritance relation.
1243 * Returns the dynamically referenced entity if the static entity and the
1244 * dynamic type are given.
1245 * Search downwards in overwritten tree. */
1246 entity *resolve_ent_polymorphy(type *dynamic_class, entity* static_ent) {
1248 assert(static_ent && static_ent->kind == k_entity);
1250 res = resolve_ent_polymorphy2(dynamic_class, static_ent);
1253 printf(" Could not find entity "); DDME(static_ent);
1254 printf(" in "); DDMT(dynamic_class);
1256 dump_entity(static_ent);
1257 dump_type(get_entity_owner(static_ent));
1258 dump_type(dynamic_class);