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 ir_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, ir_type *owner, ident *name, ir_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->attr.mtd_attr.irg_add_properties = mtp_property_inherited;
127 res->attr.mtd_attr.vtable_number = VTABLE_NUM_NOT_SET;
128 res->attr.mtd_attr.param_access = NULL;
129 res->attr.mtd_attr.param_weight = NULL;
130 res->attr.mtd_attr.irg = NULL;
132 else if (is_compound_type(type)) {
133 res->variability = variability_uninitialized;
135 res->attr.cmpd_attr.values = NULL;
136 res->attr.cmpd_attr.val_paths = NULL;
139 res->variability = variability_uninitialized;
143 if (is_Class_type(owner)) {
144 res->overwrites = NEW_ARR_F(entity *, 0);
145 res->overwrittenby = NEW_ARR_F(entity *, 0);
147 res->overwrites = NULL;
148 res->overwrittenby = NULL;
152 res->nr = get_irp_new_node_nr();
153 #endif /* DEBUG_libfirm */
156 set_entity_dbg_info(res, db);
162 new_d_entity (ir_type *owner, ident *name, ir_type *type, dbg_info *db) {
165 assert_legal_owner_of_ent(owner);
166 res = new_rd_entity(db, owner, name, type);
167 /* Remember entity in it's owner. */
168 insert_entity_in_owner (res);
170 hook_new_entity(res);
175 new_entity (ir_type *owner, ident *name, ir_type *type) {
176 return new_d_entity(owner, name, type, NULL);
180 * Free entity attributes.
182 * @param ent the entity
184 static void free_entity_attrs(entity *ent) {
186 if (get_type_tpop(get_entity_owner(ent)) == type_class) {
187 DEL_ARR_F(ent->overwrites); ent->overwrites = NULL;
188 DEL_ARR_F(ent->overwrittenby); ent->overwrittenby = NULL;
190 assert(ent->overwrites == NULL);
191 assert(ent->overwrittenby == NULL);
193 if (is_compound_entity(ent)) {
194 if (ent->attr.cmpd_attr.val_paths) {
195 for (i = 0; i < get_compound_ent_n_values(ent); i++)
196 if (ent->attr.cmpd_attr.val_paths[i]) {
197 /* free_compound_graph_path(ent->attr.cmpd_attr.val_paths[i]) ; * @@@ warum nich? */
198 /* Geht nich: wird mehrfach verwendet!!! ==> mehrfach frei gegeben. */
199 /* DEL_ARR_F(ent->attr.cmpd_attr.val_paths); */
201 ent->attr.cmpd_attr.val_paths = NULL;
203 /* if (ent->attr.cmpd_attr.values) DEL_ARR_F(ent->attr.cmpd_attr.values); *//* @@@ warum nich? */
204 ent->attr.cmpd_attr.values = NULL;
206 else if (is_method_entity(ent)) {
207 if (ent->attr.mtd_attr.param_access) {
208 DEL_ARR_F(ent->attr.mtd_attr.param_access);
209 ent->attr.mtd_attr.param_access = NULL;
211 if (ent->attr.mtd_attr.param_weight) {
212 DEL_ARR_F(ent->attr.mtd_attr.param_weight);
213 ent->attr.mtd_attr.param_weight = NULL;
219 copy_entity_own (entity *old, ir_type *new_owner) {
221 assert(old && old->kind == k_entity);
222 assert_legal_owner_of_ent(new_owner);
224 if (old->owner == new_owner) return old;
225 newe = xmalloc(sizeof(*newe));
226 memcpy (newe, old, sizeof(*newe));
227 newe->owner = new_owner;
228 if (is_Class_type(new_owner)) {
229 newe->overwrites = NEW_ARR_F(entity *, 0);
230 newe->overwrittenby = NEW_ARR_F(entity *, 0);
233 newe->nr = get_irp_new_node_nr();
236 insert_entity_in_owner (newe);
242 copy_entity_name (entity *old, ident *new_name) {
244 assert(old && old->kind == k_entity);
246 if (old->name == new_name) return old;
247 newe = xmalloc(sizeof(*newe));
248 memcpy(newe, old, sizeof(*newe));
249 newe->name = new_name;
250 newe->ld_name = NULL;
251 if (is_Class_type(newe->owner)) {
252 newe->overwrites = DUP_ARR_F(entity *, old->overwrites);
253 newe->overwrittenby = DUP_ARR_F(entity *, old->overwrittenby);
256 newe->nr = get_irp_new_node_nr();
259 insert_entity_in_owner (newe);
266 free_entity (entity *ent) {
267 assert(ent && ent->kind == k_entity);
268 free_entity_attrs(ent);
273 /* Outputs a unique number for this node */
275 get_entity_nr(entity *ent) {
276 assert(ent && ent->kind == k_entity);
280 return (long)PTR_TO_INT(ent);
285 (get_entity_name)(const entity *ent) {
286 return _get_entity_name(ent);
290 (get_entity_ident)(const entity *ent) {
291 return _get_entity_ident(ent);
295 (set_entity_ident)(entity *ent, ident *id) {
296 _set_entity_ident(ent, id);
300 (get_entity_owner)(entity *ent) {
301 return _get_entity_owner(ent);
305 set_entity_owner (entity *ent, ir_type *owner) {
306 assert(ent && ent->kind == k_entity);
307 assert_legal_owner_of_ent(owner);
311 void /* should this go into type.c? */
312 assert_legal_owner_of_ent(ir_type *owner) {
313 assert(get_type_tpop_code(owner) == tpo_class ||
314 get_type_tpop_code(owner) == tpo_union ||
315 get_type_tpop_code(owner) == tpo_struct ||
316 get_type_tpop_code(owner) == tpo_array); /* Yes, array has an entity
317 -- to select fields! */
321 (get_entity_ld_ident)(entity *ent) {
322 return _get_entity_ld_ident(ent);
326 (set_entity_ld_ident)(entity *ent, ident *ld_ident) {
327 _set_entity_ld_ident(ent, ld_ident);
331 (get_entity_ld_name)(entity *ent) {
332 return _get_entity_ld_name(ent);
336 (get_entity_type)(entity *ent) {
337 return _get_entity_type(ent);
341 (set_entity_type)(entity *ent, ir_type *type) {
342 _set_entity_type(ent, type);
346 (get_entity_allocation)(const entity *ent) {
347 return _get_entity_allocation(ent);
351 (set_entity_allocation)(entity *ent, ent_allocation al) {
352 _set_entity_allocation(ent, al);
355 /* return the name of the visibility */
356 const char *get_allocation_name(ent_allocation all)
358 #define X(a) case a: return #a
360 X(allocation_automatic);
361 X(allocation_parameter);
362 X(allocation_dynamic);
363 X(allocation_static);
364 default: return "BAD VALUE";
371 (get_entity_visibility)(const entity *ent) {
372 return _get_entity_visibility(ent);
376 set_entity_visibility (entity *ent, visibility vis) {
377 assert(ent && ent->kind == k_entity);
378 if (vis != visibility_local)
379 assert((ent->allocation == allocation_static) ||
380 (ent->allocation == allocation_automatic));
381 /* @@@ Test that the owner type is not local, but how??
382 && get_class_visibility(get_entity_owner(ent)) != local));*/
383 ent->visibility = vis;
386 /* return the name of the visibility */
387 const char *get_visibility_name(visibility vis)
389 #define X(a) case a: return #a
392 X(visibility_external_visible);
393 X(visibility_external_allocated);
394 default: return "BAD VALUE";
400 (get_entity_variability)(const entity *ent) {
401 return _get_entity_variability(ent);
405 set_entity_variability (entity *ent, ent_variability var)
407 assert(ent && ent->kind == k_entity);
408 if (var == variability_part_constant)
409 assert(is_Class_type(ent->type) || is_Struct_type(ent->type));
411 if ((is_compound_type(ent->type)) &&
412 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
413 /* Allocate data structures for constant values */
414 ent->attr.cmpd_attr.values = NEW_ARR_F(ir_node *, 0);
415 ent->attr.cmpd_attr.val_paths = NEW_ARR_F(compound_graph_path *, 0);
417 if ((is_atomic_type(ent->type)) &&
418 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
419 /* Set default constant value. */
420 ent->value = new_rd_Unknown(get_const_code_irg(), get_type_mode(ent->type));
423 if ((is_compound_type(ent->type)) &&
424 (var == variability_uninitialized) && (ent->variability != variability_uninitialized)) {
425 /* Free data structures for constant values */
426 DEL_ARR_F(ent->attr.cmpd_attr.values); ent->attr.cmpd_attr.values = NULL;
427 DEL_ARR_F(ent->attr.cmpd_attr.val_paths); ent->attr.cmpd_attr.val_paths = NULL;
429 ent->variability = var;
432 /* return the name of the variability */
433 const char *get_variability_name(ent_variability var)
435 #define X(a) case a: return #a
437 X(variability_uninitialized);
438 X(variability_initialized);
439 X(variability_part_constant);
440 X(variability_constant);
441 default: return "BAD VALUE";
447 (get_entity_volatility)(const entity *ent) {
448 return _get_entity_volatility(ent);
452 (set_entity_volatility)(entity *ent, ent_volatility vol) {
453 _set_entity_volatility(ent, vol);
456 /* return the name of the volatility */
457 const char *get_volatility_name(ent_volatility var)
459 #define X(a) case a: return #a
461 X(volatility_non_volatile);
462 X(volatility_is_volatile);
463 default: return "BAD VALUE";
469 (get_entity_peculiarity)(const entity *ent) {
470 return _get_entity_peculiarity(ent);
474 (set_entity_peculiarity)(entity *ent, peculiarity pec) {
475 _set_entity_peculiarity(ent, pec);
478 /* Get the entity's stickyness */
480 (get_entity_stickyness)(const entity *ent) {
481 return _get_entity_stickyness(ent);
484 /* Set the entity's stickyness */
486 (set_entity_stickyness)(entity *ent, ent_stickyness stickyness) {
487 _set_entity_stickyness(ent, stickyness);
490 /* Set has no effect for existent entities of type method. */
492 get_atomic_ent_value(entity *ent)
494 assert(ent && is_atomic_entity(ent));
495 assert(ent->variability != variability_uninitialized);
496 return skip_Id (ent->value);
500 set_atomic_ent_value(entity *ent, ir_node *val) {
501 assert(is_atomic_entity(ent) && (ent->variability != variability_uninitialized));
502 if (is_Method_type(ent->type) && (ent->peculiarity == peculiarity_existent))
507 /* Returns true if the the node is representable as code on
509 int is_irn_const_expression(ir_node *n) {
512 /* we are in danger iff an exception will arise. TODO: be more precisely,
513 * for instance Div. will NOT rise if divisor != 0
515 if (is_binop(n) && !is_fragile_op(n))
516 return is_irn_const_expression(get_binop_left(n)) && is_irn_const_expression(get_binop_right(n));
519 switch(get_irn_opcode(n)) {
526 return is_irn_const_expression(get_irn_n(n, 0));
534 * Copies a firm subgraph that complies to the restrictions for
535 * constant expressions to current_block in current_ir_graph.
537 ir_node *copy_const_value(dbg_info *dbg, ir_node *n) {
541 /* @@@ GL I think we should implement this using the routines from irgopt for
542 dead node elimination/inlineing. */
545 switch (get_irn_opcode(n)) {
547 nn = new_d_Const_type(dbg, m, get_Const_tarval(n), get_Const_type(n));
550 nn = new_d_SymConst_type(dbg, get_SymConst_symbol(n), get_SymConst_kind(n),
551 get_SymConst_value_type(n));
554 nn = new_d_Add(dbg, copy_const_value(dbg, get_Add_left(n)),
555 copy_const_value(dbg, get_Add_right(n)), m); break;
557 nn = new_d_Sub(dbg, copy_const_value(dbg, get_Sub_left(n)),
558 copy_const_value(dbg, get_Sub_right(n)), m); break;
560 nn = new_d_Mul(dbg, copy_const_value(dbg, get_Mul_left(n)),
561 copy_const_value(dbg, get_Mul_right(n)), m); break;
563 nn = new_d_And(dbg, copy_const_value(dbg, get_And_left(n)),
564 copy_const_value(dbg, get_And_right(n)), m); break;
566 nn = new_d_Or(dbg, copy_const_value(dbg, get_Or_left(n)),
567 copy_const_value(dbg, get_Or_right(n)), m); break;
569 nn = new_d_Eor(dbg, copy_const_value(dbg, get_Eor_left(n)),
570 copy_const_value(dbg, get_Eor_right(n)), m); break;
572 nn = new_d_Cast(dbg, copy_const_value(dbg, get_Cast_op(n)), get_Cast_type(n)); break;
574 nn = new_d_Conv(dbg, copy_const_value(dbg, get_Conv_op(n)), m); break;
576 nn = new_d_Unknown(m); break;
579 assert(0 && "opcode invalid or not implemented");
586 /* Creates a new compound graph path. */
587 compound_graph_path *
588 new_compound_graph_path(ir_type *tp, int length) {
589 compound_graph_path *res;
591 assert(is_type(tp) && is_compound_type(tp));
594 res = xmalloc(sizeof(*res) + (length-1) * sizeof(res->list[0]));
595 memset(res, 0, sizeof(*res) + (length-1) * sizeof(res->list[0]));
596 res->kind = k_ir_compound_graph_path;
603 /* Frees an graph path object */
604 void free_compound_graph_path (compound_graph_path *gr) {
605 assert(gr && is_compound_graph_path(gr));
610 /* Returns non-zero if an object is a compound graph path */
611 int is_compound_graph_path(void *thing) {
612 return (get_kind(thing) == k_ir_compound_graph_path);
615 /* Checks whether the path up to pos is correct. If the path contains a NULL,
616 * assumes the path is not complete and returns 'true'. */
617 int is_proper_compound_graph_path(compound_graph_path *gr, int pos) {
620 ir_type *owner = gr->tp;
622 for (i = 0; i <= pos; i++) {
623 node = get_compound_graph_path_node(gr, i);
625 /* Path not yet complete. */
627 if (get_entity_owner(node) != owner)
629 owner = get_entity_type(node);
631 if (pos == get_compound_graph_path_length(gr))
632 if (!is_atomic_type(owner))
637 /* Returns the length of a graph path */
638 int get_compound_graph_path_length(compound_graph_path *gr) {
639 assert(gr && is_compound_graph_path(gr));
644 get_compound_graph_path_node(compound_graph_path *gr, int pos) {
645 assert(gr && is_compound_graph_path(gr));
646 assert(pos >= 0 && pos < gr->len);
647 return gr->list[pos].node;
651 set_compound_graph_path_node(compound_graph_path *gr, int pos, entity *node) {
652 assert(gr && is_compound_graph_path(gr));
653 assert(pos >= 0 && pos < gr->len);
654 assert(is_entity(node));
655 gr->list[pos].node = node;
656 assert(is_proper_compound_graph_path(gr, pos));
660 get_compound_graph_path_array_index(compound_graph_path *gr, int pos) {
661 assert(gr && is_compound_graph_path(gr));
662 assert(pos >= 0 && pos < gr->len);
663 return gr->list[pos].index;
667 set_compound_graph_path_array_index(compound_graph_path *gr, int pos, int index) {
668 assert(gr && is_compound_graph_path(gr));
669 assert(pos >= 0 && pos < gr->len);
670 gr->list[pos].index = index;
673 /* A value of a compound entity is a pair of value and the corresponding path to a member of
676 add_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path) {
677 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
678 ARR_APP1 (ir_node *, ent->attr.cmpd_attr.values, val);
679 ARR_APP1 (compound_graph_path *, ent->attr.cmpd_attr.val_paths, path);
683 set_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path, int pos) {
684 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
685 ent->attr.cmpd_attr.values[pos] = val;
686 ent->attr.cmpd_attr.val_paths[pos] = path;
690 get_compound_ent_n_values(entity *ent) {
691 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
692 return (ARR_LEN (ent->attr.cmpd_attr.values));
696 get_compound_ent_value(entity *ent, int pos) {
697 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
698 return ent->attr.cmpd_attr.values[pos];
701 compound_graph_path *
702 get_compound_ent_value_path(entity *ent, int pos) {
703 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
704 return ent->attr.cmpd_attr.val_paths[pos];
708 * Returns non-zero, if two compound_graph_pathes are equal
710 static int equal_paths(compound_graph_path *path1, int *visited_indicees, compound_graph_path *path2) {
712 int len1 = get_compound_graph_path_length(path1);
713 int len2 = get_compound_graph_path_length(path2);
715 if (len2 > len1) return 0;
717 for (i = 0; i < len1; i++) {
719 entity *node1 = get_compound_graph_path_node(path1, i);
720 entity *node2 = get_compound_graph_path_node(path2, i);
722 if (node1 != node2) return 0;
724 tp = get_entity_owner(node1);
725 if (is_Array_type(tp)) {
728 /* Compute the index of this node. */
729 assert(get_array_n_dimensions(tp) == 1 && "multidim not implemented");
731 low = get_array_lower_bound_int(tp, 0);
732 if (low + visited_indicees[i] < get_compound_graph_path_array_index(path2, i)) {
733 visited_indicees[i]++;
737 assert(low + visited_indicees[i] == get_compound_graph_path_array_index(path2, i));
743 /* Returns the position of a value with the given path.
744 * The path must contain array indicees for all array element entities. */
745 int get_compound_ent_pos_by_path(entity *ent, compound_graph_path *path) {
746 int i, n_paths = get_compound_ent_n_values(ent);
747 int *visited_indicees = (int *)xcalloc(get_compound_graph_path_length(path), sizeof(int));
748 for (i = 0; i < n_paths; i ++) {
749 if (equal_paths(get_compound_ent_value_path(ent, i), visited_indicees, path))
756 printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
757 printf("Entity %s : ", get_entity_name(ent));
758 for (j = 0; j < get_compound_graph_path_length(path); ++j) {
759 entity *node = get_compound_graph_path_node(path, j);
760 printf("%s", get_entity_name(node));
761 if (is_Array_type(get_entity_owner(node)))
762 printf("[%d]", get_compound_graph_path_array_index(path, j));
764 printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
768 assert(0 && "path not found");
772 /* Returns a constant value given the access path.
773 * The path must contain array indicees for all array element entities. */
774 ir_node *get_compound_ent_value_by_path(entity *ent, compound_graph_path *path) {
775 return get_compound_ent_value(ent, get_compound_ent_pos_by_path(ent, path));
780 remove_compound_ent_value(entity *ent, entity *value_ent) {
782 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
783 for (i = 0; i < (ARR_LEN (ent->attr.cmpd_attr.val_paths)); i++) {
784 compound_graph_path *path = ent->attr.cmpd_attr.val_paths[i];
785 if (path->list[path->len-1].node == value_ent) {
786 for(; i < (ARR_LEN (ent->attr.cmpd_attr.val_paths))-1; i++) {
787 ent->attr.cmpd_attr.val_paths[i] = ent->attr.cmpd_attr.val_paths[i+1];
788 ent->attr.cmpd_attr.values[i] = ent->attr.cmpd_attr.values[i+1];
790 ARR_SETLEN(entity*, ent->attr.cmpd_attr.val_paths, ARR_LEN(ent->attr.cmpd_attr.val_paths) - 1);
791 ARR_SETLEN(ir_node*, ent->attr.cmpd_attr.values, ARR_LEN(ent->attr.cmpd_attr.values) - 1);
798 add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
799 compound_graph_path *path;
800 ir_type *owner_tp = get_entity_owner(member);
801 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
802 path = new_compound_graph_path(get_entity_type(ent), 1);
803 path->list[0].node = member;
804 if (is_Array_type(owner_tp)) {
808 assert(get_array_n_dimensions(owner_tp) == 1 && has_array_lower_bound(owner_tp, 0));
809 max = get_array_lower_bound_int(owner_tp, 0) -1;
810 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
811 int index = get_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0);
816 path->list[0].index = max + 1;
818 add_compound_ent_value_w_path(ent, val, path);
821 /* Copies the firm subgraph referenced by val to const_code_irg and adds
822 the node as constant initialization to ent.
823 The subgraph may not contain control flow operations.
825 copy_and_add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
826 ir_graph *rem = current_ir_graph;
828 assert(get_entity_variability(ent) != variability_uninitialized);
829 current_ir_graph = get_const_code_irg();
831 val = copy_const_value(val);
832 add_compound_ent_value(ent, val, member);
833 current_ir_graph = rem;
836 /* Copies the value i of the entity to current_block in current_ir_graph.
838 copy_compound_ent_value(entity *ent, int pos) {
839 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
840 return copy_const_value(ent->values[pos+1]);
844 get_compound_ent_value_member(entity *ent, int pos) {
845 compound_graph_path *path;
846 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
847 path = get_compound_ent_value_path(ent, pos);
849 return get_compound_graph_path_node(path, get_compound_graph_path_length(path)-1);
853 set_compound_ent_value(entity *ent, ir_node *val, entity *member, int pos) {
854 compound_graph_path *path;
855 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
856 path = get_compound_ent_value_path(ent, pos);
857 set_compound_graph_path_node(path, 0, member);
858 set_compound_ent_value_w_path(ent, val, path, pos);
862 set_array_entity_values(entity *ent, tarval **values, int num_vals) {
864 ir_graph *rem = current_ir_graph;
865 ir_type *arrtp = get_entity_type(ent);
867 ir_type *elttp = get_array_element_type(arrtp);
869 assert(is_Array_type(arrtp));
870 assert(get_array_n_dimensions(arrtp) == 1);
871 /* One bound is sufficient, the number of constant fields makes the
873 assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
874 assert(get_entity_variability(ent) != variability_uninitialized);
875 current_ir_graph = get_const_code_irg();
877 for (i = 0; i < num_vals; i++) {
878 val = new_Const_type(values[i], elttp);
879 add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
880 set_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0, i);
882 current_ir_graph = rem;
885 int get_compound_ent_value_offset_bits(entity *ent, int pos) {
886 compound_graph_path *path;
890 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
892 path = get_compound_ent_value_path(ent, pos);
893 path_len = get_compound_graph_path_length(path);
895 for (i = 0; i < path_len; ++i) {
896 entity *node = get_compound_graph_path_node(path, i);
897 ir_type *node_tp = get_entity_type(node);
898 ir_type *owner_tp = get_entity_owner(node);
899 if (is_Array_type(owner_tp)) {
900 int size = get_type_size_bits(node_tp);
901 int align = get_type_alignment_bits(node_tp);
905 assert(size % align == 0);
906 /* ansonsten aufrunden */
908 offset += size * get_compound_graph_path_array_index(path, i);
910 offset += get_entity_offset_bits(node);
916 int get_compound_ent_value_offset_bytes(entity *ent, int pos) {
917 int offset = get_compound_ent_value_offset_bits(ent, pos);
918 assert(offset % 8 == 0);
923 static void init_index(ir_type *arr) {
927 assert(get_array_n_dimensions(arr) == 1);
929 if (has_array_lower_bound(arr, dim))
930 init = get_array_lower_bound_int(arr, 0) -1;
932 init = get_array_upper_bound_int(arr, 0) +1;
934 set_entity_link(get_array_element_entity(arr), INT_TO_PTR(init));
938 static int get_next_index(entity *elem_ent) {
939 ir_type *arr = get_entity_owner(elem_ent);
943 assert(get_array_n_dimensions(arr) == 1);
945 if (has_array_lower_bound(arr, dim)) {
946 next = PTR_TO_INT(get_entity_link(elem_ent)) + 1;
947 if (has_array_upper_bound(arr, dim)) {
948 int upper = get_array_upper_bound_int(arr, dim);
949 if (next == upper) next = get_array_lower_bound_int(arr, dim);
952 next = PTR_TO_INT(get_entity_link(elem_ent)) - 1;
953 if (has_array_lower_bound(arr, dim)) {
954 int upper = get_array_upper_bound_int(arr, dim);
955 if (next == upper) next = get_array_upper_bound_int(arr, dim);
959 set_entity_link(elem_ent, INT_TO_PTR(next));
963 /* Compute the array indices in compound graph paths of initialized entities.
965 * All arrays must have fixed lower and upper bounds. One array can
966 * have an open bound. If there are several open bounds, we do
967 * nothing. There must be initializer elements for all array
968 * elements. Uses the link field in the array element entities. The
969 * array bounds must be representable as ints.
971 * (If the bounds are not representable as ints we have to represent
972 * the indices as firm nodes. But still we must be able to
973 * evaluate the index against the upper bound.)
975 void compute_compound_ent_array_indicees(entity *ent) {
976 ir_type *tp = get_entity_type(ent);
978 entity *unknown_bound_entity = NULL;
980 if (!is_compound_type(tp) ||
981 (ent->variability == variability_uninitialized)) return ;
983 n_vals = get_compound_ent_n_values(ent);
984 if (n_vals == 0) return;
986 /* We can not compute the indexes if there is more than one array
987 with an unknown bound. For this remember the first entity that
988 represents such an array. It could be ent. */
989 if (is_Array_type(tp)) {
992 assert(get_array_n_dimensions(tp) == 1 && "other not implemented");
993 if (!has_array_lower_bound(tp, dim) || !has_array_upper_bound(tp, dim))
994 unknown_bound_entity = ent;
997 /* Initialize the entity links to lower bound -1 and test all path elements
999 for (i = 0; i < n_vals; ++i) {
1000 compound_graph_path *path = get_compound_ent_value_path(ent, i);
1001 int j, path_len = get_compound_graph_path_length(path);
1002 for (j = 0; j < path_len; ++j) {
1003 entity *node = get_compound_graph_path_node(path, j);
1004 ir_type *elem_tp = get_entity_type(node);
1006 if (is_Array_type(elem_tp)) {
1008 assert(get_array_n_dimensions(elem_tp) == 1 && "other not implemented");
1009 if (!has_array_lower_bound(elem_tp, dim) || !has_array_upper_bound(elem_tp, dim)) {
1010 if (!unknown_bound_entity) unknown_bound_entity = node;
1011 if (node != unknown_bound_entity) return;
1014 init_index(elem_tp);
1019 /* Finally compute the indexes ... */
1020 for (i = 0; i < n_vals; ++i) {
1021 compound_graph_path *path = get_compound_ent_value_path(ent, i);
1022 int j, path_len = get_compound_graph_path_length(path);
1023 for (j = 0; j < path_len; ++j) {
1024 entity *node = get_compound_graph_path_node(path, j);
1025 ir_type *owner_tp = get_entity_owner(node);
1026 if (is_Array_type(owner_tp))
1027 set_compound_graph_path_array_index (path, j, get_next_index(node));
1032 /** resize: double the allocated buffer */
1033 static int *resize (int *buf, int *size) {
1034 int new_size = *size * 2;
1035 int *new_buf = xcalloc(new_size, sizeof(new_buf[0]));
1036 memcpy(new_buf, buf, *size);
1042 /* We sort the elements by placing them at their bit offset in an
1043 array where each entry represents one bit called permutation. In
1044 fact, we do not place the values themselves, as we would have to
1045 copy two things, the value and the path. We only remember the
1046 position in the old order. Each value should have a distinct
1047 position in the permutation.
1049 A second iteration now permutes the actual elements into two
1051 void sort_compound_ent_values(entity *ent) {
1059 ir_node **my_values;
1060 compound_graph_path **my_paths;
1062 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
1064 tp = get_entity_type(ent);
1065 n_vals = get_compound_ent_n_values(ent);
1066 tp_size = get_type_size_bits(tp);
1068 if (!is_compound_type(tp) ||
1069 (ent->variability == variability_uninitialized) ||
1070 (get_type_state(tp) != layout_fixed) ||
1071 (n_vals == 0) ) return;
1073 /* estimated upper bound for size. Better: use flexible array ... */
1074 size = ((tp_size > (n_vals * 32)) ? tp_size : (n_vals * 32)) * 4;
1075 permutation = xcalloc(size, sizeof(permutation[0]));
1077 for (i = 0; i < n_vals; ++i) {
1078 int pos = get_compound_ent_value_offset_bits(ent, i);
1079 while (pos >= size) {
1080 permutation = resize(permutation, &size);
1083 assert(permutation[pos] == 0 && "two values with the same offset");
1084 permutation[pos] = i + 1; /* We initialized with 0, so we can not distinguish entry 0.
1085 So inc all entries by one. */
1086 //fprintf(stderr, "i: %d, pos: %d \n", i, pos);
1090 my_values = NEW_ARR_F(ir_node *, n_vals);
1091 my_paths = NEW_ARR_F(compound_graph_path *, n_vals);
1092 for (i = 0; i < size; ++i) {
1093 int pos = permutation[i];
1095 //fprintf(stderr, "pos: %d i: %d next %d \n", i, pos, next);
1096 assert(next < n_vals);
1097 pos--; /* We increased the pos by one */
1098 my_values[next] = get_compound_ent_value (ent, pos);
1099 my_paths [next] = get_compound_ent_value_path(ent, pos);
1105 DEL_ARR_F(ent->attr.cmpd_attr.values);
1106 ent->attr.cmpd_attr.values = my_values;
1107 DEL_ARR_F(ent->attr.cmpd_attr.val_paths);
1108 ent->attr.cmpd_attr.val_paths = my_paths;
1112 (get_entity_offset_bytes)(const entity *ent) {
1113 return _get_entity_offset_bytes(ent);
1117 (get_entity_offset_bits)(const entity *ent) {
1118 return _get_entity_offset_bits(ent);
1122 (set_entity_offset_bytes)(entity *ent, int offset) {
1123 _set_entity_offset_bytes(ent, offset);
1127 (set_entity_offset_bits)(entity *ent, int offset) {
1128 _set_entity_offset_bits(ent, offset);
1132 add_entity_overwrites(entity *ent, entity *overwritten) {
1133 assert(ent && is_Class_type(get_entity_owner(ent)));
1134 ARR_APP1(entity *, ent->overwrites, overwritten);
1135 ARR_APP1(entity *, overwritten->overwrittenby, ent);
1139 get_entity_n_overwrites(entity *ent) {
1140 assert(ent && is_Class_type(get_entity_owner(ent)));
1141 return (ARR_LEN(ent->overwrites));
1145 get_entity_overwrites_index(entity *ent, entity *overwritten) {
1147 assert(ent && is_Class_type(get_entity_owner(ent)));
1148 for (i = 0; i < get_entity_n_overwrites(ent); i++)
1149 if (get_entity_overwrites(ent, i) == overwritten)
1155 get_entity_overwrites (entity *ent, int pos) {
1156 assert(ent && is_Class_type(get_entity_owner(ent)));
1157 assert(pos < get_entity_n_overwrites(ent));
1158 return ent->overwrites[pos];
1162 set_entity_overwrites (entity *ent, int pos, entity *overwritten) {
1163 assert(ent && is_Class_type(get_entity_owner(ent)));
1164 assert(pos < get_entity_n_overwrites(ent));
1165 ent->overwrites[pos] = overwritten;
1169 remove_entity_overwrites(entity *ent, entity *overwritten) {
1171 assert(ent && is_Class_type(get_entity_owner(ent)));
1172 for (i = 0; i < (ARR_LEN (ent->overwrites)); i++)
1173 if (ent->overwrites[i] == overwritten) {
1174 for(; i < (ARR_LEN (ent->overwrites))-1; i++)
1175 ent->overwrites[i] = ent->overwrites[i+1];
1176 ARR_SETLEN(entity*, ent->overwrites, ARR_LEN(ent->overwrites) - 1);
1182 add_entity_overwrittenby (entity *ent, entity *overwrites) {
1183 assert(ent && is_Class_type(get_entity_owner(ent)));
1184 add_entity_overwrites(overwrites, ent);
1188 get_entity_n_overwrittenby (entity *ent) {
1189 assert(ent && is_Class_type(get_entity_owner(ent)));
1190 return (ARR_LEN (ent->overwrittenby));
1194 get_entity_overwrittenby_index(entity *ent, entity *overwrites) {
1196 assert(ent && is_Class_type(get_entity_owner(ent)));
1197 for (i = 0; i < get_entity_n_overwrittenby(ent); i++)
1198 if (get_entity_overwrittenby(ent, i) == overwrites)
1204 get_entity_overwrittenby (entity *ent, int pos) {
1205 assert(ent && is_Class_type(get_entity_owner(ent)));
1206 assert(pos < get_entity_n_overwrittenby(ent));
1207 return ent->overwrittenby[pos];
1211 set_entity_overwrittenby (entity *ent, int pos, entity *overwrites) {
1212 assert(ent && is_Class_type(get_entity_owner(ent)));
1213 assert(pos < get_entity_n_overwrittenby(ent));
1214 ent->overwrittenby[pos] = overwrites;
1217 void remove_entity_overwrittenby(entity *ent, entity *overwrites) {
1219 assert(ent && is_Class_type(get_entity_owner(ent)));
1220 for (i = 0; i < (ARR_LEN (ent->overwrittenby)); i++)
1221 if (ent->overwrittenby[i] == overwrites) {
1222 for(; i < (ARR_LEN (ent->overwrittenby))-1; i++)
1223 ent->overwrittenby[i] = ent->overwrittenby[i+1];
1224 ARR_SETLEN(entity*, ent->overwrittenby, ARR_LEN(ent->overwrittenby) - 1);
1229 /* A link to store intermediate information */
1231 (get_entity_link)(const entity *ent) {
1232 return _get_entity_link(ent);
1236 (set_entity_link)(entity *ent, void *l) {
1237 _set_entity_link(ent, l);
1241 (get_entity_irg)(const entity *ent) {
1242 return _get_entity_irg(ent);
1246 set_entity_irg(entity *ent, ir_graph *irg) {
1247 assert(ent && is_method_entity(ent));
1248 /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
1249 * Methode selbst nicht mehr aufgerufen werden kann, die Entität
1250 * aber erhalten bleiben soll? Wandle die Entitaet in description oder
1253 assert((irg && ent->peculiarity == peculiarity_existent) ||
1254 (!irg && (ent->peculiarity == peculiarity_existent)
1255 && (ent -> visibility == visibility_external_allocated)) ||
1256 (!irg && ent->peculiarity == peculiarity_description) ||
1257 (!irg && ent->peculiarity == peculiarity_inherited));
1258 ent->attr.mtd_attr.irg = irg;
1261 unsigned get_entity_vtable_number(entity *ent) {
1262 assert(ent && is_method_entity(ent));
1263 return ent->attr.mtd_attr.vtable_number;
1266 void set_entity_vtable_number(entity *ent, unsigned vtable_number) {
1267 assert(ent && is_method_entity(ent));
1268 ent->attr.mtd_attr.vtable_number = vtable_number;
1272 (is_entity)(const void *thing) {
1273 return _is_entity(thing);
1276 int is_atomic_entity(entity *ent) {
1277 ir_type *t = get_entity_type(ent);
1278 assert(ent && ent->kind == k_entity);
1279 return (is_Primitive_type(t) || is_Pointer_type(t) ||
1280 is_Enumeration_type(t) || is_Method_type(t));
1283 int is_compound_entity(entity *ent) {
1284 ir_type *t = get_entity_type(ent);
1285 assert(ent && ent->kind == k_entity);
1286 return (is_Class_type(t) || is_Struct_type(t) ||
1287 is_Array_type(t) || is_Union_type(t));
1290 int is_method_entity(entity *ent) {
1291 ir_type *t = get_entity_type(ent);
1292 assert(ent && ent->kind == k_entity);
1293 return (is_Method_type(t));
1297 * @todo not implemented!!! */
1298 int equal_entity(entity *ent1, entity *ent2) {
1299 fprintf(stderr, " calling unimplemented equal entity!!! \n");
1304 unsigned long (get_entity_visited)(entity *ent) {
1305 return _get_entity_visited(ent);
1308 void (set_entity_visited)(entity *ent, unsigned long num) {
1309 _set_entity_visited(ent, num);
1312 /* Sets visited field in entity to entity_visited. */
1313 void (mark_entity_visited)(entity *ent) {
1314 _mark_entity_visited(ent);
1317 int (entity_visited)(entity *ent) {
1318 return _entity_visited(ent);
1321 int (entity_not_visited)(entity *ent) {
1322 return _entity_not_visited(ent);
1325 /* Returns the mask of the additional entity properties. */
1326 unsigned get_entity_additional_properties(entity *ent) {
1329 assert(is_method_entity(ent));
1331 /* first check, if the graph has additional properties */
1332 irg = get_entity_irg(ent);
1335 return get_irg_additional_properties(irg);
1337 if (ent->attr.mtd_attr.irg_add_properties & mtp_property_inherited)
1338 return get_method_additional_properties(get_entity_type(ent));
1340 return ent->attr.mtd_attr.irg_add_properties;
1343 /* Sets the mask of the additional graph properties. */
1344 void set_entity_additional_properties(entity *ent, unsigned property_mask)
1348 assert(is_method_entity(ent));
1350 /* first check, if the graph exists */
1351 irg = get_entity_irg(ent);
1353 set_irg_additional_properties(irg, property_mask);
1355 /* do not allow to set the mtp_property_inherited flag or
1356 * the automatic inheritance of flags will not work */
1357 ent->attr.mtd_attr.irg_add_properties = property_mask & ~mtp_property_inherited;
1361 /* Sets one additional graph property. */
1362 void set_entity_additional_property(entity *ent, mtp_additional_property flag)
1366 assert(is_method_entity(ent));
1368 /* first check, if the graph exists */
1369 irg = get_entity_irg(ent);
1371 set_irg_additional_property(irg, flag);
1373 unsigned mask = ent->attr.mtd_attr.irg_add_properties;
1375 if (mask & mtp_property_inherited)
1376 mask = get_method_additional_properties(get_entity_type(ent));
1378 /* do not allow to set the mtp_property_inherited flag or
1379 * the automatic inheritance of flags will not work */
1380 ent->attr.mtd_attr.irg_add_properties = mask | (flag & ~mtp_property_inherited);
1384 /* Initialize entity module. */
1385 void firm_init_entity(void)
1387 symconst_symbol sym;
1389 assert(firm_unknown_type && "Call init_type() before firm_init_entity()!");
1390 assert(!unknown_entity && "Call firm_init_entity() only once!");
1392 unknown_entity = new_rd_entity(NULL, firm_unknown_type, new_id_from_str(UNKNOWN_ENTITY_NAME), firm_unknown_type);
1393 set_entity_visibility(unknown_entity, visibility_external_allocated);
1394 set_entity_ld_ident(unknown_entity, get_entity_ident(unknown_entity));
1396 current_ir_graph = get_const_code_irg();
1397 sym.entity_p = unknown_entity;
1398 unknown_entity->value = new_SymConst(sym, symconst_addr_ent);