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;
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 */
370 (get_entity_visibility)(const ir_entity *ent) {
371 return _get_entity_visibility(ent);
372 } /* get_entity_visibility */
375 set_entity_visibility(ir_entity *ent, ir_visibility vis) {
376 assert(ent && ent->kind == k_entity);
377 if (vis != visibility_local)
378 assert((ent->allocation == allocation_static) ||
379 (ent->allocation == allocation_automatic));
380 /* @@@ Test that the owner type is not local, but how??
381 && get_class_visibility(get_entity_owner(ent)) != local));*/
382 ent->visibility = vis;
383 } /* set_entity_visibility */
385 /* return the name of the visibility */
386 const char *get_visibility_name(ir_visibility vis)
388 #define X(a) case a: return #a
391 X(visibility_external_visible);
392 X(visibility_external_allocated);
393 default: return "BAD VALUE";
396 } /* get_visibility_name */
399 (get_entity_variability)(const ir_entity *ent) {
400 return _get_entity_variability(ent);
401 } /* get_entity_variability */
404 set_entity_variability(ir_entity *ent, ir_variability var)
406 assert(ent && ent->kind == k_entity);
407 if (var == variability_part_constant)
408 assert(is_Class_type(ent->type) || is_Struct_type(ent->type));
410 if ((is_compound_type(ent->type)) &&
411 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
412 /* Allocate data structures for constant values */
413 ent->attr.cmpd_attr.values = NEW_ARR_F(ir_node *, 0);
414 ent->attr.cmpd_attr.val_paths = NEW_ARR_F(compound_graph_path *, 0);
416 if ((is_atomic_type(ent->type)) &&
417 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
418 /* Set default constant value. */
419 ent->value = new_rd_Unknown(get_const_code_irg(), get_type_mode(ent->type));
422 if ((is_compound_type(ent->type)) &&
423 (var == variability_uninitialized) && (ent->variability != variability_uninitialized)) {
424 /* Free data structures for constant values */
425 DEL_ARR_F(ent->attr.cmpd_attr.values); ent->attr.cmpd_attr.values = NULL;
426 DEL_ARR_F(ent->attr.cmpd_attr.val_paths); ent->attr.cmpd_attr.val_paths = NULL;
428 ent->variability = var;
429 } /* set_entity_variability */
431 /* return the name of the variability */
432 const char *get_variability_name(ir_variability var)
434 #define X(a) case a: return #a
436 X(variability_uninitialized);
437 X(variability_initialized);
438 X(variability_part_constant);
439 X(variability_constant);
440 default: return "BAD VALUE";
443 } /* get_variability_name */
446 (get_entity_volatility)(const ir_entity *ent) {
447 return _get_entity_volatility(ent);
448 } /* get_entity_volatility */
451 (set_entity_volatility)(ir_entity *ent, ir_volatility vol) {
452 _set_entity_volatility(ent, vol);
453 } /* set_entity_volatility */
455 /* return the name of the volatility */
456 const char *get_volatility_name(ir_volatility var)
458 #define X(a) case a: return #a
460 X(volatility_non_volatile);
461 X(volatility_is_volatile);
462 default: return "BAD VALUE";
465 } /* get_volatility_name */
468 (get_entity_peculiarity)(const ir_entity *ent) {
469 return _get_entity_peculiarity(ent);
470 } /* get_entity_peculiarity */
473 (set_entity_peculiarity)(ir_entity *ent, ir_peculiarity pec) {
474 _set_entity_peculiarity(ent, pec);
475 } /* set_entity_peculiarity */
477 /* Checks if an entity cannot be overridden anymore. */
478 int (is_entity_final)(const ir_entity *ent) {
479 return _is_entity_final(ent);
480 } /* is_entity_final */
482 /* Sets/resets the final flag of an entity. */
483 void (set_entity_final)(ir_entity *ent, int final) {
484 _set_entity_final(ent, final);
485 } /* set_entity_final */
487 /* Checks if an entity is compiler generated */
488 int (is_entity_compiler_generated)(const ir_entity *ent) {
489 return _is_entity_compiler_generated(ent);
490 } /* is_entity_compiler_generated */
492 /* Sets/resets the compiler generated flag */
493 void (set_entity_compiler_generated)(ir_entity *ent, int flag) {
494 _set_entity_compiler_generated(ent, flag);
495 } /* set_entity_compiler_generated */
497 /* Checks if the address of an entity was taken. */
498 ir_address_taken_state (get_entity_address_taken)(const ir_entity *ent) {
499 return _get_entity_address_taken(ent);
500 } /* is_entity_address_taken */
502 /* Sets/resets the address taken flag. */
503 void (set_entity_address_taken)(ir_entity *ent, ir_address_taken_state flag) {
504 _set_entity_address_taken(ent, flag);
505 } /* set_entity_address_taken */
507 /* Get the entity's stickyness */
509 (get_entity_stickyness)(const ir_entity *ent) {
510 return _get_entity_stickyness(ent);
511 } /* get_entity_stickyness */
513 /* Set the entity's stickyness */
515 (set_entity_stickyness)(ir_entity *ent, ir_stickyness stickyness) {
516 _set_entity_stickyness(ent, stickyness);
517 } /* set_entity_stickyness */
519 /* Set has no effect for existent entities of type method. */
521 get_atomic_ent_value(ir_entity *ent)
523 assert(ent && is_atomic_entity(ent));
524 assert(ent->variability != variability_uninitialized);
525 return skip_Id(ent->value);
526 } /* get_atomic_ent_value */
529 set_atomic_ent_value(ir_entity *ent, ir_node *val) {
530 assert(is_atomic_entity(ent) && (ent->variability != variability_uninitialized));
531 if (is_Method_type(ent->type) && (ent->peculiarity == peculiarity_existent))
534 } /* set_atomic_ent_value */
536 /* Returns true if the the node is representable as code on
538 int is_irn_const_expression(ir_node *n) {
541 /* we are in danger iff an exception will arise. TODO: be more precisely,
542 * for instance Div. will NOT rise if divisor != 0
544 if (is_binop(n) && !is_fragile_op(n))
545 return is_irn_const_expression(get_binop_left(n)) && is_irn_const_expression(get_binop_right(n));
548 switch (get_irn_opcode(n)) {
555 return is_irn_const_expression(get_irn_n(n, 0));
560 } /* is_irn_const_expression */
563 * Copies a firm subgraph that complies to the restrictions for
564 * constant expressions to current_block in current_ir_graph.
566 ir_node *copy_const_value(dbg_info *dbg, ir_node *n) {
570 /* @@@ GL I think we should implement this using the routines from irgopt for
571 dead node elimination/inlineing. */
574 switch (get_irn_opcode(n)) {
576 nn = new_d_Const_type(dbg, m, get_Const_tarval(n), get_Const_type(n));
579 nn = new_d_SymConst_type(dbg, get_SymConst_symbol(n), get_SymConst_kind(n),
580 get_SymConst_value_type(n));
583 nn = new_d_Add(dbg, copy_const_value(dbg, get_Add_left(n)),
584 copy_const_value(dbg, get_Add_right(n)), m); break;
586 nn = new_d_Sub(dbg, copy_const_value(dbg, get_Sub_left(n)),
587 copy_const_value(dbg, get_Sub_right(n)), m); break;
589 nn = new_d_Mul(dbg, copy_const_value(dbg, get_Mul_left(n)),
590 copy_const_value(dbg, get_Mul_right(n)), m); break;
592 nn = new_d_And(dbg, copy_const_value(dbg, get_And_left(n)),
593 copy_const_value(dbg, get_And_right(n)), m); break;
595 nn = new_d_Or(dbg, copy_const_value(dbg, get_Or_left(n)),
596 copy_const_value(dbg, get_Or_right(n)), m); break;
598 nn = new_d_Eor(dbg, copy_const_value(dbg, get_Eor_left(n)),
599 copy_const_value(dbg, get_Eor_right(n)), m); break;
601 nn = new_d_Cast(dbg, copy_const_value(dbg, get_Cast_op(n)), get_Cast_type(n)); break;
603 nn = new_d_Conv(dbg, copy_const_value(dbg, get_Conv_op(n)), m); break;
605 nn = new_d_Unknown(m); break;
608 assert(0 && "opcode invalid or not implemented");
613 } /* copy_const_value */
615 /* Creates a new compound graph path. */
616 compound_graph_path *
617 new_compound_graph_path(ir_type *tp, int length) {
618 compound_graph_path *res;
620 assert(is_compound_type(tp));
623 res = xmalloc(sizeof(*res) + (length-1) * sizeof(res->list[0]));
624 memset(res, 0, sizeof(*res) + (length-1) * sizeof(res->list[0]));
625 res->kind = k_ir_compound_graph_path;
630 } /* new_compound_graph_path */
632 /* Frees an graph path object */
633 void free_compound_graph_path (compound_graph_path *gr) {
634 assert(gr && is_compound_graph_path(gr));
637 } /* free_compound_graph_path */
639 /* Returns non-zero if an object is a compound graph path */
640 int is_compound_graph_path(const void *thing) {
641 return (get_kind(thing) == k_ir_compound_graph_path);
642 } /* is_compound_graph_path */
644 /* Checks whether the path up to pos is correct. If the path contains a NULL,
645 * assumes the path is not complete and returns 'true'. */
646 int is_proper_compound_graph_path(compound_graph_path *gr, int pos) {
649 ir_type *owner = gr->tp;
651 for (i = 0; i <= pos; i++) {
652 node = get_compound_graph_path_node(gr, i);
654 /* Path not yet complete. */
656 if (get_entity_owner(node) != owner)
658 owner = get_entity_type(node);
660 if (pos == get_compound_graph_path_length(gr))
661 if (!is_atomic_type(owner))
664 } /* is_proper_compound_graph_path */
666 /* Returns the length of a graph path */
667 int get_compound_graph_path_length(const compound_graph_path *gr) {
668 assert(gr && is_compound_graph_path(gr));
670 } /* get_compound_graph_path_length */
673 get_compound_graph_path_node(const compound_graph_path *gr, int pos) {
674 assert(gr && is_compound_graph_path(gr));
675 assert(pos >= 0 && pos < gr->len);
676 return gr->list[pos].node;
677 } /* get_compound_graph_path_node */
680 set_compound_graph_path_node(compound_graph_path *gr, int pos, ir_entity *node) {
681 assert(gr && is_compound_graph_path(gr));
682 assert(pos >= 0 && pos < gr->len);
683 assert(is_entity(node));
684 gr->list[pos].node = node;
685 assert(is_proper_compound_graph_path(gr, pos));
686 } /* set_compound_graph_path_node */
689 get_compound_graph_path_array_index(const compound_graph_path *gr, int pos) {
690 assert(gr && is_compound_graph_path(gr));
691 assert(pos >= 0 && pos < gr->len);
692 return gr->list[pos].index;
693 } /* get_compound_graph_path_array_index */
696 set_compound_graph_path_array_index(compound_graph_path *gr, int pos, int index) {
697 assert(gr && is_compound_graph_path(gr));
698 assert(pos >= 0 && pos < gr->len);
699 gr->list[pos].index = index;
700 } /* set_compound_graph_path_array_index */
702 /* A value of a compound entity is a pair of value and the corresponding path to a member of
705 add_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path) {
706 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
707 assert(is_compound_graph_path(path));
708 ARR_APP1(ir_node *, ent->attr.cmpd_attr.values, val);
709 ARR_APP1(compound_graph_path *, ent->attr.cmpd_attr.val_paths, path);
710 } /* add_compound_ent_value_w_path */
713 set_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path, int pos) {
714 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
715 assert(is_compound_graph_path(path));
716 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
717 ent->attr.cmpd_attr.values[pos] = val;
718 ent->attr.cmpd_attr.val_paths[pos] = path;
719 } /* set_compound_ent_value_w_path */
722 get_compound_ent_n_values(ir_entity *ent) {
723 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
724 return ARR_LEN(ent->attr.cmpd_attr.values);
725 } /* get_compound_ent_n_values */
728 get_compound_ent_value(ir_entity *ent, int pos) {
729 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
730 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
731 return ent->attr.cmpd_attr.values[pos];
732 } /* get_compound_ent_value */
734 compound_graph_path *
735 get_compound_ent_value_path(ir_entity *ent, int pos) {
736 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
737 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.val_paths));
738 return ent->attr.cmpd_attr.val_paths[pos];
739 } /* get_compound_ent_value_path */
742 * Returns non-zero, if two compound_graph_pathes are equal
744 * @param path1 the first path
745 * @param visited_indices
746 * @param path2 the second path
748 static int equal_paths(compound_graph_path *path1, int *visited_indices, compound_graph_path *path2) {
750 int len1 = get_compound_graph_path_length(path1);
751 int len2 = get_compound_graph_path_length(path2);
753 if (len2 != len1) return 0;
755 for (i = 0; i < len1; i++) {
757 ir_entity *node1 = get_compound_graph_path_node(path1, i);
758 ir_entity *node2 = get_compound_graph_path_node(path2, i);
760 if (node1 != node2) return 0;
762 /* FIXME: Strange code. What is it good for? */
763 tp = get_entity_owner(node1);
764 if (is_Array_type(tp)) {
767 /* Compute the index of this node. */
768 assert(get_array_n_dimensions(tp) == 1 && "multidim not implemented");
770 low = get_array_lower_bound_int(tp, 0);
771 if (low + visited_indices[i] < get_compound_graph_path_array_index(path2, i)) {
772 visited_indices[i]++;
775 assert(low + visited_indices[i] == get_compound_graph_path_array_index(path2, i));
782 * Returns the position of a value with the given path.
783 * The path must contain array indices for all array element entities.
785 * @todo This implementation is very low and should be replaced when the new tree oriented
786 * value representation is finally implemented.
788 static int get_compound_ent_pos_by_path(ir_entity *ent, compound_graph_path *path) {
789 int i, n_paths = get_compound_ent_n_values(ent);
790 int *visited_indices;
791 int path_len = get_compound_graph_path_length(path);
793 NEW_ARR_A(int *, visited_indices, path_len);
794 memset(visited_indices, 0, sizeof(*visited_indices) * path_len);
795 for (i = 0; i < n_paths; i ++) {
796 if (equal_paths(get_compound_ent_value_path(ent, i), visited_indices, path))
803 printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
804 printf("Entity %s : ", get_entity_name(ent));
805 for (j = 0; j < get_compound_graph_path_length(path); ++j) {
806 ir_entity *node = get_compound_graph_path_node(path, j);
807 printf("%s", get_entity_name(node));
808 if (is_Array_type(get_entity_owner(node)))
809 printf("[%d]", get_compound_graph_path_array_index(path, j));
811 printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
815 assert(0 && "path not found");
817 } /* get_compound_ent_pos_by_path */
819 /* Returns a constant value given the access path.
820 * The path must contain array indices for all array element entities. */
821 ir_node *get_compound_ent_value_by_path(ir_entity *ent, compound_graph_path *path) {
822 return get_compound_ent_value(ent, get_compound_ent_pos_by_path(ent, path));
823 } /* get_compound_ent_value_by_path */
827 remove_compound_ent_value(ir_entity *ent, ir_entity *value_ent) {
829 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
830 for (i = 0; i < (ARR_LEN(ent->attr.cmpd_attr.val_paths)); ++i) {
831 compound_graph_path *path = ent->attr.cmpd_attr.val_paths[i];
832 if (path->list[path->len-1].node == value_ent) {
833 for (; i < (ARR_LEN(ent->attr.cmpd_attr.val_paths))-1; ++i) {
834 ent->attr.cmpd_attr.val_paths[i] = ent->attr.cmpd_attr.val_paths[i+1];
835 ent->attr.cmpd_attr.values[i] = ent->attr.cmpd_attr.values[i+1];
837 ARR_SETLEN(ir_entity*, ent->attr.cmpd_attr.val_paths, ARR_LEN(ent->attr.cmpd_attr.val_paths) - 1);
838 ARR_SETLEN(ir_node*, ent->attr.cmpd_attr.values, ARR_LEN(ent->attr.cmpd_attr.values) - 1);
842 } /* remove_compound_ent_value */
845 add_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member) {
846 compound_graph_path *path;
847 ir_type *owner_tp = get_entity_owner(member);
848 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
849 path = new_compound_graph_path(get_entity_type(ent), 1);
850 path->list[0].node = member;
851 if (is_Array_type(owner_tp)) {
855 assert(get_array_n_dimensions(owner_tp) == 1 && has_array_lower_bound(owner_tp, 0));
856 max = get_array_lower_bound_int(owner_tp, 0) -1;
857 for (i = 0, n = get_compound_ent_n_values(ent); i < n; ++i) {
858 int index = get_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0);
863 path->list[0].index = max + 1;
865 add_compound_ent_value_w_path(ent, val, path);
866 } /* add_compound_ent_value */
870 get_compound_ent_value_member(ir_entity *ent, 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);
875 return get_compound_graph_path_node(path, get_compound_graph_path_length(path)-1);
876 } /* get_compound_ent_value_member */
879 set_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member, int pos) {
880 compound_graph_path *path;
881 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
882 path = get_compound_ent_value_path(ent, pos);
883 set_compound_graph_path_node(path, 0, member);
884 set_compound_ent_value_w_path(ent, val, path, pos);
885 } /* set_compound_ent_value */
888 set_array_entity_values(ir_entity *ent, tarval **values, int num_vals) {
890 ir_graph *rem = current_ir_graph;
891 ir_type *arrtp = get_entity_type(ent);
893 ir_type *elttp = get_array_element_type(arrtp);
895 assert(is_Array_type(arrtp));
896 assert(get_array_n_dimensions(arrtp) == 1);
897 /* One bound is sufficient, the number of constant fields makes the
899 assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
900 assert(get_entity_variability(ent) != variability_uninitialized);
901 current_ir_graph = get_const_code_irg();
903 for (i = 0; i < num_vals; i++) {
904 val = new_Const_type(values[i], elttp);
905 add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
906 set_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0, i);
908 current_ir_graph = rem;
909 } /* set_array_entity_values */
911 /* Return the overall offset of value at position pos in bytes. */
912 int get_compound_ent_value_offset_bytes(ir_entity *ent, int pos) {
913 compound_graph_path *path;
917 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
919 path = get_compound_ent_value_path(ent, pos);
920 path_len = get_compound_graph_path_length(path);
922 for (i = 0; i < path_len; ++i) {
923 ir_entity *node = get_compound_graph_path_node(path, i);
924 ir_type *node_tp = get_entity_type(node);
925 ir_type *owner_tp = get_entity_owner(node);
927 if (is_Array_type(owner_tp)) {
928 int size = get_type_size_bits(node_tp);
929 int align = get_type_alignment_bits(node_tp);
930 if(size % align > 0) {
931 size += align - (size % align);
933 assert(size % 8 == 0);
935 offset += size * get_compound_graph_path_array_index(path, i);
937 offset += get_entity_offset(node);
942 } /* get_compound_ent_value_offset_bytes */
944 /* Return the offset in bits from the last byte address. */
945 int get_compound_ent_value_offset_bit_remainder(ir_entity *ent, int pos) {
946 compound_graph_path *path;
948 ir_entity *last_node;
950 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
952 path = get_compound_ent_value_path(ent, pos);
953 path_len = get_compound_graph_path_length(path);
954 last_node = get_compound_graph_path_node(path, path_len - 1);
956 return get_entity_offset_bits_remainder(last_node);
957 } /* get_compound_ent_value_offset_bit_remainder */
960 int n_elems; /**< number of elements the array can hold */
961 int current_elem; /**< current array index */
965 /* Compute the array indices in compound graph paths of initialized entities.
967 * All arrays must have fixed lower and upper bounds. One array can
968 * have an open bound. If there are several open bounds, we do
969 * nothing. There must be initializer elements for all array
970 * elements. Uses the link field in the array element entities. The
971 * array bounds must be representable as ints.
973 * WARNING: it is impossible to get this 100% right with the current
974 * design... (in array of structs you cant know when a struct is
975 * really finished and the next array element starts)
977 * (If the bounds are not representable as ints we have to represent
978 * the indices as firm nodes. But still we must be able to
979 * evaluate the index against the upper bound.)
981 int compute_compound_ent_array_indices(ir_entity *ent) {
982 ir_type *tp = get_entity_type(ent);
985 array_info *array_infos;
987 assert(is_compound_type(tp));
989 if (!is_compound_type(tp) ||
990 (ent->variability == variability_uninitialized))
993 n_vals = get_compound_ent_n_values(ent);
994 for(i = 0; i < n_vals; ++i) {
995 compound_graph_path *path = get_compound_ent_value_path(ent, i);
996 int len = get_compound_graph_path_length(path);
1001 array_infos = alloca(max_len * sizeof(array_infos[0]));
1002 memset(array_infos, 0, max_len * sizeof(array_infos[0]));
1004 for(i = 0; i < n_vals; ++i) {
1005 compound_graph_path *path = get_compound_ent_value_path(ent, i);
1006 int path_len = get_compound_graph_path_length(path);
1009 ir_entity *prev_node = NULL;
1011 for(j = path_len-1; j >= 0; --j) {
1014 ir_entity *node = get_compound_graph_path_node(path, j);
1015 const ir_type *node_type = get_entity_type(node);
1016 array_info *info = &array_infos[j];
1018 if(is_atomic_entity(node)) {
1020 set_compound_graph_path_array_index(path, j, -1);
1023 } else if(is_compound_type(node_type) && !is_Array_type(node_type)) {
1024 int n_members = get_compound_n_members(node_type);
1025 ir_entity *last = get_compound_member(node_type, n_members - 1);
1026 if(needadd && last == prev_node) {
1031 set_compound_graph_path_array_index(path, j, -1);
1036 if(info->ent != node) {
1038 dims = get_array_n_dimensions(node_type);
1039 for(dim = 0; dim < dims; ++dim) {
1040 long lower_bound = 0;
1041 long upper_bound = -1;
1043 if(has_array_lower_bound(node_type, 0)) {
1044 lower_bound = get_array_lower_bound_int(node_type, 0);
1046 if(has_array_upper_bound(node_type, 0)) {
1047 upper_bound = get_array_upper_bound_int(node_type, 0);
1048 assert(upper_bound >= lower_bound);
1049 n_elems *= (upper_bound - lower_bound);
1051 assert(dim == dims-1);
1057 info->n_elems = n_elems;
1058 info->current_elem = 0;
1061 set_compound_graph_path_array_index(path, j, info->current_elem);
1064 info->current_elem++;
1065 if(info->current_elem >= info->n_elems) {
1067 info->current_elem = 0;
1078 } /* compute_compound_ent_array_indices */
1081 (get_entity_offset)(const ir_entity *ent) {
1082 return _get_entity_offset(ent);
1083 } /* get_entity_offset */
1086 (set_entity_offset)(ir_entity *ent, int offset) {
1087 _set_entity_offset(ent, offset);
1088 } /* set_entity_offset */
1091 (get_entity_offset_bits_remainder)(const ir_entity *ent) {
1092 return _get_entity_offset_bits_remainder(ent);
1093 } /* get_entity_offset_bits_remainder */
1096 (set_entity_offset_bits_remainder)(ir_entity *ent, unsigned char offset) {
1097 _set_entity_offset_bits_remainder(ent, offset);
1098 } /* set_entity_offset_bits_remainder */
1101 add_entity_overwrites(ir_entity *ent, ir_entity *overwritten) {
1103 ir_type *owner = get_entity_owner(ent);
1104 ir_type *ovw_ovner = get_entity_owner(overwritten);
1105 assert(is_Class_type(owner));
1106 assert(is_Class_type(ovw_ovner));
1107 assert(! is_class_final(ovw_ovner));
1109 ARR_APP1(ir_entity *, ent->overwrites, overwritten);
1110 ARR_APP1(ir_entity *, overwritten->overwrittenby, ent);
1111 } /* add_entity_overwrites */
1114 get_entity_n_overwrites(ir_entity *ent) {
1115 assert(is_Class_type(get_entity_owner(ent)));
1116 return (ARR_LEN(ent->overwrites));
1117 } /* get_entity_n_overwrites */
1120 get_entity_overwrites_index(ir_entity *ent, ir_entity *overwritten) {
1122 assert(is_Class_type(get_entity_owner(ent)));
1123 for (i = 0; i < get_entity_n_overwrites(ent); i++)
1124 if (get_entity_overwrites(ent, i) == overwritten)
1127 } /* get_entity_overwrites_index */
1130 get_entity_overwrites(ir_entity *ent, int pos) {
1131 assert(is_Class_type(get_entity_owner(ent)));
1132 assert(pos < get_entity_n_overwrites(ent));
1133 return ent->overwrites[pos];
1134 } /* get_entity_overwrites */
1137 set_entity_overwrites(ir_entity *ent, int pos, ir_entity *overwritten) {
1138 assert(is_Class_type(get_entity_owner(ent)));
1139 assert(pos < get_entity_n_overwrites(ent));
1140 ent->overwrites[pos] = overwritten;
1141 } /* set_entity_overwrites */
1144 remove_entity_overwrites(ir_entity *ent, ir_entity *overwritten) {
1146 assert(is_Class_type(get_entity_owner(ent)));
1147 for (i = 0; i < (ARR_LEN (ent->overwrites)); i++)
1148 if (ent->overwrites[i] == overwritten) {
1149 for(; i < (ARR_LEN (ent->overwrites))-1; i++)
1150 ent->overwrites[i] = ent->overwrites[i+1];
1151 ARR_SETLEN(ir_entity*, ent->overwrites, ARR_LEN(ent->overwrites) - 1);
1154 } /* remove_entity_overwrites */
1157 add_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites) {
1158 add_entity_overwrites(overwrites, ent);
1159 } /* add_entity_overwrittenby */
1162 get_entity_n_overwrittenby(ir_entity *ent) {
1163 assert(is_Class_type(get_entity_owner(ent)));
1164 return (ARR_LEN (ent->overwrittenby));
1165 } /* get_entity_n_overwrittenby */
1168 get_entity_overwrittenby_index(ir_entity *ent, ir_entity *overwrites) {
1170 assert(is_Class_type(get_entity_owner(ent)));
1171 for (i = 0; i < get_entity_n_overwrittenby(ent); i++)
1172 if (get_entity_overwrittenby(ent, i) == overwrites)
1175 } /* get_entity_overwrittenby_index */
1178 get_entity_overwrittenby(ir_entity *ent, int pos) {
1179 assert(is_Class_type(get_entity_owner(ent)));
1180 assert(pos < get_entity_n_overwrittenby(ent));
1181 return ent->overwrittenby[pos];
1182 } /* get_entity_overwrittenby */
1185 set_entity_overwrittenby(ir_entity *ent, int pos, ir_entity *overwrites) {
1186 assert(is_Class_type(get_entity_owner(ent)));
1187 assert(pos < get_entity_n_overwrittenby(ent));
1188 ent->overwrittenby[pos] = overwrites;
1189 } /* set_entity_overwrittenby */
1191 void remove_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites) {
1193 assert(is_Class_type(get_entity_owner(ent)));
1194 for (i = 0; i < (ARR_LEN (ent->overwrittenby)); i++)
1195 if (ent->overwrittenby[i] == overwrites) {
1196 for(; i < (ARR_LEN (ent->overwrittenby))-1; i++)
1197 ent->overwrittenby[i] = ent->overwrittenby[i+1];
1198 ARR_SETLEN(ir_entity*, ent->overwrittenby, ARR_LEN(ent->overwrittenby) - 1);
1201 } /* remove_entity_overwrittenby */
1203 /* A link to store intermediate information */
1205 (get_entity_link)(const ir_entity *ent) {
1206 return _get_entity_link(ent);
1207 } /* get_entity_link */
1210 (set_entity_link)(ir_entity *ent, void *l) {
1211 _set_entity_link(ent, l);
1212 } /* set_entity_link */
1215 (get_entity_irg)(const ir_entity *ent) {
1216 return _get_entity_irg(ent);
1217 } /* get_entity_irg */
1220 set_entity_irg(ir_entity *ent, ir_graph *irg) {
1221 assert(is_method_entity(ent));
1222 /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
1223 * Methode selbst nicht mehr aufgerufen werden kann, die Entität
1224 * aber erhalten bleiben soll? Wandle die Entitaet in description oder
1227 assert((irg && ent->peculiarity == peculiarity_existent) ||
1228 (!irg && (ent->peculiarity == peculiarity_existent)
1229 && (ent -> visibility == visibility_external_allocated)) ||
1230 (!irg && ent->peculiarity == peculiarity_description) ||
1231 (!irg && ent->peculiarity == peculiarity_inherited));
1232 ent->attr.mtd_attr.irg = irg;
1233 } /* set_entity_irg */
1235 unsigned get_entity_vtable_number(const ir_entity *ent) {
1236 assert(is_method_entity((ir_entity *)ent));
1237 return ent->attr.mtd_attr.vtable_number;
1238 } /* get_entity_vtable_number */
1240 void set_entity_vtable_number(ir_entity *ent, unsigned vtable_number) {
1241 assert(is_method_entity(ent));
1242 ent->attr.mtd_attr.vtable_number = vtable_number;
1243 } /* set_entity_vtable_number */
1245 /* Returns the section of a method. */
1246 ir_img_section get_method_img_section(const ir_entity *ent) {
1247 assert(is_method_entity((ir_entity *)ent));
1248 return ent->attr.mtd_attr.section;
1249 } /* get_method_img_section */
1251 /* Sets the section of a method. */
1252 void set_method_img_section(ir_entity *ent, ir_img_section section) {
1253 assert(is_method_entity(ent));
1254 ent->attr.mtd_attr.section = section;
1255 } /* set_method_img_section */
1258 (is_entity)(const void *thing) {
1259 return _is_entity(thing);
1262 int is_atomic_entity(ir_entity *ent) {
1263 ir_type *t = get_entity_type(ent);
1264 const tp_op *op = get_type_tpop(t);
1265 return (op == type_primitive || op == type_pointer ||
1266 op == type_enumeration || op == type_method);
1267 } /* is_atomic_entity */
1269 int is_compound_entity(ir_entity *ent) {
1270 ir_type *t = get_entity_type(ent);
1271 const tp_op *op = get_type_tpop(t);
1272 return (op == type_class || op == type_struct ||
1273 op == type_array || op == type_union);
1274 } /* is_compound_entity */
1276 int is_method_entity(ir_entity *ent) {
1277 ir_type *t = get_entity_type(ent);
1278 return is_Method_type(t);
1279 } /* is_method_entity */
1282 * @todo not implemented!!! */
1283 int equal_entity(ir_entity *ent1, ir_entity *ent2) {
1284 fprintf(stderr, " calling unimplemented equal entity!!! \n");
1286 } /* equal_entity */
1289 unsigned long (get_entity_visited)(ir_entity *ent) {
1290 return _get_entity_visited(ent);
1291 } /* get_entity_visited */
1293 void (set_entity_visited)(ir_entity *ent, unsigned long num) {
1294 _set_entity_visited(ent, num);
1295 } /* set_entity_visited */
1297 /* Sets visited field in ir_entity to entity_visited. */
1298 void (mark_entity_visited)(ir_entity *ent) {
1299 _mark_entity_visited(ent);
1300 } /* mark_entity_visited */
1302 int (entity_visited)(ir_entity *ent) {
1303 return _entity_visited(ent);
1304 } /* entity_visited */
1306 int (entity_not_visited)(ir_entity *ent) {
1307 return _entity_not_visited(ent);
1308 } /* entity_not_visited */
1310 /* Returns the mask of the additional entity properties. */
1311 unsigned get_entity_additional_properties(ir_entity *ent) {
1314 assert(is_method_entity(ent));
1316 /* first check, if the graph has additional properties */
1317 irg = get_entity_irg(ent);
1320 return get_irg_additional_properties(irg);
1322 if (ent->attr.mtd_attr.irg_add_properties & mtp_property_inherited)
1323 return get_method_additional_properties(get_entity_type(ent));
1325 return ent->attr.mtd_attr.irg_add_properties;
1326 } /* get_entity_additional_properties */
1328 /* Sets the mask of the additional graph properties. */
1329 void set_entity_additional_properties(ir_entity *ent, unsigned property_mask)
1333 assert(is_method_entity(ent));
1335 /* first check, if the graph exists */
1336 irg = get_entity_irg(ent);
1338 set_irg_additional_properties(irg, property_mask);
1340 /* do not allow to set the mtp_property_inherited flag or
1341 * the automatic inheritance of flags will not work */
1342 ent->attr.mtd_attr.irg_add_properties = property_mask & ~mtp_property_inherited;
1344 } /* set_entity_additional_properties */
1346 /* Sets one additional graph property. */
1347 void set_entity_additional_property(ir_entity *ent, mtp_additional_property flag)
1351 assert(is_method_entity(ent));
1353 /* first check, if the graph exists */
1354 irg = get_entity_irg(ent);
1356 set_irg_additional_property(irg, flag);
1358 unsigned mask = ent->attr.mtd_attr.irg_add_properties;
1360 if (mask & mtp_property_inherited)
1361 mask = get_method_additional_properties(get_entity_type(ent));
1363 /* do not allow to set the mtp_property_inherited flag or
1364 * the automatic inheritance of flags will not work */
1365 ent->attr.mtd_attr.irg_add_properties = mask | (flag & ~mtp_property_inherited);
1367 } /* set_entity_additional_property */
1369 /* Returns the class type that this type info entity represents or NULL
1370 if ent is no type info entity. */
1371 ir_type *(get_entity_repr_class)(const ir_entity *ent) {
1372 return _get_entity_repr_class(ent);
1373 } /* get_entity_repr_class */
1375 /* Initialize entity module. */
1376 void firm_init_entity(void)
1378 symconst_symbol sym;
1380 assert(firm_unknown_type && "Call init_type() before firm_init_entity()!");
1381 assert(!unknown_entity && "Call firm_init_entity() only once!");
1383 unknown_entity = new_rd_entity(NULL, firm_unknown_type, new_id_from_str(UNKNOWN_ENTITY_NAME), firm_unknown_type);
1384 set_entity_visibility(unknown_entity, visibility_external_allocated);
1385 set_entity_ld_ident(unknown_entity, get_entity_ident(unknown_entity));
1387 current_ir_graph = get_const_code_irg();
1388 sym.entity_p = unknown_entity;
1389 unknown_entity->value = new_SymConst(sym, symconst_addr_ent);
1390 } /* firm_init_entity */