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"
34 /* All this is needed to build the constant node for methods: */
35 # include "irprog_t.h"
40 # include "irdump.h" /* for output if errors occur. */
43 # include "callgraph.h" /* for dumping debug output */
45 /*******************************************************************/
47 /*******************************************************************/
49 entity *unknown_entity = NULL;
51 entity *get_unknown_entity(void) { return unknown_entity; }
53 #define UNKNOWN_ENTITY_NAME "unknown_entity"
55 /*-----------------------------------------------------------------*/
57 /*-----------------------------------------------------------------*/
59 static INLINE void insert_entity_in_owner (entity *ent) {
60 type *owner = ent->owner;
61 switch (get_type_tpop_code(owner)) {
63 add_class_member (owner, ent);
66 add_struct_member (owner, ent);
69 add_union_member (owner, ent);
72 set_array_element_entity(owner, ent);
79 * Creates a new entity. This entity is NOT inserted in the owner type.
81 * @param db debug info for this entity
82 * @param owner the owner type of the new entity
83 * @param name the name of the new entity
84 * @param type the type of the new entity
86 * @return the new created entity
88 static INLINE entity *
89 new_rd_entity (dbg_info *db, type *owner, ident *name, type *type)
94 assert(!id_contains_char(name, ' ') && "entity name should not contain spaces");
96 res = xmalloc(sizeof(*res));
97 memset(res, 0, sizeof(*res));
105 if (get_type_tpop(type) == type_method)
106 res->allocation = allocation_static;
108 res->allocation = allocation_automatic;
110 res->visibility = visibility_local;
111 res->volatility = volatility_non_volatile;
112 res->stickyness = stickyness_unsticky;
114 res->peculiarity = peculiarity_existent;
118 if (is_Method_type(type)) {
121 rem = current_ir_graph;
122 current_ir_graph = get_const_code_irg();
123 res->value = new_SymConst(sym, symconst_addr_ent);
124 current_ir_graph = rem;
125 res->variability = variability_constant;
126 res->irg_add_properties = irg_no_property;
127 res->param_access = NULL;
128 res->param_weight = NULL;
131 res->variability = variability_uninitialized;
134 res->val_paths = NULL;
137 if (is_Class_type(owner)) {
138 res->overwrites = NEW_ARR_F(entity *, 0);
139 res->overwrittenby = NEW_ARR_F(entity *, 0);
141 res->overwrites = NULL;
142 res->overwrittenby = NULL;
147 res->nr = get_irp_new_node_nr();
148 #endif /* DEBUG_libfirm */
151 set_entity_dbg_info(res, db);
157 new_d_entity (type *owner, ident *name, type *type, dbg_info *db) {
160 assert_legal_owner_of_ent(owner);
161 res = new_rd_entity(db, owner, name, type);
162 /* Remember entity in it's owner. */
163 insert_entity_in_owner (res);
165 hook_new_entity(res);
170 new_entity (type *owner, ident *name, type *type) {
171 return new_d_entity(owner, name, type, NULL);
177 static void free_entity_attrs(entity *ent) {
179 if (get_type_tpop(get_entity_owner(ent)) == type_class) {
180 DEL_ARR_F(ent->overwrites); ent->overwrites = NULL;
181 DEL_ARR_F(ent->overwrittenby); ent->overwrittenby = NULL;
183 assert(ent->overwrites == NULL);
184 assert(ent->overwrittenby == NULL);
186 /* if (ent->values) DEL_ARR_F(ent->values); *//* @@@ warum nich? */
187 if (ent->val_paths) {
188 if (is_compound_entity(ent))
189 for (i = 0; i < get_compound_ent_n_values(ent); i++)
190 if (ent->val_paths[i]) ;
191 /* free_compound_graph_path(ent->val_paths[i]) ; * @@@ warum nich? */
192 /* Geht nich: wird mehrfach verwendet!!! ==> mehrfach frei gegeben. */
193 /* DEL_ARR_F(ent->val_paths); */
195 ent->val_paths = NULL;
198 if (ent->param_access) {
199 DEL_ARR_F(ent->param_access);
200 ent->param_access = NULL;
202 if (ent->param_weight) {
203 DEL_ARR_F(ent->param_weight);
209 copy_entity_own (entity *old, type *new_owner) {
211 assert(old && old->kind == k_entity);
212 assert_legal_owner_of_ent(new_owner);
214 if (old->owner == new_owner) return old;
215 newe = xmalloc(sizeof(*newe));
216 memcpy (newe, old, sizeof(*newe));
217 newe->owner = new_owner;
218 if (is_Class_type(new_owner)) {
219 newe->overwrites = NEW_ARR_F(entity *, 0);
220 newe->overwrittenby = NEW_ARR_F(entity *, 0);
223 newe->nr = get_irp_new_node_nr();
226 insert_entity_in_owner (newe);
232 copy_entity_name (entity *old, ident *new_name) {
234 assert(old && old->kind == k_entity);
236 if (old->name == new_name) return old;
237 newe = xmalloc(sizeof(*newe));
238 memcpy(newe, old, sizeof(*newe));
239 newe->name = new_name;
240 newe->ld_name = NULL;
241 if (is_Class_type(newe->owner)) {
242 newe->overwrites = DUP_ARR_F(entity *, old->overwrites);
243 newe->overwrittenby = DUP_ARR_F(entity *, old->overwrittenby);
246 newe->nr = get_irp_new_node_nr();
249 insert_entity_in_owner (newe);
256 free_entity (entity *ent) {
257 assert(ent && ent->kind == k_entity);
258 free_entity_attrs(ent);
263 /* Outputs a unique number for this node */
265 get_entity_nr(entity *ent) {
266 assert(ent && ent->kind == k_entity);
275 (get_entity_name)(const entity *ent) {
276 return _get_entity_name(ent);
280 (get_entity_ident)(const entity *ent) {
281 return get_entity_ident(ent);
285 (get_entity_owner)(entity *ent) {
286 return _get_entity_owner(ent);
290 set_entity_owner (entity *ent, type *owner) {
291 assert(ent && ent->kind == k_entity);
292 assert_legal_owner_of_ent(owner);
296 void /* should this go into type.c? */
297 assert_legal_owner_of_ent(type *owner) {
298 assert(get_type_tpop_code(owner) == tpo_class ||
299 get_type_tpop_code(owner) == tpo_union ||
300 get_type_tpop_code(owner) == tpo_struct ||
301 get_type_tpop_code(owner) == tpo_array); /* Yes, array has an entity
302 -- to select fields! */
306 (get_entity_ld_ident)(entity *ent) {
307 return _get_entity_ld_ident(ent);
311 (set_entity_ld_ident)(entity *ent, ident *ld_ident) {
312 _set_entity_ld_ident(ent, ld_ident);
316 (get_entity_ld_name)(entity *ent) {
317 return _get_entity_ld_name(ent);
321 (get_entity_type)(entity *ent) {
322 return _get_entity_type(ent);
326 (set_entity_type)(entity *ent, type *type) {
327 _set_entity_type(ent, type);
331 (get_entity_allocation)(const entity *ent) {
332 return _get_entity_allocation(ent);
336 (set_entity_allocation)(entity *ent, ent_allocation al) {
337 _set_entity_allocation(ent, al);
340 /* return the name of the visibility */
341 const char *get_allocation_name(ent_allocation all)
343 #define X(a) case a: return #a
345 X(allocation_automatic);
346 X(allocation_parameter);
347 X(allocation_dynamic);
348 X(allocation_static);
349 default: return "BAD VALUE";
356 (get_entity_visibility)(const entity *ent) {
357 return _get_entity_visibility(ent);
361 set_entity_visibility (entity *ent, visibility vis) {
362 assert(ent && ent->kind == k_entity);
363 if (vis != visibility_local)
364 assert((ent->allocation == allocation_static) ||
365 (ent->allocation == allocation_automatic));
366 /* @@@ Test that the owner type is not local, but how??
367 && get_class_visibility(get_entity_owner(ent)) != local));*/
368 ent->visibility = vis;
371 /* return the name of the visibility */
372 const char *get_visibility_name(visibility vis)
374 #define X(a) case a: return #a
377 X(visibility_external_visible);
378 X(visibility_external_allocated);
379 default: return "BAD VALUE";
385 (get_entity_variability)(const entity *ent) {
386 return _get_entity_variability(ent);
390 set_entity_variability (entity *ent, ent_variability var)
392 assert(ent && ent->kind == k_entity);
393 if (var == variability_part_constant)
394 assert(is_Class_type(ent->type) || is_Struct_type(ent->type));
396 if ((is_compound_type(ent->type)) &&
397 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
398 /* Allocate data structures for constant values */
399 ent->values = NEW_ARR_F(ir_node *, 0);
400 ent->val_paths = NEW_ARR_F(compound_graph_path *, 0);
402 if ((is_atomic_type(ent->type)) &&
403 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
404 /* Set default constant value. */
405 ent->value = new_rd_Unknown(get_const_code_irg(), get_type_mode(ent->type));
408 if ((is_compound_type(ent->type)) &&
409 (var == variability_uninitialized) && (ent->variability != variability_uninitialized)) {
410 /* Free data structures for constant values */
411 DEL_ARR_F(ent->values); ent->values = NULL;
412 DEL_ARR_F(ent->val_paths); ent->val_paths = NULL;
414 ent->variability = var;
417 /* return the name of the variablity */
418 const char *get_variability_name(ent_variability var)
420 #define X(a) case a: return #a
422 X(variability_uninitialized);
423 X(variability_initialized);
424 X(variability_part_constant);
425 X(variability_constant);
426 default: return "BAD VALUE";
432 (get_entity_volatility)(const entity *ent) {
433 return _get_entity_volatility(ent);
437 (set_entity_volatility)(entity *ent, ent_volatility vol) {
438 _set_entity_volatility(ent, vol);
441 /* return the name of the volatility */
442 const char *get_volatility_name(ent_volatility var)
444 #define X(a) case a: return #a
446 X(volatility_non_volatile);
447 X(volatility_is_volatile);
448 default: return "BAD VALUE";
454 (get_entity_peculiarity)(const entity *ent) {
455 return _get_entity_peculiarity(ent);
459 (set_entity_peculiarity)(entity *ent, peculiarity pec) {
460 _set_entity_peculiarity(ent, pec);
463 /* return the name of the peculiarity */
464 const char *get_peculiarity_name(peculiarity var)
466 #define X(a) case a: return #a
468 X(peculiarity_description);
469 X(peculiarity_inherited);
470 X(peculiarity_existent);
471 default: return "BAD VALUE";
476 /* Get the entity's stickyness */
478 (get_entity_stickyness)(const entity *ent) {
479 return _get_entity_stickyness(ent);
482 /* Set the entity's stickyness */
484 (set_entity_stickyness)(entity *ent, ent_stickyness stickyness) {
485 _set_entity_stickyness(ent, stickyness);
488 /* Set has no effect for existent entities of type method. */
490 get_atomic_ent_value(entity *ent)
492 assert(ent && is_atomic_entity(ent));
493 assert(ent->variability != variability_uninitialized);
494 return skip_Id (ent->value);
498 set_atomic_ent_value(entity *ent, ir_node *val) {
499 assert(is_atomic_entity(ent) && (ent->variability != variability_uninitialized));
500 if (is_Method_type(ent->type) && (ent->peculiarity == peculiarity_existent))
505 /* Returns true if the the node is representable as code on
507 int is_irn_const_expression(ir_node *n) {
510 /* we are in danger iff an exception will arise. TODO: be more precisely,
511 * for instance Div. will NOT rise if divisor != 0
513 if (is_binop(n) && !is_fragile_op(n))
514 return is_irn_const_expression(get_binop_left(n)) && is_irn_const_expression(get_binop_right(n));
517 switch(get_irn_opcode(n)) {
524 return is_irn_const_expression(get_irn_n(n, 0));
533 * Copies a firm subgraph that complies to the restrictions for
534 * constant expressions to current_block in current_ir_graph.
536 ir_node *copy_const_value(dbg_info *dbg, ir_node *n) {
540 /* @@@ GL I think we should implement this using the routines from irgopt for
541 dead node elimination/inlineing. */
544 switch (get_irn_opcode(n)) {
546 nn = new_d_Const_type(dbg, m, get_Const_tarval(n), get_Const_type(n));
549 nn = new_d_SymConst_type(dbg, get_SymConst_symbol(n), get_SymConst_kind(n),
550 get_SymConst_value_type(n));
553 nn = new_d_Add(dbg, copy_const_value(dbg, get_Add_left(n)),
554 copy_const_value(dbg, get_Add_right(n)), m); break;
556 nn = new_d_Sub(dbg, copy_const_value(dbg, get_Sub_left(n)),
557 copy_const_value(dbg, get_Sub_right(n)), m); break;
559 nn = new_d_Mul(dbg, copy_const_value(dbg, get_Mul_left(n)),
560 copy_const_value(dbg, get_Mul_right(n)), m); break;
562 nn = new_d_And(dbg, copy_const_value(dbg, get_And_left(n)),
563 copy_const_value(dbg, get_And_right(n)), m); break;
565 nn = new_d_Or(dbg, copy_const_value(dbg, get_Or_left(n)),
566 copy_const_value(dbg, get_Or_right(n)), m); break;
568 nn = new_d_Eor(dbg, copy_const_value(dbg, get_Eor_left(n)),
569 copy_const_value(dbg, get_Eor_right(n)), m); break;
571 nn = new_d_Cast(dbg, copy_const_value(dbg, get_Cast_op(n)), get_Cast_type(n)); break;
573 nn = new_d_Conv(dbg, copy_const_value(dbg, get_Conv_op(n)), m); break;
575 nn = new_d_Unknown(m); break;
578 assert(0 && "opcode invalid or not implemented");
585 /* Creates a new compound graph path. */
586 compound_graph_path *
587 new_compound_graph_path(type *tp, int length) {
588 compound_graph_path *res;
590 assert(is_type(tp) && is_compound_type(tp));
593 res = xmalloc(sizeof(*res) + (length-1) * sizeof(res->list[0]));
594 memset(res, 0, sizeof(*res) + (length-1) * sizeof(res->list[0]));
595 res->kind = k_ir_compound_graph_path;
602 /* Frees an graph path object */
603 void free_compound_graph_path (compound_graph_path *gr) {
604 assert(gr && is_compound_graph_path(gr));
609 /* Returns non-zero if an object is a compound graph path */
610 int is_compound_graph_path(void *thing) {
611 return (get_kind(thing) == k_ir_compound_graph_path);
614 /* Checks whether the path up to pos is correct. If the path contains a NULL,
615 * assumes the path is not complete and returns 'true'. */
616 int is_proper_compound_graph_path(compound_graph_path *gr, int pos) {
619 type *owner = gr->tp;
621 for (i = 0; i <= pos; i++) {
622 node = get_compound_graph_path_node(gr, i);
624 /* Path not yet complete. */
626 if (get_entity_owner(node) != owner)
628 owner = get_entity_type(node);
630 if (pos == get_compound_graph_path_length(gr))
631 if (!is_atomic_type(owner))
636 /* Returns the length of a graph path */
637 int get_compound_graph_path_length(compound_graph_path *gr) {
638 assert(gr && is_compound_graph_path(gr));
643 get_compound_graph_path_node(compound_graph_path *gr, int pos) {
644 assert(gr && is_compound_graph_path(gr));
645 assert(pos >= 0 && pos < gr->len);
646 return gr->list[pos].node;
650 set_compound_graph_path_node(compound_graph_path *gr, int pos, entity *node) {
651 assert(gr && is_compound_graph_path(gr));
652 assert(pos >= 0 && pos < gr->len);
653 assert(is_entity(node));
654 gr->list[pos].node = node;
655 assert(is_proper_compound_graph_path(gr, pos));
659 get_compound_graph_path_array_index(compound_graph_path *gr, int pos) {
660 assert(gr && is_compound_graph_path(gr));
661 assert(pos >= 0 && pos < gr->len);
662 return gr->list[pos].index;
666 set_compound_graph_path_array_index(compound_graph_path *gr, int pos, int index) {
667 assert(gr && is_compound_graph_path(gr));
668 assert(pos >= 0 && pos < gr->len);
669 gr->list[pos].index = index;
672 /* A value of a compound entity is a pair of value and the corresponding path to a member of
675 add_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path) {
676 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
677 ARR_APP1 (ir_node *, ent->values, val);
678 ARR_APP1 (compound_graph_path *, ent->val_paths, path);
682 set_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path, int pos) {
683 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
684 ent->values[pos] = val;
685 ent->val_paths[pos] = path;
689 get_compound_ent_n_values(entity *ent) {
690 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
691 return (ARR_LEN (ent->values));
695 get_compound_ent_value(entity *ent, int pos) {
696 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
697 return ent->values[pos];
700 compound_graph_path *
701 get_compound_ent_value_path(entity *ent, int pos) {
702 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
703 return ent->val_paths[pos];
707 * Returns non-zero, if two compound_graph_pathes are equal
709 static int equal_paths(compound_graph_path *path1, int *visited_indicees, compound_graph_path *path2) {
711 int len1 = get_compound_graph_path_length(path1);
712 int len2 = get_compound_graph_path_length(path2);
714 if (len2 > len1) return false;
716 for (i = 0; i < len1; i++) {
718 entity *node1 = get_compound_graph_path_node(path1, i);
719 entity *node2 = get_compound_graph_path_node(path2, i);
721 if (node1 != node2) return false;
723 tp = get_entity_owner(node1);
724 if (is_Array_type(tp)) {
727 /* Compute the index of this node. */
728 assert(get_array_n_dimensions(tp) == 1 && "multidim not implemented");
730 low = get_array_lower_bound_int(tp, 0);
731 if (low + visited_indicees[i] < get_compound_graph_path_array_index(path2, i)) {
732 visited_indicees[i]++;
736 assert(low + visited_indicees[i] == get_compound_graph_path_array_index(path2, i));
742 /* Returns the position of a value with the given path.
743 * The path must contain array indicees for all array element entities. */
744 int get_compound_ent_pos_by_path(entity *ent, compound_graph_path *path) {
745 int i, n_paths = get_compound_ent_n_values(ent);
746 int *visited_indicees = (int *)xcalloc(get_compound_graph_path_length(path), sizeof(int));
747 for (i = 0; i < n_paths; i ++) {
748 if (equal_paths(get_compound_ent_value_path(ent, i), visited_indicees, path))
755 printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
756 printf("Entity %s : ", get_entity_name(ent));
757 for (j = 0; j < get_compound_graph_path_length(path); ++j) {
758 entity *node = get_compound_graph_path_node(path, j);
759 printf("%s", get_entity_name(node));
760 if (is_Array_type(get_entity_owner(node)))
761 printf("[%d]", get_compound_graph_path_array_index(path, j));
763 printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
767 assert(0 && "path not found");
771 /* Returns a constant value given the access path.
772 * The path must contain array indicees for all array element entities. */
773 ir_node *get_compound_ent_value_by_path(entity *ent, compound_graph_path *path) {
774 return get_compound_ent_value(ent, get_compound_ent_pos_by_path(ent, path));
779 remove_compound_ent_value(entity *ent, entity *value_ent) {
781 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
782 for (i = 0; i < (ARR_LEN (ent->val_paths)); i++) {
783 compound_graph_path *path = ent->val_paths[i];
784 if (path->list[path->len-1].node == value_ent) {
785 for(; i < (ARR_LEN (ent->val_paths))-1; i++) {
786 ent->val_paths[i] = ent->val_paths[i+1];
787 ent->values[i] = ent->values[i+1];
789 ARR_SETLEN(entity*, ent->val_paths, ARR_LEN(ent->val_paths) - 1);
790 ARR_SETLEN(ir_node*, ent->values, ARR_LEN(ent->values) - 1);
797 add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
798 compound_graph_path *path;
799 type *owner_tp = get_entity_owner(member);
800 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
801 path = new_compound_graph_path(get_entity_type(ent), 1);
802 path->list[0].node = member;
803 if (is_Array_type(owner_tp)) {
807 assert(get_array_n_dimensions(owner_tp) == 1 && has_array_lower_bound(owner_tp, 0));
808 max = get_array_lower_bound_int(owner_tp, 0) -1;
809 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
810 int index = get_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0);
815 path->list[0].index = max + 1;
817 add_compound_ent_value_w_path(ent, val, path);
820 /* Copies the firm subgraph referenced by val to const_code_irg and adds
821 the node as constant initialization to ent.
822 The subgraph may not contain control flow operations.
824 copy_and_add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
825 ir_graph *rem = current_ir_graph;
827 assert(get_entity_variability(ent) != variability_uninitialized);
828 current_ir_graph = get_const_code_irg();
830 val = copy_const_value(val);
831 add_compound_ent_value(ent, val, member);
832 current_ir_graph = rem;
835 /* Copies the value i of the entity to current_block in current_ir_graph.
837 copy_compound_ent_value(entity *ent, int pos) {
838 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
839 return copy_const_value(ent->values[pos+1]);
843 get_compound_ent_value_member(entity *ent, int pos) {
844 compound_graph_path *path;
845 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
846 path = get_compound_ent_value_path(ent, pos);
848 return get_compound_graph_path_node(path, get_compound_graph_path_length(path)-1);
852 set_compound_ent_value(entity *ent, ir_node *val, entity *member, int pos) {
853 compound_graph_path *path;
854 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
855 path = get_compound_ent_value_path(ent, pos);
856 set_compound_graph_path_node(path, 0, member);
857 set_compound_ent_value_w_path(ent, val, path, pos);
861 set_array_entity_values(entity *ent, tarval **values, int num_vals) {
863 ir_graph *rem = current_ir_graph;
864 type *arrtp = get_entity_type(ent);
866 type *elttp = get_array_element_type(arrtp);
868 assert(is_Array_type(arrtp));
869 assert(get_array_n_dimensions(arrtp) == 1);
870 /* One bound is sufficient, the number of constant fields makes the
872 assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
873 assert(get_entity_variability(ent) != variability_uninitialized);
874 current_ir_graph = get_const_code_irg();
876 for (i = 0; i < num_vals; i++) {
877 val = new_Const_type(values[i], elttp);
878 add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
879 set_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0, i);
881 current_ir_graph = rem;
884 int get_compound_ent_value_offset_bits(entity *ent, int pos) {
885 compound_graph_path *path;
889 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
891 path = get_compound_ent_value_path(ent, pos);
892 path_len = get_compound_graph_path_length(path);
894 for (i = 0; i < path_len; ++i) {
895 entity *node = get_compound_graph_path_node(path, i);
896 type *node_tp = get_entity_type(node);
897 type *owner_tp = get_entity_owner(node);
898 if (is_Array_type(owner_tp)) {
899 int size = get_type_size_bits(node_tp);
900 int align = get_type_alignment_bits(node_tp);
904 assert(size % align == 0);
905 /* ansonsten aufrunden */
907 offset += size * get_compound_graph_path_array_index(path, i);
909 offset += get_entity_offset_bits(node);
915 int get_compound_ent_value_offset_bytes(entity *ent, int pos) {
916 int offset = get_compound_ent_value_offset_bits(ent, pos);
917 assert(offset % 8 == 0);
922 static void init_index(type *arr) {
926 assert(get_array_n_dimensions(arr) == 1);
928 if (has_array_lower_bound(arr, dim))
929 init = get_array_lower_bound_int(arr, 0) -1;
931 init = get_array_upper_bound_int(arr, 0) +1;
933 set_entity_link(get_array_element_entity(arr), INT_TO_PTR(init));
937 static int get_next_index(entity *elem_ent) {
938 type *arr = get_entity_owner(elem_ent);
942 assert(get_array_n_dimensions(arr) == 1);
944 if (has_array_lower_bound(arr, dim)) {
945 next = PTR_TO_INT(get_entity_link(elem_ent)) + 1;
946 if (has_array_upper_bound(arr, dim)) {
947 int upper = get_array_upper_bound_int(arr, dim);
948 if (next == upper) next = get_array_lower_bound_int(arr, dim);
951 next = PTR_TO_INT(get_entity_link(elem_ent)) - 1;
952 if (has_array_lower_bound(arr, dim)) {
953 int upper = get_array_upper_bound_int(arr, dim);
954 if (next == upper) next = get_array_upper_bound_int(arr, dim);
958 set_entity_link(elem_ent, INT_TO_PTR(next));
962 /* Compute the array indices in compound graph paths of initialized entities.
964 * All arrays must have fixed lower and upper bounds. One array can
965 * have an open bound. If there are several open bounds, we do
966 * nothing. There must be initializer elements for all array
967 * elements. Uses the link field in the array element entities. The
968 * array bounds must be representable as ints.
970 * (If the bounds are not representable as ints we have to represent
971 * the indices as firm nodes. But still we must be able to
972 * evaluate the index against the upper bound.)
974 void compute_compound_ent_array_indicees(entity *ent) {
975 type *tp = get_entity_type(ent);
977 entity *unknown_bound_entity = NULL;
979 if (!is_compound_type(tp) ||
980 (ent->variability == variability_uninitialized)) return ;
982 n_vals = get_compound_ent_n_values(ent);
983 if (n_vals == 0) return;
985 /* We can not compute the indexes if there is more than one array
986 with an unknown bound. For this remember the first entity that
987 represents such an array. It could be ent. */
988 if (is_Array_type(tp)) {
991 assert(get_array_n_dimensions(tp) == 1 && "other not implemented");
992 if (!has_array_lower_bound(tp, dim) || !has_array_upper_bound(tp, dim))
993 unknown_bound_entity = ent;
996 /* Initialize the entity links to lower bound -1 and test all path elements
998 for (i = 0; i < n_vals; ++i) {
999 compound_graph_path *path = get_compound_ent_value_path(ent, i);
1000 int j, path_len = get_compound_graph_path_length(path);
1001 for (j = 0; j < path_len; ++j) {
1002 entity *node = get_compound_graph_path_node(path, j);
1003 type *elem_tp = get_entity_type(node);
1005 if (is_Array_type(elem_tp)) {
1007 assert(get_array_n_dimensions(elem_tp) == 1 && "other not implemented");
1008 if (!has_array_lower_bound(elem_tp, dim) || !has_array_upper_bound(elem_tp, dim)) {
1009 if (!unknown_bound_entity) unknown_bound_entity = node;
1010 if (node != unknown_bound_entity) return;
1013 init_index(elem_tp);
1018 /* Finally compute the indexes ... */
1019 for (i = 0; i < n_vals; ++i) {
1020 compound_graph_path *path = get_compound_ent_value_path(ent, i);
1021 int j, path_len = get_compound_graph_path_length(path);
1022 for (j = 0; j < path_len; ++j) {
1023 entity *node = get_compound_graph_path_node(path, j);
1024 type *owner_tp = get_entity_owner(node);
1025 if (is_Array_type(owner_tp))
1026 set_compound_graph_path_array_index (path, j, get_next_index(node));
1031 /** resize: double the allocated buffer */
1032 static int *resize (int *buf, int *size) {
1033 int new_size = *size * 2;
1034 int *new_buf = xcalloc(new_size, sizeof(new_buf[0]));
1035 memcpy(new_buf, buf, *size);
1041 /* We sort the elements by placing them at their bit offset in an
1042 array where each entry represents one bit called permutation. In
1043 fact, we do not place the values themselves, as we would have to
1044 copy two things, the value and the path. We only remember the
1045 position in the old order. Each value should have a distinct
1046 position in the permutation.
1048 A second iteration now permutes the actual elements into two
1050 void sort_compound_ent_values(entity *ent) {
1058 ir_node **my_values;
1059 compound_graph_path **my_paths;
1061 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
1063 tp = get_entity_type(ent);
1064 n_vals = get_compound_ent_n_values(ent);
1065 tp_size = get_type_size_bits(tp);
1067 if (!is_compound_type(tp) ||
1068 (ent->variability == variability_uninitialized) ||
1069 (get_type_state(tp) != layout_fixed) ||
1070 (n_vals == 0) ) return;
1072 /* estimated upper bound for size. Better: use flexible array ... */
1073 size = ((tp_size > (n_vals * 32)) ? tp_size : (n_vals * 32)) * 4;
1074 permutation = xcalloc(size, sizeof(permutation[0]));
1076 for (i = 0; i < n_vals; ++i) {
1077 int pos = get_compound_ent_value_offset_bits(ent, i);
1078 while (pos >= size) {
1079 permutation = resize(permutation, &size);
1082 assert(permutation[pos] == 0 && "two values with the same offset");
1083 permutation[pos] = i + 1; /* We initialized with 0, so we can not distinguish entry 0.
1084 So inc all entries by one. */
1085 //fprintf(stderr, "i: %d, pos: %d \n", i, pos);
1089 my_values = NEW_ARR_F(ir_node *, n_vals);
1090 my_paths = NEW_ARR_F(compound_graph_path *, n_vals);
1091 for (i = 0; i < size; ++i) {
1092 int pos = permutation[i];
1094 //fprintf(stderr, "pos: %d i: %d next %d \n", i, pos, next);
1095 assert(next < n_vals);
1096 pos--; /* We increased the pos by one */
1097 my_values[next] = get_compound_ent_value (ent, pos);
1098 my_paths [next] = get_compound_ent_value_path(ent, pos);
1104 DEL_ARR_F(ent->values);
1105 ent->values = my_values;
1106 DEL_ARR_F(ent->val_paths);
1107 ent->val_paths = my_paths;
1111 (get_entity_offset_bytes)(const entity *ent) {
1112 return _get_entity_offset_bytes(ent);
1116 (get_entity_offset_bits)(const entity *ent) {
1117 return _get_entity_offset_bits(ent);
1121 (set_entity_offset_bytes)(entity *ent, int offset) {
1122 _set_entity_offset_bytes(ent, offset);
1126 (set_entity_offset_bits)(entity *ent, int offset) {
1127 _set_entity_offset_bits(ent, offset);
1131 add_entity_overwrites(entity *ent, entity *overwritten) {
1132 assert(ent && is_Class_type(get_entity_owner(ent)));
1133 ARR_APP1(entity *, ent->overwrites, overwritten);
1134 ARR_APP1(entity *, overwritten->overwrittenby, ent);
1138 get_entity_n_overwrites(entity *ent) {
1139 assert(ent && is_Class_type(get_entity_owner(ent)));
1140 return (ARR_LEN(ent->overwrites));
1144 get_entity_overwrites_index(entity *ent, entity *overwritten) {
1146 assert(ent && is_Class_type(get_entity_owner(ent)));
1147 for (i = 0; i < get_entity_n_overwrites(ent); i++)
1148 if (get_entity_overwrites(ent, i) == overwritten)
1154 get_entity_overwrites (entity *ent, int pos) {
1155 assert(ent && is_Class_type(get_entity_owner(ent)));
1156 assert(pos < get_entity_n_overwrites(ent));
1157 return ent->overwrites[pos];
1161 set_entity_overwrites (entity *ent, int pos, entity *overwritten) {
1162 assert(ent && is_Class_type(get_entity_owner(ent)));
1163 assert(pos < get_entity_n_overwrites(ent));
1164 ent->overwrites[pos] = overwritten;
1168 remove_entity_overwrites(entity *ent, entity *overwritten) {
1170 assert(ent && is_Class_type(get_entity_owner(ent)));
1171 for (i = 0; i < (ARR_LEN (ent->overwrites)); i++)
1172 if (ent->overwrites[i] == overwritten) {
1173 for(; i < (ARR_LEN (ent->overwrites))-1; i++)
1174 ent->overwrites[i] = ent->overwrites[i+1];
1175 ARR_SETLEN(entity*, ent->overwrites, ARR_LEN(ent->overwrites) - 1);
1181 add_entity_overwrittenby (entity *ent, entity *overwrites) {
1182 assert(ent && is_Class_type(get_entity_owner(ent)));
1183 add_entity_overwrites(overwrites, ent);
1187 get_entity_n_overwrittenby (entity *ent) {
1188 assert(ent && is_Class_type(get_entity_owner(ent)));
1189 return (ARR_LEN (ent->overwrittenby));
1193 get_entity_overwrittenby_index(entity *ent, entity *overwrites) {
1195 assert(ent && is_Class_type(get_entity_owner(ent)));
1196 for (i = 0; i < get_entity_n_overwrittenby(ent); i++)
1197 if (get_entity_overwrittenby(ent, i) == overwrites)
1203 get_entity_overwrittenby (entity *ent, int pos) {
1204 assert(ent && is_Class_type(get_entity_owner(ent)));
1205 assert(pos < get_entity_n_overwrittenby(ent));
1206 return ent->overwrittenby[pos];
1210 set_entity_overwrittenby (entity *ent, int pos, entity *overwrites) {
1211 assert(ent && is_Class_type(get_entity_owner(ent)));
1212 assert(pos < get_entity_n_overwrittenby(ent));
1213 ent->overwrittenby[pos] = overwrites;
1216 void remove_entity_overwrittenby(entity *ent, entity *overwrites) {
1218 assert(ent && is_Class_type(get_entity_owner(ent)));
1219 for (i = 0; i < (ARR_LEN (ent->overwrittenby)); i++)
1220 if (ent->overwrittenby[i] == overwrites) {
1221 for(; i < (ARR_LEN (ent->overwrittenby))-1; i++)
1222 ent->overwrittenby[i] = ent->overwrittenby[i+1];
1223 ARR_SETLEN(entity*, ent->overwrittenby, ARR_LEN(ent->overwrittenby) - 1);
1228 /* A link to store intermediate information */
1230 (get_entity_link)(const entity *ent) {
1231 return _get_entity_link(ent);
1235 (set_entity_link)(entity *ent, void *l) {
1236 _set_entity_link(ent, l);
1240 (get_entity_irg)(const entity *ent) {
1241 return _get_entity_irg(ent);
1245 set_entity_irg(entity *ent, ir_graph *irg) {
1246 assert(ent && is_Method_type(get_entity_type(ent)));
1247 /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
1248 * Methode selbst nicht mehr aufgerufen werden kann, die Entität
1249 * aber erhalten bleiben soll? Wandle die Entitaet in description oder
1252 assert((irg && ent->peculiarity == peculiarity_existent) ||
1253 (!irg && (ent->peculiarity == peculiarity_existent)
1254 && (ent -> visibility == visibility_external_allocated)) ||
1255 (!irg && ent->peculiarity == peculiarity_description) ||
1256 (!irg && ent->peculiarity == peculiarity_inherited));
1261 (is_entity)(const void *thing) {
1262 return _is_entity(thing);
1265 int is_atomic_entity(entity *ent) {
1266 type* t = get_entity_type(ent);
1267 assert(ent && ent->kind == k_entity);
1268 return (is_Primitive_type(t) || is_Pointer_type(t) ||
1269 is_Enumeration_type(t) || is_Method_type(t));
1272 int is_compound_entity(entity *ent) {
1273 type* t = get_entity_type(ent);
1274 assert(ent && ent->kind == k_entity);
1275 return (is_Class_type(t) || is_Struct_type(t) ||
1276 is_Array_type(t) || is_Union_type(t));
1280 * @todo not implemented!!! */
1281 bool equal_entity(entity *ent1, entity *ent2) {
1282 fprintf(stderr, " calling unimplemented equal entity!!! \n");
1287 unsigned long (get_entity_visited)(entity *ent) {
1288 return _get_entity_visited(ent);
1291 void (set_entity_visited)(entity *ent, unsigned long num) {
1292 _set_entity_visited(ent, num);
1295 /* Sets visited field in entity to entity_visited. */
1296 void (mark_entity_visited)(entity *ent) {
1297 _mark_entity_visited(ent);
1300 int (entity_visited)(entity *ent) {
1301 return _entity_visited(ent);
1304 int (entity_not_visited)(entity *ent) {
1305 return _entity_not_visited(ent);
1308 unsigned (get_entity_additional_properties)(const entity *ent) {
1309 return _get_entity_additional_properties(ent);
1312 void (set_entity_additional_properties)(entity *ent, unsigned property_mask) {
1313 _set_entity_additional_properties(ent, property_mask);
1316 void (set_entity_additional_property)(entity *ent, unsigned flag) {
1317 _set_entity_additional_property(ent, (irg_additional_property)flag);
1320 /* Returns the calling convention of an entities graph. */
1321 unsigned (get_entity_calling_convention)(const entity *ent) {
1322 return _get_entity_calling_convention(ent);
1325 /* Sets the calling convention of an entities graph. */
1326 void (set_entity_calling_convention)(entity *ent, unsigned cc_mask) {
1327 _set_entity_calling_convention(ent, cc_mask);
1330 void firm_init_entity(void)
1332 symconst_symbol sym;
1334 assert(firm_unknown_type && "Call init_type() before firm_init_entity()!");
1335 assert(!unknown_entity && "Call firm_init_entity() only once!");
1336 unknown_entity = new_rd_entity(NULL, firm_unknown_type, new_id_from_str(UNKNOWN_ENTITY_NAME), firm_unknown_type);
1337 set_entity_visibility(unknown_entity, visibility_external_allocated);
1338 set_entity_ld_ident(unknown_entity, get_entity_ident(unknown_entity));
1340 sym.entity_p = unknown_entity;
1341 current_ir_graph = get_const_code_irg();
1342 unknown_entity->value = new_SymConst(sym, symconst_addr_ent);