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.
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;
119 res->address_taken = ir_address_taken_unknown;
121 res->compiler_gen = 0;
123 res->offset_bit_remainder = 0;
125 res->repr_class = NULL;
127 if (is_Method_type(type)) {
130 rem = current_ir_graph;
131 current_ir_graph = get_const_code_irg();
132 res->value = new_SymConst(sym, symconst_addr_ent);
133 current_ir_graph = rem;
134 res->allocation = allocation_static;
135 res->variability = variability_constant;
136 res->attr.mtd_attr.irg_add_properties = mtp_property_inherited;
137 res->attr.mtd_attr.vtable_number = VTABLE_NUM_NOT_SET;
138 res->attr.mtd_attr.param_access = NULL;
139 res->attr.mtd_attr.param_weight = NULL;
140 res->attr.mtd_attr.irg = NULL;
141 res->attr.mtd_attr.section = section_text;
142 } else if (is_compound_type(type)) {
143 res->variability = variability_uninitialized;
145 res->attr.cmpd_attr.values = NULL;
146 res->attr.cmpd_attr.val_paths = NULL;
148 res->variability = variability_uninitialized;
152 if (is_Class_type(owner)) {
153 res->overwrites = NEW_ARR_F(ir_entity *, 0);
154 res->overwrittenby = NEW_ARR_F(ir_entity *, 0);
156 res->overwrites = NULL;
157 res->overwrittenby = NULL;
161 res->nr = get_irp_new_node_nr();
162 #endif /* DEBUG_libfirm */
165 set_entity_dbg_info(res, db);
168 } /* new_rd_entity */
171 new_d_entity(ir_type *owner, ident *name, ir_type *type, dbg_info *db) {
174 assert(is_compound_type(owner));
175 res = new_rd_entity(db, owner, name, type);
176 /* Remember entity in it's owner. */
177 insert_entity_in_owner(res);
179 hook_new_entity(res);
184 new_entity(ir_type *owner, ident *name, ir_type *type) {
185 return new_d_entity(owner, name, type, NULL);
189 * Free entity attributes.
191 * @param ent the entity
193 static void free_entity_attrs(ir_entity *ent) {
195 if (get_type_tpop(get_entity_owner(ent)) == type_class) {
196 DEL_ARR_F(ent->overwrites); ent->overwrites = NULL;
197 DEL_ARR_F(ent->overwrittenby); ent->overwrittenby = NULL;
199 assert(ent->overwrites == NULL);
200 assert(ent->overwrittenby == NULL);
202 if (is_compound_entity(ent)) {
203 if (ent->attr.cmpd_attr.val_paths) {
204 for (i = 0; i < get_compound_ent_n_values(ent); i++)
205 if (ent->attr.cmpd_attr.val_paths[i]) {
206 /* free_compound_graph_path(ent->attr.cmpd_attr.val_paths[i]) ; * @@@ warum nich? */
207 /* Geht nich: wird mehrfach verwendet!!! ==> mehrfach frei gegeben. */
208 /* DEL_ARR_F(ent->attr.cmpd_attr.val_paths); */
210 ent->attr.cmpd_attr.val_paths = NULL;
212 /* if (ent->attr.cmpd_attr.values) DEL_ARR_F(ent->attr.cmpd_attr.values); *//* @@@ warum nich? */
213 ent->attr.cmpd_attr.values = NULL;
214 } else if (is_method_entity(ent)) {
215 if (ent->attr.mtd_attr.param_access) {
216 DEL_ARR_F(ent->attr.mtd_attr.param_access);
217 ent->attr.mtd_attr.param_access = NULL;
219 if (ent->attr.mtd_attr.param_weight) {
220 DEL_ARR_F(ent->attr.mtd_attr.param_weight);
221 ent->attr.mtd_attr.param_weight = NULL;
224 } /* free_entity_attrs */
227 copy_entity_own(ir_entity *old, ir_type *new_owner) {
229 assert(is_entity(old));
230 assert(is_compound_type(new_owner));
232 if (old->owner == new_owner) return old;
233 newe = xmalloc(sizeof(*newe));
234 memcpy(newe, old, sizeof(*newe));
235 newe->owner = new_owner;
236 if (is_Class_type(new_owner)) {
237 newe->overwrites = NEW_ARR_F(ir_entity *, 0);
238 newe->overwrittenby = NEW_ARR_F(ir_entity *, 0);
241 newe->nr = get_irp_new_node_nr();
244 insert_entity_in_owner(newe);
247 } /* copy_entity_own */
250 copy_entity_name(ir_entity *old, ident *new_name) {
252 assert(old && old->kind == k_entity);
254 if (old->name == new_name) return old;
255 newe = xmalloc(sizeof(*newe));
256 memcpy(newe, old, sizeof(*newe));
257 newe->name = new_name;
258 newe->ld_name = NULL;
259 if (is_Class_type(newe->owner)) {
260 newe->overwrites = DUP_ARR_F(ir_entity *, old->overwrites);
261 newe->overwrittenby = DUP_ARR_F(ir_entity *, old->overwrittenby);
264 newe->nr = get_irp_new_node_nr();
267 insert_entity_in_owner(newe);
270 } /* copy_entity_name */
274 free_entity(ir_entity *ent) {
275 assert(ent && ent->kind == k_entity);
276 free_entity_attrs(ent);
281 /* Outputs a unique number for this node */
283 get_entity_nr(const ir_entity *ent) {
284 assert(ent && ent->kind == k_entity);
288 return (long)PTR_TO_INT(ent);
290 } /* get_entity_nr */
293 (get_entity_name)(const ir_entity *ent) {
294 return _get_entity_name(ent);
295 } /* get_entity_name */
298 (get_entity_ident)(const ir_entity *ent) {
299 return _get_entity_ident(ent);
300 } /* get_entity_ident */
303 (set_entity_ident)(ir_entity *ent, ident *id) {
304 _set_entity_ident(ent, id);
305 } /* set_entity_ident */
308 (get_entity_owner)(ir_entity *ent) {
309 return _get_entity_owner(ent);
310 } /* get_entity_owner */
313 set_entity_owner(ir_entity *ent, ir_type *owner) {
314 assert(is_entity(ent));
315 assert(is_compound_type(owner));
317 } /* set_entity_owner */
320 (get_entity_ld_ident)(ir_entity *ent) {
321 return _get_entity_ld_ident(ent);
322 } /* get_entity_ld_ident */
325 (set_entity_ld_ident)(ir_entity *ent, ident *ld_ident) {
326 _set_entity_ld_ident(ent, ld_ident);
327 } /* set_entity_ld_ident */
330 (get_entity_ld_name)(ir_entity *ent) {
331 return _get_entity_ld_name(ent);
332 } /* get_entity_ld_name */
335 (get_entity_type)(ir_entity *ent) {
336 return _get_entity_type(ent);
337 } /* get_entity_type */
340 (set_entity_type)(ir_entity *ent, ir_type *type) {
341 _set_entity_type(ent, type);
342 } /* set_entity_type */
345 (get_entity_allocation)(const ir_entity *ent) {
346 return _get_entity_allocation(ent);
347 } /* get_entity_allocation */
350 (set_entity_allocation)(ir_entity *ent, ir_allocation al) {
351 _set_entity_allocation(ent, al);
352 } /* set_entity_allocation */
354 /* return the name of the visibility */
355 const char *get_allocation_name(ir_allocation all)
357 #define X(a) case a: return #a
359 X(allocation_automatic);
360 X(allocation_parameter);
361 X(allocation_dynamic);
362 X(allocation_static);
363 default: return "BAD VALUE";
366 } /* 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 (is_entity_final)(const ir_entity *ent) {
478 return _is_entity_final(ent);
479 } /* is_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);
484 } /* set_entity_final */
486 /* Checks if an entity is compiler generated */
487 int (is_entity_compiler_generated)(const ir_entity *ent) {
488 return _is_entity_compiler_generated(ent);
489 } /* is_entity_compiler_generated */
491 /* Sets/resets the compiler generated flag */
492 void (set_entity_compiler_generated)(ir_entity *ent, int flag) {
493 _set_entity_compiler_generated(ent, flag);
494 } /* set_entity_compiler_generated */
496 /* Checks if the address of an entity was taken. */
497 ir_address_taken_state (get_entity_address_taken)(const ir_entity *ent) {
498 return _get_entity_address_taken(ent);
499 } /* is_entity_address_taken */
501 /* Sets/resets the address taken flag. */
502 void (set_entity_address_taken)(ir_entity *ent, ir_address_taken_state flag) {
503 _set_entity_address_taken(ent, flag);
504 } /* set_entity_address_taken */
506 /* Return the name of the address_taken state. */
507 const char *get_address_taken_state_name(ir_address_taken_state state) {
508 #define X(a) case a: return #a
510 X(ir_address_not_taken);
511 X(ir_address_taken_unknown);
513 default: return "BAD VALUE";
516 } /* get_address_taken_state_name */
518 /* Get the entity's stickyness */
520 (get_entity_stickyness)(const ir_entity *ent) {
521 return _get_entity_stickyness(ent);
522 } /* get_entity_stickyness */
524 /* Set the entity's stickyness */
526 (set_entity_stickyness)(ir_entity *ent, ir_stickyness stickyness) {
527 _set_entity_stickyness(ent, stickyness);
528 } /* set_entity_stickyness */
530 /* Set has no effect for existent entities of type method. */
532 get_atomic_ent_value(ir_entity *ent)
534 assert(ent && is_atomic_entity(ent));
535 assert(ent->variability != variability_uninitialized);
536 return skip_Id(ent->value);
537 } /* get_atomic_ent_value */
540 set_atomic_ent_value(ir_entity *ent, ir_node *val) {
541 assert(is_atomic_entity(ent) && (ent->variability != variability_uninitialized));
542 if (is_Method_type(ent->type) && (ent->peculiarity == peculiarity_existent))
545 } /* set_atomic_ent_value */
547 /* Returns true if the the node is representable as code on
549 int is_irn_const_expression(ir_node *n) {
552 /* we are in danger iff an exception will arise. TODO: be more precisely,
553 * for instance Div. will NOT rise if divisor != 0
555 if (is_binop(n) && !is_fragile_op(n))
556 return is_irn_const_expression(get_binop_left(n)) && is_irn_const_expression(get_binop_right(n));
559 switch (get_irn_opcode(n)) {
566 return is_irn_const_expression(get_irn_n(n, 0));
571 } /* is_irn_const_expression */
574 * Copies a firm subgraph that complies to the restrictions for
575 * constant expressions to current_block in current_ir_graph.
577 ir_node *copy_const_value(dbg_info *dbg, ir_node *n) {
581 /* @@@ GL I think we should implement this using the routines from irgopt for
582 dead node elimination/inlineing. */
585 switch (get_irn_opcode(n)) {
587 nn = new_d_Const_type(dbg, m, get_Const_tarval(n), get_Const_type(n));
590 nn = new_d_SymConst_type(dbg, get_SymConst_symbol(n), get_SymConst_kind(n),
591 get_SymConst_value_type(n));
594 nn = new_d_Add(dbg, copy_const_value(dbg, get_Add_left(n)),
595 copy_const_value(dbg, get_Add_right(n)), m); break;
597 nn = new_d_Sub(dbg, copy_const_value(dbg, get_Sub_left(n)),
598 copy_const_value(dbg, get_Sub_right(n)), m); break;
600 nn = new_d_Mul(dbg, copy_const_value(dbg, get_Mul_left(n)),
601 copy_const_value(dbg, get_Mul_right(n)), m); break;
603 nn = new_d_And(dbg, copy_const_value(dbg, get_And_left(n)),
604 copy_const_value(dbg, get_And_right(n)), m); break;
606 nn = new_d_Or(dbg, copy_const_value(dbg, get_Or_left(n)),
607 copy_const_value(dbg, get_Or_right(n)), m); break;
609 nn = new_d_Eor(dbg, copy_const_value(dbg, get_Eor_left(n)),
610 copy_const_value(dbg, get_Eor_right(n)), m); break;
612 nn = new_d_Cast(dbg, copy_const_value(dbg, get_Cast_op(n)), get_Cast_type(n)); break;
614 nn = new_d_Conv(dbg, copy_const_value(dbg, get_Conv_op(n)), m); break;
616 nn = new_d_Unknown(m); break;
619 assert(0 && "opcode invalid or not implemented");
624 } /* copy_const_value */
626 /* Creates a new compound graph path. */
627 compound_graph_path *
628 new_compound_graph_path(ir_type *tp, int length) {
629 compound_graph_path *res;
631 assert(is_compound_type(tp));
634 res = xmalloc(sizeof(*res) + (length-1) * sizeof(res->list[0]));
635 memset(res, 0, sizeof(*res) + (length-1) * sizeof(res->list[0]));
636 res->kind = k_ir_compound_graph_path;
641 } /* new_compound_graph_path */
643 /* Frees an graph path object */
644 void free_compound_graph_path (compound_graph_path *gr) {
645 assert(gr && is_compound_graph_path(gr));
648 } /* free_compound_graph_path */
650 /* Returns non-zero if an object is a compound graph path */
651 int is_compound_graph_path(const void *thing) {
652 return (get_kind(thing) == k_ir_compound_graph_path);
653 } /* is_compound_graph_path */
655 /* Checks whether the path up to pos is correct. If the path contains a NULL,
656 * assumes the path is not complete and returns 'true'. */
657 int is_proper_compound_graph_path(compound_graph_path *gr, int pos) {
660 ir_type *owner = gr->tp;
662 for (i = 0; i <= pos; i++) {
663 node = get_compound_graph_path_node(gr, i);
665 /* Path not yet complete. */
667 if (get_entity_owner(node) != owner)
669 owner = get_entity_type(node);
671 if (pos == get_compound_graph_path_length(gr))
672 if (!is_atomic_type(owner))
675 } /* is_proper_compound_graph_path */
677 /* Returns the length of a graph path */
678 int get_compound_graph_path_length(const compound_graph_path *gr) {
679 assert(gr && is_compound_graph_path(gr));
681 } /* get_compound_graph_path_length */
684 get_compound_graph_path_node(const compound_graph_path *gr, int pos) {
685 assert(gr && is_compound_graph_path(gr));
686 assert(pos >= 0 && pos < gr->len);
687 return gr->list[pos].node;
688 } /* get_compound_graph_path_node */
691 set_compound_graph_path_node(compound_graph_path *gr, int pos, ir_entity *node) {
692 assert(gr && is_compound_graph_path(gr));
693 assert(pos >= 0 && pos < gr->len);
694 assert(is_entity(node));
695 gr->list[pos].node = node;
696 assert(is_proper_compound_graph_path(gr, pos));
697 } /* set_compound_graph_path_node */
700 get_compound_graph_path_array_index(const compound_graph_path *gr, int pos) {
701 assert(gr && is_compound_graph_path(gr));
702 assert(pos >= 0 && pos < gr->len);
703 return gr->list[pos].index;
704 } /* get_compound_graph_path_array_index */
707 set_compound_graph_path_array_index(compound_graph_path *gr, int pos, int index) {
708 assert(gr && is_compound_graph_path(gr));
709 assert(pos >= 0 && pos < gr->len);
710 gr->list[pos].index = index;
711 } /* set_compound_graph_path_array_index */
713 /* A value of a compound entity is a pair of value and the corresponding path to a member of
716 add_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path) {
717 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
718 assert(is_compound_graph_path(path));
719 ARR_APP1(ir_node *, ent->attr.cmpd_attr.values, val);
720 ARR_APP1(compound_graph_path *, ent->attr.cmpd_attr.val_paths, path);
721 } /* add_compound_ent_value_w_path */
724 set_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path, int pos) {
725 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
726 assert(is_compound_graph_path(path));
727 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
728 ent->attr.cmpd_attr.values[pos] = val;
729 ent->attr.cmpd_attr.val_paths[pos] = path;
730 } /* set_compound_ent_value_w_path */
733 get_compound_ent_n_values(ir_entity *ent) {
734 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
735 return ARR_LEN(ent->attr.cmpd_attr.values);
736 } /* get_compound_ent_n_values */
739 get_compound_ent_value(ir_entity *ent, int pos) {
740 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
741 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
742 return ent->attr.cmpd_attr.values[pos];
743 } /* get_compound_ent_value */
745 compound_graph_path *
746 get_compound_ent_value_path(ir_entity *ent, int pos) {
747 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
748 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.val_paths));
749 return ent->attr.cmpd_attr.val_paths[pos];
750 } /* get_compound_ent_value_path */
753 * Returns non-zero, if two compound_graph_pathes are equal
755 * @param path1 the first path
756 * @param visited_indices
757 * @param path2 the second path
759 static int equal_paths(compound_graph_path *path1, int *visited_indices, compound_graph_path *path2) {
761 int len1 = get_compound_graph_path_length(path1);
762 int len2 = get_compound_graph_path_length(path2);
764 if (len2 != len1) return 0;
766 for (i = 0; i < len1; i++) {
768 ir_entity *node1 = get_compound_graph_path_node(path1, i);
769 ir_entity *node2 = get_compound_graph_path_node(path2, i);
771 if (node1 != node2) return 0;
773 /* FIXME: Strange code. What is it good for? */
774 tp = get_entity_owner(node1);
775 if (is_Array_type(tp)) {
778 /* Compute the index of this node. */
779 assert(get_array_n_dimensions(tp) == 1 && "multidim not implemented");
781 low = get_array_lower_bound_int(tp, 0);
782 if (low + visited_indices[i] < get_compound_graph_path_array_index(path2, i)) {
783 visited_indices[i]++;
786 assert(low + visited_indices[i] == get_compound_graph_path_array_index(path2, i));
793 * Returns the position of a value with the given path.
794 * The path must contain array indices for all array element entities.
796 * @todo This implementation is very low and should be replaced when the new tree oriented
797 * value representation is finally implemented.
799 static int get_compound_ent_pos_by_path(ir_entity *ent, compound_graph_path *path) {
800 int i, n_paths = get_compound_ent_n_values(ent);
801 int *visited_indices;
802 int path_len = get_compound_graph_path_length(path);
804 NEW_ARR_A(int *, visited_indices, path_len);
805 memset(visited_indices, 0, sizeof(*visited_indices) * path_len);
806 for (i = 0; i < n_paths; i ++) {
807 if (equal_paths(get_compound_ent_value_path(ent, i), visited_indices, path))
814 printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
815 printf("Entity %s : ", get_entity_name(ent));
816 for (j = 0; j < get_compound_graph_path_length(path); ++j) {
817 ir_entity *node = get_compound_graph_path_node(path, j);
818 printf("%s", get_entity_name(node));
819 if (is_Array_type(get_entity_owner(node)))
820 printf("[%d]", get_compound_graph_path_array_index(path, j));
822 printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
826 assert(0 && "path not found");
828 } /* get_compound_ent_pos_by_path */
830 /* Returns a constant value given the access path.
831 * The path must contain array indices for all array element entities. */
832 ir_node *get_compound_ent_value_by_path(ir_entity *ent, compound_graph_path *path) {
833 return get_compound_ent_value(ent, get_compound_ent_pos_by_path(ent, path));
834 } /* get_compound_ent_value_by_path */
838 remove_compound_ent_value(ir_entity *ent, ir_entity *value_ent) {
840 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
841 for (i = 0; i < (ARR_LEN(ent->attr.cmpd_attr.val_paths)); ++i) {
842 compound_graph_path *path = ent->attr.cmpd_attr.val_paths[i];
843 if (path->list[path->len-1].node == value_ent) {
844 for (; i < (ARR_LEN(ent->attr.cmpd_attr.val_paths))-1; ++i) {
845 ent->attr.cmpd_attr.val_paths[i] = ent->attr.cmpd_attr.val_paths[i+1];
846 ent->attr.cmpd_attr.values[i] = ent->attr.cmpd_attr.values[i+1];
848 ARR_SETLEN(ir_entity*, ent->attr.cmpd_attr.val_paths, ARR_LEN(ent->attr.cmpd_attr.val_paths) - 1);
849 ARR_SETLEN(ir_node*, ent->attr.cmpd_attr.values, ARR_LEN(ent->attr.cmpd_attr.values) - 1);
853 } /* remove_compound_ent_value */
856 add_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member) {
857 compound_graph_path *path;
858 ir_type *owner_tp = get_entity_owner(member);
859 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
860 path = new_compound_graph_path(get_entity_type(ent), 1);
861 path->list[0].node = member;
862 if (is_Array_type(owner_tp)) {
866 assert(get_array_n_dimensions(owner_tp) == 1 && has_array_lower_bound(owner_tp, 0));
867 max = get_array_lower_bound_int(owner_tp, 0) -1;
868 for (i = 0, n = get_compound_ent_n_values(ent); i < n; ++i) {
869 int index = get_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0);
874 path->list[0].index = max + 1;
876 add_compound_ent_value_w_path(ent, val, path);
877 } /* add_compound_ent_value */
881 get_compound_ent_value_member(ir_entity *ent, int pos) {
882 compound_graph_path *path;
883 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
884 path = get_compound_ent_value_path(ent, pos);
886 return get_compound_graph_path_node(path, get_compound_graph_path_length(path)-1);
887 } /* get_compound_ent_value_member */
890 set_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member, int pos) {
891 compound_graph_path *path;
892 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
893 path = get_compound_ent_value_path(ent, pos);
894 set_compound_graph_path_node(path, 0, member);
895 set_compound_ent_value_w_path(ent, val, path, pos);
896 } /* set_compound_ent_value */
899 set_array_entity_values(ir_entity *ent, tarval **values, int num_vals) {
901 ir_graph *rem = current_ir_graph;
902 ir_type *arrtp = get_entity_type(ent);
904 ir_type *elttp = get_array_element_type(arrtp);
906 assert(is_Array_type(arrtp));
907 assert(get_array_n_dimensions(arrtp) == 1);
908 /* One bound is sufficient, the number of constant fields makes the
910 assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
911 assert(get_entity_variability(ent) != variability_uninitialized);
912 current_ir_graph = get_const_code_irg();
914 for (i = 0; i < num_vals; i++) {
915 val = new_Const_type(values[i], elttp);
916 add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
917 set_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0, i);
919 current_ir_graph = rem;
920 } /* set_array_entity_values */
922 /* Return the overall offset of value at position pos in bytes. */
923 int get_compound_ent_value_offset_bytes(ir_entity *ent, int pos) {
924 compound_graph_path *path;
928 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
930 path = get_compound_ent_value_path(ent, pos);
931 path_len = get_compound_graph_path_length(path);
933 for (i = 0; i < path_len; ++i) {
934 ir_entity *node = get_compound_graph_path_node(path, i);
935 ir_type *node_tp = get_entity_type(node);
936 ir_type *owner_tp = get_entity_owner(node);
938 if (is_Array_type(owner_tp)) {
939 int size = get_type_size_bits(node_tp);
940 int align = get_type_alignment_bits(node_tp);
941 if(size % align > 0) {
942 size += align - (size % align);
944 assert(size % 8 == 0);
946 offset += size * get_compound_graph_path_array_index(path, i);
948 offset += get_entity_offset(node);
953 } /* get_compound_ent_value_offset_bytes */
955 /* Return the offset in bits from the last byte address. */
956 int get_compound_ent_value_offset_bit_remainder(ir_entity *ent, int pos) {
957 compound_graph_path *path;
959 ir_entity *last_node;
961 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
963 path = get_compound_ent_value_path(ent, pos);
964 path_len = get_compound_graph_path_length(path);
965 last_node = get_compound_graph_path_node(path, path_len - 1);
967 return get_entity_offset_bits_remainder(last_node);
968 } /* get_compound_ent_value_offset_bit_remainder */
971 int n_elems; /**< number of elements the array can hold */
972 int current_elem; /**< current array index */
976 /* Compute the array indices in compound graph paths of initialized entities.
978 * All arrays must have fixed lower and upper bounds. One array can
979 * have an open bound. If there are several open bounds, we do
980 * nothing. There must be initializer elements for all array
981 * elements. Uses the link field in the array element entities. The
982 * array bounds must be representable as ints.
984 * WARNING: it is impossible to get this 100% right with the current
985 * design... (in array of structs you cant know when a struct is
986 * really finished and the next array element starts)
988 * (If the bounds are not representable as ints we have to represent
989 * the indices as firm nodes. But still we must be able to
990 * evaluate the index against the upper bound.)
992 int compute_compound_ent_array_indices(ir_entity *ent) {
993 ir_type *tp = get_entity_type(ent);
996 array_info *array_infos;
998 assert(is_compound_type(tp));
1000 if (!is_compound_type(tp) ||
1001 (ent->variability == variability_uninitialized))
1004 n_vals = get_compound_ent_n_values(ent);
1005 for(i = 0; i < n_vals; ++i) {
1006 compound_graph_path *path = get_compound_ent_value_path(ent, i);
1007 int len = get_compound_graph_path_length(path);
1012 array_infos = alloca(max_len * sizeof(array_infos[0]));
1013 memset(array_infos, 0, max_len * sizeof(array_infos[0]));
1015 for(i = 0; i < n_vals; ++i) {
1016 compound_graph_path *path = get_compound_ent_value_path(ent, i);
1017 int path_len = get_compound_graph_path_length(path);
1020 ir_entity *prev_node = NULL;
1022 for(j = path_len-1; j >= 0; --j) {
1025 ir_entity *node = get_compound_graph_path_node(path, j);
1026 const ir_type *node_type = get_entity_type(node);
1027 array_info *info = &array_infos[j];
1029 if(is_atomic_entity(node)) {
1031 set_compound_graph_path_array_index(path, j, -1);
1034 } else if(is_compound_type(node_type) && !is_Array_type(node_type)) {
1035 int n_members = get_compound_n_members(node_type);
1036 ir_entity *last = get_compound_member(node_type, n_members - 1);
1037 if(needadd && last == prev_node) {
1042 set_compound_graph_path_array_index(path, j, -1);
1047 if(info->ent != node) {
1049 dims = get_array_n_dimensions(node_type);
1050 for(dim = 0; dim < dims; ++dim) {
1051 long lower_bound = 0;
1052 long upper_bound = -1;
1054 if(has_array_lower_bound(node_type, 0)) {
1055 lower_bound = get_array_lower_bound_int(node_type, 0);
1057 if(has_array_upper_bound(node_type, 0)) {
1058 upper_bound = get_array_upper_bound_int(node_type, 0);
1059 assert(upper_bound >= lower_bound);
1060 n_elems *= (upper_bound - lower_bound);
1062 assert(dim == dims-1);
1068 info->n_elems = n_elems;
1069 info->current_elem = 0;
1072 set_compound_graph_path_array_index(path, j, info->current_elem);
1075 info->current_elem++;
1076 if(info->current_elem >= info->n_elems) {
1078 info->current_elem = 0;
1089 } /* compute_compound_ent_array_indices */
1092 (get_entity_offset)(const ir_entity *ent) {
1093 return _get_entity_offset(ent);
1094 } /* get_entity_offset */
1097 (set_entity_offset)(ir_entity *ent, int offset) {
1098 _set_entity_offset(ent, offset);
1099 } /* set_entity_offset */
1102 (get_entity_offset_bits_remainder)(const ir_entity *ent) {
1103 return _get_entity_offset_bits_remainder(ent);
1104 } /* get_entity_offset_bits_remainder */
1107 (set_entity_offset_bits_remainder)(ir_entity *ent, unsigned char offset) {
1108 _set_entity_offset_bits_remainder(ent, offset);
1109 } /* set_entity_offset_bits_remainder */
1112 add_entity_overwrites(ir_entity *ent, ir_entity *overwritten) {
1114 ir_type *owner = get_entity_owner(ent);
1115 ir_type *ovw_ovner = get_entity_owner(overwritten);
1116 assert(is_Class_type(owner));
1117 assert(is_Class_type(ovw_ovner));
1118 assert(! is_class_final(ovw_ovner));
1120 ARR_APP1(ir_entity *, ent->overwrites, overwritten);
1121 ARR_APP1(ir_entity *, overwritten->overwrittenby, ent);
1122 } /* add_entity_overwrites */
1125 get_entity_n_overwrites(ir_entity *ent) {
1126 assert(is_Class_type(get_entity_owner(ent)));
1127 return (ARR_LEN(ent->overwrites));
1128 } /* get_entity_n_overwrites */
1131 get_entity_overwrites_index(ir_entity *ent, ir_entity *overwritten) {
1133 assert(is_Class_type(get_entity_owner(ent)));
1134 for (i = 0; i < get_entity_n_overwrites(ent); i++)
1135 if (get_entity_overwrites(ent, i) == overwritten)
1138 } /* get_entity_overwrites_index */
1141 get_entity_overwrites(ir_entity *ent, int pos) {
1142 assert(is_Class_type(get_entity_owner(ent)));
1143 assert(pos < get_entity_n_overwrites(ent));
1144 return ent->overwrites[pos];
1145 } /* get_entity_overwrites */
1148 set_entity_overwrites(ir_entity *ent, int pos, ir_entity *overwritten) {
1149 assert(is_Class_type(get_entity_owner(ent)));
1150 assert(pos < get_entity_n_overwrites(ent));
1151 ent->overwrites[pos] = overwritten;
1152 } /* set_entity_overwrites */
1155 remove_entity_overwrites(ir_entity *ent, ir_entity *overwritten) {
1157 assert(is_Class_type(get_entity_owner(ent)));
1158 for (i = 0; i < (ARR_LEN (ent->overwrites)); i++)
1159 if (ent->overwrites[i] == overwritten) {
1160 for(; i < (ARR_LEN (ent->overwrites))-1; i++)
1161 ent->overwrites[i] = ent->overwrites[i+1];
1162 ARR_SETLEN(ir_entity*, ent->overwrites, ARR_LEN(ent->overwrites) - 1);
1165 } /* remove_entity_overwrites */
1168 add_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites) {
1169 add_entity_overwrites(overwrites, ent);
1170 } /* add_entity_overwrittenby */
1173 get_entity_n_overwrittenby(ir_entity *ent) {
1174 assert(is_Class_type(get_entity_owner(ent)));
1175 return (ARR_LEN (ent->overwrittenby));
1176 } /* get_entity_n_overwrittenby */
1179 get_entity_overwrittenby_index(ir_entity *ent, ir_entity *overwrites) {
1181 assert(is_Class_type(get_entity_owner(ent)));
1182 for (i = 0; i < get_entity_n_overwrittenby(ent); i++)
1183 if (get_entity_overwrittenby(ent, i) == overwrites)
1186 } /* get_entity_overwrittenby_index */
1189 get_entity_overwrittenby(ir_entity *ent, int pos) {
1190 assert(is_Class_type(get_entity_owner(ent)));
1191 assert(pos < get_entity_n_overwrittenby(ent));
1192 return ent->overwrittenby[pos];
1193 } /* get_entity_overwrittenby */
1196 set_entity_overwrittenby(ir_entity *ent, int pos, ir_entity *overwrites) {
1197 assert(is_Class_type(get_entity_owner(ent)));
1198 assert(pos < get_entity_n_overwrittenby(ent));
1199 ent->overwrittenby[pos] = overwrites;
1200 } /* set_entity_overwrittenby */
1202 void remove_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites) {
1204 assert(is_Class_type(get_entity_owner(ent)));
1205 for (i = 0; i < (ARR_LEN (ent->overwrittenby)); i++)
1206 if (ent->overwrittenby[i] == overwrites) {
1207 for(; i < (ARR_LEN (ent->overwrittenby))-1; i++)
1208 ent->overwrittenby[i] = ent->overwrittenby[i+1];
1209 ARR_SETLEN(ir_entity*, ent->overwrittenby, ARR_LEN(ent->overwrittenby) - 1);
1212 } /* remove_entity_overwrittenby */
1214 /* A link to store intermediate information */
1216 (get_entity_link)(const ir_entity *ent) {
1217 return _get_entity_link(ent);
1218 } /* get_entity_link */
1221 (set_entity_link)(ir_entity *ent, void *l) {
1222 _set_entity_link(ent, l);
1223 } /* set_entity_link */
1226 (get_entity_irg)(const ir_entity *ent) {
1227 return _get_entity_irg(ent);
1228 } /* get_entity_irg */
1231 set_entity_irg(ir_entity *ent, ir_graph *irg) {
1232 assert(is_method_entity(ent));
1233 /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
1234 * Methode selbst nicht mehr aufgerufen werden kann, die Entität
1235 * aber erhalten bleiben soll? Wandle die Entitaet in description oder
1238 assert((irg && ent->peculiarity == peculiarity_existent) ||
1239 (!irg && (ent->peculiarity == peculiarity_existent)
1240 && (ent -> visibility == visibility_external_allocated)) ||
1241 (!irg && ent->peculiarity == peculiarity_description) ||
1242 (!irg && ent->peculiarity == peculiarity_inherited));
1243 ent->attr.mtd_attr.irg = irg;
1244 } /* set_entity_irg */
1246 unsigned get_entity_vtable_number(const ir_entity *ent) {
1247 assert(is_method_entity((ir_entity *)ent));
1248 return ent->attr.mtd_attr.vtable_number;
1249 } /* get_entity_vtable_number */
1251 void set_entity_vtable_number(ir_entity *ent, unsigned vtable_number) {
1252 assert(is_method_entity(ent));
1253 ent->attr.mtd_attr.vtable_number = vtable_number;
1254 } /* set_entity_vtable_number */
1256 /* Returns the section of a method. */
1257 ir_img_section get_method_img_section(const ir_entity *ent) {
1258 assert(is_method_entity((ir_entity *)ent));
1259 return ent->attr.mtd_attr.section;
1260 } /* get_method_img_section */
1262 /* Sets the section of a method. */
1263 void set_method_img_section(ir_entity *ent, ir_img_section section) {
1264 assert(is_method_entity(ent));
1265 ent->attr.mtd_attr.section = section;
1266 } /* set_method_img_section */
1269 (is_entity)(const void *thing) {
1270 return _is_entity(thing);
1273 int is_atomic_entity(ir_entity *ent) {
1274 ir_type *t = get_entity_type(ent);
1275 const tp_op *op = get_type_tpop(t);
1276 return (op == type_primitive || op == type_pointer ||
1277 op == type_enumeration || op == type_method);
1278 } /* is_atomic_entity */
1280 int is_compound_entity(ir_entity *ent) {
1281 ir_type *t = get_entity_type(ent);
1282 const tp_op *op = get_type_tpop(t);
1283 return (op == type_class || op == type_struct ||
1284 op == type_array || op == type_union);
1285 } /* is_compound_entity */
1287 int is_method_entity(ir_entity *ent) {
1288 ir_type *t = get_entity_type(ent);
1289 return is_Method_type(t);
1290 } /* is_method_entity */
1293 * @todo not implemented!!! */
1294 int equal_entity(ir_entity *ent1, ir_entity *ent2) {
1295 fprintf(stderr, " calling unimplemented equal entity!!! \n");
1297 } /* equal_entity */
1300 unsigned long (get_entity_visited)(ir_entity *ent) {
1301 return _get_entity_visited(ent);
1302 } /* get_entity_visited */
1304 void (set_entity_visited)(ir_entity *ent, unsigned long num) {
1305 _set_entity_visited(ent, num);
1306 } /* set_entity_visited */
1308 /* Sets visited field in ir_entity to entity_visited. */
1309 void (mark_entity_visited)(ir_entity *ent) {
1310 _mark_entity_visited(ent);
1311 } /* mark_entity_visited */
1313 int (entity_visited)(ir_entity *ent) {
1314 return _entity_visited(ent);
1315 } /* entity_visited */
1317 int (entity_not_visited)(ir_entity *ent) {
1318 return _entity_not_visited(ent);
1319 } /* entity_not_visited */
1321 /* Returns the mask of the additional entity properties. */
1322 unsigned get_entity_additional_properties(ir_entity *ent) {
1325 assert(is_method_entity(ent));
1327 /* first check, if the graph has additional properties */
1328 irg = get_entity_irg(ent);
1331 return get_irg_additional_properties(irg);
1333 if (ent->attr.mtd_attr.irg_add_properties & mtp_property_inherited)
1334 return get_method_additional_properties(get_entity_type(ent));
1336 return ent->attr.mtd_attr.irg_add_properties;
1337 } /* get_entity_additional_properties */
1339 /* Sets the mask of the additional graph properties. */
1340 void set_entity_additional_properties(ir_entity *ent, unsigned property_mask)
1344 assert(is_method_entity(ent));
1346 /* first check, if the graph exists */
1347 irg = get_entity_irg(ent);
1349 set_irg_additional_properties(irg, property_mask);
1351 /* do not allow to set the mtp_property_inherited flag or
1352 * the automatic inheritance of flags will not work */
1353 ent->attr.mtd_attr.irg_add_properties = property_mask & ~mtp_property_inherited;
1355 } /* set_entity_additional_properties */
1357 /* Sets one additional graph property. */
1358 void set_entity_additional_property(ir_entity *ent, mtp_additional_property flag)
1362 assert(is_method_entity(ent));
1364 /* first check, if the graph exists */
1365 irg = get_entity_irg(ent);
1367 set_irg_additional_property(irg, flag);
1369 unsigned mask = ent->attr.mtd_attr.irg_add_properties;
1371 if (mask & mtp_property_inherited)
1372 mask = get_method_additional_properties(get_entity_type(ent));
1374 /* do not allow to set the mtp_property_inherited flag or
1375 * the automatic inheritance of flags will not work */
1376 ent->attr.mtd_attr.irg_add_properties = mask | (flag & ~mtp_property_inherited);
1378 } /* set_entity_additional_property */
1380 /* Returns the class type that this type info entity represents or NULL
1381 if ent is no type info entity. */
1382 ir_type *(get_entity_repr_class)(const ir_entity *ent) {
1383 return _get_entity_repr_class(ent);
1384 } /* get_entity_repr_class */
1386 /* Initialize entity module. */
1387 void firm_init_entity(void)
1389 symconst_symbol sym;
1391 assert(firm_unknown_type && "Call init_type() before firm_init_entity()!");
1392 assert(!unknown_entity && "Call firm_init_entity() only once!");
1394 unknown_entity = new_rd_entity(NULL, firm_unknown_type, new_id_from_str(UNKNOWN_ENTITY_NAME), firm_unknown_type);
1395 set_entity_visibility(unknown_entity, visibility_external_allocated);
1396 set_entity_ld_ident(unknown_entity, get_entity_ident(unknown_entity));
1398 current_ir_graph = get_const_code_irg();
1399 sym.entity_p = unknown_entity;
1400 unknown_entity->value = new_SymConst(sym, symconst_addr_ent);
1401 } /* firm_init_entity */