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 ir_entity *unknown_entity = NULL;
57 ir_entity *get_unknown_entity(void) { return unknown_entity; }
59 #define UNKNOWN_ENTITY_NAME "unknown_entity"
61 /*-----------------------------------------------------------------*/
63 /*-----------------------------------------------------------------*/
66 * Add an entity to it's already set owner type.
68 static INLINE void insert_entity_in_owner(ir_entity *ent) {
69 ir_type *owner = ent->owner;
70 switch (get_type_tpop_code(owner)) {
72 add_class_member(owner, ent);
75 add_struct_member(owner, ent);
78 add_union_member(owner, ent);
81 set_array_element_entity(owner, ent);
85 } /* insert_entity_in_owner */
88 * Creates a new entity. This entity is NOT inserted in the owner type.
90 * @param db debug info for this entity
91 * @param owner the owner type of the new entity
92 * @param name the name of the new entity
93 * @param type the type of the new entity
95 * @return the new created entity
97 static INLINE ir_entity *
98 new_rd_entity(dbg_info *db, ir_type *owner, ident *name, ir_type *type)
103 assert(!id_contains_char(name, ' ') && "entity name should not contain spaces");
105 res = xmalloc(sizeof(*res));
106 memset(res, 0, sizeof(*res));
108 res->kind = k_entity;
114 res->allocation = allocation_automatic;
115 res->visibility = visibility_local;
116 res->volatility = volatility_non_volatile;
117 res->stickyness = stickyness_unsticky;
118 res->peculiarity = peculiarity_existent;
120 res->compiler_gen = 0;
122 res->offset_bit_remainder = 0;
124 res->repr_class = NULL;
126 if (is_Method_type(type)) {
129 rem = current_ir_graph;
130 current_ir_graph = get_const_code_irg();
131 res->value = new_SymConst(sym, symconst_addr_ent);
132 current_ir_graph = rem;
133 res->allocation = allocation_static;
134 res->variability = variability_constant;
135 res->attr.mtd_attr.irg_add_properties = mtp_property_inherited;
136 res->attr.mtd_attr.vtable_number = VTABLE_NUM_NOT_SET;
137 res->attr.mtd_attr.param_access = NULL;
138 res->attr.mtd_attr.param_weight = NULL;
139 res->attr.mtd_attr.irg = NULL;
140 res->attr.mtd_attr.section = section_text;
141 } else if (is_compound_type(type)) {
142 res->variability = variability_uninitialized;
144 res->attr.cmpd_attr.values = NULL;
145 res->attr.cmpd_attr.val_paths = NULL;
147 res->variability = variability_uninitialized;
151 if (is_Class_type(owner)) {
152 res->overwrites = NEW_ARR_F(ir_entity *, 0);
153 res->overwrittenby = NEW_ARR_F(ir_entity *, 0);
155 res->overwrites = NULL;
156 res->overwrittenby = NULL;
160 res->nr = get_irp_new_node_nr();
161 #endif /* DEBUG_libfirm */
164 set_entity_dbg_info(res, db);
167 } /* new_rd_entity */
170 new_d_entity(ir_type *owner, ident *name, ir_type *type, dbg_info *db) {
173 assert(is_compound_type(owner));
174 res = new_rd_entity(db, owner, name, type);
175 /* Remember entity in it's owner. */
176 insert_entity_in_owner(res);
178 hook_new_entity(res);
183 new_entity(ir_type *owner, ident *name, ir_type *type) {
184 return new_d_entity(owner, name, type, NULL);
188 * Free entity attributes.
190 * @param ent the entity
192 static void free_entity_attrs(ir_entity *ent) {
194 if (get_type_tpop(get_entity_owner(ent)) == type_class) {
195 DEL_ARR_F(ent->overwrites); ent->overwrites = NULL;
196 DEL_ARR_F(ent->overwrittenby); ent->overwrittenby = NULL;
198 assert(ent->overwrites == NULL);
199 assert(ent->overwrittenby == NULL);
201 if (is_compound_entity(ent)) {
202 if (ent->attr.cmpd_attr.val_paths) {
203 for (i = 0; i < get_compound_ent_n_values(ent); i++)
204 if (ent->attr.cmpd_attr.val_paths[i]) {
205 /* free_compound_graph_path(ent->attr.cmpd_attr.val_paths[i]) ; * @@@ warum nich? */
206 /* Geht nich: wird mehrfach verwendet!!! ==> mehrfach frei gegeben. */
207 /* DEL_ARR_F(ent->attr.cmpd_attr.val_paths); */
209 ent->attr.cmpd_attr.val_paths = NULL;
211 /* if (ent->attr.cmpd_attr.values) DEL_ARR_F(ent->attr.cmpd_attr.values); *//* @@@ warum nich? */
212 ent->attr.cmpd_attr.values = NULL;
213 } else if (is_method_entity(ent)) {
214 if (ent->attr.mtd_attr.param_access) {
215 DEL_ARR_F(ent->attr.mtd_attr.param_access);
216 ent->attr.mtd_attr.param_access = NULL;
218 if (ent->attr.mtd_attr.param_weight) {
219 DEL_ARR_F(ent->attr.mtd_attr.param_weight);
220 ent->attr.mtd_attr.param_weight = NULL;
223 } /* free_entity_attrs */
226 copy_entity_own(ir_entity *old, ir_type *new_owner) {
228 assert(is_entity(old));
229 assert(is_compound_type(new_owner));
231 if (old->owner == new_owner) return old;
232 newe = xmalloc(sizeof(*newe));
233 memcpy(newe, old, sizeof(*newe));
234 newe->owner = new_owner;
235 if (is_Class_type(new_owner)) {
236 newe->overwrites = NEW_ARR_F(ir_entity *, 0);
237 newe->overwrittenby = NEW_ARR_F(ir_entity *, 0);
240 newe->nr = get_irp_new_node_nr();
243 insert_entity_in_owner(newe);
246 } /* copy_entity_own */
249 copy_entity_name(ir_entity *old, ident *new_name) {
251 assert(old && old->kind == k_entity);
253 if (old->name == new_name) return old;
254 newe = xmalloc(sizeof(*newe));
255 memcpy(newe, old, sizeof(*newe));
256 newe->name = new_name;
257 newe->ld_name = NULL;
258 if (is_Class_type(newe->owner)) {
259 newe->overwrites = DUP_ARR_F(ir_entity *, old->overwrites);
260 newe->overwrittenby = DUP_ARR_F(ir_entity *, old->overwrittenby);
263 newe->nr = get_irp_new_node_nr();
266 insert_entity_in_owner(newe);
269 } /* copy_entity_name */
273 free_entity(ir_entity *ent) {
274 assert(ent && ent->kind == k_entity);
275 free_entity_attrs(ent);
280 /* Outputs a unique number for this node */
282 get_entity_nr(const ir_entity *ent) {
283 assert(ent && ent->kind == k_entity);
287 return (long)PTR_TO_INT(ent);
289 } /* get_entity_nr */
292 (get_entity_name)(const ir_entity *ent) {
293 return _get_entity_name(ent);
294 } /* get_entity_name */
297 (get_entity_ident)(const ir_entity *ent) {
298 return _get_entity_ident(ent);
299 } /* get_entity_ident */
302 (set_entity_ident)(ir_entity *ent, ident *id) {
303 _set_entity_ident(ent, id);
304 } /* set_entity_ident */
307 (get_entity_owner)(ir_entity *ent) {
308 return _get_entity_owner(ent);
309 } /* get_entity_owner */
312 set_entity_owner(ir_entity *ent, ir_type *owner) {
313 assert(is_entity(ent));
314 assert(is_compound_type(owner));
316 } /* set_entity_owner */
319 (get_entity_ld_ident)(ir_entity *ent) {
320 return _get_entity_ld_ident(ent);
321 } /* get_entity_ld_ident */
324 (set_entity_ld_ident)(ir_entity *ent, ident *ld_ident) {
325 _set_entity_ld_ident(ent, ld_ident);
326 } /* set_entity_ld_ident */
329 (get_entity_ld_name)(ir_entity *ent) {
330 return _get_entity_ld_name(ent);
331 } /* get_entity_ld_name */
334 (get_entity_type)(ir_entity *ent) {
335 return _get_entity_type(ent);
336 } /* get_entity_type */
339 (set_entity_type)(ir_entity *ent, ir_type *type) {
340 _set_entity_type(ent, type);
341 } /* set_entity_type */
344 (get_entity_allocation)(const ir_entity *ent) {
345 return _get_entity_allocation(ent);
346 } /* get_entity_allocation */
349 (set_entity_allocation)(ir_entity *ent, ir_allocation al) {
350 _set_entity_allocation(ent, al);
351 } /* set_entity_allocation */
353 /* return the name of the visibility */
354 const char *get_allocation_name(ir_allocation all)
356 #define X(a) case a: return #a
358 X(allocation_automatic);
359 X(allocation_parameter);
360 X(allocation_dynamic);
361 X(allocation_static);
362 default: return "BAD VALUE";
365 } /* get_allocation_name */
369 (get_entity_visibility)(const ir_entity *ent) {
370 return _get_entity_visibility(ent);
371 } /* get_entity_visibility */
374 set_entity_visibility(ir_entity *ent, ir_visibility vis) {
375 assert(ent && ent->kind == k_entity);
376 if (vis != visibility_local)
377 assert((ent->allocation == allocation_static) ||
378 (ent->allocation == allocation_automatic));
379 /* @@@ Test that the owner type is not local, but how??
380 && get_class_visibility(get_entity_owner(ent)) != local));*/
381 ent->visibility = vis;
382 } /* set_entity_visibility */
384 /* return the name of the visibility */
385 const char *get_visibility_name(ir_visibility vis)
387 #define X(a) case a: return #a
390 X(visibility_external_visible);
391 X(visibility_external_allocated);
392 default: return "BAD VALUE";
395 } /* get_visibility_name */
398 (get_entity_variability)(const ir_entity *ent) {
399 return _get_entity_variability(ent);
400 } /* get_entity_variability */
403 set_entity_variability(ir_entity *ent, ir_variability var)
405 assert(ent && ent->kind == k_entity);
406 if (var == variability_part_constant)
407 assert(is_Class_type(ent->type) || is_Struct_type(ent->type));
409 if ((is_compound_type(ent->type)) &&
410 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
411 /* Allocate data structures for constant values */
412 ent->attr.cmpd_attr.values = NEW_ARR_F(ir_node *, 0);
413 ent->attr.cmpd_attr.val_paths = NEW_ARR_F(compound_graph_path *, 0);
415 if ((is_atomic_type(ent->type)) &&
416 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
417 /* Set default constant value. */
418 ent->value = new_rd_Unknown(get_const_code_irg(), get_type_mode(ent->type));
421 if ((is_compound_type(ent->type)) &&
422 (var == variability_uninitialized) && (ent->variability != variability_uninitialized)) {
423 /* Free data structures for constant values */
424 DEL_ARR_F(ent->attr.cmpd_attr.values); ent->attr.cmpd_attr.values = NULL;
425 DEL_ARR_F(ent->attr.cmpd_attr.val_paths); ent->attr.cmpd_attr.val_paths = NULL;
427 ent->variability = var;
428 } /* set_entity_variability */
430 /* return the name of the variability */
431 const char *get_variability_name(ir_variability var)
433 #define X(a) case a: return #a
435 X(variability_uninitialized);
436 X(variability_initialized);
437 X(variability_part_constant);
438 X(variability_constant);
439 default: return "BAD VALUE";
442 } /* get_variability_name */
445 (get_entity_volatility)(const ir_entity *ent) {
446 return _get_entity_volatility(ent);
447 } /* get_entity_volatility */
450 (set_entity_volatility)(ir_entity *ent, ir_volatility vol) {
451 _set_entity_volatility(ent, vol);
452 } /* set_entity_volatility */
454 /* return the name of the volatility */
455 const char *get_volatility_name(ir_volatility var)
457 #define X(a) case a: return #a
459 X(volatility_non_volatile);
460 X(volatility_is_volatile);
461 default: return "BAD VALUE";
464 } /* get_volatility_name */
467 (get_entity_peculiarity)(const ir_entity *ent) {
468 return _get_entity_peculiarity(ent);
469 } /* get_entity_peculiarity */
472 (set_entity_peculiarity)(ir_entity *ent, ir_peculiarity pec) {
473 _set_entity_peculiarity(ent, pec);
474 } /* set_entity_peculiarity */
476 /* Checks if an entity cannot be overridden anymore. */
477 int (get_entity_final)(const ir_entity *ent) {
478 return _get_entity_final(ent);
479 } /* get_entity_final */
481 /* Sets/resets the final flag of an entity. */
482 void (set_entity_final)(ir_entity *ent, int final) {
483 _set_entity_final(ent, final);
486 /* Checks if an entity is compiler generated */
487 int is_entity_compiler_generated(const ir_entity *ent) {
488 assert(is_entity(ent));
489 return ent->compiler_gen;
490 } /* is_entity_compiler_generated */
492 /* Sets/resets the compiler generated flag */
493 void set_entity_compiler_generated(ir_entity *ent, int flag) {
494 assert(is_entity(ent));
495 ent->compiler_gen = flag ? 1 : 0;
496 } /* set_entity_compiler_generated */
498 /* Get the entity's stickyness */
500 (get_entity_stickyness)(const ir_entity *ent) {
501 return _get_entity_stickyness(ent);
502 } /* get_entity_stickyness */
504 /* Set the entity's stickyness */
506 (set_entity_stickyness)(ir_entity *ent, ir_stickyness stickyness) {
507 _set_entity_stickyness(ent, stickyness);
508 } /* set_entity_stickyness */
510 /* Set has no effect for existent entities of type method. */
512 get_atomic_ent_value(ir_entity *ent)
514 assert(ent && is_atomic_entity(ent));
515 assert(ent->variability != variability_uninitialized);
516 return skip_Id(ent->value);
517 } /* get_atomic_ent_value */
520 set_atomic_ent_value(ir_entity *ent, ir_node *val) {
521 assert(is_atomic_entity(ent) && (ent->variability != variability_uninitialized));
522 if (is_Method_type(ent->type) && (ent->peculiarity == peculiarity_existent))
525 } /* set_atomic_ent_value */
527 /* Returns true if the the node is representable as code on
529 int is_irn_const_expression(ir_node *n) {
532 /* we are in danger iff an exception will arise. TODO: be more precisely,
533 * for instance Div. will NOT rise if divisor != 0
535 if (is_binop(n) && !is_fragile_op(n))
536 return is_irn_const_expression(get_binop_left(n)) && is_irn_const_expression(get_binop_right(n));
539 switch (get_irn_opcode(n)) {
546 return is_irn_const_expression(get_irn_n(n, 0));
551 } /* is_irn_const_expression */
554 * Copies a firm subgraph that complies to the restrictions for
555 * constant expressions to current_block in current_ir_graph.
557 ir_node *copy_const_value(dbg_info *dbg, ir_node *n) {
561 /* @@@ GL I think we should implement this using the routines from irgopt for
562 dead node elimination/inlineing. */
565 switch (get_irn_opcode(n)) {
567 nn = new_d_Const_type(dbg, m, get_Const_tarval(n), get_Const_type(n));
570 nn = new_d_SymConst_type(dbg, get_SymConst_symbol(n), get_SymConst_kind(n),
571 get_SymConst_value_type(n));
574 nn = new_d_Add(dbg, copy_const_value(dbg, get_Add_left(n)),
575 copy_const_value(dbg, get_Add_right(n)), m); break;
577 nn = new_d_Sub(dbg, copy_const_value(dbg, get_Sub_left(n)),
578 copy_const_value(dbg, get_Sub_right(n)), m); break;
580 nn = new_d_Mul(dbg, copy_const_value(dbg, get_Mul_left(n)),
581 copy_const_value(dbg, get_Mul_right(n)), m); break;
583 nn = new_d_And(dbg, copy_const_value(dbg, get_And_left(n)),
584 copy_const_value(dbg, get_And_right(n)), m); break;
586 nn = new_d_Or(dbg, copy_const_value(dbg, get_Or_left(n)),
587 copy_const_value(dbg, get_Or_right(n)), m); break;
589 nn = new_d_Eor(dbg, copy_const_value(dbg, get_Eor_left(n)),
590 copy_const_value(dbg, get_Eor_right(n)), m); break;
592 nn = new_d_Cast(dbg, copy_const_value(dbg, get_Cast_op(n)), get_Cast_type(n)); break;
594 nn = new_d_Conv(dbg, copy_const_value(dbg, get_Conv_op(n)), m); break;
596 nn = new_d_Unknown(m); break;
599 assert(0 && "opcode invalid or not implemented");
604 } /* copy_const_value */
606 /* Creates a new compound graph path. */
607 compound_graph_path *
608 new_compound_graph_path(ir_type *tp, int length) {
609 compound_graph_path *res;
611 assert(is_compound_type(tp));
614 res = xmalloc(sizeof(*res) + (length-1) * sizeof(res->list[0]));
615 memset(res, 0, sizeof(*res) + (length-1) * sizeof(res->list[0]));
616 res->kind = k_ir_compound_graph_path;
621 } /* new_compound_graph_path */
623 /* Frees an graph path object */
624 void free_compound_graph_path (compound_graph_path *gr) {
625 assert(gr && is_compound_graph_path(gr));
628 } /* free_compound_graph_path */
630 /* Returns non-zero if an object is a compound graph path */
631 int is_compound_graph_path(const void *thing) {
632 return (get_kind(thing) == k_ir_compound_graph_path);
633 } /* is_compound_graph_path */
635 /* Checks whether the path up to pos is correct. If the path contains a NULL,
636 * assumes the path is not complete and returns 'true'. */
637 int is_proper_compound_graph_path(compound_graph_path *gr, int pos) {
640 ir_type *owner = gr->tp;
642 for (i = 0; i <= pos; i++) {
643 node = get_compound_graph_path_node(gr, i);
645 /* Path not yet complete. */
647 if (get_entity_owner(node) != owner)
649 owner = get_entity_type(node);
651 if (pos == get_compound_graph_path_length(gr))
652 if (!is_atomic_type(owner))
655 } /* is_proper_compound_graph_path */
657 /* Returns the length of a graph path */
658 int get_compound_graph_path_length(const compound_graph_path *gr) {
659 assert(gr && is_compound_graph_path(gr));
661 } /* get_compound_graph_path_length */
664 get_compound_graph_path_node(const compound_graph_path *gr, int pos) {
665 assert(gr && is_compound_graph_path(gr));
666 assert(pos >= 0 && pos < gr->len);
667 return gr->list[pos].node;
668 } /* get_compound_graph_path_node */
671 set_compound_graph_path_node(compound_graph_path *gr, int pos, ir_entity *node) {
672 assert(gr && is_compound_graph_path(gr));
673 assert(pos >= 0 && pos < gr->len);
674 assert(is_entity(node));
675 gr->list[pos].node = node;
676 assert(is_proper_compound_graph_path(gr, pos));
677 } /* set_compound_graph_path_node */
680 get_compound_graph_path_array_index(const compound_graph_path *gr, int pos) {
681 assert(gr && is_compound_graph_path(gr));
682 assert(pos >= 0 && pos < gr->len);
683 return gr->list[pos].index;
684 } /* get_compound_graph_path_array_index */
687 set_compound_graph_path_array_index(compound_graph_path *gr, int pos, int index) {
688 assert(gr && is_compound_graph_path(gr));
689 assert(pos >= 0 && pos < gr->len);
690 gr->list[pos].index = index;
691 } /* set_compound_graph_path_array_index */
693 /* A value of a compound entity is a pair of value and the corresponding path to a member of
696 add_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path) {
697 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
698 assert(is_compound_graph_path(path));
699 ARR_APP1(ir_node *, ent->attr.cmpd_attr.values, val);
700 ARR_APP1(compound_graph_path *, ent->attr.cmpd_attr.val_paths, path);
701 } /* add_compound_ent_value_w_path */
704 set_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path, int pos) {
705 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
706 assert(is_compound_graph_path(path));
707 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
708 ent->attr.cmpd_attr.values[pos] = val;
709 ent->attr.cmpd_attr.val_paths[pos] = path;
710 } /* set_compound_ent_value_w_path */
713 get_compound_ent_n_values(ir_entity *ent) {
714 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
715 return ARR_LEN(ent->attr.cmpd_attr.values);
716 } /* get_compound_ent_n_values */
719 get_compound_ent_value(ir_entity *ent, int pos) {
720 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
721 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
722 return ent->attr.cmpd_attr.values[pos];
723 } /* get_compound_ent_value */
725 compound_graph_path *
726 get_compound_ent_value_path(ir_entity *ent, int pos) {
727 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
728 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.val_paths));
729 return ent->attr.cmpd_attr.val_paths[pos];
730 } /* get_compound_ent_value_path */
733 * Returns non-zero, if two compound_graph_pathes are equal
735 * @param path1 the first path
736 * @param visited_indices
737 * @param path2 the second path
739 static int equal_paths(compound_graph_path *path1, int *visited_indices, compound_graph_path *path2) {
741 int len1 = get_compound_graph_path_length(path1);
742 int len2 = get_compound_graph_path_length(path2);
744 if (len2 != len1) return 0;
746 for (i = 0; i < len1; i++) {
748 ir_entity *node1 = get_compound_graph_path_node(path1, i);
749 ir_entity *node2 = get_compound_graph_path_node(path2, i);
751 if (node1 != node2) return 0;
753 /* FIXME: Strange code. What is it good for? */
754 tp = get_entity_owner(node1);
755 if (is_Array_type(tp)) {
758 /* Compute the index of this node. */
759 assert(get_array_n_dimensions(tp) == 1 && "multidim not implemented");
761 low = get_array_lower_bound_int(tp, 0);
762 if (low + visited_indices[i] < get_compound_graph_path_array_index(path2, i)) {
763 visited_indices[i]++;
766 assert(low + visited_indices[i] == get_compound_graph_path_array_index(path2, i));
773 * Returns the position of a value with the given path.
774 * The path must contain array indices for all array element entities.
776 * @todo This implementation is very low and should be replaced when the new tree oriented
777 * value representation is finally implemented.
779 static int get_compound_ent_pos_by_path(ir_entity *ent, compound_graph_path *path) {
780 int i, n_paths = get_compound_ent_n_values(ent);
781 int *visited_indices;
782 int path_len = get_compound_graph_path_length(path);
784 NEW_ARR_A(int *, visited_indices, path_len);
785 memset(visited_indices, 0, sizeof(*visited_indices) * path_len);
786 for (i = 0; i < n_paths; i ++) {
787 if (equal_paths(get_compound_ent_value_path(ent, i), visited_indices, path))
794 printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
795 printf("Entity %s : ", get_entity_name(ent));
796 for (j = 0; j < get_compound_graph_path_length(path); ++j) {
797 ir_entity *node = get_compound_graph_path_node(path, j);
798 printf("%s", get_entity_name(node));
799 if (is_Array_type(get_entity_owner(node)))
800 printf("[%d]", get_compound_graph_path_array_index(path, j));
802 printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
806 assert(0 && "path not found");
808 } /* get_compound_ent_pos_by_path */
810 /* Returns a constant value given the access path.
811 * The path must contain array indices for all array element entities. */
812 ir_node *get_compound_ent_value_by_path(ir_entity *ent, compound_graph_path *path) {
813 return get_compound_ent_value(ent, get_compound_ent_pos_by_path(ent, path));
814 } /* get_compound_ent_value_by_path */
818 remove_compound_ent_value(ir_entity *ent, ir_entity *value_ent) {
820 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
821 for (i = 0; i < (ARR_LEN(ent->attr.cmpd_attr.val_paths)); ++i) {
822 compound_graph_path *path = ent->attr.cmpd_attr.val_paths[i];
823 if (path->list[path->len-1].node == value_ent) {
824 for (; i < (ARR_LEN(ent->attr.cmpd_attr.val_paths))-1; ++i) {
825 ent->attr.cmpd_attr.val_paths[i] = ent->attr.cmpd_attr.val_paths[i+1];
826 ent->attr.cmpd_attr.values[i] = ent->attr.cmpd_attr.values[i+1];
828 ARR_SETLEN(ir_entity*, ent->attr.cmpd_attr.val_paths, ARR_LEN(ent->attr.cmpd_attr.val_paths) - 1);
829 ARR_SETLEN(ir_node*, ent->attr.cmpd_attr.values, ARR_LEN(ent->attr.cmpd_attr.values) - 1);
833 } /* remove_compound_ent_value */
836 add_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member) {
837 compound_graph_path *path;
838 ir_type *owner_tp = get_entity_owner(member);
839 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
840 path = new_compound_graph_path(get_entity_type(ent), 1);
841 path->list[0].node = member;
842 if (is_Array_type(owner_tp)) {
846 assert(get_array_n_dimensions(owner_tp) == 1 && has_array_lower_bound(owner_tp, 0));
847 max = get_array_lower_bound_int(owner_tp, 0) -1;
848 for (i = 0, n = get_compound_ent_n_values(ent); i < n; ++i) {
849 int index = get_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0);
854 path->list[0].index = max + 1;
856 add_compound_ent_value_w_path(ent, val, path);
857 } /* add_compound_ent_value */
861 get_compound_ent_value_member(ir_entity *ent, int pos) {
862 compound_graph_path *path;
863 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
864 path = get_compound_ent_value_path(ent, pos);
866 return get_compound_graph_path_node(path, get_compound_graph_path_length(path)-1);
867 } /* get_compound_ent_value_member */
870 set_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member, int pos) {
871 compound_graph_path *path;
872 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
873 path = get_compound_ent_value_path(ent, pos);
874 set_compound_graph_path_node(path, 0, member);
875 set_compound_ent_value_w_path(ent, val, path, pos);
876 } /* set_compound_ent_value */
879 set_array_entity_values(ir_entity *ent, tarval **values, int num_vals) {
881 ir_graph *rem = current_ir_graph;
882 ir_type *arrtp = get_entity_type(ent);
884 ir_type *elttp = get_array_element_type(arrtp);
886 assert(is_Array_type(arrtp));
887 assert(get_array_n_dimensions(arrtp) == 1);
888 /* One bound is sufficient, the number of constant fields makes the
890 assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
891 assert(get_entity_variability(ent) != variability_uninitialized);
892 current_ir_graph = get_const_code_irg();
894 for (i = 0; i < num_vals; i++) {
895 val = new_Const_type(values[i], elttp);
896 add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
897 set_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0, i);
899 current_ir_graph = rem;
900 } /* set_array_entity_values */
902 /* Return the overall offset of value at position pos in bytes. */
903 int get_compound_ent_value_offset_bytes(ir_entity *ent, int pos) {
904 compound_graph_path *path;
908 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
910 path = get_compound_ent_value_path(ent, pos);
911 path_len = get_compound_graph_path_length(path);
913 for (i = 0; i < path_len; ++i) {
914 ir_entity *node = get_compound_graph_path_node(path, i);
915 ir_type *node_tp = get_entity_type(node);
916 ir_type *owner_tp = get_entity_owner(node);
918 if (is_Array_type(owner_tp)) {
919 int size = get_type_size_bits(node_tp);
920 int align = get_type_alignment_bits(node_tp);
921 if(size % align > 0) {
922 size += align - (size % align);
924 assert(size % 8 == 0);
926 offset += size * get_compound_graph_path_array_index(path, i);
928 offset += get_entity_offset(node);
933 } /* get_compound_ent_value_offset_bytes */
935 /* Return the offset in bits from the last byte address. */
936 int get_compound_ent_value_offset_bit_remainder(ir_entity *ent, int pos) {
937 compound_graph_path *path;
939 ir_entity *last_node;
941 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
943 path = get_compound_ent_value_path(ent, pos);
944 path_len = get_compound_graph_path_length(path);
945 last_node = get_compound_graph_path_node(path, path_len - 1);
947 return get_entity_offset_bits_remainder(last_node);
948 } /* get_compound_ent_value_offset_bit_remainder */
951 int n_elems; /**< number of elements the array can hold */
952 int current_elem; /**< current array index */
956 /* Compute the array indices in compound graph paths of initialized entities.
958 * All arrays must have fixed lower and upper bounds. One array can
959 * have an open bound. If there are several open bounds, we do
960 * nothing. There must be initializer elements for all array
961 * elements. Uses the link field in the array element entities. The
962 * array bounds must be representable as ints.
964 * WARNING: it is impossible to get this 100% right with the current
965 * design... (in array of structs you cant know when a struct is
966 * really finished and the next array element starts)
968 * (If the bounds are not representable as ints we have to represent
969 * the indices as firm nodes. But still we must be able to
970 * evaluate the index against the upper bound.)
972 int compute_compound_ent_array_indices(ir_entity *ent) {
973 ir_type *tp = get_entity_type(ent);
976 array_info *array_infos;
978 assert(is_compound_type(tp));
980 if (!is_compound_type(tp) ||
981 (ent->variability == variability_uninitialized))
984 n_vals = get_compound_ent_n_values(ent);
985 for(i = 0; i < n_vals; ++i) {
986 compound_graph_path *path = get_compound_ent_value_path(ent, i);
987 int len = get_compound_graph_path_length(path);
992 array_infos = alloca(max_len * sizeof(array_infos[0]));
993 memset(array_infos, 0, max_len * sizeof(array_infos[0]));
995 for(i = 0; i < n_vals; ++i) {
996 compound_graph_path *path = get_compound_ent_value_path(ent, i);
997 int path_len = get_compound_graph_path_length(path);
1000 ir_entity *prev_node = NULL;
1002 for(j = path_len-1; j >= 0; --j) {
1005 ir_entity *node = get_compound_graph_path_node(path, j);
1006 const ir_type *node_type = get_entity_type(node);
1007 array_info *info = &array_infos[j];
1009 if(is_atomic_entity(node)) {
1011 set_compound_graph_path_array_index(path, j, -1);
1014 } else if(is_compound_type(node_type) && !is_Array_type(node_type)) {
1015 int n_members = get_compound_n_members(node_type);
1016 ir_entity *last = get_compound_member(node_type, n_members - 1);
1017 if(needadd && last == prev_node) {
1022 set_compound_graph_path_array_index(path, j, -1);
1027 if(info->ent != node) {
1029 dims = get_array_n_dimensions(node_type);
1030 for(dim = 0; dim < dims; ++dim) {
1031 long lower_bound = 0;
1032 long upper_bound = -1;
1034 if(has_array_lower_bound(node_type, 0)) {
1035 lower_bound = get_array_lower_bound_int(node_type, 0);
1037 if(has_array_upper_bound(node_type, 0)) {
1038 upper_bound = get_array_upper_bound_int(node_type, 0);
1039 assert(upper_bound >= lower_bound);
1040 n_elems *= (upper_bound - lower_bound);
1042 assert(dim == dims-1);
1048 info->n_elems = n_elems;
1049 info->current_elem = 0;
1052 set_compound_graph_path_array_index(path, j, info->current_elem);
1055 info->current_elem++;
1056 if(info->current_elem >= info->n_elems) {
1058 info->current_elem = 0;
1069 } /* compute_compound_ent_array_indices */
1072 (get_entity_offset)(const ir_entity *ent) {
1073 return _get_entity_offset(ent);
1074 } /* get_entity_offset */
1077 (set_entity_offset)(ir_entity *ent, int offset) {
1078 _set_entity_offset(ent, offset);
1079 } /* set_entity_offset */
1082 (get_entity_offset_bits_remainder)(const ir_entity *ent) {
1083 return _get_entity_offset_bits_remainder(ent);
1084 } /* get_entity_offset_bits_remainder */
1087 (set_entity_offset_bits_remainder)(ir_entity *ent, unsigned char offset) {
1088 _set_entity_offset_bits_remainder(ent, offset);
1089 } /* set_entity_offset_bits_remainder */
1092 add_entity_overwrites(ir_entity *ent, ir_entity *overwritten) {
1094 ir_type *owner = get_entity_owner(ent);
1095 ir_type *ovw_ovner = get_entity_owner(overwritten);
1096 assert(is_Class_type(owner));
1097 assert(is_Class_type(ovw_ovner));
1098 assert(! is_class_final(ovw_ovner));
1100 ARR_APP1(ir_entity *, ent->overwrites, overwritten);
1101 ARR_APP1(ir_entity *, overwritten->overwrittenby, ent);
1102 } /* add_entity_overwrites */
1105 get_entity_n_overwrites(ir_entity *ent) {
1106 assert(is_Class_type(get_entity_owner(ent)));
1107 return (ARR_LEN(ent->overwrites));
1108 } /* get_entity_n_overwrites */
1111 get_entity_overwrites_index(ir_entity *ent, ir_entity *overwritten) {
1113 assert(is_Class_type(get_entity_owner(ent)));
1114 for (i = 0; i < get_entity_n_overwrites(ent); i++)
1115 if (get_entity_overwrites(ent, i) == overwritten)
1118 } /* get_entity_overwrites_index */
1121 get_entity_overwrites(ir_entity *ent, int pos) {
1122 assert(is_Class_type(get_entity_owner(ent)));
1123 assert(pos < get_entity_n_overwrites(ent));
1124 return ent->overwrites[pos];
1125 } /* get_entity_overwrites */
1128 set_entity_overwrites(ir_entity *ent, int pos, ir_entity *overwritten) {
1129 assert(is_Class_type(get_entity_owner(ent)));
1130 assert(pos < get_entity_n_overwrites(ent));
1131 ent->overwrites[pos] = overwritten;
1132 } /* set_entity_overwrites */
1135 remove_entity_overwrites(ir_entity *ent, ir_entity *overwritten) {
1137 assert(is_Class_type(get_entity_owner(ent)));
1138 for (i = 0; i < (ARR_LEN (ent->overwrites)); i++)
1139 if (ent->overwrites[i] == overwritten) {
1140 for(; i < (ARR_LEN (ent->overwrites))-1; i++)
1141 ent->overwrites[i] = ent->overwrites[i+1];
1142 ARR_SETLEN(ir_entity*, ent->overwrites, ARR_LEN(ent->overwrites) - 1);
1145 } /* remove_entity_overwrites */
1148 add_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites) {
1149 add_entity_overwrites(overwrites, ent);
1150 } /* add_entity_overwrittenby */
1153 get_entity_n_overwrittenby(ir_entity *ent) {
1154 assert(is_Class_type(get_entity_owner(ent)));
1155 return (ARR_LEN (ent->overwrittenby));
1156 } /* get_entity_n_overwrittenby */
1159 get_entity_overwrittenby_index(ir_entity *ent, ir_entity *overwrites) {
1161 assert(is_Class_type(get_entity_owner(ent)));
1162 for (i = 0; i < get_entity_n_overwrittenby(ent); i++)
1163 if (get_entity_overwrittenby(ent, i) == overwrites)
1166 } /* get_entity_overwrittenby_index */
1169 get_entity_overwrittenby(ir_entity *ent, int pos) {
1170 assert(is_Class_type(get_entity_owner(ent)));
1171 assert(pos < get_entity_n_overwrittenby(ent));
1172 return ent->overwrittenby[pos];
1173 } /* get_entity_overwrittenby */
1176 set_entity_overwrittenby(ir_entity *ent, int pos, ir_entity *overwrites) {
1177 assert(is_Class_type(get_entity_owner(ent)));
1178 assert(pos < get_entity_n_overwrittenby(ent));
1179 ent->overwrittenby[pos] = overwrites;
1180 } /* set_entity_overwrittenby */
1182 void remove_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites) {
1184 assert(is_Class_type(get_entity_owner(ent)));
1185 for (i = 0; i < (ARR_LEN (ent->overwrittenby)); i++)
1186 if (ent->overwrittenby[i] == overwrites) {
1187 for(; i < (ARR_LEN (ent->overwrittenby))-1; i++)
1188 ent->overwrittenby[i] = ent->overwrittenby[i+1];
1189 ARR_SETLEN(ir_entity*, ent->overwrittenby, ARR_LEN(ent->overwrittenby) - 1);
1192 } /* remove_entity_overwrittenby */
1194 /* A link to store intermediate information */
1196 (get_entity_link)(const ir_entity *ent) {
1197 return _get_entity_link(ent);
1198 } /* get_entity_link */
1201 (set_entity_link)(ir_entity *ent, void *l) {
1202 _set_entity_link(ent, l);
1203 } /* set_entity_link */
1206 (get_entity_irg)(const ir_entity *ent) {
1207 return _get_entity_irg(ent);
1208 } /* get_entity_irg */
1211 set_entity_irg(ir_entity *ent, ir_graph *irg) {
1212 assert(is_method_entity(ent));
1213 /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
1214 * Methode selbst nicht mehr aufgerufen werden kann, die Entität
1215 * aber erhalten bleiben soll? Wandle die Entitaet in description oder
1218 assert((irg && ent->peculiarity == peculiarity_existent) ||
1219 (!irg && (ent->peculiarity == peculiarity_existent)
1220 && (ent -> visibility == visibility_external_allocated)) ||
1221 (!irg && ent->peculiarity == peculiarity_description) ||
1222 (!irg && ent->peculiarity == peculiarity_inherited));
1223 ent->attr.mtd_attr.irg = irg;
1224 } /* set_entity_irg */
1226 unsigned get_entity_vtable_number(const ir_entity *ent) {
1227 assert(is_method_entity((ir_entity *)ent));
1228 return ent->attr.mtd_attr.vtable_number;
1229 } /* get_entity_vtable_number */
1231 void set_entity_vtable_number(ir_entity *ent, unsigned vtable_number) {
1232 assert(is_method_entity(ent));
1233 ent->attr.mtd_attr.vtable_number = vtable_number;
1234 } /* set_entity_vtable_number */
1236 /* Returns the section of a method. */
1237 ir_img_section get_method_img_section(const ir_entity *ent) {
1238 assert(is_method_entity((ir_entity *)ent));
1239 return ent->attr.mtd_attr.section;
1240 } /* get_method_img_section */
1242 /* Sets the section of a method. */
1243 void set_method_img_section(ir_entity *ent, ir_img_section section) {
1244 assert(is_method_entity(ent));
1245 ent->attr.mtd_attr.section = section;
1246 } /* set_method_img_section */
1249 (is_entity)(const void *thing) {
1250 return _is_entity(thing);
1253 int is_atomic_entity(ir_entity *ent) {
1254 ir_type *t = get_entity_type(ent);
1255 const tp_op *op = get_type_tpop(t);
1256 return (op == type_primitive || op == type_pointer ||
1257 op == type_enumeration || op == type_method);
1258 } /* is_atomic_entity */
1260 int is_compound_entity(ir_entity *ent) {
1261 ir_type *t = get_entity_type(ent);
1262 const tp_op *op = get_type_tpop(t);
1263 return (op == type_class || op == type_struct ||
1264 op == type_array || op == type_union);
1265 } /* is_compound_entity */
1267 int is_method_entity(ir_entity *ent) {
1268 ir_type *t = get_entity_type(ent);
1269 return is_Method_type(t);
1270 } /* is_method_entity */
1273 * @todo not implemented!!! */
1274 int equal_entity(ir_entity *ent1, ir_entity *ent2) {
1275 fprintf(stderr, " calling unimplemented equal entity!!! \n");
1277 } /* equal_entity */
1280 unsigned long (get_entity_visited)(ir_entity *ent) {
1281 return _get_entity_visited(ent);
1282 } /* get_entity_visited */
1284 void (set_entity_visited)(ir_entity *ent, unsigned long num) {
1285 _set_entity_visited(ent, num);
1286 } /* set_entity_visited */
1288 /* Sets visited field in ir_entity to entity_visited. */
1289 void (mark_entity_visited)(ir_entity *ent) {
1290 _mark_entity_visited(ent);
1291 } /* mark_entity_visited */
1293 int (entity_visited)(ir_entity *ent) {
1294 return _entity_visited(ent);
1295 } /* entity_visited */
1297 int (entity_not_visited)(ir_entity *ent) {
1298 return _entity_not_visited(ent);
1299 } /* entity_not_visited */
1301 /* Returns the mask of the additional entity properties. */
1302 unsigned get_entity_additional_properties(ir_entity *ent) {
1305 assert(is_method_entity(ent));
1307 /* first check, if the graph has additional properties */
1308 irg = get_entity_irg(ent);
1311 return get_irg_additional_properties(irg);
1313 if (ent->attr.mtd_attr.irg_add_properties & mtp_property_inherited)
1314 return get_method_additional_properties(get_entity_type(ent));
1316 return ent->attr.mtd_attr.irg_add_properties;
1317 } /* get_entity_additional_properties */
1319 /* Sets the mask of the additional graph properties. */
1320 void set_entity_additional_properties(ir_entity *ent, unsigned property_mask)
1324 assert(is_method_entity(ent));
1326 /* first check, if the graph exists */
1327 irg = get_entity_irg(ent);
1329 set_irg_additional_properties(irg, property_mask);
1331 /* do not allow to set the mtp_property_inherited flag or
1332 * the automatic inheritance of flags will not work */
1333 ent->attr.mtd_attr.irg_add_properties = property_mask & ~mtp_property_inherited;
1335 } /* set_entity_additional_properties */
1337 /* Sets one additional graph property. */
1338 void set_entity_additional_property(ir_entity *ent, mtp_additional_property flag)
1342 assert(is_method_entity(ent));
1344 /* first check, if the graph exists */
1345 irg = get_entity_irg(ent);
1347 set_irg_additional_property(irg, flag);
1349 unsigned mask = ent->attr.mtd_attr.irg_add_properties;
1351 if (mask & mtp_property_inherited)
1352 mask = get_method_additional_properties(get_entity_type(ent));
1354 /* do not allow to set the mtp_property_inherited flag or
1355 * the automatic inheritance of flags will not work */
1356 ent->attr.mtd_attr.irg_add_properties = mask | (flag & ~mtp_property_inherited);
1358 } /* set_entity_additional_property */
1360 /* Returns the class type that this type info entity represents or NULL
1361 if ent is no type info entity. */
1362 ir_type *(get_entity_repr_class)(const ir_entity *ent) {
1363 return _get_entity_repr_class(ent);
1364 } /* get_entity_repr_class */
1366 /* Initialize entity module. */
1367 void firm_init_entity(void)
1369 symconst_symbol sym;
1371 assert(firm_unknown_type && "Call init_type() before firm_init_entity()!");
1372 assert(!unknown_entity && "Call firm_init_entity() only once!");
1374 unknown_entity = new_rd_entity(NULL, firm_unknown_type, new_id_from_str(UNKNOWN_ENTITY_NAME), firm_unknown_type);
1375 set_entity_visibility(unknown_entity, visibility_external_allocated);
1376 set_entity_ld_ident(unknown_entity, get_entity_ident(unknown_entity));
1378 current_ir_graph = get_const_code_irg();
1379 sym.entity_p = unknown_entity;
1380 unknown_entity->value = new_SymConst(sym, symconst_addr_ent);
1381 } /* firm_init_entity */