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 = mtp_property_inherited;
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);
204 ent->param_weight = NULL;
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));
532 * Copies a firm subgraph that complies to the restrictions for
533 * constant expressions to current_block in current_ir_graph.
535 ir_node *copy_const_value(dbg_info *dbg, ir_node *n) {
539 /* @@@ GL I think we should implement this using the routines from irgopt for
540 dead node elimination/inlineing. */
543 switch (get_irn_opcode(n)) {
545 nn = new_d_Const_type(dbg, m, get_Const_tarval(n), get_Const_type(n));
548 nn = new_d_SymConst_type(dbg, get_SymConst_symbol(n), get_SymConst_kind(n),
549 get_SymConst_value_type(n));
552 nn = new_d_Add(dbg, copy_const_value(dbg, get_Add_left(n)),
553 copy_const_value(dbg, get_Add_right(n)), m); break;
555 nn = new_d_Sub(dbg, copy_const_value(dbg, get_Sub_left(n)),
556 copy_const_value(dbg, get_Sub_right(n)), m); break;
558 nn = new_d_Mul(dbg, copy_const_value(dbg, get_Mul_left(n)),
559 copy_const_value(dbg, get_Mul_right(n)), m); break;
561 nn = new_d_And(dbg, copy_const_value(dbg, get_And_left(n)),
562 copy_const_value(dbg, get_And_right(n)), m); break;
564 nn = new_d_Or(dbg, copy_const_value(dbg, get_Or_left(n)),
565 copy_const_value(dbg, get_Or_right(n)), m); break;
567 nn = new_d_Eor(dbg, copy_const_value(dbg, get_Eor_left(n)),
568 copy_const_value(dbg, get_Eor_right(n)), m); break;
570 nn = new_d_Cast(dbg, copy_const_value(dbg, get_Cast_op(n)), get_Cast_type(n)); break;
572 nn = new_d_Conv(dbg, copy_const_value(dbg, get_Conv_op(n)), m); break;
574 nn = new_d_Unknown(m); break;
577 assert(0 && "opcode invalid or not implemented");
584 /* Creates a new compound graph path. */
585 compound_graph_path *
586 new_compound_graph_path(type *tp, int length) {
587 compound_graph_path *res;
589 assert(is_type(tp) && is_compound_type(tp));
592 res = xmalloc(sizeof(*res) + (length-1) * sizeof(res->list[0]));
593 memset(res, 0, sizeof(*res) + (length-1) * sizeof(res->list[0]));
594 res->kind = k_ir_compound_graph_path;
601 /* Frees an graph path object */
602 void free_compound_graph_path (compound_graph_path *gr) {
603 assert(gr && is_compound_graph_path(gr));
608 /* Returns non-zero if an object is a compound graph path */
609 int is_compound_graph_path(void *thing) {
610 return (get_kind(thing) == k_ir_compound_graph_path);
613 /* Checks whether the path up to pos is correct. If the path contains a NULL,
614 * assumes the path is not complete and returns 'true'. */
615 int is_proper_compound_graph_path(compound_graph_path *gr, int pos) {
618 type *owner = gr->tp;
620 for (i = 0; i <= pos; i++) {
621 node = get_compound_graph_path_node(gr, i);
623 /* Path not yet complete. */
625 if (get_entity_owner(node) != owner)
627 owner = get_entity_type(node);
629 if (pos == get_compound_graph_path_length(gr))
630 if (!is_atomic_type(owner))
635 /* Returns the length of a graph path */
636 int get_compound_graph_path_length(compound_graph_path *gr) {
637 assert(gr && is_compound_graph_path(gr));
642 get_compound_graph_path_node(compound_graph_path *gr, int pos) {
643 assert(gr && is_compound_graph_path(gr));
644 assert(pos >= 0 && pos < gr->len);
645 return gr->list[pos].node;
649 set_compound_graph_path_node(compound_graph_path *gr, int pos, entity *node) {
650 assert(gr && is_compound_graph_path(gr));
651 assert(pos >= 0 && pos < gr->len);
652 assert(is_entity(node));
653 gr->list[pos].node = node;
654 assert(is_proper_compound_graph_path(gr, pos));
658 get_compound_graph_path_array_index(compound_graph_path *gr, int pos) {
659 assert(gr && is_compound_graph_path(gr));
660 assert(pos >= 0 && pos < gr->len);
661 return gr->list[pos].index;
665 set_compound_graph_path_array_index(compound_graph_path *gr, int pos, int index) {
666 assert(gr && is_compound_graph_path(gr));
667 assert(pos >= 0 && pos < gr->len);
668 gr->list[pos].index = index;
671 /* A value of a compound entity is a pair of value and the corresponding path to a member of
674 add_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path) {
675 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
676 ARR_APP1 (ir_node *, ent->values, val);
677 ARR_APP1 (compound_graph_path *, ent->val_paths, path);
681 set_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path, int pos) {
682 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
683 ent->values[pos] = val;
684 ent->val_paths[pos] = path;
688 get_compound_ent_n_values(entity *ent) {
689 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
690 return (ARR_LEN (ent->values));
694 get_compound_ent_value(entity *ent, int pos) {
695 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
696 return ent->values[pos];
699 compound_graph_path *
700 get_compound_ent_value_path(entity *ent, int pos) {
701 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
702 return ent->val_paths[pos];
706 * Returns non-zero, if two compound_graph_pathes are equal
708 static int equal_paths(compound_graph_path *path1, int *visited_indicees, compound_graph_path *path2) {
710 int len1 = get_compound_graph_path_length(path1);
711 int len2 = get_compound_graph_path_length(path2);
713 if (len2 > len1) return 0;
715 for (i = 0; i < len1; i++) {
717 entity *node1 = get_compound_graph_path_node(path1, i);
718 entity *node2 = get_compound_graph_path_node(path2, i);
720 if (node1 != node2) return 0;
722 tp = get_entity_owner(node1);
723 if (is_Array_type(tp)) {
726 /* Compute the index of this node. */
727 assert(get_array_n_dimensions(tp) == 1 && "multidim not implemented");
729 low = get_array_lower_bound_int(tp, 0);
730 if (low + visited_indicees[i] < get_compound_graph_path_array_index(path2, i)) {
731 visited_indicees[i]++;
735 assert(low + visited_indicees[i] == get_compound_graph_path_array_index(path2, i));
741 /* Returns the position of a value with the given path.
742 * The path must contain array indicees for all array element entities. */
743 int get_compound_ent_pos_by_path(entity *ent, compound_graph_path *path) {
744 int i, n_paths = get_compound_ent_n_values(ent);
745 int *visited_indicees = (int *)xcalloc(get_compound_graph_path_length(path), sizeof(int));
746 for (i = 0; i < n_paths; i ++) {
747 if (equal_paths(get_compound_ent_value_path(ent, i), visited_indicees, path))
754 printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
755 printf("Entity %s : ", get_entity_name(ent));
756 for (j = 0; j < get_compound_graph_path_length(path); ++j) {
757 entity *node = get_compound_graph_path_node(path, j);
758 printf("%s", get_entity_name(node));
759 if (is_Array_type(get_entity_owner(node)))
760 printf("[%d]", get_compound_graph_path_array_index(path, j));
762 printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
766 assert(0 && "path not found");
770 /* Returns a constant value given the access path.
771 * The path must contain array indicees for all array element entities. */
772 ir_node *get_compound_ent_value_by_path(entity *ent, compound_graph_path *path) {
773 return get_compound_ent_value(ent, get_compound_ent_pos_by_path(ent, path));
778 remove_compound_ent_value(entity *ent, entity *value_ent) {
780 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
781 for (i = 0; i < (ARR_LEN (ent->val_paths)); i++) {
782 compound_graph_path *path = ent->val_paths[i];
783 if (path->list[path->len-1].node == value_ent) {
784 for(; i < (ARR_LEN (ent->val_paths))-1; i++) {
785 ent->val_paths[i] = ent->val_paths[i+1];
786 ent->values[i] = ent->values[i+1];
788 ARR_SETLEN(entity*, ent->val_paths, ARR_LEN(ent->val_paths) - 1);
789 ARR_SETLEN(ir_node*, ent->values, ARR_LEN(ent->values) - 1);
796 add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
797 compound_graph_path *path;
798 type *owner_tp = get_entity_owner(member);
799 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
800 path = new_compound_graph_path(get_entity_type(ent), 1);
801 path->list[0].node = member;
802 if (is_Array_type(owner_tp)) {
806 assert(get_array_n_dimensions(owner_tp) == 1 && has_array_lower_bound(owner_tp, 0));
807 max = get_array_lower_bound_int(owner_tp, 0) -1;
808 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
809 int index = get_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0);
814 path->list[0].index = max + 1;
816 add_compound_ent_value_w_path(ent, val, path);
819 /* Copies the firm subgraph referenced by val to const_code_irg and adds
820 the node as constant initialization to ent.
821 The subgraph may not contain control flow operations.
823 copy_and_add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
824 ir_graph *rem = current_ir_graph;
826 assert(get_entity_variability(ent) != variability_uninitialized);
827 current_ir_graph = get_const_code_irg();
829 val = copy_const_value(val);
830 add_compound_ent_value(ent, val, member);
831 current_ir_graph = rem;
834 /* Copies the value i of the entity to current_block in current_ir_graph.
836 copy_compound_ent_value(entity *ent, int pos) {
837 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
838 return copy_const_value(ent->values[pos+1]);
842 get_compound_ent_value_member(entity *ent, int pos) {
843 compound_graph_path *path;
844 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
845 path = get_compound_ent_value_path(ent, pos);
847 return get_compound_graph_path_node(path, get_compound_graph_path_length(path)-1);
851 set_compound_ent_value(entity *ent, ir_node *val, entity *member, int pos) {
852 compound_graph_path *path;
853 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
854 path = get_compound_ent_value_path(ent, pos);
855 set_compound_graph_path_node(path, 0, member);
856 set_compound_ent_value_w_path(ent, val, path, pos);
860 set_array_entity_values(entity *ent, tarval **values, int num_vals) {
862 ir_graph *rem = current_ir_graph;
863 type *arrtp = get_entity_type(ent);
865 type *elttp = get_array_element_type(arrtp);
867 assert(is_Array_type(arrtp));
868 assert(get_array_n_dimensions(arrtp) == 1);
869 /* One bound is sufficient, the number of constant fields makes the
871 assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
872 assert(get_entity_variability(ent) != variability_uninitialized);
873 current_ir_graph = get_const_code_irg();
875 for (i = 0; i < num_vals; i++) {
876 val = new_Const_type(values[i], elttp);
877 add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
878 set_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0, i);
880 current_ir_graph = rem;
883 int get_compound_ent_value_offset_bits(entity *ent, int pos) {
884 compound_graph_path *path;
888 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
890 path = get_compound_ent_value_path(ent, pos);
891 path_len = get_compound_graph_path_length(path);
893 for (i = 0; i < path_len; ++i) {
894 entity *node = get_compound_graph_path_node(path, i);
895 type *node_tp = get_entity_type(node);
896 type *owner_tp = get_entity_owner(node);
897 if (is_Array_type(owner_tp)) {
898 int size = get_type_size_bits(node_tp);
899 int align = get_type_alignment_bits(node_tp);
903 assert(size % align == 0);
904 /* ansonsten aufrunden */
906 offset += size * get_compound_graph_path_array_index(path, i);
908 offset += get_entity_offset_bits(node);
914 int get_compound_ent_value_offset_bytes(entity *ent, int pos) {
915 int offset = get_compound_ent_value_offset_bits(ent, pos);
916 assert(offset % 8 == 0);
921 static void init_index(type *arr) {
925 assert(get_array_n_dimensions(arr) == 1);
927 if (has_array_lower_bound(arr, dim))
928 init = get_array_lower_bound_int(arr, 0) -1;
930 init = get_array_upper_bound_int(arr, 0) +1;
932 set_entity_link(get_array_element_entity(arr), INT_TO_PTR(init));
936 static int get_next_index(entity *elem_ent) {
937 type *arr = get_entity_owner(elem_ent);
941 assert(get_array_n_dimensions(arr) == 1);
943 if (has_array_lower_bound(arr, dim)) {
944 next = PTR_TO_INT(get_entity_link(elem_ent)) + 1;
945 if (has_array_upper_bound(arr, dim)) {
946 int upper = get_array_upper_bound_int(arr, dim);
947 if (next == upper) next = get_array_lower_bound_int(arr, dim);
950 next = PTR_TO_INT(get_entity_link(elem_ent)) - 1;
951 if (has_array_lower_bound(arr, dim)) {
952 int upper = get_array_upper_bound_int(arr, dim);
953 if (next == upper) next = get_array_upper_bound_int(arr, dim);
957 set_entity_link(elem_ent, INT_TO_PTR(next));
961 /* Compute the array indices in compound graph paths of initialized entities.
963 * All arrays must have fixed lower and upper bounds. One array can
964 * have an open bound. If there are several open bounds, we do
965 * nothing. There must be initializer elements for all array
966 * elements. Uses the link field in the array element entities. The
967 * array bounds must be representable as ints.
969 * (If the bounds are not representable as ints we have to represent
970 * the indices as firm nodes. But still we must be able to
971 * evaluate the index against the upper bound.)
973 void compute_compound_ent_array_indicees(entity *ent) {
974 type *tp = get_entity_type(ent);
976 entity *unknown_bound_entity = NULL;
978 if (!is_compound_type(tp) ||
979 (ent->variability == variability_uninitialized)) return ;
981 n_vals = get_compound_ent_n_values(ent);
982 if (n_vals == 0) return;
984 /* We can not compute the indexes if there is more than one array
985 with an unknown bound. For this remember the first entity that
986 represents such an array. It could be ent. */
987 if (is_Array_type(tp)) {
990 assert(get_array_n_dimensions(tp) == 1 && "other not implemented");
991 if (!has_array_lower_bound(tp, dim) || !has_array_upper_bound(tp, dim))
992 unknown_bound_entity = ent;
995 /* Initialize the entity links to lower bound -1 and test all path elements
997 for (i = 0; i < n_vals; ++i) {
998 compound_graph_path *path = get_compound_ent_value_path(ent, i);
999 int j, path_len = get_compound_graph_path_length(path);
1000 for (j = 0; j < path_len; ++j) {
1001 entity *node = get_compound_graph_path_node(path, j);
1002 type *elem_tp = get_entity_type(node);
1004 if (is_Array_type(elem_tp)) {
1006 assert(get_array_n_dimensions(elem_tp) == 1 && "other not implemented");
1007 if (!has_array_lower_bound(elem_tp, dim) || !has_array_upper_bound(elem_tp, dim)) {
1008 if (!unknown_bound_entity) unknown_bound_entity = node;
1009 if (node != unknown_bound_entity) return;
1012 init_index(elem_tp);
1017 /* Finally compute the indexes ... */
1018 for (i = 0; i < n_vals; ++i) {
1019 compound_graph_path *path = get_compound_ent_value_path(ent, i);
1020 int j, path_len = get_compound_graph_path_length(path);
1021 for (j = 0; j < path_len; ++j) {
1022 entity *node = get_compound_graph_path_node(path, j);
1023 type *owner_tp = get_entity_owner(node);
1024 if (is_Array_type(owner_tp))
1025 set_compound_graph_path_array_index (path, j, get_next_index(node));
1030 /** resize: double the allocated buffer */
1031 static int *resize (int *buf, int *size) {
1032 int new_size = *size * 2;
1033 int *new_buf = xcalloc(new_size, sizeof(new_buf[0]));
1034 memcpy(new_buf, buf, *size);
1040 /* We sort the elements by placing them at their bit offset in an
1041 array where each entry represents one bit called permutation. In
1042 fact, we do not place the values themselves, as we would have to
1043 copy two things, the value and the path. We only remember the
1044 position in the old order. Each value should have a distinct
1045 position in the permutation.
1047 A second iteration now permutes the actual elements into two
1049 void sort_compound_ent_values(entity *ent) {
1057 ir_node **my_values;
1058 compound_graph_path **my_paths;
1060 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
1062 tp = get_entity_type(ent);
1063 n_vals = get_compound_ent_n_values(ent);
1064 tp_size = get_type_size_bits(tp);
1066 if (!is_compound_type(tp) ||
1067 (ent->variability == variability_uninitialized) ||
1068 (get_type_state(tp) != layout_fixed) ||
1069 (n_vals == 0) ) return;
1071 /* estimated upper bound for size. Better: use flexible array ... */
1072 size = ((tp_size > (n_vals * 32)) ? tp_size : (n_vals * 32)) * 4;
1073 permutation = xcalloc(size, sizeof(permutation[0]));
1075 for (i = 0; i < n_vals; ++i) {
1076 int pos = get_compound_ent_value_offset_bits(ent, i);
1077 while (pos >= size) {
1078 permutation = resize(permutation, &size);
1081 assert(permutation[pos] == 0 && "two values with the same offset");
1082 permutation[pos] = i + 1; /* We initialized with 0, so we can not distinguish entry 0.
1083 So inc all entries by one. */
1084 //fprintf(stderr, "i: %d, pos: %d \n", i, pos);
1088 my_values = NEW_ARR_F(ir_node *, n_vals);
1089 my_paths = NEW_ARR_F(compound_graph_path *, n_vals);
1090 for (i = 0; i < size; ++i) {
1091 int pos = permutation[i];
1093 //fprintf(stderr, "pos: %d i: %d next %d \n", i, pos, next);
1094 assert(next < n_vals);
1095 pos--; /* We increased the pos by one */
1096 my_values[next] = get_compound_ent_value (ent, pos);
1097 my_paths [next] = get_compound_ent_value_path(ent, pos);
1103 DEL_ARR_F(ent->values);
1104 ent->values = my_values;
1105 DEL_ARR_F(ent->val_paths);
1106 ent->val_paths = my_paths;
1110 (get_entity_offset_bytes)(const entity *ent) {
1111 return _get_entity_offset_bytes(ent);
1115 (get_entity_offset_bits)(const entity *ent) {
1116 return _get_entity_offset_bits(ent);
1120 (set_entity_offset_bytes)(entity *ent, int offset) {
1121 _set_entity_offset_bytes(ent, offset);
1125 (set_entity_offset_bits)(entity *ent, int offset) {
1126 _set_entity_offset_bits(ent, offset);
1130 add_entity_overwrites(entity *ent, entity *overwritten) {
1131 assert(ent && is_Class_type(get_entity_owner(ent)));
1132 ARR_APP1(entity *, ent->overwrites, overwritten);
1133 ARR_APP1(entity *, overwritten->overwrittenby, ent);
1137 get_entity_n_overwrites(entity *ent) {
1138 assert(ent && is_Class_type(get_entity_owner(ent)));
1139 return (ARR_LEN(ent->overwrites));
1143 get_entity_overwrites_index(entity *ent, entity *overwritten) {
1145 assert(ent && is_Class_type(get_entity_owner(ent)));
1146 for (i = 0; i < get_entity_n_overwrites(ent); i++)
1147 if (get_entity_overwrites(ent, i) == overwritten)
1153 get_entity_overwrites (entity *ent, int pos) {
1154 assert(ent && is_Class_type(get_entity_owner(ent)));
1155 assert(pos < get_entity_n_overwrites(ent));
1156 return ent->overwrites[pos];
1160 set_entity_overwrites (entity *ent, int pos, entity *overwritten) {
1161 assert(ent && is_Class_type(get_entity_owner(ent)));
1162 assert(pos < get_entity_n_overwrites(ent));
1163 ent->overwrites[pos] = overwritten;
1167 remove_entity_overwrites(entity *ent, entity *overwritten) {
1169 assert(ent && is_Class_type(get_entity_owner(ent)));
1170 for (i = 0; i < (ARR_LEN (ent->overwrites)); i++)
1171 if (ent->overwrites[i] == overwritten) {
1172 for(; i < (ARR_LEN (ent->overwrites))-1; i++)
1173 ent->overwrites[i] = ent->overwrites[i+1];
1174 ARR_SETLEN(entity*, ent->overwrites, ARR_LEN(ent->overwrites) - 1);
1180 add_entity_overwrittenby (entity *ent, entity *overwrites) {
1181 assert(ent && is_Class_type(get_entity_owner(ent)));
1182 add_entity_overwrites(overwrites, ent);
1186 get_entity_n_overwrittenby (entity *ent) {
1187 assert(ent && is_Class_type(get_entity_owner(ent)));
1188 return (ARR_LEN (ent->overwrittenby));
1192 get_entity_overwrittenby_index(entity *ent, entity *overwrites) {
1194 assert(ent && is_Class_type(get_entity_owner(ent)));
1195 for (i = 0; i < get_entity_n_overwrittenby(ent); i++)
1196 if (get_entity_overwrittenby(ent, i) == overwrites)
1202 get_entity_overwrittenby (entity *ent, int pos) {
1203 assert(ent && is_Class_type(get_entity_owner(ent)));
1204 assert(pos < get_entity_n_overwrittenby(ent));
1205 return ent->overwrittenby[pos];
1209 set_entity_overwrittenby (entity *ent, int pos, entity *overwrites) {
1210 assert(ent && is_Class_type(get_entity_owner(ent)));
1211 assert(pos < get_entity_n_overwrittenby(ent));
1212 ent->overwrittenby[pos] = overwrites;
1215 void remove_entity_overwrittenby(entity *ent, entity *overwrites) {
1217 assert(ent && is_Class_type(get_entity_owner(ent)));
1218 for (i = 0; i < (ARR_LEN (ent->overwrittenby)); i++)
1219 if (ent->overwrittenby[i] == overwrites) {
1220 for(; i < (ARR_LEN (ent->overwrittenby))-1; i++)
1221 ent->overwrittenby[i] = ent->overwrittenby[i+1];
1222 ARR_SETLEN(entity*, ent->overwrittenby, ARR_LEN(ent->overwrittenby) - 1);
1227 /* A link to store intermediate information */
1229 (get_entity_link)(const entity *ent) {
1230 return _get_entity_link(ent);
1234 (set_entity_link)(entity *ent, void *l) {
1235 _set_entity_link(ent, l);
1239 (get_entity_irg)(const entity *ent) {
1240 return _get_entity_irg(ent);
1244 set_entity_irg(entity *ent, ir_graph *irg) {
1245 assert(ent && is_Method_type(get_entity_type(ent)));
1246 /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
1247 * Methode selbst nicht mehr aufgerufen werden kann, die Entität
1248 * aber erhalten bleiben soll? Wandle die Entitaet in description oder
1251 assert((irg && ent->peculiarity == peculiarity_existent) ||
1252 (!irg && (ent->peculiarity == peculiarity_existent)
1253 && (ent -> visibility == visibility_external_allocated)) ||
1254 (!irg && ent->peculiarity == peculiarity_description) ||
1255 (!irg && ent->peculiarity == peculiarity_inherited));
1260 (is_entity)(const void *thing) {
1261 return _is_entity(thing);
1264 int is_atomic_entity(entity *ent) {
1265 type* t = get_entity_type(ent);
1266 assert(ent && ent->kind == k_entity);
1267 return (is_Primitive_type(t) || is_Pointer_type(t) ||
1268 is_Enumeration_type(t) || is_Method_type(t));
1271 int is_compound_entity(entity *ent) {
1272 type* t = get_entity_type(ent);
1273 assert(ent && ent->kind == k_entity);
1274 return (is_Class_type(t) || is_Struct_type(t) ||
1275 is_Array_type(t) || is_Union_type(t));
1279 * @todo not implemented!!! */
1280 int equal_entity(entity *ent1, entity *ent2) {
1281 fprintf(stderr, " calling unimplemented equal entity!!! \n");
1286 unsigned long (get_entity_visited)(entity *ent) {
1287 return _get_entity_visited(ent);
1290 void (set_entity_visited)(entity *ent, unsigned long num) {
1291 _set_entity_visited(ent, num);
1294 /* Sets visited field in entity to entity_visited. */
1295 void (mark_entity_visited)(entity *ent) {
1296 _mark_entity_visited(ent);
1299 int (entity_visited)(entity *ent) {
1300 return _entity_visited(ent);
1303 int (entity_not_visited)(entity *ent) {
1304 return _entity_not_visited(ent);
1307 /* Returns the mask of the additional entity properties. */
1308 unsigned get_entity_additional_properties(entity *ent) {
1311 assert(is_Method_type(get_entity_type(ent)));
1313 /* first check, if the graph has additional properties */
1314 irg = get_entity_irg(ent);
1317 return get_irg_additional_properties(irg);
1319 if (ent->irg_add_properties & mtp_property_inherited)
1320 return get_method_additional_properties(get_entity_type(ent));
1322 return ent->irg_add_properties;
1325 /* Sets the mask of the additional graph properties. */
1326 void set_entity_additional_properties(entity *ent, unsigned property_mask)
1330 assert(is_Method_type(get_entity_type(ent)));
1332 /* first check, if the graph exists */
1333 irg = get_entity_irg(ent);
1335 set_irg_additional_properties(irg, property_mask);
1337 /* do not allow to set the mtp_property_inherited flag or
1338 * the automatic inheritance of flags will not work */
1339 ent->irg_add_properties = property_mask & ~mtp_property_inherited;
1343 /* Sets one additional graph property. */
1344 void set_entity_additional_property(entity *ent, mtp_additional_property flag)
1348 assert(is_Method_type(get_entity_type(ent)));
1350 /* first check, if the graph exists */
1351 irg = get_entity_irg(ent);
1353 set_irg_additional_property(irg, flag);
1355 unsigned mask = ent->irg_add_properties;
1357 if (mask & mtp_property_inherited)
1358 mask = get_method_additional_properties(get_entity_type(ent));
1360 /* do not allow to set the mtp_property_inherited flag or
1361 * the automatic inheritance of flags will not work */
1362 ent->irg_add_properties = mask | (flag & ~mtp_property_inherited);
1366 void firm_init_entity(void)
1368 symconst_symbol sym;
1370 assert(firm_unknown_type && "Call init_type() before firm_init_entity()!");
1371 assert(!unknown_entity && "Call firm_init_entity() only once!");
1372 unknown_entity = new_rd_entity(NULL, firm_unknown_type, new_id_from_str(UNKNOWN_ENTITY_NAME), firm_unknown_type);
1373 set_entity_visibility(unknown_entity, visibility_external_allocated);
1374 set_entity_ld_ident(unknown_entity, get_entity_ident(unknown_entity));
1376 sym.entity_p = unknown_entity;
1377 current_ir_graph = get_const_code_irg();
1378 unknown_entity->value = new_SymConst(sym, symconst_addr_ent);