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-2007 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
26 #include "firm_common_t.h"
37 /* All this is needed to build the constant node for methods: */
41 #include "irdump.h" /* for output if errors occur. */
43 #include "callgraph.h" /* for dumping debug output */
45 /*******************************************************************/
47 /*******************************************************************/
49 ir_entity *unknown_entity = NULL;
51 ir_entity *get_unknown_entity(void) { return unknown_entity; }
53 #define UNKNOWN_ENTITY_NAME "unknown_entity"
55 /*-----------------------------------------------------------------*/
57 /*-----------------------------------------------------------------*/
60 * Add an entity to it's already set owner type.
62 static INLINE void insert_entity_in_owner(ir_entity *ent) {
63 ir_type *owner = ent->owner;
64 switch (get_type_tpop_code(owner)) {
66 add_class_member(owner, ent);
69 add_struct_member(owner, ent);
72 add_union_member(owner, ent);
75 set_array_element_entity(owner, ent);
79 } /* insert_entity_in_owner */
82 * Creates a new entity. This entity is NOT inserted in the owner type.
84 * @param db debug info for this entity
85 * @param owner the owner type of the new entity
86 * @param name the name of the new entity
87 * @param type the type of the new entity
89 * @return the new created entity
91 static INLINE ir_entity *
92 new_rd_entity(dbg_info *db, ir_type *owner, ident *name, ir_type *type)
97 assert(!id_contains_char(name, ' ') && "entity name should not contain spaces");
99 res = xmalloc(sizeof(*res));
100 memset(res, 0, sizeof(*res));
102 res->kind = k_entity;
108 res->allocation = allocation_automatic;
109 res->visibility = visibility_local;
110 res->volatility = volatility_non_volatile;
111 res->stickyness = stickyness_unsticky;
112 res->peculiarity = peculiarity_existent;
113 res->address_taken = ir_address_taken_unknown;
115 res->compiler_gen = 0;
117 res->offset_bit_remainder = 0;
119 res->repr_class = NULL;
121 if (is_Method_type(type)) {
124 rem = current_ir_graph;
125 current_ir_graph = get_const_code_irg();
126 res->value = new_SymConst(sym, symconst_addr_ent);
127 current_ir_graph = rem;
128 res->allocation = allocation_static;
129 res->variability = variability_constant;
130 res->attr.mtd_attr.irg_add_properties = mtp_property_inherited;
131 res->attr.mtd_attr.vtable_number = VTABLE_NUM_NOT_SET;
132 res->attr.mtd_attr.param_access = NULL;
133 res->attr.mtd_attr.param_weight = NULL;
134 res->attr.mtd_attr.irg = NULL;
135 res->attr.mtd_attr.section = section_text;
136 } else if (is_compound_type(type)) {
137 res->variability = variability_uninitialized;
139 res->attr.cmpd_attr.values = NULL;
140 res->attr.cmpd_attr.val_paths = NULL;
142 res->variability = variability_uninitialized;
146 if (is_Class_type(owner)) {
147 res->overwrites = NEW_ARR_F(ir_entity *, 0);
148 res->overwrittenby = NEW_ARR_F(ir_entity *, 0);
150 res->overwrites = NULL;
151 res->overwrittenby = NULL;
155 res->nr = get_irp_new_node_nr();
156 #endif /* DEBUG_libfirm */
159 set_entity_dbg_info(res, db);
162 } /* new_rd_entity */
165 new_d_entity(ir_type *owner, ident *name, ir_type *type, dbg_info *db) {
168 assert(is_compound_type(owner));
169 res = new_rd_entity(db, owner, name, type);
170 /* Remember entity in it's owner. */
171 insert_entity_in_owner(res);
173 hook_new_entity(res);
178 new_entity(ir_type *owner, ident *name, ir_type *type) {
179 return new_d_entity(owner, name, type, NULL);
183 * Free entity attributes.
185 * @param ent the entity
187 static void free_entity_attrs(ir_entity *ent) {
189 if (get_type_tpop(get_entity_owner(ent)) == type_class) {
190 DEL_ARR_F(ent->overwrites); ent->overwrites = NULL;
191 DEL_ARR_F(ent->overwrittenby); ent->overwrittenby = NULL;
193 assert(ent->overwrites == NULL);
194 assert(ent->overwrittenby == NULL);
196 if (is_compound_entity(ent)) {
197 if (ent->attr.cmpd_attr.val_paths) {
198 for (i = 0; i < get_compound_ent_n_values(ent); i++)
199 if (ent->attr.cmpd_attr.val_paths[i]) {
200 /* free_compound_graph_path(ent->attr.cmpd_attr.val_paths[i]) ; * @@@ warum nich? */
201 /* Geht nich: wird mehrfach verwendet!!! ==> mehrfach frei gegeben. */
202 /* DEL_ARR_F(ent->attr.cmpd_attr.val_paths); */
204 ent->attr.cmpd_attr.val_paths = NULL;
206 /* if (ent->attr.cmpd_attr.values) DEL_ARR_F(ent->attr.cmpd_attr.values); *//* @@@ warum nich? */
207 ent->attr.cmpd_attr.values = NULL;
208 } else if (is_method_entity(ent)) {
209 if (ent->attr.mtd_attr.param_access) {
210 DEL_ARR_F(ent->attr.mtd_attr.param_access);
211 ent->attr.mtd_attr.param_access = NULL;
213 if (ent->attr.mtd_attr.param_weight) {
214 DEL_ARR_F(ent->attr.mtd_attr.param_weight);
215 ent->attr.mtd_attr.param_weight = NULL;
218 } /* free_entity_attrs */
221 copy_entity_own(ir_entity *old, ir_type *new_owner) {
223 assert(is_entity(old));
224 assert(is_compound_type(new_owner));
226 if (old->owner == new_owner) return old;
227 newe = xmalloc(sizeof(*newe));
228 memcpy(newe, old, sizeof(*newe));
229 newe->owner = new_owner;
230 if (is_Class_type(new_owner)) {
231 newe->overwrites = NEW_ARR_F(ir_entity *, 0);
232 newe->overwrittenby = NEW_ARR_F(ir_entity *, 0);
235 newe->nr = get_irp_new_node_nr();
238 insert_entity_in_owner(newe);
241 } /* copy_entity_own */
244 copy_entity_name(ir_entity *old, ident *new_name) {
246 assert(old && old->kind == k_entity);
248 if (old->name == new_name) return old;
249 newe = xmalloc(sizeof(*newe));
250 memcpy(newe, old, sizeof(*newe));
251 newe->name = new_name;
252 newe->ld_name = NULL;
253 if (is_Class_type(newe->owner)) {
254 newe->overwrites = DUP_ARR_F(ir_entity *, old->overwrites);
255 newe->overwrittenby = DUP_ARR_F(ir_entity *, old->overwrittenby);
258 newe->nr = get_irp_new_node_nr();
261 insert_entity_in_owner(newe);
264 } /* copy_entity_name */
268 free_entity(ir_entity *ent) {
269 assert(ent && ent->kind == k_entity);
270 free_entity_attrs(ent);
275 /* Outputs a unique number for this node */
277 get_entity_nr(const ir_entity *ent) {
278 assert(ent && ent->kind == k_entity);
282 return (long)PTR_TO_INT(ent);
284 } /* get_entity_nr */
287 (get_entity_name)(const ir_entity *ent) {
288 return _get_entity_name(ent);
289 } /* get_entity_name */
292 (get_entity_ident)(const ir_entity *ent) {
293 return _get_entity_ident(ent);
294 } /* get_entity_ident */
297 (set_entity_ident)(ir_entity *ent, ident *id) {
298 _set_entity_ident(ent, id);
299 } /* set_entity_ident */
302 (get_entity_owner)(ir_entity *ent) {
303 return _get_entity_owner(ent);
304 } /* get_entity_owner */
307 set_entity_owner(ir_entity *ent, ir_type *owner) {
308 assert(is_entity(ent));
309 assert(is_compound_type(owner));
311 } /* set_entity_owner */
314 (get_entity_ld_ident)(ir_entity *ent) {
315 return _get_entity_ld_ident(ent);
316 } /* get_entity_ld_ident */
319 (set_entity_ld_ident)(ir_entity *ent, ident *ld_ident) {
320 _set_entity_ld_ident(ent, ld_ident);
321 } /* set_entity_ld_ident */
324 (get_entity_ld_name)(ir_entity *ent) {
325 return _get_entity_ld_name(ent);
326 } /* get_entity_ld_name */
329 (get_entity_type)(ir_entity *ent) {
330 return _get_entity_type(ent);
331 } /* get_entity_type */
334 (set_entity_type)(ir_entity *ent, ir_type *type) {
335 _set_entity_type(ent, type);
336 } /* set_entity_type */
339 (get_entity_allocation)(const ir_entity *ent) {
340 return _get_entity_allocation(ent);
341 } /* get_entity_allocation */
344 (set_entity_allocation)(ir_entity *ent, ir_allocation al) {
345 _set_entity_allocation(ent, al);
346 } /* set_entity_allocation */
348 /* return the name of the visibility */
349 const char *get_allocation_name(ir_allocation all)
351 #define X(a) case a: return #a
353 X(allocation_automatic);
354 X(allocation_parameter);
355 X(allocation_dynamic);
356 X(allocation_static);
357 default: return "BAD VALUE";
360 } /* get_allocation_name */
363 (get_entity_visibility)(const ir_entity *ent) {
364 return _get_entity_visibility(ent);
365 } /* get_entity_visibility */
368 set_entity_visibility(ir_entity *ent, ir_visibility vis) {
369 assert(ent && ent->kind == k_entity);
370 if (vis != visibility_local)
371 assert((ent->allocation == allocation_static) ||
372 (ent->allocation == allocation_automatic));
373 /* @@@ Test that the owner type is not local, but how??
374 && get_class_visibility(get_entity_owner(ent)) != local));*/
375 ent->visibility = vis;
376 } /* set_entity_visibility */
378 /* return the name of the visibility */
379 const char *get_visibility_name(ir_visibility vis)
381 #define X(a) case a: return #a
384 X(visibility_external_visible);
385 X(visibility_external_allocated);
386 default: return "BAD VALUE";
389 } /* get_visibility_name */
392 (get_entity_variability)(const ir_entity *ent) {
393 return _get_entity_variability(ent);
394 } /* get_entity_variability */
397 set_entity_variability(ir_entity *ent, ir_variability var)
399 assert(ent && ent->kind == k_entity);
400 if (var == variability_part_constant)
401 assert(is_Class_type(ent->type) || is_Struct_type(ent->type));
403 if ((is_compound_type(ent->type)) &&
404 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
405 /* Allocate data structures for constant values */
406 ent->attr.cmpd_attr.values = NEW_ARR_F(ir_node *, 0);
407 ent->attr.cmpd_attr.val_paths = NEW_ARR_F(compound_graph_path *, 0);
409 if ((is_atomic_type(ent->type)) &&
410 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
411 /* Set default constant value. */
412 ent->value = new_rd_Unknown(get_const_code_irg(), get_type_mode(ent->type));
415 if ((is_compound_type(ent->type)) &&
416 (var == variability_uninitialized) && (ent->variability != variability_uninitialized)) {
417 /* Free data structures for constant values */
418 DEL_ARR_F(ent->attr.cmpd_attr.values); ent->attr.cmpd_attr.values = NULL;
419 DEL_ARR_F(ent->attr.cmpd_attr.val_paths); ent->attr.cmpd_attr.val_paths = NULL;
421 ent->variability = var;
422 } /* set_entity_variability */
424 /* return the name of the variability */
425 const char *get_variability_name(ir_variability var)
427 #define X(a) case a: return #a
429 X(variability_uninitialized);
430 X(variability_initialized);
431 X(variability_part_constant);
432 X(variability_constant);
433 default: return "BAD VALUE";
436 } /* get_variability_name */
439 (get_entity_volatility)(const ir_entity *ent) {
440 return _get_entity_volatility(ent);
441 } /* get_entity_volatility */
444 (set_entity_volatility)(ir_entity *ent, ir_volatility vol) {
445 _set_entity_volatility(ent, vol);
446 } /* set_entity_volatility */
448 /* return the name of the volatility */
449 const char *get_volatility_name(ir_volatility var)
451 #define X(a) case a: return #a
453 X(volatility_non_volatile);
454 X(volatility_is_volatile);
455 default: return "BAD VALUE";
458 } /* get_volatility_name */
461 (get_entity_peculiarity)(const ir_entity *ent) {
462 return _get_entity_peculiarity(ent);
463 } /* get_entity_peculiarity */
466 (set_entity_peculiarity)(ir_entity *ent, ir_peculiarity pec) {
467 _set_entity_peculiarity(ent, pec);
468 } /* set_entity_peculiarity */
470 /* Checks if an entity cannot be overridden anymore. */
471 int (is_entity_final)(const ir_entity *ent) {
472 return _is_entity_final(ent);
473 } /* is_entity_final */
475 /* Sets/resets the final flag of an entity. */
476 void (set_entity_final)(ir_entity *ent, int final) {
477 _set_entity_final(ent, final);
478 } /* set_entity_final */
480 /* Checks if an entity is compiler generated */
481 int (is_entity_compiler_generated)(const ir_entity *ent) {
482 return _is_entity_compiler_generated(ent);
483 } /* is_entity_compiler_generated */
485 /* Sets/resets the compiler generated flag */
486 void (set_entity_compiler_generated)(ir_entity *ent, int flag) {
487 _set_entity_compiler_generated(ent, flag);
488 } /* set_entity_compiler_generated */
490 /* Checks if the address of an entity was taken. */
491 ir_address_taken_state (get_entity_address_taken)(const ir_entity *ent) {
492 return _get_entity_address_taken(ent);
493 } /* is_entity_address_taken */
495 /* Sets/resets the address taken flag. */
496 void (set_entity_address_taken)(ir_entity *ent, ir_address_taken_state flag) {
497 _set_entity_address_taken(ent, flag);
498 } /* set_entity_address_taken */
500 /* Return the name of the address_taken state. */
501 const char *get_address_taken_state_name(ir_address_taken_state state) {
502 #define X(a) case a: return #a
504 X(ir_address_not_taken);
505 X(ir_address_taken_unknown);
507 default: return "BAD VALUE";
510 } /* get_address_taken_state_name */
512 /* Get the entity's stickyness */
514 (get_entity_stickyness)(const ir_entity *ent) {
515 return _get_entity_stickyness(ent);
516 } /* get_entity_stickyness */
518 /* Set the entity's stickyness */
520 (set_entity_stickyness)(ir_entity *ent, ir_stickyness stickyness) {
521 _set_entity_stickyness(ent, stickyness);
522 } /* set_entity_stickyness */
524 /* Set has no effect for existent entities of type method. */
526 get_atomic_ent_value(ir_entity *ent)
528 assert(ent && is_atomic_entity(ent));
529 assert(ent->variability != variability_uninitialized);
530 return skip_Id(ent->value);
531 } /* get_atomic_ent_value */
534 set_atomic_ent_value(ir_entity *ent, ir_node *val) {
535 assert(is_atomic_entity(ent) && (ent->variability != variability_uninitialized));
536 if (is_Method_type(ent->type) && (ent->peculiarity == peculiarity_existent))
539 } /* set_atomic_ent_value */
541 /* Returns true if the the node is representable as code on
543 int is_irn_const_expression(ir_node *n) {
546 /* we are in danger iff an exception will arise. TODO: be more precisely,
547 * for instance Div. will NOT rise if divisor != 0
549 if (is_binop(n) && !is_fragile_op(n))
550 return is_irn_const_expression(get_binop_left(n)) && is_irn_const_expression(get_binop_right(n));
553 switch (get_irn_opcode(n)) {
560 return is_irn_const_expression(get_irn_n(n, 0));
565 } /* is_irn_const_expression */
568 * Copies a firm subgraph that complies to the restrictions for
569 * constant expressions to current_block in current_ir_graph.
571 ir_node *copy_const_value(dbg_info *dbg, ir_node *n) {
575 /* @@@ GL I think we should implement this using the routines from irgopt for
576 dead node elimination/inlineing. */
579 switch (get_irn_opcode(n)) {
581 nn = new_d_Const_type(dbg, m, get_Const_tarval(n), get_Const_type(n));
584 nn = new_d_SymConst_type(dbg, get_SymConst_symbol(n), get_SymConst_kind(n),
585 get_SymConst_value_type(n));
588 nn = new_d_Add(dbg, copy_const_value(dbg, get_Add_left(n)),
589 copy_const_value(dbg, get_Add_right(n)), m); break;
591 nn = new_d_Sub(dbg, copy_const_value(dbg, get_Sub_left(n)),
592 copy_const_value(dbg, get_Sub_right(n)), m); break;
594 nn = new_d_Mul(dbg, copy_const_value(dbg, get_Mul_left(n)),
595 copy_const_value(dbg, get_Mul_right(n)), m); break;
597 nn = new_d_And(dbg, copy_const_value(dbg, get_And_left(n)),
598 copy_const_value(dbg, get_And_right(n)), m); break;
600 nn = new_d_Or(dbg, copy_const_value(dbg, get_Or_left(n)),
601 copy_const_value(dbg, get_Or_right(n)), m); break;
603 nn = new_d_Eor(dbg, copy_const_value(dbg, get_Eor_left(n)),
604 copy_const_value(dbg, get_Eor_right(n)), m); break;
606 nn = new_d_Cast(dbg, copy_const_value(dbg, get_Cast_op(n)), get_Cast_type(n)); break;
608 nn = new_d_Conv(dbg, copy_const_value(dbg, get_Conv_op(n)), m); break;
610 nn = new_d_Unknown(m); break;
613 assert(0 && "opcode invalid or not implemented");
618 } /* copy_const_value */
620 /* Creates a new compound graph path. */
621 compound_graph_path *
622 new_compound_graph_path(ir_type *tp, int length) {
623 compound_graph_path *res;
625 assert(is_compound_type(tp));
628 res = xmalloc(sizeof(*res) + (length-1) * sizeof(res->list[0]));
629 memset(res, 0, sizeof(*res) + (length-1) * sizeof(res->list[0]));
630 res->kind = k_ir_compound_graph_path;
635 } /* new_compound_graph_path */
637 /* Frees an graph path object */
638 void free_compound_graph_path (compound_graph_path *gr) {
639 assert(gr && is_compound_graph_path(gr));
642 } /* free_compound_graph_path */
644 /* Returns non-zero if an object is a compound graph path */
645 int is_compound_graph_path(const void *thing) {
646 return (get_kind(thing) == k_ir_compound_graph_path);
647 } /* is_compound_graph_path */
649 /* Checks whether the path up to pos is correct. If the path contains a NULL,
650 * assumes the path is not complete and returns 'true'. */
651 int is_proper_compound_graph_path(compound_graph_path *gr, int pos) {
654 ir_type *owner = gr->tp;
656 for (i = 0; i <= pos; i++) {
657 node = get_compound_graph_path_node(gr, i);
659 /* Path not yet complete. */
661 if (get_entity_owner(node) != owner)
663 owner = get_entity_type(node);
665 if (pos == get_compound_graph_path_length(gr))
666 if (!is_atomic_type(owner))
669 } /* is_proper_compound_graph_path */
671 /* Returns the length of a graph path */
672 int get_compound_graph_path_length(const compound_graph_path *gr) {
673 assert(gr && is_compound_graph_path(gr));
675 } /* get_compound_graph_path_length */
678 get_compound_graph_path_node(const compound_graph_path *gr, int pos) {
679 assert(gr && is_compound_graph_path(gr));
680 assert(pos >= 0 && pos < gr->len);
681 return gr->list[pos].node;
682 } /* get_compound_graph_path_node */
685 set_compound_graph_path_node(compound_graph_path *gr, int pos, ir_entity *node) {
686 assert(gr && is_compound_graph_path(gr));
687 assert(pos >= 0 && pos < gr->len);
688 assert(is_entity(node));
689 gr->list[pos].node = node;
690 assert(is_proper_compound_graph_path(gr, pos));
691 } /* set_compound_graph_path_node */
694 get_compound_graph_path_array_index(const compound_graph_path *gr, int pos) {
695 assert(gr && is_compound_graph_path(gr));
696 assert(pos >= 0 && pos < gr->len);
697 return gr->list[pos].index;
698 } /* get_compound_graph_path_array_index */
701 set_compound_graph_path_array_index(compound_graph_path *gr, int pos, int index) {
702 assert(gr && is_compound_graph_path(gr));
703 assert(pos >= 0 && pos < gr->len);
704 gr->list[pos].index = index;
705 } /* set_compound_graph_path_array_index */
707 /* A value of a compound entity is a pair of value and the corresponding path to a member of
710 add_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path) {
711 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
712 assert(is_compound_graph_path(path));
713 ARR_APP1(ir_node *, ent->attr.cmpd_attr.values, val);
714 ARR_APP1(compound_graph_path *, ent->attr.cmpd_attr.val_paths, path);
715 } /* add_compound_ent_value_w_path */
718 set_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path, int pos) {
719 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
720 assert(is_compound_graph_path(path));
721 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
722 ent->attr.cmpd_attr.values[pos] = val;
723 ent->attr.cmpd_attr.val_paths[pos] = path;
724 } /* set_compound_ent_value_w_path */
727 get_compound_ent_n_values(ir_entity *ent) {
728 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
729 return ARR_LEN(ent->attr.cmpd_attr.values);
730 } /* get_compound_ent_n_values */
733 get_compound_ent_value(ir_entity *ent, int pos) {
734 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
735 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
736 return ent->attr.cmpd_attr.values[pos];
737 } /* get_compound_ent_value */
739 compound_graph_path *
740 get_compound_ent_value_path(ir_entity *ent, int pos) {
741 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
742 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.val_paths));
743 return ent->attr.cmpd_attr.val_paths[pos];
744 } /* get_compound_ent_value_path */
747 * Returns non-zero, if two compound_graph_pathes are equal
749 * @param path1 the first path
750 * @param visited_indices
751 * @param path2 the second path
753 static int equal_paths(compound_graph_path *path1, int *visited_indices, compound_graph_path *path2) {
755 int len1 = get_compound_graph_path_length(path1);
756 int len2 = get_compound_graph_path_length(path2);
758 if (len2 != len1) return 0;
760 for (i = 0; i < len1; i++) {
762 ir_entity *node1 = get_compound_graph_path_node(path1, i);
763 ir_entity *node2 = get_compound_graph_path_node(path2, i);
765 if (node1 != node2) return 0;
767 /* FIXME: Strange code. What is it good for? */
768 tp = get_entity_owner(node1);
769 if (is_Array_type(tp)) {
772 /* Compute the index of this node. */
773 assert(get_array_n_dimensions(tp) == 1 && "multidim not implemented");
775 low = get_array_lower_bound_int(tp, 0);
776 if (low + visited_indices[i] < get_compound_graph_path_array_index(path2, i)) {
777 visited_indices[i]++;
780 assert(low + visited_indices[i] == get_compound_graph_path_array_index(path2, i));
787 * Returns the position of a value with the given path.
788 * The path must contain array indices for all array element entities.
790 * @todo This implementation is very low and should be replaced when the new tree oriented
791 * value representation is finally implemented.
793 static int get_compound_ent_pos_by_path(ir_entity *ent, compound_graph_path *path) {
794 int i, n_paths = get_compound_ent_n_values(ent);
795 int *visited_indices;
796 int path_len = get_compound_graph_path_length(path);
798 NEW_ARR_A(int *, visited_indices, path_len);
799 memset(visited_indices, 0, sizeof(*visited_indices) * path_len);
800 for (i = 0; i < n_paths; i ++) {
801 if (equal_paths(get_compound_ent_value_path(ent, i), visited_indices, path))
808 printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
809 printf("Entity %s : ", get_entity_name(ent));
810 for (j = 0; j < get_compound_graph_path_length(path); ++j) {
811 ir_entity *node = get_compound_graph_path_node(path, j);
812 printf("%s", get_entity_name(node));
813 if (is_Array_type(get_entity_owner(node)))
814 printf("[%d]", get_compound_graph_path_array_index(path, j));
816 printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
820 assert(0 && "path not found");
822 } /* get_compound_ent_pos_by_path */
824 /* Returns a constant value given the access path.
825 * The path must contain array indices for all array element entities. */
826 ir_node *get_compound_ent_value_by_path(ir_entity *ent, compound_graph_path *path) {
827 return get_compound_ent_value(ent, get_compound_ent_pos_by_path(ent, path));
828 } /* get_compound_ent_value_by_path */
832 remove_compound_ent_value(ir_entity *ent, ir_entity *value_ent) {
834 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
835 for (i = 0; i < (ARR_LEN(ent->attr.cmpd_attr.val_paths)); ++i) {
836 compound_graph_path *path = ent->attr.cmpd_attr.val_paths[i];
837 if (path->list[path->len-1].node == value_ent) {
838 for (; i < (ARR_LEN(ent->attr.cmpd_attr.val_paths))-1; ++i) {
839 ent->attr.cmpd_attr.val_paths[i] = ent->attr.cmpd_attr.val_paths[i+1];
840 ent->attr.cmpd_attr.values[i] = ent->attr.cmpd_attr.values[i+1];
842 ARR_SETLEN(ir_entity*, ent->attr.cmpd_attr.val_paths, ARR_LEN(ent->attr.cmpd_attr.val_paths) - 1);
843 ARR_SETLEN(ir_node*, ent->attr.cmpd_attr.values, ARR_LEN(ent->attr.cmpd_attr.values) - 1);
847 } /* remove_compound_ent_value */
850 add_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member) {
851 compound_graph_path *path;
852 ir_type *owner_tp = get_entity_owner(member);
853 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
854 path = new_compound_graph_path(get_entity_type(ent), 1);
855 path->list[0].node = member;
856 if (is_Array_type(owner_tp)) {
860 assert(get_array_n_dimensions(owner_tp) == 1 && has_array_lower_bound(owner_tp, 0));
861 max = get_array_lower_bound_int(owner_tp, 0) -1;
862 for (i = 0, n = get_compound_ent_n_values(ent); i < n; ++i) {
863 int index = get_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0);
868 path->list[0].index = max + 1;
870 add_compound_ent_value_w_path(ent, val, path);
871 } /* add_compound_ent_value */
875 get_compound_ent_value_member(ir_entity *ent, int pos) {
876 compound_graph_path *path;
877 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
878 path = get_compound_ent_value_path(ent, pos);
880 return get_compound_graph_path_node(path, get_compound_graph_path_length(path)-1);
881 } /* get_compound_ent_value_member */
884 set_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member, int pos) {
885 compound_graph_path *path;
886 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
887 path = get_compound_ent_value_path(ent, pos);
888 set_compound_graph_path_node(path, 0, member);
889 set_compound_ent_value_w_path(ent, val, path, pos);
890 } /* set_compound_ent_value */
893 set_array_entity_values(ir_entity *ent, tarval **values, int num_vals) {
895 ir_graph *rem = current_ir_graph;
896 ir_type *arrtp = get_entity_type(ent);
898 ir_type *elttp = get_array_element_type(arrtp);
900 assert(is_Array_type(arrtp));
901 assert(get_array_n_dimensions(arrtp) == 1);
902 /* One bound is sufficient, the number of constant fields makes the
904 assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
905 assert(get_entity_variability(ent) != variability_uninitialized);
906 current_ir_graph = get_const_code_irg();
908 for (i = 0; i < num_vals; i++) {
909 val = new_Const_type(values[i], elttp);
910 add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
911 set_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0, i);
913 current_ir_graph = rem;
914 } /* set_array_entity_values */
916 /* Return the overall offset of value at position pos in bytes. */
917 int get_compound_ent_value_offset_bytes(ir_entity *ent, int pos) {
918 compound_graph_path *path;
923 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
925 path = get_compound_ent_value_path(ent, pos);
926 path_len = get_compound_graph_path_length(path);
929 for (i = 0; i < path_len; ++i) {
930 ir_entity *node = get_compound_graph_path_node(path, i);
931 ir_type *node_tp = get_entity_type(node);
933 if (is_Array_type(curr_tp)) {
934 int size = get_type_size_bits(node_tp);
935 int align = get_type_alignment_bits(node_tp);
939 if(size % align > 0) {
940 size += align - (size % align);
942 assert(size % 8 == 0);
944 idx = get_compound_graph_path_array_index(path, i);
946 offset += size * idx;
948 offset += get_entity_offset(node);
954 } /* get_compound_ent_value_offset_bytes */
956 /* Return the offset in bits from the last byte address. */
957 int get_compound_ent_value_offset_bit_remainder(ir_entity *ent, int pos) {
958 compound_graph_path *path;
960 ir_entity *last_node;
962 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
964 path = get_compound_ent_value_path(ent, pos);
965 path_len = get_compound_graph_path_length(path);
966 last_node = get_compound_graph_path_node(path, path_len - 1);
968 return get_entity_offset_bits_remainder(last_node);
969 } /* get_compound_ent_value_offset_bit_remainder */
972 (get_entity_offset)(const ir_entity *ent) {
973 return _get_entity_offset(ent);
974 } /* get_entity_offset */
977 (set_entity_offset)(ir_entity *ent, int offset) {
978 _set_entity_offset(ent, offset);
979 } /* set_entity_offset */
982 (get_entity_offset_bits_remainder)(const ir_entity *ent) {
983 return _get_entity_offset_bits_remainder(ent);
984 } /* get_entity_offset_bits_remainder */
987 (set_entity_offset_bits_remainder)(ir_entity *ent, unsigned char offset) {
988 _set_entity_offset_bits_remainder(ent, offset);
989 } /* set_entity_offset_bits_remainder */
992 add_entity_overwrites(ir_entity *ent, ir_entity *overwritten) {
994 ir_type *owner = get_entity_owner(ent);
995 ir_type *ovw_ovner = get_entity_owner(overwritten);
996 assert(is_Class_type(owner));
997 assert(is_Class_type(ovw_ovner));
998 assert(! is_class_final(ovw_ovner));
1000 ARR_APP1(ir_entity *, ent->overwrites, overwritten);
1001 ARR_APP1(ir_entity *, overwritten->overwrittenby, ent);
1002 } /* add_entity_overwrites */
1005 get_entity_n_overwrites(ir_entity *ent) {
1006 assert(is_Class_type(get_entity_owner(ent)));
1007 return (ARR_LEN(ent->overwrites));
1008 } /* get_entity_n_overwrites */
1011 get_entity_overwrites_index(ir_entity *ent, ir_entity *overwritten) {
1013 assert(is_Class_type(get_entity_owner(ent)));
1014 for (i = 0; i < get_entity_n_overwrites(ent); i++)
1015 if (get_entity_overwrites(ent, i) == overwritten)
1018 } /* get_entity_overwrites_index */
1021 get_entity_overwrites(ir_entity *ent, int pos) {
1022 assert(is_Class_type(get_entity_owner(ent)));
1023 assert(pos < get_entity_n_overwrites(ent));
1024 return ent->overwrites[pos];
1025 } /* get_entity_overwrites */
1028 set_entity_overwrites(ir_entity *ent, int pos, ir_entity *overwritten) {
1029 assert(is_Class_type(get_entity_owner(ent)));
1030 assert(pos < get_entity_n_overwrites(ent));
1031 ent->overwrites[pos] = overwritten;
1032 } /* set_entity_overwrites */
1035 remove_entity_overwrites(ir_entity *ent, ir_entity *overwritten) {
1037 assert(is_Class_type(get_entity_owner(ent)));
1038 for (i = 0; i < (ARR_LEN (ent->overwrites)); i++)
1039 if (ent->overwrites[i] == overwritten) {
1040 for(; i < (ARR_LEN (ent->overwrites))-1; i++)
1041 ent->overwrites[i] = ent->overwrites[i+1];
1042 ARR_SETLEN(ir_entity*, ent->overwrites, ARR_LEN(ent->overwrites) - 1);
1045 } /* remove_entity_overwrites */
1048 add_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites) {
1049 add_entity_overwrites(overwrites, ent);
1050 } /* add_entity_overwrittenby */
1053 get_entity_n_overwrittenby(ir_entity *ent) {
1054 assert(is_Class_type(get_entity_owner(ent)));
1055 return (ARR_LEN (ent->overwrittenby));
1056 } /* get_entity_n_overwrittenby */
1059 get_entity_overwrittenby_index(ir_entity *ent, ir_entity *overwrites) {
1061 assert(is_Class_type(get_entity_owner(ent)));
1062 for (i = 0; i < get_entity_n_overwrittenby(ent); i++)
1063 if (get_entity_overwrittenby(ent, i) == overwrites)
1066 } /* get_entity_overwrittenby_index */
1069 get_entity_overwrittenby(ir_entity *ent, int pos) {
1070 assert(is_Class_type(get_entity_owner(ent)));
1071 assert(pos < get_entity_n_overwrittenby(ent));
1072 return ent->overwrittenby[pos];
1073 } /* get_entity_overwrittenby */
1076 set_entity_overwrittenby(ir_entity *ent, int pos, ir_entity *overwrites) {
1077 assert(is_Class_type(get_entity_owner(ent)));
1078 assert(pos < get_entity_n_overwrittenby(ent));
1079 ent->overwrittenby[pos] = overwrites;
1080 } /* set_entity_overwrittenby */
1082 void remove_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites) {
1084 assert(is_Class_type(get_entity_owner(ent)));
1085 for (i = 0; i < (ARR_LEN (ent->overwrittenby)); i++)
1086 if (ent->overwrittenby[i] == overwrites) {
1087 for(; i < (ARR_LEN (ent->overwrittenby))-1; i++)
1088 ent->overwrittenby[i] = ent->overwrittenby[i+1];
1089 ARR_SETLEN(ir_entity*, ent->overwrittenby, ARR_LEN(ent->overwrittenby) - 1);
1092 } /* remove_entity_overwrittenby */
1094 /* A link to store intermediate information */
1096 (get_entity_link)(const ir_entity *ent) {
1097 return _get_entity_link(ent);
1098 } /* get_entity_link */
1101 (set_entity_link)(ir_entity *ent, void *l) {
1102 _set_entity_link(ent, l);
1103 } /* set_entity_link */
1106 (get_entity_irg)(const ir_entity *ent) {
1107 return _get_entity_irg(ent);
1108 } /* get_entity_irg */
1111 set_entity_irg(ir_entity *ent, ir_graph *irg) {
1112 assert(is_method_entity(ent));
1113 /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
1114 * Methode selbst nicht mehr aufgerufen werden kann, die Entität
1115 * aber erhalten bleiben soll? Wandle die Entitaet in description oder
1118 assert((irg && ent->peculiarity == peculiarity_existent) ||
1119 (!irg && (ent->peculiarity == peculiarity_existent)
1120 && (ent -> visibility == visibility_external_allocated)) ||
1121 (!irg && ent->peculiarity == peculiarity_description) ||
1122 (!irg && ent->peculiarity == peculiarity_inherited));
1123 ent->attr.mtd_attr.irg = irg;
1124 } /* set_entity_irg */
1126 unsigned get_entity_vtable_number(const ir_entity *ent) {
1127 assert(is_method_entity((ir_entity *)ent));
1128 return ent->attr.mtd_attr.vtable_number;
1129 } /* get_entity_vtable_number */
1131 void set_entity_vtable_number(ir_entity *ent, unsigned vtable_number) {
1132 assert(is_method_entity(ent));
1133 ent->attr.mtd_attr.vtable_number = vtable_number;
1134 } /* set_entity_vtable_number */
1136 /* Returns the section of a method. */
1137 ir_img_section get_method_img_section(const ir_entity *ent) {
1138 assert(is_method_entity((ir_entity *)ent));
1139 return ent->attr.mtd_attr.section;
1140 } /* get_method_img_section */
1142 /* Sets the section of a method. */
1143 void set_method_img_section(ir_entity *ent, ir_img_section section) {
1144 assert(is_method_entity(ent));
1145 ent->attr.mtd_attr.section = section;
1146 } /* set_method_img_section */
1149 (is_entity)(const void *thing) {
1150 return _is_entity(thing);
1153 int is_atomic_entity(ir_entity *ent) {
1154 ir_type *t = get_entity_type(ent);
1155 const tp_op *op = get_type_tpop(t);
1156 return (op == type_primitive || op == type_pointer ||
1157 op == type_enumeration || op == type_method);
1158 } /* is_atomic_entity */
1160 int is_compound_entity(ir_entity *ent) {
1161 ir_type *t = get_entity_type(ent);
1162 const tp_op *op = get_type_tpop(t);
1163 return (op == type_class || op == type_struct ||
1164 op == type_array || op == type_union);
1165 } /* is_compound_entity */
1167 int is_method_entity(ir_entity *ent) {
1168 ir_type *t = get_entity_type(ent);
1169 return is_Method_type(t);
1170 } /* is_method_entity */
1173 * @todo not implemented!!! */
1174 int equal_entity(ir_entity *ent1, ir_entity *ent2) {
1175 fprintf(stderr, " calling unimplemented equal entity!!! \n");
1177 } /* equal_entity */
1180 unsigned long (get_entity_visited)(ir_entity *ent) {
1181 return _get_entity_visited(ent);
1182 } /* get_entity_visited */
1184 void (set_entity_visited)(ir_entity *ent, unsigned long num) {
1185 _set_entity_visited(ent, num);
1186 } /* set_entity_visited */
1188 /* Sets visited field in ir_entity to entity_visited. */
1189 void (mark_entity_visited)(ir_entity *ent) {
1190 _mark_entity_visited(ent);
1191 } /* mark_entity_visited */
1193 int (entity_visited)(ir_entity *ent) {
1194 return _entity_visited(ent);
1195 } /* entity_visited */
1197 int (entity_not_visited)(ir_entity *ent) {
1198 return _entity_not_visited(ent);
1199 } /* entity_not_visited */
1201 /* Returns the mask of the additional entity properties. */
1202 unsigned get_entity_additional_properties(ir_entity *ent) {
1205 assert(is_method_entity(ent));
1207 /* first check, if the graph has additional properties */
1208 irg = get_entity_irg(ent);
1211 return get_irg_additional_properties(irg);
1213 if (ent->attr.mtd_attr.irg_add_properties & mtp_property_inherited)
1214 return get_method_additional_properties(get_entity_type(ent));
1216 return ent->attr.mtd_attr.irg_add_properties;
1217 } /* get_entity_additional_properties */
1219 /* Sets the mask of the additional graph properties. */
1220 void set_entity_additional_properties(ir_entity *ent, unsigned property_mask)
1224 assert(is_method_entity(ent));
1226 /* first check, if the graph exists */
1227 irg = get_entity_irg(ent);
1229 set_irg_additional_properties(irg, property_mask);
1231 /* do not allow to set the mtp_property_inherited flag or
1232 * the automatic inheritance of flags will not work */
1233 ent->attr.mtd_attr.irg_add_properties = property_mask & ~mtp_property_inherited;
1235 } /* set_entity_additional_properties */
1237 /* Sets one additional graph property. */
1238 void set_entity_additional_property(ir_entity *ent, mtp_additional_property flag)
1242 assert(is_method_entity(ent));
1244 /* first check, if the graph exists */
1245 irg = get_entity_irg(ent);
1247 set_irg_additional_property(irg, flag);
1249 unsigned mask = ent->attr.mtd_attr.irg_add_properties;
1251 if (mask & mtp_property_inherited)
1252 mask = get_method_additional_properties(get_entity_type(ent));
1254 /* do not allow to set the mtp_property_inherited flag or
1255 * the automatic inheritance of flags will not work */
1256 ent->attr.mtd_attr.irg_add_properties = mask | (flag & ~mtp_property_inherited);
1258 } /* set_entity_additional_property */
1260 /* Returns the class type that this type info entity represents or NULL
1261 if ent is no type info entity. */
1262 ir_type *(get_entity_repr_class)(const ir_entity *ent) {
1263 return _get_entity_repr_class(ent);
1264 } /* get_entity_repr_class */
1266 /* Initialize entity module. */
1267 void firm_init_entity(void)
1269 symconst_symbol sym;
1271 assert(firm_unknown_type && "Call init_type() before firm_init_entity()!");
1272 assert(!unknown_entity && "Call firm_init_entity() only once!");
1274 unknown_entity = new_rd_entity(NULL, firm_unknown_type, new_id_from_str(UNKNOWN_ENTITY_NAME), firm_unknown_type);
1275 set_entity_visibility(unknown_entity, visibility_external_allocated);
1276 set_entity_ld_ident(unknown_entity, get_entity_ident(unknown_entity));
1278 current_ir_graph = get_const_code_irg();
1279 sym.entity_p = unknown_entity;
1280 unknown_entity->value = new_SymConst(sym, symconst_addr_ent);
1281 } /* firm_init_entity */