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;
117 res->compiler_gen = 0;
120 res->repr_class = NULL;
122 if (is_Method_type(type)) {
125 rem = current_ir_graph;
126 current_ir_graph = get_const_code_irg();
127 res->value = new_SymConst(sym, symconst_addr_ent);
128 current_ir_graph = rem;
129 res->allocation = allocation_static;
130 res->variability = variability_constant;
131 res->attr.mtd_attr.irg_add_properties = mtp_property_inherited;
132 res->attr.mtd_attr.vtable_number = VTABLE_NUM_NOT_SET;
133 res->attr.mtd_attr.param_access = NULL;
134 res->attr.mtd_attr.param_weight = NULL;
135 res->attr.mtd_attr.irg = NULL;
136 res->attr.mtd_attr.section = section_text;
138 else if (is_compound_type(type)) {
139 res->variability = variability_uninitialized;
141 res->attr.cmpd_attr.values = NULL;
142 res->attr.cmpd_attr.val_paths = NULL;
145 res->variability = variability_uninitialized;
149 if (is_Class_type(owner)) {
150 res->overwrites = NEW_ARR_F(entity *, 0);
151 res->overwrittenby = NEW_ARR_F(entity *, 0);
153 res->overwrites = NULL;
154 res->overwrittenby = NULL;
158 res->nr = get_irp_new_node_nr();
159 #endif /* DEBUG_libfirm */
162 set_entity_dbg_info(res, db);
168 new_d_entity(ir_type *owner, ident *name, ir_type *type, dbg_info *db) {
171 assert(is_compound_type(owner));
172 res = new_rd_entity(db, owner, name, type);
173 /* Remember entity in it's owner. */
174 insert_entity_in_owner(res);
176 hook_new_entity(res);
181 new_entity(ir_type *owner, ident *name, ir_type *type) {
182 return new_d_entity(owner, name, type, NULL);
186 * Free entity attributes.
188 * @param ent the entity
190 static void free_entity_attrs(entity *ent) {
192 if (get_type_tpop(get_entity_owner(ent)) == type_class) {
193 DEL_ARR_F(ent->overwrites); ent->overwrites = NULL;
194 DEL_ARR_F(ent->overwrittenby); ent->overwrittenby = NULL;
196 assert(ent->overwrites == NULL);
197 assert(ent->overwrittenby == NULL);
199 if (is_compound_entity(ent)) {
200 if (ent->attr.cmpd_attr.val_paths) {
201 for (i = 0; i < get_compound_ent_n_values(ent); i++)
202 if (ent->attr.cmpd_attr.val_paths[i]) {
203 /* free_compound_graph_path(ent->attr.cmpd_attr.val_paths[i]) ; * @@@ warum nich? */
204 /* Geht nich: wird mehrfach verwendet!!! ==> mehrfach frei gegeben. */
205 /* DEL_ARR_F(ent->attr.cmpd_attr.val_paths); */
207 ent->attr.cmpd_attr.val_paths = NULL;
209 /* if (ent->attr.cmpd_attr.values) DEL_ARR_F(ent->attr.cmpd_attr.values); *//* @@@ warum nich? */
210 ent->attr.cmpd_attr.values = NULL;
212 else if (is_method_entity(ent)) {
213 if (ent->attr.mtd_attr.param_access) {
214 DEL_ARR_F(ent->attr.mtd_attr.param_access);
215 ent->attr.mtd_attr.param_access = NULL;
217 if (ent->attr.mtd_attr.param_weight) {
218 DEL_ARR_F(ent->attr.mtd_attr.param_weight);
219 ent->attr.mtd_attr.param_weight = NULL;
225 copy_entity_own(entity *old, ir_type *new_owner) {
227 assert(is_entity(old));
228 assert(is_compound_type(new_owner));
230 if (old->owner == new_owner) return old;
231 newe = xmalloc(sizeof(*newe));
232 memcpy(newe, old, sizeof(*newe));
233 newe->owner = new_owner;
234 if (is_Class_type(new_owner)) {
235 newe->overwrites = NEW_ARR_F(entity *, 0);
236 newe->overwrittenby = NEW_ARR_F(entity *, 0);
239 newe->nr = get_irp_new_node_nr();
242 insert_entity_in_owner(newe);
248 copy_entity_name(entity *old, ident *new_name) {
250 assert(old && old->kind == k_entity);
252 if (old->name == new_name) return old;
253 newe = xmalloc(sizeof(*newe));
254 memcpy(newe, old, sizeof(*newe));
255 newe->name = new_name;
256 newe->ld_name = NULL;
257 if (is_Class_type(newe->owner)) {
258 newe->overwrites = DUP_ARR_F(entity *, old->overwrites);
259 newe->overwrittenby = DUP_ARR_F(entity *, old->overwrittenby);
262 newe->nr = get_irp_new_node_nr();
265 insert_entity_in_owner (newe);
272 free_entity (entity *ent) {
273 assert(ent && ent->kind == k_entity);
274 free_entity_attrs(ent);
279 /* Outputs a unique number for this node */
281 get_entity_nr(entity *ent) {
282 assert(ent && ent->kind == k_entity);
286 return (long)PTR_TO_INT(ent);
291 (get_entity_name)(const entity *ent) {
292 return _get_entity_name(ent);
296 (get_entity_ident)(const entity *ent) {
297 return _get_entity_ident(ent);
301 (set_entity_ident)(entity *ent, ident *id) {
302 _set_entity_ident(ent, id);
306 (get_entity_owner)(entity *ent) {
307 return _get_entity_owner(ent);
311 set_entity_owner(entity *ent, ir_type *owner) {
312 assert(is_entity(ent));
313 assert(is_compound_type(owner));
318 (get_entity_ld_ident)(entity *ent) {
319 return _get_entity_ld_ident(ent);
323 (set_entity_ld_ident)(entity *ent, ident *ld_ident) {
324 _set_entity_ld_ident(ent, ld_ident);
328 (get_entity_ld_name)(entity *ent) {
329 return _get_entity_ld_name(ent);
333 (get_entity_type)(entity *ent) {
334 return _get_entity_type(ent);
338 (set_entity_type)(entity *ent, ir_type *type) {
339 _set_entity_type(ent, type);
343 (get_entity_allocation)(const entity *ent) {
344 return _get_entity_allocation(ent);
348 (set_entity_allocation)(entity *ent, ir_allocation al) {
349 _set_entity_allocation(ent, al);
352 /* return the name of the visibility */
353 const char *get_allocation_name(ir_allocation all)
355 #define X(a) case a: return #a
357 X(allocation_automatic);
358 X(allocation_parameter);
359 X(allocation_dynamic);
360 X(allocation_static);
361 default: return "BAD VALUE";
368 (get_entity_visibility)(const entity *ent) {
369 return _get_entity_visibility(ent);
373 set_entity_visibility(entity *ent, ir_visibility vis) {
374 assert(ent && ent->kind == k_entity);
375 if (vis != visibility_local)
376 assert((ent->allocation == allocation_static) ||
377 (ent->allocation == allocation_automatic));
378 /* @@@ Test that the owner type is not local, but how??
379 && get_class_visibility(get_entity_owner(ent)) != local));*/
380 ent->visibility = vis;
383 /* return the name of the visibility */
384 const char *get_visibility_name(ir_visibility vis)
386 #define X(a) case a: return #a
389 X(visibility_external_visible);
390 X(visibility_external_allocated);
391 default: return "BAD VALUE";
397 (get_entity_variability)(const entity *ent) {
398 return _get_entity_variability(ent);
402 set_entity_variability (entity *ent, ir_variability var)
404 assert(ent && ent->kind == k_entity);
405 if (var == variability_part_constant)
406 assert(is_Class_type(ent->type) || is_Struct_type(ent->type));
408 if ((is_compound_type(ent->type)) &&
409 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
410 /* Allocate data structures for constant values */
411 ent->attr.cmpd_attr.values = NEW_ARR_F(ir_node *, 0);
412 ent->attr.cmpd_attr.val_paths = NEW_ARR_F(compound_graph_path *, 0);
414 if ((is_atomic_type(ent->type)) &&
415 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
416 /* Set default constant value. */
417 ent->value = new_rd_Unknown(get_const_code_irg(), get_type_mode(ent->type));
420 if ((is_compound_type(ent->type)) &&
421 (var == variability_uninitialized) && (ent->variability != variability_uninitialized)) {
422 /* Free data structures for constant values */
423 DEL_ARR_F(ent->attr.cmpd_attr.values); ent->attr.cmpd_attr.values = NULL;
424 DEL_ARR_F(ent->attr.cmpd_attr.val_paths); ent->attr.cmpd_attr.val_paths = NULL;
426 ent->variability = var;
429 /* return the name of the variability */
430 const char *get_variability_name(ir_variability var)
432 #define X(a) case a: return #a
434 X(variability_uninitialized);
435 X(variability_initialized);
436 X(variability_part_constant);
437 X(variability_constant);
438 default: return "BAD VALUE";
444 (get_entity_volatility)(const entity *ent) {
445 return _get_entity_volatility(ent);
449 (set_entity_volatility)(entity *ent, ir_volatility vol) {
450 _set_entity_volatility(ent, vol);
453 /* return the name of the volatility */
454 const char *get_volatility_name(ir_volatility var)
456 #define X(a) case a: return #a
458 X(volatility_non_volatile);
459 X(volatility_is_volatile);
460 default: return "BAD VALUE";
466 (get_entity_peculiarity)(const entity *ent) {
467 return _get_entity_peculiarity(ent);
471 (set_entity_peculiarity)(entity *ent, ir_peculiarity pec) {
472 _set_entity_peculiarity(ent, pec);
475 /* Checks if an entity cannot be overridden anymore. */
476 int (get_entity_final)(const entity *ent) {
477 return _get_entity_final(ent);
480 /* Sets/resets the final flag of an entity. */
481 void (set_entity_final)(entity *ent, int final) {
482 _set_entity_final(ent, final);
485 /* Checks if an entity is compiler generated */
486 int is_entity_compiler_generated(const entity *ent) {
487 assert(is_entity(ent));
488 return ent->compiler_gen;
491 /* Sets/resets the compiler generated flag */
492 void set_entity_compiler_generated(entity *ent, int flag) {
493 assert(is_entity(ent));
494 ent->compiler_gen = flag ? 1 : 0;
497 /* Get the entity's stickyness */
499 (get_entity_stickyness)(const entity *ent) {
500 return _get_entity_stickyness(ent);
503 /* Set the entity's stickyness */
505 (set_entity_stickyness)(entity *ent, ir_stickyness stickyness) {
506 _set_entity_stickyness(ent, stickyness);
509 /* Set has no effect for existent entities of type method. */
511 get_atomic_ent_value(entity *ent)
513 assert(ent && is_atomic_entity(ent));
514 assert(ent->variability != variability_uninitialized);
515 return skip_Id (ent->value);
519 set_atomic_ent_value(entity *ent, ir_node *val) {
520 assert(is_atomic_entity(ent) && (ent->variability != variability_uninitialized));
521 if (is_Method_type(ent->type) && (ent->peculiarity == peculiarity_existent))
526 /* Returns true if the the node is representable as code on
528 int is_irn_const_expression(ir_node *n) {
531 /* we are in danger iff an exception will arise. TODO: be more precisely,
532 * for instance Div. will NOT rise if divisor != 0
534 if (is_binop(n) && !is_fragile_op(n))
535 return is_irn_const_expression(get_binop_left(n)) && is_irn_const_expression(get_binop_right(n));
538 switch(get_irn_opcode(n)) {
545 return is_irn_const_expression(get_irn_n(n, 0));
553 * Copies a firm subgraph that complies to the restrictions for
554 * constant expressions to current_block in current_ir_graph.
556 ir_node *copy_const_value(dbg_info *dbg, ir_node *n) {
560 /* @@@ GL I think we should implement this using the routines from irgopt for
561 dead node elimination/inlineing. */
564 switch (get_irn_opcode(n)) {
566 nn = new_d_Const_type(dbg, m, get_Const_tarval(n), get_Const_type(n));
569 nn = new_d_SymConst_type(dbg, get_SymConst_symbol(n), get_SymConst_kind(n),
570 get_SymConst_value_type(n));
573 nn = new_d_Add(dbg, copy_const_value(dbg, get_Add_left(n)),
574 copy_const_value(dbg, get_Add_right(n)), m); break;
576 nn = new_d_Sub(dbg, copy_const_value(dbg, get_Sub_left(n)),
577 copy_const_value(dbg, get_Sub_right(n)), m); break;
579 nn = new_d_Mul(dbg, copy_const_value(dbg, get_Mul_left(n)),
580 copy_const_value(dbg, get_Mul_right(n)), m); break;
582 nn = new_d_And(dbg, copy_const_value(dbg, get_And_left(n)),
583 copy_const_value(dbg, get_And_right(n)), m); break;
585 nn = new_d_Or(dbg, copy_const_value(dbg, get_Or_left(n)),
586 copy_const_value(dbg, get_Or_right(n)), m); break;
588 nn = new_d_Eor(dbg, copy_const_value(dbg, get_Eor_left(n)),
589 copy_const_value(dbg, get_Eor_right(n)), m); break;
591 nn = new_d_Cast(dbg, copy_const_value(dbg, get_Cast_op(n)), get_Cast_type(n)); break;
593 nn = new_d_Conv(dbg, copy_const_value(dbg, get_Conv_op(n)), m); break;
595 nn = new_d_Unknown(m); break;
598 assert(0 && "opcode invalid or not implemented");
605 /* Creates a new compound graph path. */
606 compound_graph_path *
607 new_compound_graph_path(ir_type *tp, int length) {
608 compound_graph_path *res;
610 assert(is_compound_type(tp));
613 res = xmalloc(sizeof(*res) + (length-1) * sizeof(res->list[0]));
614 memset(res, 0, sizeof(*res) + (length-1) * sizeof(res->list[0]));
615 res->kind = k_ir_compound_graph_path;
622 /* Frees an graph path object */
623 void free_compound_graph_path (compound_graph_path *gr) {
624 assert(gr && is_compound_graph_path(gr));
629 /* Returns non-zero if an object is a compound graph path */
630 int is_compound_graph_path(void *thing) {
631 return (get_kind(thing) == k_ir_compound_graph_path);
634 /* Checks whether the path up to pos is correct. If the path contains a NULL,
635 * assumes the path is not complete and returns 'true'. */
636 int is_proper_compound_graph_path(compound_graph_path *gr, int pos) {
639 ir_type *owner = gr->tp;
641 for (i = 0; i <= pos; i++) {
642 node = get_compound_graph_path_node(gr, i);
644 /* Path not yet complete. */
646 if (get_entity_owner(node) != owner)
648 owner = get_entity_type(node);
650 if (pos == get_compound_graph_path_length(gr))
651 if (!is_atomic_type(owner))
656 /* Returns the length of a graph path */
657 int get_compound_graph_path_length(compound_graph_path *gr) {
658 assert(gr && is_compound_graph_path(gr));
663 get_compound_graph_path_node(compound_graph_path *gr, int pos) {
664 assert(gr && is_compound_graph_path(gr));
665 assert(pos >= 0 && pos < gr->len);
666 return gr->list[pos].node;
670 set_compound_graph_path_node(compound_graph_path *gr, int pos, entity *node) {
671 assert(gr && is_compound_graph_path(gr));
672 assert(pos >= 0 && pos < gr->len);
673 assert(is_entity(node));
674 gr->list[pos].node = node;
675 assert(is_proper_compound_graph_path(gr, pos));
679 get_compound_graph_path_array_index(compound_graph_path *gr, int pos) {
680 assert(gr && is_compound_graph_path(gr));
681 assert(pos >= 0 && pos < gr->len);
682 return gr->list[pos].index;
686 set_compound_graph_path_array_index(compound_graph_path *gr, int pos, int index) {
687 assert(gr && is_compound_graph_path(gr));
688 assert(pos >= 0 && pos < gr->len);
689 gr->list[pos].index = index;
692 /* A value of a compound entity is a pair of value and the corresponding path to a member of
695 add_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path) {
696 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
697 assert(is_compound_graph_path(path));
698 ARR_APP1(ir_node *, ent->attr.cmpd_attr.values, val);
699 ARR_APP1(compound_graph_path *, ent->attr.cmpd_attr.val_paths, path);
703 set_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path, int pos) {
704 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
705 assert(is_compound_graph_path(path));
706 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
707 ent->attr.cmpd_attr.values[pos] = val;
708 ent->attr.cmpd_attr.val_paths[pos] = path;
712 get_compound_ent_n_values(entity *ent) {
713 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
714 return ARR_LEN(ent->attr.cmpd_attr.values);
718 get_compound_ent_value(entity *ent, int pos) {
719 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
720 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
721 return ent->attr.cmpd_attr.values[pos];
724 compound_graph_path *
725 get_compound_ent_value_path(entity *ent, int pos) {
726 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
727 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.val_paths));
728 return ent->attr.cmpd_attr.val_paths[pos];
732 * Returns non-zero, if two compound_graph_pathes are equal
734 static int equal_paths(compound_graph_path *path1, int *visited_indicees, compound_graph_path *path2) {
736 int len1 = get_compound_graph_path_length(path1);
737 int len2 = get_compound_graph_path_length(path2);
739 if (len2 > len1) return 0;
741 for (i = 0; i < len1; i++) {
743 entity *node1 = get_compound_graph_path_node(path1, i);
744 entity *node2 = get_compound_graph_path_node(path2, i);
746 if (node1 != node2) return 0;
748 tp = get_entity_owner(node1);
749 if (is_Array_type(tp)) {
752 /* Compute the index of this node. */
753 assert(get_array_n_dimensions(tp) == 1 && "multidim not implemented");
755 low = get_array_lower_bound_int(tp, 0);
756 if (low + visited_indicees[i] < get_compound_graph_path_array_index(path2, i)) {
757 visited_indicees[i]++;
761 assert(low + visited_indicees[i] == get_compound_graph_path_array_index(path2, i));
767 /* Returns the position of a value with the given path.
768 * The path must contain array indicees for all array element entities. */
769 int get_compound_ent_pos_by_path(entity *ent, compound_graph_path *path) {
770 int i, n_paths = get_compound_ent_n_values(ent);
771 int *visited_indicees;
772 int path_len = get_compound_graph_path_length(path);
774 NEW_ARR_A(int *, visited_indicees, path_len);
775 memset(visited_indicees, 0, sizeof(*visited_indicees) * path_len);
776 for (i = 0; i < n_paths; i ++) {
777 if (equal_paths(get_compound_ent_value_path(ent, i), visited_indicees, path))
784 printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
785 printf("Entity %s : ", get_entity_name(ent));
786 for (j = 0; j < get_compound_graph_path_length(path); ++j) {
787 entity *node = get_compound_graph_path_node(path, j);
788 printf("%s", get_entity_name(node));
789 if (is_Array_type(get_entity_owner(node)))
790 printf("[%d]", get_compound_graph_path_array_index(path, j));
792 printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
796 assert(0 && "path not found");
800 /* Returns a constant value given the access path.
801 * The path must contain array indicees for all array element entities. */
802 ir_node *get_compound_ent_value_by_path(entity *ent, compound_graph_path *path) {
803 return get_compound_ent_value(ent, get_compound_ent_pos_by_path(ent, path));
808 remove_compound_ent_value(entity *ent, entity *value_ent) {
810 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
811 for (i = 0; i < (ARR_LEN(ent->attr.cmpd_attr.val_paths)); ++i) {
812 compound_graph_path *path = ent->attr.cmpd_attr.val_paths[i];
813 if (path->list[path->len-1].node == value_ent) {
814 for (; i < (ARR_LEN(ent->attr.cmpd_attr.val_paths))-1; ++i) {
815 ent->attr.cmpd_attr.val_paths[i] = ent->attr.cmpd_attr.val_paths[i+1];
816 ent->attr.cmpd_attr.values[i] = ent->attr.cmpd_attr.values[i+1];
818 ARR_SETLEN(entity*, ent->attr.cmpd_attr.val_paths, ARR_LEN(ent->attr.cmpd_attr.val_paths) - 1);
819 ARR_SETLEN(ir_node*, ent->attr.cmpd_attr.values, ARR_LEN(ent->attr.cmpd_attr.values) - 1);
826 add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
827 compound_graph_path *path;
828 ir_type *owner_tp = get_entity_owner(member);
829 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
830 path = new_compound_graph_path(get_entity_type(ent), 1);
831 path->list[0].node = member;
832 if (is_Array_type(owner_tp)) {
836 assert(get_array_n_dimensions(owner_tp) == 1 && has_array_lower_bound(owner_tp, 0));
837 max = get_array_lower_bound_int(owner_tp, 0) -1;
838 for (i = 0, n = get_compound_ent_n_values(ent); i < n; ++i) {
839 int index = get_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0);
844 path->list[0].index = max + 1;
846 add_compound_ent_value_w_path(ent, val, path);
849 /* Copies the firm subgraph referenced by val to const_code_irg and adds
850 the node as constant initialization to ent.
851 The subgraph may not contain control flow operations.
853 copy_and_add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
854 ir_graph *rem = current_ir_graph;
856 assert(get_entity_variability(ent) != variability_uninitialized);
857 current_ir_graph = get_const_code_irg();
859 val = copy_const_value(val);
860 add_compound_ent_value(ent, val, member);
861 current_ir_graph = rem;
864 /* Copies the value i of the entity to current_block in current_ir_graph.
866 copy_compound_ent_value(entity *ent, int pos) {
867 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
868 return copy_const_value(ent->values[pos+1]);
872 get_compound_ent_value_member(entity *ent, int pos) {
873 compound_graph_path *path;
874 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
875 path = get_compound_ent_value_path(ent, pos);
877 return get_compound_graph_path_node(path, get_compound_graph_path_length(path)-1);
881 set_compound_ent_value(entity *ent, ir_node *val, entity *member, int pos) {
882 compound_graph_path *path;
883 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
884 path = get_compound_ent_value_path(ent, pos);
885 set_compound_graph_path_node(path, 0, member);
886 set_compound_ent_value_w_path(ent, val, path, pos);
890 set_array_entity_values(entity *ent, tarval **values, int num_vals) {
892 ir_graph *rem = current_ir_graph;
893 ir_type *arrtp = get_entity_type(ent);
895 ir_type *elttp = get_array_element_type(arrtp);
897 assert(is_Array_type(arrtp));
898 assert(get_array_n_dimensions(arrtp) == 1);
899 /* One bound is sufficient, the number of constant fields makes the
901 assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
902 assert(get_entity_variability(ent) != variability_uninitialized);
903 current_ir_graph = get_const_code_irg();
905 for (i = 0; i < num_vals; i++) {
906 val = new_Const_type(values[i], elttp);
907 add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
908 set_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0, i);
910 current_ir_graph = rem;
913 int get_compound_ent_value_offset_bits(entity *ent, int pos) {
914 compound_graph_path *path;
918 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
920 path = get_compound_ent_value_path(ent, pos);
921 path_len = get_compound_graph_path_length(path);
923 for (i = 0; i < path_len; ++i) {
924 entity *node = get_compound_graph_path_node(path, i);
925 ir_type *node_tp = get_entity_type(node);
926 ir_type *owner_tp = get_entity_owner(node);
927 if (is_Array_type(owner_tp)) {
928 int size = get_type_size_bits(node_tp);
929 int align = get_type_alignment_bits(node_tp);
933 assert(size % align == 0);
934 /* ansonsten aufrunden */
936 offset += size * get_compound_graph_path_array_index(path, i);
938 offset += get_entity_offset_bits(node);
944 int get_compound_ent_value_offset_bytes(entity *ent, int pos) {
945 int offset = get_compound_ent_value_offset_bits(ent, pos);
946 assert(offset % 8 == 0);
951 static void init_index(ir_type *arr) {
955 assert(get_array_n_dimensions(arr) == 1);
957 if (has_array_lower_bound(arr, dim))
958 init = get_array_lower_bound_int(arr, 0) -1;
960 init = get_array_upper_bound_int(arr, 0) +1;
962 set_entity_link(get_array_element_entity(arr), INT_TO_PTR(init));
966 static int get_next_index(entity *elem_ent) {
967 ir_type *arr = get_entity_owner(elem_ent);
971 assert(get_array_n_dimensions(arr) == 1);
973 if (has_array_lower_bound(arr, dim)) {
974 next = PTR_TO_INT(get_entity_link(elem_ent)) + 1;
975 if (has_array_upper_bound(arr, dim)) {
976 int upper = get_array_upper_bound_int(arr, dim);
977 if (next == upper) next = get_array_lower_bound_int(arr, dim);
980 next = PTR_TO_INT(get_entity_link(elem_ent)) - 1;
981 if (has_array_lower_bound(arr, dim)) {
982 int upper = get_array_upper_bound_int(arr, dim);
983 if (next == upper) next = get_array_upper_bound_int(arr, dim);
987 set_entity_link(elem_ent, INT_TO_PTR(next));
991 /* Compute the array indices in compound graph paths of initialized entities.
993 * All arrays must have fixed lower and upper bounds. One array can
994 * have an open bound. If there are several open bounds, we do
995 * nothing. There must be initializer elements for all array
996 * elements. Uses the link field in the array element entities. The
997 * array bounds must be representable as ints.
999 * (If the bounds are not representable as ints we have to represent
1000 * the indices as firm nodes. But still we must be able to
1001 * evaluate the index against the upper bound.)
1003 void compute_compound_ent_array_indicees(entity *ent) {
1004 ir_type *tp = get_entity_type(ent);
1006 entity *unknown_bound_entity = NULL;
1008 if (!is_compound_type(tp) ||
1009 (ent->variability == variability_uninitialized)) return ;
1011 n_vals = get_compound_ent_n_values(ent);
1012 if (n_vals == 0) return;
1014 /* We can not compute the indexes if there is more than one array
1015 with an unknown bound. For this remember the first entity that
1016 represents such an array. It could be ent. */
1017 if (is_Array_type(tp)) {
1020 assert(get_array_n_dimensions(tp) == 1 && "other not implemented");
1021 if (!has_array_lower_bound(tp, dim) || !has_array_upper_bound(tp, dim))
1022 unknown_bound_entity = ent;
1025 /* Initialize the entity links to lower bound -1 and test all path elements
1026 for known bounds. */
1027 for (i = 0; i < n_vals; ++i) {
1028 compound_graph_path *path = get_compound_ent_value_path(ent, i);
1029 int j, path_len = get_compound_graph_path_length(path);
1030 for (j = 0; j < path_len; ++j) {
1031 entity *node = get_compound_graph_path_node(path, j);
1032 ir_type *elem_tp = get_entity_type(node);
1034 if (is_Array_type(elem_tp)) {
1036 assert(get_array_n_dimensions(elem_tp) == 1 && "other not implemented");
1037 if (!has_array_lower_bound(elem_tp, dim) || !has_array_upper_bound(elem_tp, dim)) {
1038 if (!unknown_bound_entity) unknown_bound_entity = node;
1039 if (node != unknown_bound_entity) return;
1042 init_index(elem_tp);
1047 /* Finally compute the indexes ... */
1048 for (i = 0; i < n_vals; ++i) {
1049 compound_graph_path *path = get_compound_ent_value_path(ent, i);
1050 int j, path_len = get_compound_graph_path_length(path);
1051 for (j = 0; j < path_len; ++j) {
1052 entity *node = get_compound_graph_path_node(path, j);
1053 ir_type *owner_tp = get_entity_owner(node);
1054 if (is_Array_type(owner_tp))
1055 set_compound_graph_path_array_index (path, j, get_next_index(node));
1060 /** resize: double the allocated buffer */
1061 static int *resize (int *buf, int *size) {
1062 int new_size = *size * 2;
1063 int *new_buf = xcalloc(new_size, sizeof(new_buf[0]));
1064 memcpy(new_buf, buf, *size);
1070 /* We sort the elements by placing them at their bit offset in an
1071 array where each entry represents one bit called permutation. In
1072 fact, we do not place the values themselves, as we would have to
1073 copy two things, the value and the path. We only remember the
1074 position in the old order. Each value should have a distinct
1075 position in the permutation.
1077 A second iteration now permutes the actual elements into two
1079 void sort_compound_ent_values(entity *ent) {
1087 ir_node **my_values;
1088 compound_graph_path **my_paths;
1090 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
1092 tp = get_entity_type(ent);
1093 n_vals = get_compound_ent_n_values(ent);
1094 tp_size = get_type_size_bits(tp);
1096 if (!is_compound_type(tp) ||
1097 (ent->variability == variability_uninitialized) ||
1098 (get_type_state(tp) != layout_fixed) ||
1099 (n_vals == 0) ) return;
1101 /* estimated upper bound for size. Better: use flexible array ... */
1102 size = ((tp_size > (n_vals * 32)) ? tp_size : (n_vals * 32)) * 4;
1103 permutation = xcalloc(size, sizeof(permutation[0]));
1105 for (i = 0; i < n_vals; ++i) {
1106 int pos = get_compound_ent_value_offset_bits(ent, i);
1107 while (pos >= size) {
1108 permutation = resize(permutation, &size);
1111 assert(permutation[pos] == 0 && "two values with the same offset");
1112 permutation[pos] = i + 1; /* We initialized with 0, so we can not distinguish entry 0.
1113 So inc all entries by one. */
1114 //fprintf(stderr, "i: %d, pos: %d \n", i, pos);
1118 my_values = NEW_ARR_F(ir_node *, n_vals);
1119 my_paths = NEW_ARR_F(compound_graph_path *, n_vals);
1120 for (i = 0; i < size; ++i) {
1121 int pos = permutation[i];
1123 //fprintf(stderr, "pos: %d i: %d next %d \n", i, pos, next);
1124 assert(next < n_vals);
1125 pos--; /* We increased the pos by one */
1126 my_values[next] = get_compound_ent_value (ent, pos);
1127 my_paths [next] = get_compound_ent_value_path(ent, pos);
1133 DEL_ARR_F(ent->attr.cmpd_attr.values);
1134 ent->attr.cmpd_attr.values = my_values;
1135 DEL_ARR_F(ent->attr.cmpd_attr.val_paths);
1136 ent->attr.cmpd_attr.val_paths = my_paths;
1140 (get_entity_offset_bytes)(const entity *ent) {
1141 return _get_entity_offset_bytes(ent);
1145 (get_entity_offset_bits)(const entity *ent) {
1146 return _get_entity_offset_bits(ent);
1150 (set_entity_offset_bytes)(entity *ent, int offset) {
1151 _set_entity_offset_bytes(ent, offset);
1155 (set_entity_offset_bits)(entity *ent, int offset) {
1156 _set_entity_offset_bits(ent, offset);
1160 add_entity_overwrites(entity *ent, entity *overwritten) {
1162 ir_type *owner = get_entity_owner(ent);
1163 ir_type *ovw_ovner = get_entity_owner(overwritten);
1164 assert(is_Class_type(owner));
1165 assert(is_Class_type(ovw_ovner));
1166 assert(! is_class_final(ovw_ovner));
1168 ARR_APP1(entity *, ent->overwrites, overwritten);
1169 ARR_APP1(entity *, overwritten->overwrittenby, ent);
1173 get_entity_n_overwrites(entity *ent) {
1174 assert(is_Class_type(get_entity_owner(ent)));
1175 return (ARR_LEN(ent->overwrites));
1179 get_entity_overwrites_index(entity *ent, entity *overwritten) {
1181 assert(is_Class_type(get_entity_owner(ent)));
1182 for (i = 0; i < get_entity_n_overwrites(ent); i++)
1183 if (get_entity_overwrites(ent, i) == overwritten)
1189 get_entity_overwrites (entity *ent, int pos) {
1190 assert(is_Class_type(get_entity_owner(ent)));
1191 assert(pos < get_entity_n_overwrites(ent));
1192 return ent->overwrites[pos];
1196 set_entity_overwrites (entity *ent, int pos, entity *overwritten) {
1197 assert(is_Class_type(get_entity_owner(ent)));
1198 assert(pos < get_entity_n_overwrites(ent));
1199 ent->overwrites[pos] = overwritten;
1203 remove_entity_overwrites(entity *ent, entity *overwritten) {
1205 assert(is_Class_type(get_entity_owner(ent)));
1206 for (i = 0; i < (ARR_LEN (ent->overwrites)); i++)
1207 if (ent->overwrites[i] == overwritten) {
1208 for(; i < (ARR_LEN (ent->overwrites))-1; i++)
1209 ent->overwrites[i] = ent->overwrites[i+1];
1210 ARR_SETLEN(entity*, ent->overwrites, ARR_LEN(ent->overwrites) - 1);
1216 add_entity_overwrittenby (entity *ent, entity *overwrites) {
1217 add_entity_overwrites(overwrites, ent);
1221 get_entity_n_overwrittenby (entity *ent) {
1222 assert(is_Class_type(get_entity_owner(ent)));
1223 return (ARR_LEN (ent->overwrittenby));
1227 get_entity_overwrittenby_index(entity *ent, entity *overwrites) {
1229 assert(is_Class_type(get_entity_owner(ent)));
1230 for (i = 0; i < get_entity_n_overwrittenby(ent); i++)
1231 if (get_entity_overwrittenby(ent, i) == overwrites)
1237 get_entity_overwrittenby (entity *ent, int pos) {
1238 assert(is_Class_type(get_entity_owner(ent)));
1239 assert(pos < get_entity_n_overwrittenby(ent));
1240 return ent->overwrittenby[pos];
1244 set_entity_overwrittenby (entity *ent, int pos, entity *overwrites) {
1245 assert(is_Class_type(get_entity_owner(ent)));
1246 assert(pos < get_entity_n_overwrittenby(ent));
1247 ent->overwrittenby[pos] = overwrites;
1250 void remove_entity_overwrittenby(entity *ent, entity *overwrites) {
1252 assert(is_Class_type(get_entity_owner(ent)));
1253 for (i = 0; i < (ARR_LEN (ent->overwrittenby)); i++)
1254 if (ent->overwrittenby[i] == overwrites) {
1255 for(; i < (ARR_LEN (ent->overwrittenby))-1; i++)
1256 ent->overwrittenby[i] = ent->overwrittenby[i+1];
1257 ARR_SETLEN(entity*, ent->overwrittenby, ARR_LEN(ent->overwrittenby) - 1);
1262 /* A link to store intermediate information */
1264 (get_entity_link)(const entity *ent) {
1265 return _get_entity_link(ent);
1269 (set_entity_link)(entity *ent, void *l) {
1270 _set_entity_link(ent, l);
1274 (get_entity_irg)(const entity *ent) {
1275 return _get_entity_irg(ent);
1279 set_entity_irg(entity *ent, ir_graph *irg) {
1280 assert(is_method_entity(ent));
1281 /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
1282 * Methode selbst nicht mehr aufgerufen werden kann, die Entität
1283 * aber erhalten bleiben soll? Wandle die Entitaet in description oder
1286 assert((irg && ent->peculiarity == peculiarity_existent) ||
1287 (!irg && (ent->peculiarity == peculiarity_existent)
1288 && (ent -> visibility == visibility_external_allocated)) ||
1289 (!irg && ent->peculiarity == peculiarity_description) ||
1290 (!irg && ent->peculiarity == peculiarity_inherited));
1291 ent->attr.mtd_attr.irg = irg;
1294 unsigned get_entity_vtable_number(entity *ent) {
1295 assert(is_method_entity(ent));
1296 return ent->attr.mtd_attr.vtable_number;
1299 void set_entity_vtable_number(entity *ent, unsigned vtable_number) {
1300 assert(is_method_entity(ent));
1301 ent->attr.mtd_attr.vtable_number = vtable_number;
1304 /* Returns the section of a method. */
1305 ir_img_section get_method_img_section(const entity *ent) {
1306 assert(is_method_entity(ent));
1307 return ent->attr.mtd_attr.section;
1310 /* Sets the section of a method. */
1311 void set_method_img_section(entity *ent, ir_img_section section) {
1312 assert(is_method_entity(ent));
1313 ent->attr.mtd_attr.section = section;
1317 (is_entity)(const void *thing) {
1318 return _is_entity(thing);
1321 int is_atomic_entity(entity *ent) {
1322 ir_type *t = get_entity_type(ent);
1323 const tp_op *op = get_type_tpop(t);
1324 return (op == type_primitive || op == type_pointer ||
1325 op == type_enumeration || op == type_method);
1328 int is_compound_entity(entity *ent) {
1329 ir_type *t = get_entity_type(ent);
1330 const tp_op *op = get_type_tpop(t);
1331 return (op == type_class || op == type_struct ||
1332 op == type_array || op == type_union);
1335 int is_method_entity(entity *ent) {
1336 ir_type *t = get_entity_type(ent);
1337 return is_Method_type(t);
1341 * @todo not implemented!!! */
1342 int equal_entity(entity *ent1, entity *ent2) {
1343 fprintf(stderr, " calling unimplemented equal entity!!! \n");
1348 unsigned long (get_entity_visited)(entity *ent) {
1349 return _get_entity_visited(ent);
1352 void (set_entity_visited)(entity *ent, unsigned long num) {
1353 _set_entity_visited(ent, num);
1356 /* Sets visited field in entity to entity_visited. */
1357 void (mark_entity_visited)(entity *ent) {
1358 _mark_entity_visited(ent);
1361 int (entity_visited)(entity *ent) {
1362 return _entity_visited(ent);
1365 int (entity_not_visited)(entity *ent) {
1366 return _entity_not_visited(ent);
1369 /* Returns the mask of the additional entity properties. */
1370 unsigned get_entity_additional_properties(entity *ent) {
1373 assert(is_method_entity(ent));
1375 /* first check, if the graph has additional properties */
1376 irg = get_entity_irg(ent);
1379 return get_irg_additional_properties(irg);
1381 if (ent->attr.mtd_attr.irg_add_properties & mtp_property_inherited)
1382 return get_method_additional_properties(get_entity_type(ent));
1384 return ent->attr.mtd_attr.irg_add_properties;
1387 /* Sets the mask of the additional graph properties. */
1388 void set_entity_additional_properties(entity *ent, unsigned property_mask)
1392 assert(is_method_entity(ent));
1394 /* first check, if the graph exists */
1395 irg = get_entity_irg(ent);
1397 set_irg_additional_properties(irg, property_mask);
1399 /* do not allow to set the mtp_property_inherited flag or
1400 * the automatic inheritance of flags will not work */
1401 ent->attr.mtd_attr.irg_add_properties = property_mask & ~mtp_property_inherited;
1405 /* Sets one additional graph property. */
1406 void set_entity_additional_property(entity *ent, mtp_additional_property flag)
1410 assert(is_method_entity(ent));
1412 /* first check, if the graph exists */
1413 irg = get_entity_irg(ent);
1415 set_irg_additional_property(irg, flag);
1417 unsigned mask = ent->attr.mtd_attr.irg_add_properties;
1419 if (mask & mtp_property_inherited)
1420 mask = get_method_additional_properties(get_entity_type(ent));
1422 /* do not allow to set the mtp_property_inherited flag or
1423 * the automatic inheritance of flags will not work */
1424 ent->attr.mtd_attr.irg_add_properties = mask | (flag & ~mtp_property_inherited);
1428 /* Returns the class type that this type info entity represents or NULL
1429 if ent is no type info entity. */
1430 ir_type *(get_entity_repr_class)(const entity *ent) {
1431 return _get_entity_repr_class(ent);
1434 /* Initialize entity module. */
1435 void firm_init_entity(void)
1437 symconst_symbol sym;
1439 assert(firm_unknown_type && "Call init_type() before firm_init_entity()!");
1440 assert(!unknown_entity && "Call firm_init_entity() only once!");
1442 unknown_entity = new_rd_entity(NULL, firm_unknown_type, new_id_from_str(UNKNOWN_ENTITY_NAME), firm_unknown_type);
1443 set_entity_visibility(unknown_entity, visibility_external_allocated);
1444 set_entity_ld_ident(unknown_entity, get_entity_ident(unknown_entity));
1446 current_ir_graph = get_const_code_irg();
1447 sym.entity_p = unknown_entity;
1448 unknown_entity->value = new_SymConst(sym, symconst_addr_ent);