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"
33 /* All this is needed to build the constant node for methods: */
34 # include "irprog_t.h"
39 # include "irdump.h" /* for output if errors occur. */
42 # include "callgraph.h" /* for dumping debug output */
44 /*******************************************************************/
46 /*******************************************************************/
48 entity *unknown_entity = NULL;
50 entity *get_unknown_entity(void) { return unknown_entity; }
52 #define UNKNOWN_ENTITY_NAME "unknown_entity"
54 /*-----------------------------------------------------------------*/
56 /*-----------------------------------------------------------------*/
58 static INLINE void insert_entity_in_owner (entity *ent) {
59 type *owner = ent->owner;
60 switch (get_type_tpop_code(owner)) {
62 add_class_member (owner, ent);
65 add_struct_member (owner, ent);
68 add_union_member (owner, ent);
71 set_array_element_entity(owner, ent);
78 * Creates a new entity. This entity is NOT inserted in the owner type.
80 * @param db debug info for this entity
81 * @param owner the owner type of the new entity
82 * @param name the name of the new entity
83 * @param type the type of the new entity
85 * @return the new created entity
87 static INLINE entity *
88 new_rd_entity (dbg_info *db, type *owner, ident *name, type *type)
93 assert(!id_contains_char(name, ' ') && "entity name should not contain spaces");
95 res = xmalloc(sizeof(*res));
96 memset(res, 0, sizeof(*res));
104 if (get_type_tpop(type) == type_method)
105 res->allocation = allocation_static;
107 res->allocation = allocation_automatic;
109 res->visibility = visibility_local;
111 if (is_Method_type(type)) {
114 res->variability = variability_constant;
115 rem = current_ir_graph;
116 current_ir_graph = get_const_code_irg();
117 res->value = new_SymConst(sym, symconst_addr_ent);
118 current_ir_graph = rem;
119 res->irg_add_properties = 0;
122 res->variability = variability_uninitialized;
125 res->val_paths = NULL;
128 res->volatility = volatility_non_volatile;
129 res->stickyness = stickyness_unsticky;
132 res->peculiarity = peculiarity_existent;
134 if (is_Class_type(owner)) {
135 res->overwrites = NEW_ARR_F(entity *, 0);
136 res->overwrittenby = NEW_ARR_F(entity *, 0);
138 res->overwrites = NULL;
139 res->overwrittenby = NULL;
144 res->nr = get_irp_new_node_nr();
145 #endif /* DEBUG_libfirm */
148 set_entity_dbg_info(res, db);
154 new_d_entity (type *owner, ident *name, type *type, dbg_info *db) {
157 assert_legal_owner_of_ent(owner);
158 res = new_rd_entity(db, owner, name, type);
159 /* Remember entity in it's owner. */
160 insert_entity_in_owner (res);
166 new_entity (type *owner, ident *name, type *type) {
167 return new_d_entity(owner, name, type, NULL);
173 static void free_entity_attrs(entity *ent) {
175 if (get_type_tpop(get_entity_owner(ent)) == type_class) {
176 DEL_ARR_F(ent->overwrites); ent->overwrites = NULL;
177 DEL_ARR_F(ent->overwrittenby); ent->overwrittenby = NULL;
179 assert(ent->overwrites == NULL);
180 assert(ent->overwrittenby == NULL);
182 /* if (ent->values) DEL_ARR_F(ent->values); *//* @@@ warum nich? */
183 if (ent->val_paths) {
184 if (is_compound_entity(ent))
185 for (i = 0; i < get_compound_ent_n_values(ent); i++)
186 if (ent->val_paths[i]) ;
187 /* free_compound_graph_path(ent->val_paths[i]) ; * @@@ warum nich? */
188 /* Geht nich: wird mehrfach verwendet!!! ==> mehrfach frei gegeben. */
189 /* DEL_ARR_F(ent->val_paths); */
191 ent->val_paths = NULL;
196 copy_entity_own (entity *old, type *new_owner) {
198 assert(old && old->kind == k_entity);
199 assert_legal_owner_of_ent(new_owner);
201 if (old->owner == new_owner) return old;
202 newe = xmalloc(sizeof(*newe));
203 memcpy (newe, old, sizeof(*newe));
204 newe->owner = new_owner;
205 if (is_Class_type(new_owner)) {
206 newe->overwrites = NEW_ARR_F(entity *, 0);
207 newe->overwrittenby = NEW_ARR_F(entity *, 0);
210 newe->nr = get_irp_new_node_nr();
213 insert_entity_in_owner (newe);
219 copy_entity_name (entity *old, ident *new_name) {
221 assert(old && old->kind == k_entity);
223 if (old->name == new_name) return old;
224 newe = xmalloc(sizeof(*newe));
225 memcpy(newe, old, sizeof(*newe));
226 newe->name = new_name;
227 newe->ld_name = NULL;
228 if (is_Class_type(newe->owner)) {
229 newe->overwrites = DUP_ARR_F(entity *, old->overwrites);
230 newe->overwrittenby = DUP_ARR_F(entity *, old->overwrittenby);
233 newe->nr = get_irp_new_node_nr();
236 insert_entity_in_owner (newe);
243 free_entity (entity *ent) {
244 assert(ent && ent->kind == k_entity);
245 free_entity_attrs(ent);
250 /* Outputs a unique number for this node */
252 get_entity_nr(entity *ent) {
253 assert(ent && ent->kind == k_entity);
262 (get_entity_name)(const entity *ent) {
263 return _get_entity_name(ent);
267 (get_entity_ident)(const entity *ent) {
268 return get_entity_ident(ent);
272 void set_entitye_ld_name (entity *, char *ld_name);
273 void set_entity_ld_ident (entity *, ident *ld_ident);
277 (get_entity_owner)(entity *ent) {
278 return _get_entity_owner(ent);
282 set_entity_owner (entity *ent, type *owner) {
283 assert(ent && ent->kind == k_entity);
284 assert_legal_owner_of_ent(owner);
288 void /* should this go into type.c? */
289 assert_legal_owner_of_ent(type *owner) {
290 assert(get_type_tpop_code(owner) == tpo_class ||
291 get_type_tpop_code(owner) == tpo_union ||
292 get_type_tpop_code(owner) == tpo_struct ||
293 get_type_tpop_code(owner) == tpo_array); /* Yes, array has an entity
294 -- to select fields! */
298 (get_entity_ld_ident)(entity *ent) {
299 return _get_entity_ld_ident(ent);
303 (set_entity_ld_ident)(entity *ent, ident *ld_ident) {
304 _set_entity_ld_ident(ent, ld_ident);
308 (get_entity_ld_name)(entity *ent) {
309 return _get_entity_ld_name(ent);
313 (get_entity_type)(entity *ent) {
314 return _get_entity_type(ent);
318 (set_entity_type)(entity *ent, type *type) {
319 _set_entity_type(ent, type);
323 (get_entity_allocation)(const entity *ent) {
324 return _get_entity_allocation(ent);
328 (set_entity_allocation)(entity *ent, ent_allocation al) {
329 _set_entity_allocation(ent, al);
332 /* return the name of the visibility */
333 const char *get_allocation_name(ent_allocation all)
335 #define X(a) case a: return #a
337 X(allocation_automatic);
338 X(allocation_parameter);
339 X(allocation_dynamic);
340 X(allocation_static);
341 default: return "BAD VALUE";
348 (get_entity_visibility)(const entity *ent) {
349 return _get_entity_visibility(ent);
353 set_entity_visibility (entity *ent, visibility vis) {
354 assert(ent && ent->kind == k_entity);
355 if (vis != visibility_local)
356 assert((ent->allocation == allocation_static) ||
357 (ent->allocation == allocation_automatic));
358 /* @@@ Test that the owner type is not local, but how??
359 && get_class_visibility(get_entity_owner(ent)) != local));*/
360 ent->visibility = vis;
363 /* return the name of the visibility */
364 const char *get_visibility_name(visibility vis)
366 #define X(a) case a: return #a
369 X(visibility_external_visible);
370 X(visibility_external_allocated);
371 default: return "BAD VALUE";
377 (get_entity_variability)(const entity *ent) {
378 return _get_entity_variability(ent);
382 set_entity_variability (entity *ent, ent_variability var)
384 assert(ent && ent->kind == k_entity);
385 if (var == variability_part_constant)
386 assert(is_Class_type(ent->type) || is_Struct_type(ent->type));
388 if ((is_compound_type(ent->type)) &&
389 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
390 /* Allocate datastructures for constant values */
391 ent->values = NEW_ARR_F(ir_node *, 0);
392 ent->val_paths = NEW_ARR_F(compound_graph_path *, 0);
394 if ((is_atomic_type(ent->type)) &&
395 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
396 /* Set default constant value. */
397 ent->value = new_rd_Unknown(get_const_code_irg(), get_type_mode(ent->type));
400 if ((is_compound_type(ent->type)) &&
401 (var == variability_uninitialized) && (ent->variability != variability_uninitialized)) {
402 /* Free datastructures for constant values */
403 DEL_ARR_F(ent->values); ent->values = NULL;
404 DEL_ARR_F(ent->val_paths); ent->val_paths = NULL;
406 ent->variability = var;
409 /* return the name of the variablity */
410 const char *get_variability_name(ent_variability var)
412 #define X(a) case a: return #a
414 X(variability_uninitialized);
415 X(variability_initialized);
416 X(variability_part_constant);
417 X(variability_constant);
418 default: return "BAD VALUE";
424 (get_entity_volatility)(const entity *ent) {
425 return _get_entity_volatility(ent);
429 (set_entity_volatility)(entity *ent, ent_volatility vol) {
430 _set_entity_volatility(ent, vol);
433 /* return the name of the volatility */
434 const char *get_volatility_name(ent_volatility var)
436 #define X(a) case a: return #a
438 X(volatility_non_volatile);
439 X(volatility_is_volatile);
440 default: return "BAD VALUE";
446 (get_entity_peculiarity)(const entity *ent) {
447 return _get_entity_peculiarity(ent);
451 (set_entity_peculiarity)(entity *ent, peculiarity pec) {
452 _set_entity_peculiarity(ent, pec);
455 /* return the name of the peculiarity */
456 const char *get_peculiarity_name(peculiarity var)
458 #define X(a) case a: return #a
460 X(peculiarity_description);
461 X(peculiarity_inherited);
462 X(peculiarity_existent);
463 default: return "BAD VALUE";
468 /* Get the entity's stickyness */
470 (get_entity_stickyness)(const entity *ent) {
471 return _get_entity_stickyness(ent);
474 /* Set the entity's stickyness */
476 (set_entity_stickyness)(entity *ent, ent_stickyness stickyness) {
477 _set_entity_stickyness(ent, stickyness);
480 /* Set has no effect for existent entities of type method. */
482 get_atomic_ent_value(entity *ent)
484 assert(ent && is_atomic_entity(ent));
485 assert(ent->variability != variability_uninitialized);
486 return skip_Id (ent->value);
490 set_atomic_ent_value(entity *ent, ir_node *val) {
491 assert(is_atomic_entity(ent) && (ent->variability != variability_uninitialized));
492 if (is_Method_type(ent->type) && (ent->peculiarity == peculiarity_existent))
497 /* Returns true if the the node is representable as code on
499 int is_irn_const_expression(ir_node *n) {
502 /* we are in danger iff an exception will arise. TODO: be more precisely,
503 * for instance Div. will NOT rise if divisor != 0
505 if (is_binop(n) && !is_fragile_op(n))
506 return is_irn_const_expression(get_binop_left(n)) && is_irn_const_expression(get_binop_right(n));
509 switch(get_irn_opcode(n)) {
516 return is_irn_const_expression(get_irn_n(n, 0));
525 ir_node *copy_const_value(ir_node *n) {
529 /* @@@ GL I think we should implement this using the routines from irgopt for
530 dead node elimination/inlineing. */
533 switch(get_irn_opcode(n)) {
535 nn = new_Const(m, get_Const_tarval(n)); set_Const_type(nn, get_Const_type(n));
536 //nn = new_rd_Const_type(get_irn_dbg_info(n), current_ir_graph, get_cur_block(),
537 // m, get_Const_tarval(n), get_Const_type(n));
540 nn = new_d_SymConst_type(NULL, get_SymConst_symbol(n), get_SymConst_kind(n),
541 get_SymConst_value_type(n));
544 nn = new_Add(copy_const_value(get_Add_left(n)),
545 copy_const_value(get_Add_right(n)), m); break;
547 nn = new_Sub(copy_const_value(get_Sub_left(n)),
548 copy_const_value(get_Sub_right(n)), m); break;
550 nn = new_Mul(copy_const_value(get_Mul_left(n)),
551 copy_const_value(get_Mul_right(n)), m); break;
553 nn = new_And(copy_const_value(get_And_left(n)),
554 copy_const_value(get_And_right(n)), m); break;
556 nn = new_Or(copy_const_value(get_Or_left(n)),
557 copy_const_value(get_Or_right(n)), m); break;
559 nn = new_Eor(copy_const_value(get_Eor_left(n)),
560 copy_const_value(get_Eor_right(n)), m); break;
562 nn = new_Cast(copy_const_value(get_Cast_op(n)), get_Cast_type(n)); break;
564 nn = new_Conv(copy_const_value(get_Conv_op(n)), m); break;
566 nn = new_Unknown(m); break;
569 assert(0 && "opcode invalid or not implemented");
576 /* Creates a new compound graph path. */
577 compound_graph_path *
578 new_compound_graph_path(type *tp, int length) {
579 compound_graph_path *res;
581 assert(is_type(tp) && is_compound_type(tp));
584 res = xmalloc(sizeof(*res) + (length-1) * sizeof(res->list[0]));
585 memset(res, 0, sizeof(*res) + (length-1) * sizeof(res->list[0]));
586 res->kind = k_ir_compound_graph_path;
593 /* Frees an graph path object */
594 void free_compound_graph_path (compound_graph_path *gr) {
595 assert(gr && is_compound_graph_path(gr));
600 /* Returns non-zero if an object is a compound graph path */
601 int is_compound_graph_path(void *thing) {
602 return (get_kind(thing) == k_ir_compound_graph_path);
605 /* Checks whether the path up to pos is correct. If the path contains a NULL,
606 * assumes the path is not complete and returns 'true'. */
607 int is_proper_compound_graph_path(compound_graph_path *gr, int pos) {
610 type *owner = gr->tp;
612 for (i = 0; i <= pos; i++) {
613 node = get_compound_graph_path_node(gr, i);
615 /* Path not yet complete. */
617 if (get_entity_owner(node) != owner)
619 owner = get_entity_type(node);
621 if (pos == get_compound_graph_path_length(gr))
622 if (!is_atomic_type(owner))
627 /* Returns the length of a graph path */
628 int get_compound_graph_path_length(compound_graph_path *gr) {
629 assert(gr && is_compound_graph_path(gr));
634 get_compound_graph_path_node(compound_graph_path *gr, int pos) {
635 assert(gr && is_compound_graph_path(gr));
636 assert(pos >= 0 && pos < gr->len);
637 return gr->list[pos].node;
641 set_compound_graph_path_node(compound_graph_path *gr, int pos, entity *node) {
642 assert(gr && is_compound_graph_path(gr));
643 assert(pos >= 0 && pos < gr->len);
644 assert(is_entity(node));
645 gr->list[pos].node = node;
646 assert(is_proper_compound_graph_path(gr, pos));
650 get_compound_graph_path_array_index(compound_graph_path *gr, int pos) {
651 assert(gr && is_compound_graph_path(gr));
652 assert(pos >= 0 && pos < gr->len);
653 return gr->list[pos].index;
657 set_compound_graph_path_array_index(compound_graph_path *gr, int pos, int index) {
658 assert(gr && is_compound_graph_path(gr));
659 assert(pos >= 0 && pos < gr->len);
660 gr->list[pos].index = index;
663 /* A value of a compound entity is a pair of value and the corresponding path to a member of
666 add_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path) {
667 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
668 ARR_APP1 (ir_node *, ent->values, val);
669 ARR_APP1 (compound_graph_path *, ent->val_paths, path);
673 set_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path, int pos) {
674 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
675 ent->values[pos] = val;
676 ent->val_paths[pos] = path;
680 get_compound_ent_n_values(entity *ent) {
681 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
682 return (ARR_LEN (ent->values));
686 get_compound_ent_value(entity *ent, int pos) {
687 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
688 return ent->values[pos];
691 compound_graph_path *
692 get_compound_ent_value_path(entity *ent, int pos) {
693 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
694 return ent->val_paths[pos];
698 * Returns non-zero, if two compound_graph_pathes are equal
700 static int equal_paths(compound_graph_path *path1, int *visited_indicees, compound_graph_path *path2) {
702 int len1 = get_compound_graph_path_length(path1);
703 int len2 = get_compound_graph_path_length(path2);
705 if (len2 > len1) return false;
707 for (i = 0; i < len1; i++) {
709 entity *node1 = get_compound_graph_path_node(path1, i);
710 entity *node2 = get_compound_graph_path_node(path2, i);
712 if (node1 != node2) return false;
714 tp = get_entity_owner(node1);
715 if (is_Array_type(tp)) {
718 /* Compute the index of this node. */
719 assert(get_array_n_dimensions(tp) == 1 && "multidim not implemented");
721 low = get_array_lower_bound_int(tp, 0);
722 if (low + visited_indicees[i] < get_compound_graph_path_array_index(path2, i)) {
723 visited_indicees[i]++;
727 assert(low + visited_indicees[i] == get_compound_graph_path_array_index(path2, i));
733 /* Returns the position of a value with the given path.
734 * The path must contain array indicees for all array element entities. */
735 int get_compound_ent_pos_by_path(entity *ent, compound_graph_path *path) {
736 int i, n_paths = get_compound_ent_n_values(ent);
737 int *visited_indicees = (int *)xcalloc(get_compound_graph_path_length(path), sizeof(int));
738 for (i = 0; i < n_paths; i ++) {
739 if (equal_paths(get_compound_ent_value_path(ent, i), visited_indicees, path))
746 printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
747 printf("Entity %s : ", get_entity_name(ent));
748 for (j = 0; j < get_compound_graph_path_length(path); ++j) {
749 entity *node = get_compound_graph_path_node(path, j);
750 printf("%s", get_entity_name(node));
751 if (is_Array_type(get_entity_owner(node)))
752 printf("[%d]", get_compound_graph_path_array_index(path, j));
754 printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
758 assert(0 && "path not found");
762 /* Returns a constant value given the access path.
763 * The path must contain array indicees for all array element entities. */
764 ir_node *get_compound_ent_value_by_path(entity *ent, compound_graph_path *path) {
765 return get_compound_ent_value(ent, get_compound_ent_pos_by_path(ent, path));
770 remove_compound_ent_value(entity *ent, entity *value_ent) {
772 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
773 for (i = 0; i < (ARR_LEN (ent->val_paths)); i++) {
774 compound_graph_path *path = ent->val_paths[i];
775 if (path->list[path->len-1].node == value_ent) {
776 for(; i < (ARR_LEN (ent->val_paths))-1; i++) {
777 ent->val_paths[i] = ent->val_paths[i+1];
778 ent->values[i] = ent->values[i+1];
780 ARR_SETLEN(entity*, ent->val_paths, ARR_LEN(ent->val_paths) - 1);
781 ARR_SETLEN(ir_node*, ent->values, ARR_LEN(ent->values) - 1);
788 add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
789 compound_graph_path *path;
790 type *owner_tp = get_entity_owner(member);
791 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
792 path = new_compound_graph_path(get_entity_type(ent), 1);
793 path->list[0].node = member;
794 if (is_Array_type(owner_tp)) {
798 assert(get_array_n_dimensions(owner_tp) == 1 && has_array_lower_bound(owner_tp, 0));
799 max = get_array_lower_bound_int(owner_tp, 0) -1;
800 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
801 int index = get_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0);
806 path->list[0].index = max + 1;
808 add_compound_ent_value_w_path(ent, val, path);
811 /* Copies the firm subgraph referenced by val to const_code_irg and adds
812 the node as constant initialization to ent.
813 The subgraph may not contain control flow operations.
815 copy_and_add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
816 ir_graph *rem = current_ir_graph;
818 assert(get_entity_variability(ent) != variability_uninitialized);
819 current_ir_graph = get_const_code_irg();
821 val = copy_const_value(val);
822 add_compound_ent_value(ent, val, member);
823 current_ir_graph = rem;
826 /* Copies the value i of the entity to current_block in current_ir_graph.
828 copy_compound_ent_value(entity *ent, int pos) {
829 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
830 return copy_const_value(ent->values[pos+1]);
834 get_compound_ent_value_member(entity *ent, int pos) {
835 compound_graph_path *path;
836 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
837 path = get_compound_ent_value_path(ent, pos);
839 return get_compound_graph_path_node(path, get_compound_graph_path_length(path)-1);
843 set_compound_ent_value(entity *ent, ir_node *val, entity *member, int pos) {
844 compound_graph_path *path;
845 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
846 path = get_compound_ent_value_path(ent, pos);
847 set_compound_graph_path_node(path, 0, member);
848 set_compound_ent_value_w_path(ent, val, path, pos);
852 set_array_entity_values(entity *ent, tarval **values, int num_vals) {
854 ir_graph *rem = current_ir_graph;
855 type *arrtp = get_entity_type(ent);
857 type *elttp = get_array_element_type(arrtp);
859 assert(is_Array_type(arrtp));
860 assert(get_array_n_dimensions(arrtp) == 1);
861 /* One bound is sufficient, the number of constant fields makes the
863 assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
864 assert(get_entity_variability(ent) != variability_uninitialized);
865 current_ir_graph = get_const_code_irg();
867 for (i = 0; i < num_vals; i++) {
868 val = new_Const_type(values[i], elttp);
869 add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
870 set_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0, i);
872 current_ir_graph = rem;
875 int get_compound_ent_value_offset_bits(entity *ent, int pos) {
876 compound_graph_path *path;
880 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
882 path = get_compound_ent_value_path(ent, pos);
883 path_len = get_compound_graph_path_length(path);
885 for (i = 0; i < path_len; ++i) {
886 entity *node = get_compound_graph_path_node(path, i);
887 type *node_tp = get_entity_type(node);
888 type *owner_tp = get_entity_owner(node);
889 if (is_Array_type(owner_tp)) {
890 int size = get_type_size_bits(node_tp);
891 int align = get_type_alignment_bits(node_tp);
895 assert(size % align == 0);
896 /* ansonsten aufrunden */
898 offset += size * get_compound_graph_path_array_index(path, i);
900 offset += get_entity_offset_bits(node);
906 int get_compound_ent_value_offset_bytes(entity *ent, int pos) {
907 int offset = get_compound_ent_value_offset_bits(ent, pos);
908 assert(offset % 8 == 0);
913 static void init_index(type *arr) {
917 assert(get_array_n_dimensions(arr) == 1);
919 if (has_array_lower_bound(arr, dim))
920 init = get_array_lower_bound_int(arr, 0) -1;
922 init = get_array_upper_bound_int(arr, 0) +1;
924 set_entity_link(get_array_element_entity(arr), INT_TO_PTR(init));
928 static int get_next_index(entity *elem_ent) {
929 type *arr = get_entity_owner(elem_ent);
933 assert(get_array_n_dimensions(arr) == 1);
935 if (has_array_lower_bound(arr, dim)) {
936 next = PTR_TO_INT(get_entity_link(elem_ent)) + 1;
937 if (has_array_upper_bound(arr, dim)) {
938 int upper = get_array_upper_bound_int(arr, dim);
939 if (next == upper) next = get_array_lower_bound_int(arr, dim);
942 next = PTR_TO_INT(get_entity_link(elem_ent)) - 1;
943 if (has_array_lower_bound(arr, dim)) {
944 int upper = get_array_upper_bound_int(arr, dim);
945 if (next == upper) next = get_array_upper_bound_int(arr, dim);
949 set_entity_link(elem_ent, INT_TO_PTR(next));
953 /* Compute the array indices in compound graph paths of initialized entities.
955 * All arrays must have fixed lower and upper bounds. One array can
956 * have an open bound. If there are several open bounds, we do
957 * nothing. There must be initializer elements for all array
958 * elements. Uses the link field in the array element entities. The
959 * array bounds must be representable as ints.
961 * (If the bounds are not representable as ints we have to represent
962 * the indices as firm nodes. But still we must be able to
963 * evaluate the index against the upper bound.)
965 void compute_compound_ent_array_indicees(entity *ent) {
966 type *tp = get_entity_type(ent);
968 entity *unknown_bound_entity = NULL;
970 if (!is_compound_type(tp) ||
971 (ent->variability == variability_uninitialized)) return ;
973 n_vals = get_compound_ent_n_values(ent);
974 if (n_vals == 0) return;
976 /* We can not compute the indexes if there is more than one array
977 with an unknown bound. For this remember the first entity that
978 represents such an array. It could be ent. */
979 if (is_Array_type(tp)) {
982 assert(get_array_n_dimensions(tp) == 1 && "other not implemented");
983 if (!has_array_lower_bound(tp, dim) || !has_array_upper_bound(tp, dim))
984 unknown_bound_entity = ent;
987 /* Initialize the entity links to lower bound -1 and test all path elements
989 for (i = 0; i < n_vals; ++i) {
990 compound_graph_path *path = get_compound_ent_value_path(ent, i);
991 int j, path_len = get_compound_graph_path_length(path);
992 for (j = 0; j < path_len; ++j) {
993 entity *node = get_compound_graph_path_node(path, j);
994 type *elem_tp = get_entity_type(node);
996 if (is_Array_type(elem_tp)) {
998 assert(get_array_n_dimensions(elem_tp) == 1 && "other not implemented");
999 if (!has_array_lower_bound(elem_tp, dim) || !has_array_upper_bound(elem_tp, dim)) {
1000 if (!unknown_bound_entity) unknown_bound_entity = node;
1001 if (node != unknown_bound_entity) return;
1004 init_index(elem_tp);
1009 /* Finally compute the indexes ... */
1010 for (i = 0; i < n_vals; ++i) {
1011 compound_graph_path *path = get_compound_ent_value_path(ent, i);
1012 int j, path_len = get_compound_graph_path_length(path);
1013 for (j = 0; j < path_len; ++j) {
1014 entity *node = get_compound_graph_path_node(path, j);
1015 type *owner_tp = get_entity_owner(node);
1016 if (is_Array_type(owner_tp))
1017 set_compound_graph_path_array_index (path, j, get_next_index(node));
1022 /** resize: double the allocated buffer */
1023 static int *resize (int *buf, int *size) {
1024 int new_size = *size * 2;
1025 int *new_buf = xcalloc(new_size, sizeof(new_buf[0]));
1026 memcpy(new_buf, buf, *size);
1032 /* We sort the elements by placing them at their bit offset in an
1033 array where each entry represents one bit called permutation. In
1034 fact, we do not place the values themselves, as we would have to
1035 copy two things, the value and the path. We only remember the
1036 position in the old order. Each value should have a distinct
1037 position in the permutation.
1039 A second iteration now permutes the actual elements into two
1041 void sort_compound_ent_values(entity *ent) {
1049 ir_node **my_values;
1050 compound_graph_path **my_paths;
1052 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
1054 tp = get_entity_type(ent);
1055 n_vals = get_compound_ent_n_values(ent);
1056 tp_size = get_type_size_bits(tp);
1058 if (!is_compound_type(tp) ||
1059 (ent->variability == variability_uninitialized) ||
1060 (get_type_state(tp) != layout_fixed) ||
1061 (n_vals == 0) ) return;
1063 /* estimated upper bound for size. Better: use flexible array ... */
1064 size = ((tp_size > (n_vals * 32)) ? tp_size : (n_vals * 32)) * 4;
1065 permutation = xcalloc(size, sizeof(permutation[0]));
1067 for (i = 0; i < n_vals; ++i) {
1068 int pos = get_compound_ent_value_offset_bits(ent, i);
1069 while (pos >= size) {
1070 permutation = resize(permutation, &size);
1073 assert(permutation[pos] == 0 && "two values with the same offset");
1074 permutation[pos] = i + 1; /* We initialized with 0, so we can not distinguish entry 0.
1075 So inc all entries by one. */
1076 //fprintf(stderr, "i: %d, pos: %d \n", i, pos);
1080 my_values = NEW_ARR_F(ir_node *, n_vals);
1081 my_paths = NEW_ARR_F(compound_graph_path *, n_vals);
1082 for (i = 0; i < size; ++i) {
1083 int pos = permutation[i];
1085 //fprintf(stderr, "pos: %d i: %d next %d \n", i, pos, next);
1086 assert(next < n_vals);
1087 pos--; /* We increased the pos by one */
1088 my_values[next] = get_compound_ent_value (ent, pos);
1089 my_paths [next] = get_compound_ent_value_path(ent, pos);
1095 DEL_ARR_F(ent->values);
1096 ent->values = my_values;
1097 DEL_ARR_F(ent->val_paths);
1098 ent->val_paths = my_paths;
1102 (get_entity_offset_bytes)(const entity *ent) {
1103 return _get_entity_offset_bytes(ent);
1107 (get_entity_offset_bits)(const entity *ent) {
1108 return _get_entity_offset_bits(ent);
1112 (set_entity_offset_bytes)(entity *ent, int offset) {
1113 _set_entity_offset_bytes(ent, offset);
1117 (set_entity_offset_bits)(entity *ent, int offset) {
1118 _set_entity_offset_bits(ent, offset);
1122 add_entity_overwrites(entity *ent, entity *overwritten) {
1123 assert(ent && is_Class_type(get_entity_owner(ent)));
1124 ARR_APP1(entity *, ent->overwrites, overwritten);
1125 ARR_APP1(entity *, overwritten->overwrittenby, ent);
1129 get_entity_n_overwrites(entity *ent) {
1130 assert(ent && is_Class_type(get_entity_owner(ent)));
1131 return (ARR_LEN(ent->overwrites));
1135 get_entity_overwrites_index(entity *ent, entity *overwritten) {
1137 assert(ent && is_Class_type(get_entity_owner(ent)));
1138 for (i = 0; i < get_entity_n_overwrites(ent); i++)
1139 if (get_entity_overwrites(ent, i) == overwritten)
1145 get_entity_overwrites (entity *ent, int pos) {
1146 assert(ent && is_Class_type(get_entity_owner(ent)));
1147 assert(pos < get_entity_n_overwrites(ent));
1148 return ent->overwrites[pos];
1152 set_entity_overwrites (entity *ent, int pos, entity *overwritten) {
1153 assert(ent && is_Class_type(get_entity_owner(ent)));
1154 assert(pos < get_entity_n_overwrites(ent));
1155 ent->overwrites[pos] = overwritten;
1159 remove_entity_overwrites(entity *ent, entity *overwritten) {
1161 assert(ent && is_Class_type(get_entity_owner(ent)));
1162 for (i = 0; i < (ARR_LEN (ent->overwrites)); i++)
1163 if (ent->overwrites[i] == overwritten) {
1164 for(; i < (ARR_LEN (ent->overwrites))-1; i++)
1165 ent->overwrites[i] = ent->overwrites[i+1];
1166 ARR_SETLEN(entity*, ent->overwrites, ARR_LEN(ent->overwrites) - 1);
1172 add_entity_overwrittenby (entity *ent, entity *overwrites) {
1173 assert(ent && is_Class_type(get_entity_owner(ent)));
1174 add_entity_overwrites(overwrites, ent);
1178 get_entity_n_overwrittenby (entity *ent) {
1179 assert(ent && is_Class_type(get_entity_owner(ent)));
1180 return (ARR_LEN (ent->overwrittenby));
1184 get_entity_overwrittenby_index(entity *ent, entity *overwrites) {
1186 assert(ent && is_Class_type(get_entity_owner(ent)));
1187 for (i = 0; i < get_entity_n_overwrittenby(ent); i++)
1188 if (get_entity_overwrittenby(ent, i) == overwrites)
1194 get_entity_overwrittenby (entity *ent, int pos) {
1195 assert(ent && is_Class_type(get_entity_owner(ent)));
1196 assert(pos < get_entity_n_overwrittenby(ent));
1197 return ent->overwrittenby[pos];
1201 set_entity_overwrittenby (entity *ent, int pos, entity *overwrites) {
1202 assert(ent && is_Class_type(get_entity_owner(ent)));
1203 assert(pos < get_entity_n_overwrittenby(ent));
1204 ent->overwrittenby[pos] = overwrites;
1207 void remove_entity_overwrittenby(entity *ent, entity *overwrites) {
1209 assert(ent && is_Class_type(get_entity_owner(ent)));
1210 for (i = 0; i < (ARR_LEN (ent->overwrittenby)); i++)
1211 if (ent->overwrittenby[i] == overwrites) {
1212 for(; i < (ARR_LEN (ent->overwrittenby))-1; i++)
1213 ent->overwrittenby[i] = ent->overwrittenby[i+1];
1214 ARR_SETLEN(entity*, ent->overwrittenby, ARR_LEN(ent->overwrittenby) - 1);
1219 /* A link to store intermediate information */
1221 (get_entity_link)(const entity *ent) {
1222 return _get_entity_link(ent);
1226 (set_entity_link)(entity *ent, void *l) {
1227 _set_entity_link(ent, l);
1231 (get_entity_irg)(const entity *ent) {
1232 return _get_entity_irg(ent);
1236 set_entity_irg(entity *ent, ir_graph *irg) {
1237 assert(ent && is_Method_type(get_entity_type(ent)));
1238 /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
1239 * Methode selbst nicht mehr aufgerufen werden kann, die Entität
1240 * aber erhalten bleiben soll? Wandle die Entitaet in description oder
1243 assert((irg && ent->peculiarity == peculiarity_existent) ||
1244 (!irg && (ent->peculiarity == peculiarity_existent)
1245 && (ent -> visibility == visibility_external_allocated)) ||
1246 (!irg && ent->peculiarity == peculiarity_description) ||
1247 (!irg && ent->peculiarity == peculiarity_inherited));
1252 (is_entity)(const void *thing) {
1253 return _is_entity(thing);
1256 int is_atomic_entity(entity *ent) {
1257 type* t = get_entity_type(ent);
1258 assert(ent && ent->kind == k_entity);
1259 return (is_Primitive_type(t) || is_Pointer_type(t) ||
1260 is_Enumeration_type(t) || is_Method_type(t));
1263 int is_compound_entity(entity *ent) {
1264 type* t = get_entity_type(ent);
1265 assert(ent && ent->kind == k_entity);
1266 return (is_Class_type(t) || is_Struct_type(t) ||
1267 is_Array_type(t) || is_Union_type(t));
1271 * @todo not implemented!!! */
1272 bool equal_entity(entity *ent1, entity *ent2) {
1273 fprintf(stderr, " calling unimplemented equal entity!!! \n");
1278 unsigned long (get_entity_visited)(entity *ent) {
1279 return _get_entity_visited(ent);
1282 void (set_entity_visited)(entity *ent, unsigned long num) {
1283 _set_entity_visited(ent, num);
1286 /* Sets visited field in entity to entity_visited. */
1287 void (mark_entity_visited)(entity *ent) {
1288 _mark_entity_visited(ent);
1291 int (entity_visited)(entity *ent) {
1292 return _entity_visited(ent);
1295 int (entity_not_visited)(entity *ent) {
1296 return _entity_not_visited(ent);
1299 unsigned (get_entity_additional_properties)(const entity *ent) {
1300 return _get_entity_additional_properties(ent);
1303 void (set_entity_additional_properties)(entity *ent, unsigned property_mask) {
1304 _set_entity_additional_properties(ent, property_mask);
1307 void (set_entity_additional_property)(entity *ent, unsigned flag) {
1308 _set_entity_additional_property(ent, (irg_additional_property)flag);
1311 /* Returns the calling convention of an entities graph. */
1312 unsigned (get_entity_calling_convention)(const entity *ent) {
1313 return _get_entity_calling_convention(ent);
1316 /* Sets the calling convention of an entities graph. */
1317 void (set_entity_calling_convention)(entity *ent, unsigned cc_mask) {
1318 _set_entity_calling_convention(ent, cc_mask);
1321 void firm_init_entity(void)
1323 symconst_symbol sym;
1325 assert(firm_unknown_type && "Call init_type() before firm_init_entity()!");
1326 assert(!unknown_entity && "Call firm_init_entity() only once!");
1327 unknown_entity = new_rd_entity(NULL, firm_unknown_type, new_id_from_str(UNKNOWN_ENTITY_NAME), firm_unknown_type);
1328 set_entity_visibility(unknown_entity, visibility_external_allocated);
1329 set_entity_ld_ident(unknown_entity, get_entity_ident(unknown_entity));
1331 sym.entity_p = unknown_entity;
1332 current_ir_graph = get_const_code_irg();
1333 unknown_entity->value = new_SymConst(sym, symconst_addr_ent);