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, Michael Beck
9 * Copyright: (c) 1998-2006 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
32 #include "firm_common_t.h"
43 /* All this is needed to build the constant node for methods: */
47 #include "irdump.h" /* for output if errors occur. */
49 #include "callgraph.h" /* for dumping debug output */
51 /*******************************************************************/
53 /*******************************************************************/
55 entity *unknown_entity = NULL;
57 entity *get_unknown_entity(void) { return unknown_entity; }
59 #define UNKNOWN_ENTITY_NAME "unknown_entity"
61 /*-----------------------------------------------------------------*/
63 /*-----------------------------------------------------------------*/
65 static INLINE void insert_entity_in_owner(entity *ent) {
66 ir_type *owner = ent->owner;
67 switch (get_type_tpop_code(owner)) {
69 add_class_member (owner, ent);
72 add_struct_member (owner, ent);
75 add_union_member (owner, ent);
78 set_array_element_entity(owner, ent);
85 * Creates a new entity. This entity is NOT inserted in the owner type.
87 * @param db debug info for this entity
88 * @param owner the owner type of the new entity
89 * @param name the name of the new entity
90 * @param type the type of the new entity
92 * @return the new created entity
94 static INLINE entity *
95 new_rd_entity(dbg_info *db, ir_type *owner, ident *name, ir_type *type)
100 assert(!id_contains_char(name, ' ') && "entity name should not contain spaces");
102 res = xmalloc(sizeof(*res));
103 memset(res, 0, sizeof(*res));
105 res->kind = k_entity;
111 res->allocation = allocation_automatic;
112 res->visibility = visibility_local;
113 res->volatility = volatility_non_volatile;
114 res->stickyness = stickyness_unsticky;
115 res->peculiarity = peculiarity_existent;
116 res->compiler_gen = 0;
120 if (is_Method_type(type)) {
123 rem = current_ir_graph;
124 current_ir_graph = get_const_code_irg();
125 res->value = new_SymConst(sym, symconst_addr_ent);
126 current_ir_graph = rem;
127 res->allocation = allocation_static;
128 res->variability = variability_constant;
129 res->attr.mtd_attr.irg_add_properties = mtp_property_inherited;
130 res->attr.mtd_attr.vtable_number = VTABLE_NUM_NOT_SET;
131 res->attr.mtd_attr.param_access = NULL;
132 res->attr.mtd_attr.param_weight = NULL;
133 res->attr.mtd_attr.irg = NULL;
134 res->attr.mtd_attr.section = section_text;
136 else if (is_compound_type(type)) {
137 res->variability = variability_uninitialized;
139 res->attr.cmpd_attr.values = NULL;
140 res->attr.cmpd_attr.val_paths = NULL;
143 res->variability = variability_uninitialized;
147 if (is_Class_type(owner)) {
148 res->overwrites = NEW_ARR_F(entity *, 0);
149 res->overwrittenby = NEW_ARR_F(entity *, 0);
151 res->overwrites = NULL;
152 res->overwrittenby = NULL;
156 res->nr = get_irp_new_node_nr();
157 #endif /* DEBUG_libfirm */
160 set_entity_dbg_info(res, db);
166 new_d_entity (ir_type *owner, ident *name, ir_type *type, dbg_info *db) {
169 assert_legal_owner_of_ent(owner);
170 res = new_rd_entity(db, owner, name, type);
171 /* Remember entity in it's owner. */
172 insert_entity_in_owner (res);
174 hook_new_entity(res);
179 new_entity (ir_type *owner, ident *name, ir_type *type) {
180 return new_d_entity(owner, name, type, NULL);
184 * Free entity attributes.
186 * @param ent the entity
188 static void free_entity_attrs(entity *ent) {
190 if (get_type_tpop(get_entity_owner(ent)) == type_class) {
191 DEL_ARR_F(ent->overwrites); ent->overwrites = NULL;
192 DEL_ARR_F(ent->overwrittenby); ent->overwrittenby = NULL;
194 assert(ent->overwrites == NULL);
195 assert(ent->overwrittenby == NULL);
197 if (is_compound_entity(ent)) {
198 if (ent->attr.cmpd_attr.val_paths) {
199 for (i = 0; i < get_compound_ent_n_values(ent); i++)
200 if (ent->attr.cmpd_attr.val_paths[i]) {
201 /* free_compound_graph_path(ent->attr.cmpd_attr.val_paths[i]) ; * @@@ warum nich? */
202 /* Geht nich: wird mehrfach verwendet!!! ==> mehrfach frei gegeben. */
203 /* DEL_ARR_F(ent->attr.cmpd_attr.val_paths); */
205 ent->attr.cmpd_attr.val_paths = NULL;
207 /* if (ent->attr.cmpd_attr.values) DEL_ARR_F(ent->attr.cmpd_attr.values); *//* @@@ warum nich? */
208 ent->attr.cmpd_attr.values = NULL;
210 else if (is_method_entity(ent)) {
211 if (ent->attr.mtd_attr.param_access) {
212 DEL_ARR_F(ent->attr.mtd_attr.param_access);
213 ent->attr.mtd_attr.param_access = NULL;
215 if (ent->attr.mtd_attr.param_weight) {
216 DEL_ARR_F(ent->attr.mtd_attr.param_weight);
217 ent->attr.mtd_attr.param_weight = NULL;
223 copy_entity_own (entity *old, ir_type *new_owner) {
225 assert(old && old->kind == k_entity);
226 assert_legal_owner_of_ent(new_owner);
228 if (old->owner == new_owner) return old;
229 newe = xmalloc(sizeof(*newe));
230 memcpy (newe, old, sizeof(*newe));
231 newe->owner = new_owner;
232 if (is_Class_type(new_owner)) {
233 newe->overwrites = NEW_ARR_F(entity *, 0);
234 newe->overwrittenby = NEW_ARR_F(entity *, 0);
237 newe->nr = get_irp_new_node_nr();
240 insert_entity_in_owner (newe);
246 copy_entity_name (entity *old, ident *new_name) {
248 assert(old && old->kind == k_entity);
250 if (old->name == new_name) return old;
251 newe = xmalloc(sizeof(*newe));
252 memcpy(newe, old, sizeof(*newe));
253 newe->name = new_name;
254 newe->ld_name = NULL;
255 if (is_Class_type(newe->owner)) {
256 newe->overwrites = DUP_ARR_F(entity *, old->overwrites);
257 newe->overwrittenby = DUP_ARR_F(entity *, old->overwrittenby);
260 newe->nr = get_irp_new_node_nr();
263 insert_entity_in_owner (newe);
270 free_entity (entity *ent) {
271 assert(ent && ent->kind == k_entity);
272 free_entity_attrs(ent);
277 /* Outputs a unique number for this node */
279 get_entity_nr(entity *ent) {
280 assert(ent && ent->kind == k_entity);
284 return (long)PTR_TO_INT(ent);
289 (get_entity_name)(const entity *ent) {
290 return _get_entity_name(ent);
294 (get_entity_ident)(const entity *ent) {
295 return _get_entity_ident(ent);
299 (set_entity_ident)(entity *ent, ident *id) {
300 _set_entity_ident(ent, id);
304 (get_entity_owner)(entity *ent) {
305 return _get_entity_owner(ent);
309 set_entity_owner (entity *ent, ir_type *owner) {
310 assert(ent && ent->kind == k_entity);
311 assert_legal_owner_of_ent(owner);
315 void /* should this go into type.c? */
316 assert_legal_owner_of_ent(ir_type *owner) {
317 assert(get_type_tpop_code(owner) == tpo_class ||
318 get_type_tpop_code(owner) == tpo_union ||
319 get_type_tpop_code(owner) == tpo_struct ||
320 get_type_tpop_code(owner) == tpo_array); /* Yes, array has an entity
321 -- to select fields! */
325 (get_entity_ld_ident)(entity *ent) {
326 return _get_entity_ld_ident(ent);
330 (set_entity_ld_ident)(entity *ent, ident *ld_ident) {
331 _set_entity_ld_ident(ent, ld_ident);
335 (get_entity_ld_name)(entity *ent) {
336 return _get_entity_ld_name(ent);
340 (get_entity_type)(entity *ent) {
341 return _get_entity_type(ent);
345 (set_entity_type)(entity *ent, ir_type *type) {
346 _set_entity_type(ent, type);
350 (get_entity_allocation)(const entity *ent) {
351 return _get_entity_allocation(ent);
355 (set_entity_allocation)(entity *ent, ir_allocation al) {
356 _set_entity_allocation(ent, al);
359 /* return the name of the visibility */
360 const char *get_allocation_name(ir_allocation all)
362 #define X(a) case a: return #a
364 X(allocation_automatic);
365 X(allocation_parameter);
366 X(allocation_dynamic);
367 X(allocation_static);
368 default: return "BAD VALUE";
375 (get_entity_visibility)(const entity *ent) {
376 return _get_entity_visibility(ent);
380 set_entity_visibility(entity *ent, ir_visibility vis) {
381 assert(ent && ent->kind == k_entity);
382 if (vis != visibility_local)
383 assert((ent->allocation == allocation_static) ||
384 (ent->allocation == allocation_automatic));
385 /* @@@ Test that the owner type is not local, but how??
386 && get_class_visibility(get_entity_owner(ent)) != local));*/
387 ent->visibility = vis;
390 /* return the name of the visibility */
391 const char *get_visibility_name(ir_visibility vis)
393 #define X(a) case a: return #a
396 X(visibility_external_visible);
397 X(visibility_external_allocated);
398 default: return "BAD VALUE";
404 (get_entity_variability)(const entity *ent) {
405 return _get_entity_variability(ent);
409 set_entity_variability (entity *ent, ir_variability var)
411 assert(ent && ent->kind == k_entity);
412 if (var == variability_part_constant)
413 assert(is_Class_type(ent->type) || is_Struct_type(ent->type));
415 if ((is_compound_type(ent->type)) &&
416 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
417 /* Allocate data structures for constant values */
418 ent->attr.cmpd_attr.values = NEW_ARR_F(ir_node *, 0);
419 ent->attr.cmpd_attr.val_paths = NEW_ARR_F(compound_graph_path *, 0);
421 if ((is_atomic_type(ent->type)) &&
422 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
423 /* Set default constant value. */
424 ent->value = new_rd_Unknown(get_const_code_irg(), get_type_mode(ent->type));
427 if ((is_compound_type(ent->type)) &&
428 (var == variability_uninitialized) && (ent->variability != variability_uninitialized)) {
429 /* Free data structures for constant values */
430 DEL_ARR_F(ent->attr.cmpd_attr.values); ent->attr.cmpd_attr.values = NULL;
431 DEL_ARR_F(ent->attr.cmpd_attr.val_paths); ent->attr.cmpd_attr.val_paths = NULL;
433 ent->variability = var;
436 /* return the name of the variability */
437 const char *get_variability_name(ir_variability var)
439 #define X(a) case a: return #a
441 X(variability_uninitialized);
442 X(variability_initialized);
443 X(variability_part_constant);
444 X(variability_constant);
445 default: return "BAD VALUE";
451 (get_entity_volatility)(const entity *ent) {
452 return _get_entity_volatility(ent);
456 (set_entity_volatility)(entity *ent, ir_volatility vol) {
457 _set_entity_volatility(ent, vol);
460 /* return the name of the volatility */
461 const char *get_volatility_name(ir_volatility var)
463 #define X(a) case a: return #a
465 X(volatility_non_volatile);
466 X(volatility_is_volatile);
467 default: return "BAD VALUE";
473 (get_entity_peculiarity)(const entity *ent) {
474 return _get_entity_peculiarity(ent);
478 (set_entity_peculiarity)(entity *ent, ir_peculiarity pec) {
479 _set_entity_peculiarity(ent, pec);
482 /* Checks if an entity is compiler generated */
483 int is_entity_compiler_generated(const entity *ent) {
484 assert(is_entity(ent));
485 return ent->compiler_gen;
488 /* Sets/resets the compiler generated flag */
489 void set_entity_compiler_generated(entity *ent, int flag) {
490 assert(is_entity(ent));
491 ent->compiler_gen = flag ? 1 : 0;
494 /* Get the entity's stickyness */
496 (get_entity_stickyness)(const entity *ent) {
497 return _get_entity_stickyness(ent);
500 /* Set the entity's stickyness */
502 (set_entity_stickyness)(entity *ent, ir_stickyness stickyness) {
503 _set_entity_stickyness(ent, stickyness);
506 /* Set has no effect for existent entities of type method. */
508 get_atomic_ent_value(entity *ent)
510 assert(ent && is_atomic_entity(ent));
511 assert(ent->variability != variability_uninitialized);
512 return skip_Id (ent->value);
516 set_atomic_ent_value(entity *ent, ir_node *val) {
517 assert(is_atomic_entity(ent) && (ent->variability != variability_uninitialized));
518 if (is_Method_type(ent->type) && (ent->peculiarity == peculiarity_existent))
523 /* Returns true if the the node is representable as code on
525 int is_irn_const_expression(ir_node *n) {
528 /* we are in danger iff an exception will arise. TODO: be more precisely,
529 * for instance Div. will NOT rise if divisor != 0
531 if (is_binop(n) && !is_fragile_op(n))
532 return is_irn_const_expression(get_binop_left(n)) && is_irn_const_expression(get_binop_right(n));
535 switch(get_irn_opcode(n)) {
542 return is_irn_const_expression(get_irn_n(n, 0));
550 * Copies a firm subgraph that complies to the restrictions for
551 * constant expressions to current_block in current_ir_graph.
553 ir_node *copy_const_value(dbg_info *dbg, ir_node *n) {
557 /* @@@ GL I think we should implement this using the routines from irgopt for
558 dead node elimination/inlineing. */
561 switch (get_irn_opcode(n)) {
563 nn = new_d_Const_type(dbg, m, get_Const_tarval(n), get_Const_type(n));
566 nn = new_d_SymConst_type(dbg, get_SymConst_symbol(n), get_SymConst_kind(n),
567 get_SymConst_value_type(n));
570 nn = new_d_Add(dbg, copy_const_value(dbg, get_Add_left(n)),
571 copy_const_value(dbg, get_Add_right(n)), m); break;
573 nn = new_d_Sub(dbg, copy_const_value(dbg, get_Sub_left(n)),
574 copy_const_value(dbg, get_Sub_right(n)), m); break;
576 nn = new_d_Mul(dbg, copy_const_value(dbg, get_Mul_left(n)),
577 copy_const_value(dbg, get_Mul_right(n)), m); break;
579 nn = new_d_And(dbg, copy_const_value(dbg, get_And_left(n)),
580 copy_const_value(dbg, get_And_right(n)), m); break;
582 nn = new_d_Or(dbg, copy_const_value(dbg, get_Or_left(n)),
583 copy_const_value(dbg, get_Or_right(n)), m); break;
585 nn = new_d_Eor(dbg, copy_const_value(dbg, get_Eor_left(n)),
586 copy_const_value(dbg, get_Eor_right(n)), m); break;
588 nn = new_d_Cast(dbg, copy_const_value(dbg, get_Cast_op(n)), get_Cast_type(n)); break;
590 nn = new_d_Conv(dbg, copy_const_value(dbg, get_Conv_op(n)), m); break;
592 nn = new_d_Unknown(m); break;
595 assert(0 && "opcode invalid or not implemented");
602 /* Creates a new compound graph path. */
603 compound_graph_path *
604 new_compound_graph_path(ir_type *tp, int length) {
605 compound_graph_path *res;
607 assert(is_compound_type(tp));
610 res = xmalloc(sizeof(*res) + (length-1) * sizeof(res->list[0]));
611 memset(res, 0, sizeof(*res) + (length-1) * sizeof(res->list[0]));
612 res->kind = k_ir_compound_graph_path;
619 /* Frees an graph path object */
620 void free_compound_graph_path (compound_graph_path *gr) {
621 assert(gr && is_compound_graph_path(gr));
626 /* Returns non-zero if an object is a compound graph path */
627 int is_compound_graph_path(void *thing) {
628 return (get_kind(thing) == k_ir_compound_graph_path);
631 /* Checks whether the path up to pos is correct. If the path contains a NULL,
632 * assumes the path is not complete and returns 'true'. */
633 int is_proper_compound_graph_path(compound_graph_path *gr, int pos) {
636 ir_type *owner = gr->tp;
638 for (i = 0; i <= pos; i++) {
639 node = get_compound_graph_path_node(gr, i);
641 /* Path not yet complete. */
643 if (get_entity_owner(node) != owner)
645 owner = get_entity_type(node);
647 if (pos == get_compound_graph_path_length(gr))
648 if (!is_atomic_type(owner))
653 /* Returns the length of a graph path */
654 int get_compound_graph_path_length(compound_graph_path *gr) {
655 assert(gr && is_compound_graph_path(gr));
660 get_compound_graph_path_node(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].node;
667 set_compound_graph_path_node(compound_graph_path *gr, int pos, entity *node) {
668 assert(gr && is_compound_graph_path(gr));
669 assert(pos >= 0 && pos < gr->len);
670 assert(is_entity(node));
671 gr->list[pos].node = node;
672 assert(is_proper_compound_graph_path(gr, pos));
676 get_compound_graph_path_array_index(compound_graph_path *gr, int pos) {
677 assert(gr && is_compound_graph_path(gr));
678 assert(pos >= 0 && pos < gr->len);
679 return gr->list[pos].index;
683 set_compound_graph_path_array_index(compound_graph_path *gr, int pos, int index) {
684 assert(gr && is_compound_graph_path(gr));
685 assert(pos >= 0 && pos < gr->len);
686 gr->list[pos].index = index;
689 /* A value of a compound entity is a pair of value and the corresponding path to a member of
692 add_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path) {
693 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
694 assert(is_compound_graph_path(path));
695 ARR_APP1(ir_node *, ent->attr.cmpd_attr.values, val);
696 ARR_APP1(compound_graph_path *, ent->attr.cmpd_attr.val_paths, path);
700 set_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path, int pos) {
701 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
702 assert(is_compound_graph_path(path));
703 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
704 ent->attr.cmpd_attr.values[pos] = val;
705 ent->attr.cmpd_attr.val_paths[pos] = path;
709 get_compound_ent_n_values(entity *ent) {
710 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
711 return ARR_LEN(ent->attr.cmpd_attr.values);
715 get_compound_ent_value(entity *ent, int pos) {
716 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
717 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
718 return ent->attr.cmpd_attr.values[pos];
721 compound_graph_path *
722 get_compound_ent_value_path(entity *ent, int pos) {
723 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
724 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.val_paths));
725 return ent->attr.cmpd_attr.val_paths[pos];
729 * Returns non-zero, if two compound_graph_pathes are equal
731 static int equal_paths(compound_graph_path *path1, int *visited_indicees, compound_graph_path *path2) {
733 int len1 = get_compound_graph_path_length(path1);
734 int len2 = get_compound_graph_path_length(path2);
736 if (len2 > len1) return 0;
738 for (i = 0; i < len1; i++) {
740 entity *node1 = get_compound_graph_path_node(path1, i);
741 entity *node2 = get_compound_graph_path_node(path2, i);
743 if (node1 != node2) return 0;
745 tp = get_entity_owner(node1);
746 if (is_Array_type(tp)) {
749 /* Compute the index of this node. */
750 assert(get_array_n_dimensions(tp) == 1 && "multidim not implemented");
752 low = get_array_lower_bound_int(tp, 0);
753 if (low + visited_indicees[i] < get_compound_graph_path_array_index(path2, i)) {
754 visited_indicees[i]++;
758 assert(low + visited_indicees[i] == get_compound_graph_path_array_index(path2, i));
764 /* Returns the position of a value with the given path.
765 * The path must contain array indicees for all array element entities. */
766 int get_compound_ent_pos_by_path(entity *ent, compound_graph_path *path) {
767 int i, n_paths = get_compound_ent_n_values(ent);
768 int *visited_indicees;
769 int path_len = get_compound_graph_path_length(path);
771 NEW_ARR_A(int *, visited_indicees, path_len);
772 memset(visited_indicees, 0, sizeof(*visited_indicees) * path_len);
773 for (i = 0; i < n_paths; i ++) {
774 if (equal_paths(get_compound_ent_value_path(ent, i), visited_indicees, path))
781 printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
782 printf("Entity %s : ", get_entity_name(ent));
783 for (j = 0; j < get_compound_graph_path_length(path); ++j) {
784 entity *node = get_compound_graph_path_node(path, j);
785 printf("%s", get_entity_name(node));
786 if (is_Array_type(get_entity_owner(node)))
787 printf("[%d]", get_compound_graph_path_array_index(path, j));
789 printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
793 assert(0 && "path not found");
797 /* Returns a constant value given the access path.
798 * The path must contain array indicees for all array element entities. */
799 ir_node *get_compound_ent_value_by_path(entity *ent, compound_graph_path *path) {
800 return get_compound_ent_value(ent, get_compound_ent_pos_by_path(ent, path));
805 remove_compound_ent_value(entity *ent, entity *value_ent) {
807 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
808 for (i = 0; i < (ARR_LEN(ent->attr.cmpd_attr.val_paths)); ++i) {
809 compound_graph_path *path = ent->attr.cmpd_attr.val_paths[i];
810 if (path->list[path->len-1].node == value_ent) {
811 for (; i < (ARR_LEN(ent->attr.cmpd_attr.val_paths))-1; ++i) {
812 ent->attr.cmpd_attr.val_paths[i] = ent->attr.cmpd_attr.val_paths[i+1];
813 ent->attr.cmpd_attr.values[i] = ent->attr.cmpd_attr.values[i+1];
815 ARR_SETLEN(entity*, ent->attr.cmpd_attr.val_paths, ARR_LEN(ent->attr.cmpd_attr.val_paths) - 1);
816 ARR_SETLEN(ir_node*, ent->attr.cmpd_attr.values, ARR_LEN(ent->attr.cmpd_attr.values) - 1);
823 add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
824 compound_graph_path *path;
825 ir_type *owner_tp = get_entity_owner(member);
826 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
827 path = new_compound_graph_path(get_entity_type(ent), 1);
828 path->list[0].node = member;
829 if (is_Array_type(owner_tp)) {
833 assert(get_array_n_dimensions(owner_tp) == 1 && has_array_lower_bound(owner_tp, 0));
834 max = get_array_lower_bound_int(owner_tp, 0) -1;
835 for (i = 0, n = get_compound_ent_n_values(ent); i < n; ++i) {
836 int index = get_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0);
841 path->list[0].index = max + 1;
843 add_compound_ent_value_w_path(ent, val, path);
846 /* Copies the firm subgraph referenced by val to const_code_irg and adds
847 the node as constant initialization to ent.
848 The subgraph may not contain control flow operations.
850 copy_and_add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
851 ir_graph *rem = current_ir_graph;
853 assert(get_entity_variability(ent) != variability_uninitialized);
854 current_ir_graph = get_const_code_irg();
856 val = copy_const_value(val);
857 add_compound_ent_value(ent, val, member);
858 current_ir_graph = rem;
861 /* Copies the value i of the entity to current_block in current_ir_graph.
863 copy_compound_ent_value(entity *ent, int pos) {
864 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
865 return copy_const_value(ent->values[pos+1]);
869 get_compound_ent_value_member(entity *ent, int pos) {
870 compound_graph_path *path;
871 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
872 path = get_compound_ent_value_path(ent, pos);
874 return get_compound_graph_path_node(path, get_compound_graph_path_length(path)-1);
878 set_compound_ent_value(entity *ent, ir_node *val, entity *member, int pos) {
879 compound_graph_path *path;
880 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
881 path = get_compound_ent_value_path(ent, pos);
882 set_compound_graph_path_node(path, 0, member);
883 set_compound_ent_value_w_path(ent, val, path, pos);
887 set_array_entity_values(entity *ent, tarval **values, int num_vals) {
889 ir_graph *rem = current_ir_graph;
890 ir_type *arrtp = get_entity_type(ent);
892 ir_type *elttp = get_array_element_type(arrtp);
894 assert(is_Array_type(arrtp));
895 assert(get_array_n_dimensions(arrtp) == 1);
896 /* One bound is sufficient, the number of constant fields makes the
898 assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
899 assert(get_entity_variability(ent) != variability_uninitialized);
900 current_ir_graph = get_const_code_irg();
902 for (i = 0; i < num_vals; i++) {
903 val = new_Const_type(values[i], elttp);
904 add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
905 set_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0, i);
907 current_ir_graph = rem;
910 int get_compound_ent_value_offset_bits(entity *ent, int pos) {
911 compound_graph_path *path;
915 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
917 path = get_compound_ent_value_path(ent, pos);
918 path_len = get_compound_graph_path_length(path);
920 for (i = 0; i < path_len; ++i) {
921 entity *node = get_compound_graph_path_node(path, i);
922 ir_type *node_tp = get_entity_type(node);
923 ir_type *owner_tp = get_entity_owner(node);
924 if (is_Array_type(owner_tp)) {
925 int size = get_type_size_bits(node_tp);
926 int align = get_type_alignment_bits(node_tp);
930 assert(size % align == 0);
931 /* ansonsten aufrunden */
933 offset += size * get_compound_graph_path_array_index(path, i);
935 offset += get_entity_offset_bits(node);
941 int get_compound_ent_value_offset_bytes(entity *ent, int pos) {
942 int offset = get_compound_ent_value_offset_bits(ent, pos);
943 assert(offset % 8 == 0);
948 static void init_index(ir_type *arr) {
952 assert(get_array_n_dimensions(arr) == 1);
954 if (has_array_lower_bound(arr, dim))
955 init = get_array_lower_bound_int(arr, 0) -1;
957 init = get_array_upper_bound_int(arr, 0) +1;
959 set_entity_link(get_array_element_entity(arr), INT_TO_PTR(init));
963 static int get_next_index(entity *elem_ent) {
964 ir_type *arr = get_entity_owner(elem_ent);
968 assert(get_array_n_dimensions(arr) == 1);
970 if (has_array_lower_bound(arr, dim)) {
971 next = PTR_TO_INT(get_entity_link(elem_ent)) + 1;
972 if (has_array_upper_bound(arr, dim)) {
973 int upper = get_array_upper_bound_int(arr, dim);
974 if (next == upper) next = get_array_lower_bound_int(arr, dim);
977 next = PTR_TO_INT(get_entity_link(elem_ent)) - 1;
978 if (has_array_lower_bound(arr, dim)) {
979 int upper = get_array_upper_bound_int(arr, dim);
980 if (next == upper) next = get_array_upper_bound_int(arr, dim);
984 set_entity_link(elem_ent, INT_TO_PTR(next));
988 /* Compute the array indices in compound graph paths of initialized entities.
990 * All arrays must have fixed lower and upper bounds. One array can
991 * have an open bound. If there are several open bounds, we do
992 * nothing. There must be initializer elements for all array
993 * elements. Uses the link field in the array element entities. The
994 * array bounds must be representable as ints.
996 * (If the bounds are not representable as ints we have to represent
997 * the indices as firm nodes. But still we must be able to
998 * evaluate the index against the upper bound.)
1000 void compute_compound_ent_array_indicees(entity *ent) {
1001 ir_type *tp = get_entity_type(ent);
1003 entity *unknown_bound_entity = NULL;
1005 if (!is_compound_type(tp) ||
1006 (ent->variability == variability_uninitialized)) return ;
1008 n_vals = get_compound_ent_n_values(ent);
1009 if (n_vals == 0) return;
1011 /* We can not compute the indexes if there is more than one array
1012 with an unknown bound. For this remember the first entity that
1013 represents such an array. It could be ent. */
1014 if (is_Array_type(tp)) {
1017 assert(get_array_n_dimensions(tp) == 1 && "other not implemented");
1018 if (!has_array_lower_bound(tp, dim) || !has_array_upper_bound(tp, dim))
1019 unknown_bound_entity = ent;
1022 /* Initialize the entity links to lower bound -1 and test all path elements
1023 for known bounds. */
1024 for (i = 0; i < n_vals; ++i) {
1025 compound_graph_path *path = get_compound_ent_value_path(ent, i);
1026 int j, path_len = get_compound_graph_path_length(path);
1027 for (j = 0; j < path_len; ++j) {
1028 entity *node = get_compound_graph_path_node(path, j);
1029 ir_type *elem_tp = get_entity_type(node);
1031 if (is_Array_type(elem_tp)) {
1033 assert(get_array_n_dimensions(elem_tp) == 1 && "other not implemented");
1034 if (!has_array_lower_bound(elem_tp, dim) || !has_array_upper_bound(elem_tp, dim)) {
1035 if (!unknown_bound_entity) unknown_bound_entity = node;
1036 if (node != unknown_bound_entity) return;
1039 init_index(elem_tp);
1044 /* Finally compute the indexes ... */
1045 for (i = 0; i < n_vals; ++i) {
1046 compound_graph_path *path = get_compound_ent_value_path(ent, i);
1047 int j, path_len = get_compound_graph_path_length(path);
1048 for (j = 0; j < path_len; ++j) {
1049 entity *node = get_compound_graph_path_node(path, j);
1050 ir_type *owner_tp = get_entity_owner(node);
1051 if (is_Array_type(owner_tp))
1052 set_compound_graph_path_array_index (path, j, get_next_index(node));
1057 /** resize: double the allocated buffer */
1058 static int *resize (int *buf, int *size) {
1059 int new_size = *size * 2;
1060 int *new_buf = xcalloc(new_size, sizeof(new_buf[0]));
1061 memcpy(new_buf, buf, *size);
1067 /* We sort the elements by placing them at their bit offset in an
1068 array where each entry represents one bit called permutation. In
1069 fact, we do not place the values themselves, as we would have to
1070 copy two things, the value and the path. We only remember the
1071 position in the old order. Each value should have a distinct
1072 position in the permutation.
1074 A second iteration now permutes the actual elements into two
1076 void sort_compound_ent_values(entity *ent) {
1084 ir_node **my_values;
1085 compound_graph_path **my_paths;
1087 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
1089 tp = get_entity_type(ent);
1090 n_vals = get_compound_ent_n_values(ent);
1091 tp_size = get_type_size_bits(tp);
1093 if (!is_compound_type(tp) ||
1094 (ent->variability == variability_uninitialized) ||
1095 (get_type_state(tp) != layout_fixed) ||
1096 (n_vals == 0) ) return;
1098 /* estimated upper bound for size. Better: use flexible array ... */
1099 size = ((tp_size > (n_vals * 32)) ? tp_size : (n_vals * 32)) * 4;
1100 permutation = xcalloc(size, sizeof(permutation[0]));
1102 for (i = 0; i < n_vals; ++i) {
1103 int pos = get_compound_ent_value_offset_bits(ent, i);
1104 while (pos >= size) {
1105 permutation = resize(permutation, &size);
1108 assert(permutation[pos] == 0 && "two values with the same offset");
1109 permutation[pos] = i + 1; /* We initialized with 0, so we can not distinguish entry 0.
1110 So inc all entries by one. */
1111 //fprintf(stderr, "i: %d, pos: %d \n", i, pos);
1115 my_values = NEW_ARR_F(ir_node *, n_vals);
1116 my_paths = NEW_ARR_F(compound_graph_path *, n_vals);
1117 for (i = 0; i < size; ++i) {
1118 int pos = permutation[i];
1120 //fprintf(stderr, "pos: %d i: %d next %d \n", i, pos, next);
1121 assert(next < n_vals);
1122 pos--; /* We increased the pos by one */
1123 my_values[next] = get_compound_ent_value (ent, pos);
1124 my_paths [next] = get_compound_ent_value_path(ent, pos);
1130 DEL_ARR_F(ent->attr.cmpd_attr.values);
1131 ent->attr.cmpd_attr.values = my_values;
1132 DEL_ARR_F(ent->attr.cmpd_attr.val_paths);
1133 ent->attr.cmpd_attr.val_paths = my_paths;
1137 (get_entity_offset_bytes)(const entity *ent) {
1138 return _get_entity_offset_bytes(ent);
1142 (get_entity_offset_bits)(const entity *ent) {
1143 return _get_entity_offset_bits(ent);
1147 (set_entity_offset_bytes)(entity *ent, int offset) {
1148 _set_entity_offset_bytes(ent, offset);
1152 (set_entity_offset_bits)(entity *ent, int offset) {
1153 _set_entity_offset_bits(ent, offset);
1157 add_entity_overwrites(entity *ent, entity *overwritten) {
1158 assert(is_Class_type(get_entity_owner(ent)));
1159 ARR_APP1(entity *, ent->overwrites, overwritten);
1160 ARR_APP1(entity *, overwritten->overwrittenby, ent);
1164 get_entity_n_overwrites(entity *ent) {
1165 assert(is_Class_type(get_entity_owner(ent)));
1166 return (ARR_LEN(ent->overwrites));
1170 get_entity_overwrites_index(entity *ent, entity *overwritten) {
1172 assert(is_Class_type(get_entity_owner(ent)));
1173 for (i = 0; i < get_entity_n_overwrites(ent); i++)
1174 if (get_entity_overwrites(ent, i) == overwritten)
1180 get_entity_overwrites (entity *ent, int pos) {
1181 assert(is_Class_type(get_entity_owner(ent)));
1182 assert(pos < get_entity_n_overwrites(ent));
1183 return ent->overwrites[pos];
1187 set_entity_overwrites (entity *ent, int pos, entity *overwritten) {
1188 assert(is_Class_type(get_entity_owner(ent)));
1189 assert(pos < get_entity_n_overwrites(ent));
1190 ent->overwrites[pos] = overwritten;
1194 remove_entity_overwrites(entity *ent, entity *overwritten) {
1196 assert(is_Class_type(get_entity_owner(ent)));
1197 for (i = 0; i < (ARR_LEN (ent->overwrites)); i++)
1198 if (ent->overwrites[i] == overwritten) {
1199 for(; i < (ARR_LEN (ent->overwrites))-1; i++)
1200 ent->overwrites[i] = ent->overwrites[i+1];
1201 ARR_SETLEN(entity*, ent->overwrites, ARR_LEN(ent->overwrites) - 1);
1207 add_entity_overwrittenby (entity *ent, entity *overwrites) {
1208 assert(is_Class_type(get_entity_owner(ent)));
1209 add_entity_overwrites(overwrites, ent);
1213 get_entity_n_overwrittenby (entity *ent) {
1214 assert(is_Class_type(get_entity_owner(ent)));
1215 return (ARR_LEN (ent->overwrittenby));
1219 get_entity_overwrittenby_index(entity *ent, entity *overwrites) {
1221 assert(is_Class_type(get_entity_owner(ent)));
1222 for (i = 0; i < get_entity_n_overwrittenby(ent); i++)
1223 if (get_entity_overwrittenby(ent, i) == overwrites)
1229 get_entity_overwrittenby (entity *ent, int pos) {
1230 assert(is_Class_type(get_entity_owner(ent)));
1231 assert(pos < get_entity_n_overwrittenby(ent));
1232 return ent->overwrittenby[pos];
1236 set_entity_overwrittenby (entity *ent, int pos, entity *overwrites) {
1237 assert(is_Class_type(get_entity_owner(ent)));
1238 assert(pos < get_entity_n_overwrittenby(ent));
1239 ent->overwrittenby[pos] = overwrites;
1242 void remove_entity_overwrittenby(entity *ent, entity *overwrites) {
1244 assert(is_Class_type(get_entity_owner(ent)));
1245 for (i = 0; i < (ARR_LEN (ent->overwrittenby)); i++)
1246 if (ent->overwrittenby[i] == overwrites) {
1247 for(; i < (ARR_LEN (ent->overwrittenby))-1; i++)
1248 ent->overwrittenby[i] = ent->overwrittenby[i+1];
1249 ARR_SETLEN(entity*, ent->overwrittenby, ARR_LEN(ent->overwrittenby) - 1);
1254 /* A link to store intermediate information */
1256 (get_entity_link)(const entity *ent) {
1257 return _get_entity_link(ent);
1261 (set_entity_link)(entity *ent, void *l) {
1262 _set_entity_link(ent, l);
1266 (get_entity_irg)(const entity *ent) {
1267 return _get_entity_irg(ent);
1271 set_entity_irg(entity *ent, ir_graph *irg) {
1272 assert(is_method_entity(ent));
1273 /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
1274 * Methode selbst nicht mehr aufgerufen werden kann, die Entität
1275 * aber erhalten bleiben soll? Wandle die Entitaet in description oder
1278 assert((irg && ent->peculiarity == peculiarity_existent) ||
1279 (!irg && (ent->peculiarity == peculiarity_existent)
1280 && (ent -> visibility == visibility_external_allocated)) ||
1281 (!irg && ent->peculiarity == peculiarity_description) ||
1282 (!irg && ent->peculiarity == peculiarity_inherited));
1283 ent->attr.mtd_attr.irg = irg;
1286 unsigned get_entity_vtable_number(entity *ent) {
1287 assert(is_method_entity(ent));
1288 return ent->attr.mtd_attr.vtable_number;
1291 void set_entity_vtable_number(entity *ent, unsigned vtable_number) {
1292 assert(is_method_entity(ent));
1293 ent->attr.mtd_attr.vtable_number = vtable_number;
1296 /* Returns the section of a method. */
1297 ir_img_section get_method_img_section(const entity *ent) {
1298 assert(is_method_entity(ent));
1299 return ent->attr.mtd_attr.section;
1302 /* Sets the section of a method. */
1303 void set_method_img_section(entity *ent, ir_img_section section) {
1304 assert(is_method_entity(ent));
1305 ent->attr.mtd_attr.section = section;
1309 (is_entity)(const void *thing) {
1310 return _is_entity(thing);
1313 int is_atomic_entity(entity *ent) {
1314 ir_type *t = get_entity_type(ent);
1315 tp_op *op = get_type_tpop(t);
1316 return (op == type_primitive || op == type_pointer ||
1317 op == type_enumeration || op == type_method);
1320 int is_compound_entity(entity *ent) {
1321 ir_type *t = get_entity_type(ent);
1322 tp_op *op = get_type_tpop(t);
1323 return (op == type_class || op == type_struct ||
1324 op == type_array || op == type_union);
1327 int is_method_entity(entity *ent) {
1328 ir_type *t = get_entity_type(ent);
1329 return is_Method_type(t);
1333 * @todo not implemented!!! */
1334 int equal_entity(entity *ent1, entity *ent2) {
1335 fprintf(stderr, " calling unimplemented equal entity!!! \n");
1340 unsigned long (get_entity_visited)(entity *ent) {
1341 return _get_entity_visited(ent);
1344 void (set_entity_visited)(entity *ent, unsigned long num) {
1345 _set_entity_visited(ent, num);
1348 /* Sets visited field in entity to entity_visited. */
1349 void (mark_entity_visited)(entity *ent) {
1350 _mark_entity_visited(ent);
1353 int (entity_visited)(entity *ent) {
1354 return _entity_visited(ent);
1357 int (entity_not_visited)(entity *ent) {
1358 return _entity_not_visited(ent);
1361 /* Returns the mask of the additional entity properties. */
1362 unsigned get_entity_additional_properties(entity *ent) {
1365 assert(is_method_entity(ent));
1367 /* first check, if the graph has additional properties */
1368 irg = get_entity_irg(ent);
1371 return get_irg_additional_properties(irg);
1373 if (ent->attr.mtd_attr.irg_add_properties & mtp_property_inherited)
1374 return get_method_additional_properties(get_entity_type(ent));
1376 return ent->attr.mtd_attr.irg_add_properties;
1379 /* Sets the mask of the additional graph properties. */
1380 void set_entity_additional_properties(entity *ent, unsigned property_mask)
1384 assert(is_method_entity(ent));
1386 /* first check, if the graph exists */
1387 irg = get_entity_irg(ent);
1389 set_irg_additional_properties(irg, property_mask);
1391 /* do not allow to set the mtp_property_inherited flag or
1392 * the automatic inheritance of flags will not work */
1393 ent->attr.mtd_attr.irg_add_properties = property_mask & ~mtp_property_inherited;
1397 /* Sets one additional graph property. */
1398 void set_entity_additional_property(entity *ent, mtp_additional_property flag)
1402 assert(is_method_entity(ent));
1404 /* first check, if the graph exists */
1405 irg = get_entity_irg(ent);
1407 set_irg_additional_property(irg, flag);
1409 unsigned mask = ent->attr.mtd_attr.irg_add_properties;
1411 if (mask & mtp_property_inherited)
1412 mask = get_method_additional_properties(get_entity_type(ent));
1414 /* do not allow to set the mtp_property_inherited flag or
1415 * the automatic inheritance of flags will not work */
1416 ent->attr.mtd_attr.irg_add_properties = mask | (flag & ~mtp_property_inherited);
1420 /* Initialize entity module. */
1421 void firm_init_entity(void)
1423 symconst_symbol sym;
1425 assert(firm_unknown_type && "Call init_type() before firm_init_entity()!");
1426 assert(!unknown_entity && "Call firm_init_entity() only once!");
1428 unknown_entity = new_rd_entity(NULL, firm_unknown_type, new_id_from_str(UNKNOWN_ENTITY_NAME), firm_unknown_type);
1429 set_entity_visibility(unknown_entity, visibility_external_allocated);
1430 set_entity_ld_ident(unknown_entity, get_entity_ident(unknown_entity));
1432 current_ir_graph = get_const_code_irg();
1433 sym.entity_p = unknown_entity;
1434 unknown_entity->value = new_SymConst(sym, symconst_addr_ent);