2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Representation of all program known entities.
23 * @author Martin Trapp, Christian Schaefer, Goetz Lindenmaier, Michael Beck
34 #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 */
52 * An interval initializer.
54 typedef struct interval_initializer interval_initializer;
57 * A value initializer.
59 typedef struct value_initializer value_initializer;
61 struct interval_initializer {
62 int first_index; /**< The first index of the initialized interval. */
63 int last_index; /**< The last index of the initialized interval. */
64 interval_initializer *next; /**< Points to the next interval initializer. */
67 struct value_initializer {
68 ir_entity *ent; /**< The initialized entity. */
69 value_initializer *next; /**< Points to the next value initializer. */
72 typedef union initializer {
73 ir_node *value; /**< The value of the initializer. */
74 ir_node **values; /**< The values of an interval. */
75 value_initializer *val_init; /**< Points the the head of the next value initializers. */
76 interval_initializer *int_init; /**< Points to the head of the next value initializers. */
79 /*-----------------------------------------------------------------*/
81 /*-----------------------------------------------------------------*/
83 ir_entity *unknown_entity = NULL;
85 ir_entity *get_unknown_entity(void) { return unknown_entity; }
87 /** The name of the unknown entity. */
88 #define UNKNOWN_ENTITY_NAME "unknown_entity"
90 /*-----------------------------------------------------------------*/
92 /*-----------------------------------------------------------------*/
95 * Add an entity to it's already set owner type.
97 static INLINE void insert_entity_in_owner(ir_entity *ent) {
98 ir_type *owner = ent->owner;
99 switch (get_type_tpop_code(owner)) {
101 add_class_member(owner, ent);
104 add_struct_member(owner, ent);
107 add_union_member(owner, ent);
110 set_array_element_entity(owner, ent);
114 } /* insert_entity_in_owner */
117 * Creates a new entity. This entity is NOT inserted in the owner type.
119 * @param db debug info for this entity
120 * @param owner the owner type of the new entity
121 * @param name the name of the new entity
122 * @param type the type of the new entity
124 * @return the new created entity
126 static INLINE ir_entity *
127 new_rd_entity(dbg_info *db, ir_type *owner, ident *name, ir_type *type)
132 assert(!id_contains_char(name, ' ') && "entity name should not contain spaces");
134 res = xmalloc(sizeof(*res));
135 memset(res, 0, sizeof(*res));
137 res->kind = k_entity;
143 res->allocation = allocation_automatic;
144 res->visibility = visibility_local;
145 res->volatility = volatility_non_volatile;
146 res->align = align_is_aligned;
147 res->stickyness = stickyness_unsticky;
148 res->peculiarity = peculiarity_existent;
149 res->address_taken = ir_address_taken_unknown;
151 res->compiler_gen = 0;
152 res->backend_marked = 0;
154 res->offset_bit_remainder = 0;
156 res->repr_class = NULL;
158 if (is_Method_type(type)) {
160 ir_mode *mode = is_Method_type(type) ? mode_P_code : mode_P_data;
162 rem = current_ir_graph;
163 current_ir_graph = get_const_code_irg();
164 res->value = new_SymConst(mode, sym, symconst_addr_ent);
165 current_ir_graph = rem;
166 res->allocation = allocation_static;
167 res->variability = variability_constant;
168 res->attr.mtd_attr.irg_add_properties = mtp_property_inherited;
169 res->attr.mtd_attr.vtable_number = VTABLE_NUM_NOT_SET;
170 res->attr.mtd_attr.param_access = NULL;
171 res->attr.mtd_attr.param_weight = NULL;
172 res->attr.mtd_attr.irg = NULL;
173 res->attr.mtd_attr.section = section_text;
174 } else if (is_compound_type(type)) {
175 res->variability = variability_uninitialized;
177 res->attr.cmpd_attr.values = NULL;
178 res->attr.cmpd_attr.val_paths = NULL;
180 res->variability = variability_uninitialized;
184 if (is_Class_type(owner)) {
185 res->overwrites = NEW_ARR_F(ir_entity *, 0);
186 res->overwrittenby = NEW_ARR_F(ir_entity *, 0);
188 res->overwrites = NULL;
189 res->overwrittenby = NULL;
193 res->nr = get_irp_new_node_nr();
194 #endif /* DEBUG_libfirm */
197 set_entity_dbg_info(res, db);
200 } /* new_rd_entity */
203 new_d_entity(ir_type *owner, ident *name, ir_type *type, dbg_info *db) {
206 assert(is_compound_type(owner));
207 res = new_rd_entity(db, owner, name, type);
208 /* Remember entity in it's owner. */
209 insert_entity_in_owner(res);
211 hook_new_entity(res);
216 new_entity(ir_type *owner, ident *name, ir_type *type) {
217 return new_d_entity(owner, name, type, NULL);
221 * Free entity attributes.
223 * @param ent the entity
225 static void free_entity_attrs(ir_entity *ent) {
227 if (get_type_tpop(get_entity_owner(ent)) == type_class) {
228 DEL_ARR_F(ent->overwrites); ent->overwrites = NULL;
229 DEL_ARR_F(ent->overwrittenby); ent->overwrittenby = NULL;
231 assert(ent->overwrites == NULL);
232 assert(ent->overwrittenby == NULL);
234 if (is_compound_entity(ent)) {
235 if (ent->attr.cmpd_attr.val_paths) {
236 for (i = get_compound_ent_n_values(ent) - 1; i >= 0; --i)
237 if (ent->attr.cmpd_attr.val_paths[i]) {
238 /* free_compound_graph_path(ent->attr.cmpd_attr.val_paths[i]) ; * @@@ warum nich? */
239 /* Geht nich: wird mehrfach verwendet!!! ==> mehrfach frei gegeben. */
240 /* DEL_ARR_F(ent->attr.cmpd_attr.val_paths); */
242 ent->attr.cmpd_attr.val_paths = NULL;
244 /* if (ent->attr.cmpd_attr.values) DEL_ARR_F(ent->attr.cmpd_attr.values); *//* @@@ warum nich? */
245 ent->attr.cmpd_attr.values = NULL;
246 } else if (is_method_entity(ent)) {
247 if (ent->attr.mtd_attr.param_access) {
248 DEL_ARR_F(ent->attr.mtd_attr.param_access);
249 ent->attr.mtd_attr.param_access = NULL;
251 if (ent->attr.mtd_attr.param_weight) {
252 DEL_ARR_F(ent->attr.mtd_attr.param_weight);
253 ent->attr.mtd_attr.param_weight = NULL;
256 } /* free_entity_attrs */
259 copy_entity_own(ir_entity *old, ir_type *new_owner) {
261 assert(is_entity(old));
262 assert(is_compound_type(new_owner));
264 if (old->owner == new_owner) return old;
265 newe = xmalloc(sizeof(*newe));
266 memcpy(newe, old, sizeof(*newe));
267 newe->owner = new_owner;
268 if (is_Class_type(new_owner)) {
269 newe->overwrites = NEW_ARR_F(ir_entity *, 0);
270 newe->overwrittenby = NEW_ARR_F(ir_entity *, 0);
273 newe->nr = get_irp_new_node_nr();
276 insert_entity_in_owner(newe);
279 } /* copy_entity_own */
282 copy_entity_name(ir_entity *old, ident *new_name) {
284 assert(old && old->kind == k_entity);
286 if (old->name == new_name) return old;
287 newe = xmalloc(sizeof(*newe));
288 memcpy(newe, old, sizeof(*newe));
289 newe->name = new_name;
290 newe->ld_name = NULL;
291 if (is_Class_type(newe->owner)) {
292 newe->overwrites = DUP_ARR_F(ir_entity *, old->overwrites);
293 newe->overwrittenby = DUP_ARR_F(ir_entity *, old->overwrittenby);
296 newe->nr = get_irp_new_node_nr();
299 insert_entity_in_owner(newe);
302 } /* copy_entity_name */
306 free_entity(ir_entity *ent) {
307 assert(ent && ent->kind == k_entity);
308 free_entity_attrs(ent);
313 /* Outputs a unique number for this node */
315 get_entity_nr(const ir_entity *ent) {
316 assert(ent && ent->kind == k_entity);
320 return (long)PTR_TO_INT(ent);
322 } /* get_entity_nr */
325 (get_entity_name)(const ir_entity *ent) {
326 return _get_entity_name(ent);
327 } /* get_entity_name */
330 (get_entity_ident)(const ir_entity *ent) {
331 return _get_entity_ident(ent);
332 } /* get_entity_ident */
335 (set_entity_ident)(ir_entity *ent, ident *id) {
336 _set_entity_ident(ent, id);
337 } /* set_entity_ident */
340 (get_entity_owner)(ir_entity *ent) {
341 return _get_entity_owner(ent);
342 } /* get_entity_owner */
345 set_entity_owner(ir_entity *ent, ir_type *owner) {
346 assert(is_entity(ent));
347 assert(is_compound_type(owner));
349 } /* set_entity_owner */
352 (get_entity_ld_ident)(ir_entity *ent) {
353 return _get_entity_ld_ident(ent);
354 } /* get_entity_ld_ident */
357 (set_entity_ld_ident)(ir_entity *ent, ident *ld_ident) {
358 _set_entity_ld_ident(ent, ld_ident);
359 } /* set_entity_ld_ident */
362 (get_entity_ld_name)(ir_entity *ent) {
363 return _get_entity_ld_name(ent);
364 } /* get_entity_ld_name */
367 (get_entity_type)(ir_entity *ent) {
368 return _get_entity_type(ent);
369 } /* get_entity_type */
372 (set_entity_type)(ir_entity *ent, ir_type *type) {
373 _set_entity_type(ent, type);
374 } /* set_entity_type */
377 (get_entity_allocation)(const ir_entity *ent) {
378 return _get_entity_allocation(ent);
379 } /* get_entity_allocation */
382 (set_entity_allocation)(ir_entity *ent, ir_allocation al) {
383 _set_entity_allocation(ent, al);
384 } /* set_entity_allocation */
386 /* return the name of the visibility */
387 const char *get_allocation_name(ir_allocation all)
389 #define X(a) case a: return #a
391 X(allocation_automatic);
392 X(allocation_parameter);
393 X(allocation_dynamic);
394 X(allocation_static);
395 default: return "BAD VALUE";
398 } /* get_allocation_name */
401 (get_entity_visibility)(const ir_entity *ent) {
402 return _get_entity_visibility(ent);
403 } /* get_entity_visibility */
406 set_entity_visibility(ir_entity *ent, ir_visibility vis) {
407 assert(ent && ent->kind == k_entity);
408 if (vis != visibility_local)
409 assert((ent->allocation == allocation_static) ||
410 (ent->allocation == allocation_automatic));
411 /* @@@ Test that the owner type is not local, but how??
412 && get_class_visibility(get_entity_owner(ent)) != local));*/
413 ent->visibility = vis;
414 } /* set_entity_visibility */
416 /* return the name of the visibility */
417 const char *get_visibility_name(ir_visibility vis)
419 #define X(a) case a: return #a
422 X(visibility_external_visible);
423 X(visibility_external_allocated);
424 default: return "BAD VALUE";
427 } /* get_visibility_name */
430 (get_entity_variability)(const ir_entity *ent) {
431 return _get_entity_variability(ent);
432 } /* get_entity_variability */
435 set_entity_variability(ir_entity *ent, ir_variability var)
437 assert(ent && ent->kind == k_entity);
438 if (var == variability_part_constant)
439 assert(is_Class_type(ent->type) || is_Struct_type(ent->type));
441 if ((is_compound_type(ent->type)) &&
442 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
443 /* Allocate data structures for constant values */
444 ent->attr.cmpd_attr.values = NEW_ARR_F(ir_node *, 0);
445 ent->attr.cmpd_attr.val_paths = NEW_ARR_F(compound_graph_path *, 0);
447 if ((is_atomic_type(ent->type)) &&
448 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
449 /* Set default constant value. */
450 ent->value = new_rd_Unknown(get_const_code_irg(), get_type_mode(ent->type));
453 if ((is_compound_type(ent->type)) &&
454 (var == variability_uninitialized) && (ent->variability != variability_uninitialized)) {
455 /* Free data structures for constant values */
456 DEL_ARR_F(ent->attr.cmpd_attr.values); ent->attr.cmpd_attr.values = NULL;
457 DEL_ARR_F(ent->attr.cmpd_attr.val_paths); ent->attr.cmpd_attr.val_paths = NULL;
459 ent->variability = var;
460 } /* set_entity_variability */
462 /* return the name of the variability */
463 const char *get_variability_name(ir_variability var)
465 #define X(a) case a: return #a
467 X(variability_uninitialized);
468 X(variability_initialized);
469 X(variability_part_constant);
470 X(variability_constant);
471 default: return "BAD VALUE";
474 } /* get_variability_name */
477 (get_entity_volatility)(const ir_entity *ent) {
478 return _get_entity_volatility(ent);
479 } /* get_entity_volatility */
482 (set_entity_volatility)(ir_entity *ent, ir_volatility vol) {
483 _set_entity_volatility(ent, vol);
484 } /* set_entity_volatility */
486 /* Return the name of the volatility. */
487 const char *get_volatility_name(ir_volatility var)
489 #define X(a) case a: return #a
491 X(volatility_non_volatile);
492 X(volatility_is_volatile);
493 default: return "BAD VALUE";
496 } /* get_volatility_name */
499 (get_entity_align)(const ir_entity *ent) {
500 return _get_entity_align(ent);
501 } /* get_entity_align */
504 (set_entity_align)(ir_entity *ent, ir_align a) {
505 _set_entity_align(ent, a);
506 } /* set_entity_align */
508 /* Return the name of the alignment. */
509 const char *get_align_name(ir_align a)
511 #define X(a) case a: return #a
513 X(align_non_aligned);
515 default: return "BAD VALUE";
518 } /* get_align_name */
521 (get_entity_peculiarity)(const ir_entity *ent) {
522 return _get_entity_peculiarity(ent);
523 } /* get_entity_peculiarity */
526 (set_entity_peculiarity)(ir_entity *ent, ir_peculiarity pec) {
527 _set_entity_peculiarity(ent, pec);
528 } /* set_entity_peculiarity */
530 /* Checks if an entity cannot be overridden anymore. */
531 int (is_entity_final)(const ir_entity *ent) {
532 return _is_entity_final(ent);
533 } /* is_entity_final */
535 /* Sets/resets the final flag of an entity. */
536 void (set_entity_final)(ir_entity *ent, int final) {
537 _set_entity_final(ent, final);
538 } /* set_entity_final */
540 /* Checks if an entity is compiler generated */
541 int (is_entity_compiler_generated)(const ir_entity *ent) {
542 return _is_entity_compiler_generated(ent);
543 } /* is_entity_compiler_generated */
545 /* Sets/resets the compiler generated flag */
546 void (set_entity_compiler_generated)(ir_entity *ent, int flag) {
547 _set_entity_compiler_generated(ent, flag);
548 } /* set_entity_compiler_generated */
550 /* Checks if an entity is marked by the backend */
551 int (is_entity_backend_marked)(const ir_entity *ent) {
552 return _is_entity_backend_marked(ent);
553 } /* is_entity_backend_marked */
555 /* Sets/resets the compiler generated flag */
556 void (set_entity_backend_marked)(ir_entity *ent, int flag) {
557 _set_entity_backend_marked(ent, flag);
558 } /* set_entity_backend_marked */
560 /* Checks if the address of an entity was taken. */
561 ir_address_taken_state (get_entity_address_taken)(const ir_entity *ent) {
562 return _get_entity_address_taken(ent);
563 } /* is_entity_address_taken */
565 /* Sets/resets the address taken flag. */
566 void (set_entity_address_taken)(ir_entity *ent, ir_address_taken_state flag) {
567 _set_entity_address_taken(ent, flag);
568 } /* set_entity_address_taken */
570 /* Return the name of the address_taken state. */
571 const char *get_address_taken_state_name(ir_address_taken_state state) {
572 #define X(a) case a: return #a
574 X(ir_address_not_taken);
575 X(ir_address_taken_unknown);
577 default: return "BAD VALUE";
580 } /* get_address_taken_state_name */
582 /* Get the entity's stickyness */
584 (get_entity_stickyness)(const ir_entity *ent) {
585 return _get_entity_stickyness(ent);
586 } /* get_entity_stickyness */
588 /* Set the entity's stickyness */
590 (set_entity_stickyness)(ir_entity *ent, ir_stickyness stickyness) {
591 _set_entity_stickyness(ent, stickyness);
592 } /* set_entity_stickyness */
594 /* Set has no effect for existent entities of type method. */
596 get_atomic_ent_value(ir_entity *ent)
598 assert(ent && is_atomic_entity(ent));
599 assert(ent->variability != variability_uninitialized);
600 return skip_Id(ent->value);
601 } /* get_atomic_ent_value */
604 set_atomic_ent_value(ir_entity *ent, ir_node *val) {
605 assert(is_atomic_entity(ent) && (ent->variability != variability_uninitialized));
606 if (is_Method_type(ent->type) && (ent->peculiarity == peculiarity_existent))
608 assert(get_irn_mode(val) == get_type_mode(ent->type));
610 } /* set_atomic_ent_value */
612 /* Returns true if the the node is representable as code on
614 int is_irn_const_expression(ir_node *n) {
617 /* we are in danger iff an exception will arise. TODO: be more precisely,
618 * for instance Div. will NOT rise if divisor != 0
620 if (is_binop(n) && !is_fragile_op(n))
621 return is_irn_const_expression(get_binop_left(n)) && is_irn_const_expression(get_binop_right(n));
624 switch (get_irn_opcode(n)) {
631 return is_irn_const_expression(get_irn_n(n, 0));
636 } /* is_irn_const_expression */
639 * Copies a firm subgraph that complies to the restrictions for
640 * constant expressions to current_block in current_ir_graph.
642 ir_node *copy_const_value(dbg_info *dbg, ir_node *n) {
646 /* @@@ GL I think we should implement this using the routines from irgopt for
647 dead node elimination/inlineing. */
650 switch (get_irn_opcode(n)) {
652 nn = new_d_Const_type(dbg, m, get_Const_tarval(n), get_Const_type(n));
655 nn = new_d_SymConst_type(dbg, get_irn_mode(n), get_SymConst_symbol(n), get_SymConst_kind(n),
656 get_SymConst_value_type(n));
659 nn = new_d_Add(dbg, copy_const_value(dbg, get_Add_left(n)),
660 copy_const_value(dbg, get_Add_right(n)), m); break;
662 nn = new_d_Sub(dbg, copy_const_value(dbg, get_Sub_left(n)),
663 copy_const_value(dbg, get_Sub_right(n)), m); break;
665 nn = new_d_Mul(dbg, copy_const_value(dbg, get_Mul_left(n)),
666 copy_const_value(dbg, get_Mul_right(n)), m); break;
668 nn = new_d_And(dbg, copy_const_value(dbg, get_And_left(n)),
669 copy_const_value(dbg, get_And_right(n)), m); break;
671 nn = new_d_Or(dbg, copy_const_value(dbg, get_Or_left(n)),
672 copy_const_value(dbg, get_Or_right(n)), m); break;
674 nn = new_d_Eor(dbg, copy_const_value(dbg, get_Eor_left(n)),
675 copy_const_value(dbg, get_Eor_right(n)), m); break;
677 nn = new_d_Cast(dbg, copy_const_value(dbg, get_Cast_op(n)), get_Cast_type(n)); break;
679 nn = new_d_Conv(dbg, copy_const_value(dbg, get_Conv_op(n)), m); break;
681 nn = new_d_Unknown(m); break;
683 assert(0 && "opcode invalid or not implemented");
688 } /* copy_const_value */
690 /* Creates a new compound graph path. */
691 compound_graph_path *
692 new_compound_graph_path(ir_type *tp, int length) {
693 compound_graph_path *res;
695 assert(is_compound_type(tp));
698 res = xmalloc(sizeof(*res) + (length-1) * sizeof(res->list[0]));
699 memset(res, 0, sizeof(*res) + (length-1) * sizeof(res->list[0]));
700 res->kind = k_ir_compound_graph_path;
705 } /* new_compound_graph_path */
707 /* Frees an graph path object */
708 void free_compound_graph_path (compound_graph_path *gr) {
709 assert(gr && is_compound_graph_path(gr));
712 } /* free_compound_graph_path */
714 /* Returns non-zero if an object is a compound graph path */
715 int is_compound_graph_path(const void *thing) {
716 return (get_kind(thing) == k_ir_compound_graph_path);
717 } /* is_compound_graph_path */
719 /* Checks whether the path up to pos is correct. If the path contains a NULL,
720 * assumes the path is not complete and returns 'true'. */
721 int is_proper_compound_graph_path(compound_graph_path *gr, int pos) {
724 ir_type *owner = gr->tp;
726 for (i = 0; i <= pos; i++) {
727 node = get_compound_graph_path_node(gr, i);
729 /* Path not yet complete. */
731 if (get_entity_owner(node) != owner)
733 owner = get_entity_type(node);
735 if (pos == get_compound_graph_path_length(gr))
736 if (!is_atomic_type(owner))
739 } /* is_proper_compound_graph_path */
741 /* Returns the length of a graph path */
742 int get_compound_graph_path_length(const compound_graph_path *gr) {
743 assert(gr && is_compound_graph_path(gr));
745 } /* get_compound_graph_path_length */
748 get_compound_graph_path_node(const compound_graph_path *gr, int pos) {
749 assert(gr && is_compound_graph_path(gr));
750 assert(pos >= 0 && pos < gr->len);
751 return gr->list[pos].node;
752 } /* get_compound_graph_path_node */
755 set_compound_graph_path_node(compound_graph_path *gr, int pos, ir_entity *node) {
756 assert(gr && is_compound_graph_path(gr));
757 assert(pos >= 0 && pos < gr->len);
758 assert(is_entity(node));
759 gr->list[pos].node = node;
760 assert(is_proper_compound_graph_path(gr, pos));
761 } /* set_compound_graph_path_node */
764 get_compound_graph_path_array_index(const compound_graph_path *gr, int pos) {
765 assert(gr && is_compound_graph_path(gr));
766 assert(pos >= 0 && pos < gr->len);
767 return gr->list[pos].index;
768 } /* get_compound_graph_path_array_index */
771 set_compound_graph_path_array_index(compound_graph_path *gr, int pos, int index) {
772 assert(gr && is_compound_graph_path(gr));
773 assert(pos >= 0 && pos < gr->len);
774 gr->list[pos].index = index;
775 } /* set_compound_graph_path_array_index */
777 /* A value of a compound entity is a pair of value and the corresponding path to a member of
780 add_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path) {
781 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
782 assert(is_compound_graph_path(path));
783 ARR_APP1(ir_node *, ent->attr.cmpd_attr.values, val);
784 ARR_APP1(compound_graph_path *, ent->attr.cmpd_attr.val_paths, path);
785 } /* add_compound_ent_value_w_path */
788 set_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path, int pos) {
789 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
790 assert(is_compound_graph_path(path));
791 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
792 ent->attr.cmpd_attr.values[pos] = val;
793 ent->attr.cmpd_attr.val_paths[pos] = path;
794 } /* set_compound_ent_value_w_path */
797 get_compound_ent_n_values(ir_entity *ent) {
798 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
799 return ARR_LEN(ent->attr.cmpd_attr.values);
800 } /* get_compound_ent_n_values */
803 get_compound_ent_value(ir_entity *ent, int pos) {
804 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
805 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
806 return ent->attr.cmpd_attr.values[pos];
807 } /* get_compound_ent_value */
809 compound_graph_path *
810 get_compound_ent_value_path(ir_entity *ent, int pos) {
811 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
812 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.val_paths));
813 return ent->attr.cmpd_attr.val_paths[pos];
814 } /* get_compound_ent_value_path */
817 * Returns non-zero, if two compound_graph_pathes are equal
819 * @param path1 the first path
820 * @param visited_indices
821 * @param path2 the second path
823 static int equal_paths(compound_graph_path *path1, int *visited_indices, compound_graph_path *path2) {
825 int len1 = get_compound_graph_path_length(path1);
826 int len2 = get_compound_graph_path_length(path2);
828 if (len2 != len1) return 0;
830 for (i = 0; i < len1; i++) {
832 ir_entity *node1 = get_compound_graph_path_node(path1, i);
833 ir_entity *node2 = get_compound_graph_path_node(path2, i);
835 if (node1 != node2) return 0;
837 /* FIXME: Strange code. What is it good for? */
838 tp = get_entity_owner(node1);
839 if (is_Array_type(tp)) {
842 /* Compute the index of this node. */
843 assert(get_array_n_dimensions(tp) == 1 && "multidim not implemented");
845 low = get_array_lower_bound_int(tp, 0);
846 if (low + visited_indices[i] < get_compound_graph_path_array_index(path2, i)) {
847 visited_indices[i]++;
850 assert(low + visited_indices[i] == get_compound_graph_path_array_index(path2, i));
857 * Returns the position of a value with the given path.
858 * The path must contain array indices for all array element entities.
860 * @todo This implementation is very slow (O(number of initializers^2) and should
861 * be replaced when the new tree oriented
862 * value representation is finally implemented.
864 static int get_compound_ent_pos_by_path(ir_entity *ent, compound_graph_path *path) {
865 int i, n_paths = get_compound_ent_n_values(ent);
866 int *visited_indices;
867 int path_len = get_compound_graph_path_length(path);
869 NEW_ARR_A(int *, visited_indices, path_len);
870 memset(visited_indices, 0, sizeof(*visited_indices) * path_len);
871 for (i = 0; i < n_paths; i ++) {
872 if (equal_paths(get_compound_ent_value_path(ent, i), visited_indices, path))
879 printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
880 printf("Entity %s : ", get_entity_name(ent));
881 for (j = 0; j < get_compound_graph_path_length(path); ++j) {
882 ir_entity *node = get_compound_graph_path_node(path, j);
883 printf("%s", get_entity_name(node));
884 if (is_Array_type(get_entity_owner(node)))
885 printf("[%d]", get_compound_graph_path_array_index(path, j));
887 printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
891 assert(0 && "path not found");
893 } /* get_compound_ent_pos_by_path */
895 /* Returns a constant value given the access path.
896 * The path must contain array indices for all array element entities. */
897 ir_node *get_compound_ent_value_by_path(ir_entity *ent, compound_graph_path *path) {
898 return get_compound_ent_value(ent, get_compound_ent_pos_by_path(ent, path));
899 } /* get_compound_ent_value_by_path */
903 remove_compound_ent_value(ir_entity *ent, ir_entity *value_ent) {
905 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
907 n = ARR_LEN(ent->attr.cmpd_attr.val_paths);
908 for (i = 0; i < n; ++i) {
909 compound_graph_path *path = ent->attr.cmpd_attr.val_paths[i];
910 if (path->list[path->len-1].node == value_ent) {
911 for (; i < n - 1; ++i) {
912 ent->attr.cmpd_attr.val_paths[i] = ent->attr.cmpd_attr.val_paths[i+1];
913 ent->attr.cmpd_attr.values[i] = ent->attr.cmpd_attr.values[i+1];
915 ARR_SETLEN(ir_entity*, ent->attr.cmpd_attr.val_paths, n - 1);
916 ARR_SETLEN(ir_node*, ent->attr.cmpd_attr.values, n - 1);
920 } /* remove_compound_ent_value */
923 add_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member) {
924 compound_graph_path *path;
925 ir_type *owner_tp = get_entity_owner(member);
926 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
927 path = new_compound_graph_path(get_entity_type(ent), 1);
928 path->list[0].node = member;
929 if (is_Array_type(owner_tp)) {
933 assert(get_array_n_dimensions(owner_tp) == 1 && has_array_lower_bound(owner_tp, 0));
934 max = get_array_lower_bound_int(owner_tp, 0) -1;
935 for (i = 0, n = get_compound_ent_n_values(ent); i < n; ++i) {
936 int index = get_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0);
941 path->list[0].index = max + 1;
943 add_compound_ent_value_w_path(ent, val, path);
944 } /* add_compound_ent_value */
948 get_compound_ent_value_member(ir_entity *ent, int pos) {
949 compound_graph_path *path;
950 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
951 path = get_compound_ent_value_path(ent, pos);
953 return get_compound_graph_path_node(path, get_compound_graph_path_length(path)-1);
954 } /* get_compound_ent_value_member */
957 set_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member, int pos) {
958 compound_graph_path *path;
959 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
960 path = get_compound_ent_value_path(ent, pos);
961 set_compound_graph_path_node(path, 0, member);
962 set_compound_ent_value_w_path(ent, val, path, pos);
963 } /* set_compound_ent_value */
966 set_array_entity_values(ir_entity *ent, tarval **values, int num_vals) {
968 ir_graph *rem = current_ir_graph;
969 ir_type *arrtp = get_entity_type(ent);
971 ir_type *elttp = get_array_element_type(arrtp);
973 assert(is_Array_type(arrtp));
974 assert(get_array_n_dimensions(arrtp) == 1);
975 /* One bound is sufficient, the number of constant fields makes the
977 assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
978 assert(get_entity_variability(ent) != variability_uninitialized);
979 current_ir_graph = get_const_code_irg();
981 for (i = 0; i < num_vals; i++) {
982 val = new_Const_type(values[i], elttp);
983 add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
984 set_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0, i);
986 current_ir_graph = rem;
987 } /* set_array_entity_values */
989 /* Return the overall offset of value at position pos in bytes. */
990 unsigned get_compound_ent_value_offset_bytes(ir_entity *ent, int pos) {
991 compound_graph_path *path;
996 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
998 path = get_compound_ent_value_path(ent, pos);
999 path_len = get_compound_graph_path_length(path);
1002 for (i = 0; i < path_len; ++i) {
1003 if (is_Array_type(curr_tp)) {
1004 ir_type *elem_type = get_array_element_type(curr_tp);
1005 unsigned size = get_type_size_bytes(elem_type);
1006 unsigned align = get_type_alignment_bytes(elem_type);
1010 if(size % align > 0) {
1011 size += align - (size % align);
1013 idx = get_compound_graph_path_array_index(path, i);
1015 offset += size * idx;
1016 curr_tp = elem_type;
1018 ir_entity *node = get_compound_graph_path_node(path, i);
1019 offset += get_entity_offset(node);
1020 curr_tp = get_entity_type(node);
1025 } /* get_compound_ent_value_offset_bytes */
1027 /* Return the offset in bits from the last byte address. */
1028 unsigned get_compound_ent_value_offset_bit_remainder(ir_entity *ent, int pos) {
1029 compound_graph_path *path;
1031 ir_entity *last_node;
1033 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
1035 path = get_compound_ent_value_path(ent, pos);
1036 path_len = get_compound_graph_path_length(path);
1037 last_node = get_compound_graph_path_node(path, path_len - 1);
1039 if(last_node == NULL)
1042 return get_entity_offset_bits_remainder(last_node);
1043 } /* get_compound_ent_value_offset_bit_remainder */
1046 (get_entity_offset)(const ir_entity *ent) {
1047 return _get_entity_offset(ent);
1048 } /* get_entity_offset */
1051 (set_entity_offset)(ir_entity *ent, int offset) {
1052 _set_entity_offset(ent, offset);
1053 } /* set_entity_offset */
1056 (get_entity_offset_bits_remainder)(const ir_entity *ent) {
1057 return _get_entity_offset_bits_remainder(ent);
1058 } /* get_entity_offset_bits_remainder */
1061 (set_entity_offset_bits_remainder)(ir_entity *ent, unsigned char offset) {
1062 _set_entity_offset_bits_remainder(ent, offset);
1063 } /* set_entity_offset_bits_remainder */
1066 add_entity_overwrites(ir_entity *ent, ir_entity *overwritten) {
1068 ir_type *owner = get_entity_owner(ent);
1069 ir_type *ovw_ovner = get_entity_owner(overwritten);
1070 assert(is_Class_type(owner));
1071 assert(is_Class_type(ovw_ovner));
1072 assert(! is_class_final(ovw_ovner));
1074 ARR_APP1(ir_entity *, ent->overwrites, overwritten);
1075 ARR_APP1(ir_entity *, overwritten->overwrittenby, ent);
1076 } /* add_entity_overwrites */
1079 get_entity_n_overwrites(ir_entity *ent) {
1080 assert(is_Class_type(get_entity_owner(ent)));
1081 return (ARR_LEN(ent->overwrites));
1082 } /* get_entity_n_overwrites */
1085 get_entity_overwrites_index(ir_entity *ent, ir_entity *overwritten) {
1087 assert(is_Class_type(get_entity_owner(ent)));
1088 n = get_entity_n_overwrites(ent);
1089 for (i = 0; i < n; ++i) {
1090 if (get_entity_overwrites(ent, i) == overwritten)
1094 } /* get_entity_overwrites_index */
1097 get_entity_overwrites(ir_entity *ent, int pos) {
1098 assert(is_Class_type(get_entity_owner(ent)));
1099 assert(pos < get_entity_n_overwrites(ent));
1100 return ent->overwrites[pos];
1101 } /* get_entity_overwrites */
1104 set_entity_overwrites(ir_entity *ent, int pos, ir_entity *overwritten) {
1105 assert(is_Class_type(get_entity_owner(ent)));
1106 assert(pos < get_entity_n_overwrites(ent));
1107 ent->overwrites[pos] = overwritten;
1108 } /* set_entity_overwrites */
1111 remove_entity_overwrites(ir_entity *ent, ir_entity *overwritten) {
1113 assert(is_Class_type(get_entity_owner(ent)));
1114 n = ARR_LEN(ent->overwrites);
1115 for (i = 0; i < n; ++i) {
1116 if (ent->overwrites[i] == overwritten) {
1117 for (; i < n - 1; i++)
1118 ent->overwrites[i] = ent->overwrites[i+1];
1119 ARR_SETLEN(ir_entity*, ent->overwrites, n - 1);
1123 } /* remove_entity_overwrites */
1126 add_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites) {
1127 add_entity_overwrites(overwrites, ent);
1128 } /* add_entity_overwrittenby */
1131 get_entity_n_overwrittenby(ir_entity *ent) {
1132 assert(is_Class_type(get_entity_owner(ent)));
1133 return ARR_LEN(ent->overwrittenby);
1134 } /* get_entity_n_overwrittenby */
1137 get_entity_overwrittenby_index(ir_entity *ent, ir_entity *overwrites) {
1139 assert(is_Class_type(get_entity_owner(ent)));
1140 n = get_entity_n_overwrittenby(ent);
1141 for (i = 0; i < n; ++i) {
1142 if (get_entity_overwrittenby(ent, i) == overwrites)
1146 } /* get_entity_overwrittenby_index */
1149 get_entity_overwrittenby(ir_entity *ent, int pos) {
1150 assert(is_Class_type(get_entity_owner(ent)));
1151 assert(pos < get_entity_n_overwrittenby(ent));
1152 return ent->overwrittenby[pos];
1153 } /* get_entity_overwrittenby */
1156 set_entity_overwrittenby(ir_entity *ent, int pos, ir_entity *overwrites) {
1157 assert(is_Class_type(get_entity_owner(ent)));
1158 assert(pos < get_entity_n_overwrittenby(ent));
1159 ent->overwrittenby[pos] = overwrites;
1160 } /* set_entity_overwrittenby */
1162 void remove_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites) {
1164 assert(is_Class_type(get_entity_owner(ent)));
1166 n = ARR_LEN(ent->overwrittenby);
1167 for (i = 0; i < n; ++i) {
1168 if (ent->overwrittenby[i] == overwrites) {
1169 for(; i < n - 1; ++i)
1170 ent->overwrittenby[i] = ent->overwrittenby[i+1];
1171 ARR_SETLEN(ir_entity*, ent->overwrittenby, n - 1);
1175 } /* remove_entity_overwrittenby */
1177 /* A link to store intermediate information */
1179 (get_entity_link)(const ir_entity *ent) {
1180 return _get_entity_link(ent);
1181 } /* get_entity_link */
1184 (set_entity_link)(ir_entity *ent, void *l) {
1185 _set_entity_link(ent, l);
1186 } /* set_entity_link */
1189 (get_entity_irg)(const ir_entity *ent) {
1190 return _get_entity_irg(ent);
1191 } /* get_entity_irg */
1194 set_entity_irg(ir_entity *ent, ir_graph *irg) {
1195 assert(is_method_entity(ent));
1196 /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
1197 * Methode selbst nicht mehr aufgerufen werden kann, die Entität
1198 * aber erhalten bleiben soll? Wandle die Entitaet in description oder
1201 assert((irg && ent->peculiarity == peculiarity_existent) ||
1202 (!irg && (ent->peculiarity == peculiarity_existent)
1203 && (ent -> visibility == visibility_external_allocated)) ||
1204 (!irg && ent->peculiarity == peculiarity_description) ||
1205 (!irg && ent->peculiarity == peculiarity_inherited));
1206 ent->attr.mtd_attr.irg = irg;
1207 } /* set_entity_irg */
1209 unsigned get_entity_vtable_number(const ir_entity *ent) {
1210 assert(is_method_entity((ir_entity *)ent));
1211 return ent->attr.mtd_attr.vtable_number;
1212 } /* get_entity_vtable_number */
1214 void set_entity_vtable_number(ir_entity *ent, unsigned vtable_number) {
1215 assert(is_method_entity(ent));
1216 ent->attr.mtd_attr.vtable_number = vtable_number;
1217 } /* set_entity_vtable_number */
1219 /* Returns the section of a method. */
1220 ir_img_section get_method_img_section(const ir_entity *ent) {
1221 assert(is_method_entity((ir_entity *)ent));
1222 return ent->attr.mtd_attr.section;
1223 } /* get_method_img_section */
1225 /* Sets the section of a method. */
1226 void set_method_img_section(ir_entity *ent, ir_img_section section) {
1227 assert(is_method_entity(ent));
1228 ent->attr.mtd_attr.section = section;
1229 } /* set_method_img_section */
1232 (is_entity)(const void *thing) {
1233 return _is_entity(thing);
1236 int is_atomic_entity(ir_entity *ent) {
1237 ir_type *t = get_entity_type(ent);
1238 const tp_op *op = get_type_tpop(t);
1239 return (op == type_primitive || op == type_pointer ||
1240 op == type_enumeration || op == type_method);
1241 } /* is_atomic_entity */
1243 int is_compound_entity(ir_entity *ent) {
1244 ir_type *t = get_entity_type(ent);
1245 const tp_op *op = get_type_tpop(t);
1246 return (op == type_class || op == type_struct ||
1247 op == type_array || op == type_union);
1248 } /* is_compound_entity */
1250 int is_method_entity(ir_entity *ent) {
1251 ir_type *t = get_entity_type(ent);
1252 return is_Method_type(t);
1253 } /* is_method_entity */
1256 * @todo not implemented!!! */
1257 int equal_entity(ir_entity *ent1, ir_entity *ent2) {
1260 fprintf(stderr, " calling unimplemented equal entity!!! \n");
1262 } /* equal_entity */
1265 unsigned long (get_entity_visited)(ir_entity *ent) {
1266 return _get_entity_visited(ent);
1267 } /* get_entity_visited */
1269 void (set_entity_visited)(ir_entity *ent, unsigned long num) {
1270 _set_entity_visited(ent, num);
1271 } /* set_entity_visited */
1273 /* Sets visited field in ir_entity to entity_visited. */
1274 void (mark_entity_visited)(ir_entity *ent) {
1275 _mark_entity_visited(ent);
1276 } /* mark_entity_visited */
1278 int (entity_visited)(ir_entity *ent) {
1279 return _entity_visited(ent);
1280 } /* entity_visited */
1282 int (entity_not_visited)(ir_entity *ent) {
1283 return _entity_not_visited(ent);
1284 } /* entity_not_visited */
1286 /* Returns the mask of the additional entity properties. */
1287 unsigned get_entity_additional_properties(ir_entity *ent) {
1290 assert(is_method_entity(ent));
1292 /* first check, if the graph has additional properties */
1293 irg = get_entity_irg(ent);
1296 return get_irg_additional_properties(irg);
1298 if (ent->attr.mtd_attr.irg_add_properties & mtp_property_inherited)
1299 return get_method_additional_properties(get_entity_type(ent));
1301 return ent->attr.mtd_attr.irg_add_properties;
1302 } /* get_entity_additional_properties */
1304 /* Sets the mask of the additional graph properties. */
1305 void set_entity_additional_properties(ir_entity *ent, unsigned property_mask)
1309 assert(is_method_entity(ent));
1311 /* first check, if the graph exists */
1312 irg = get_entity_irg(ent);
1314 set_irg_additional_properties(irg, property_mask);
1316 /* do not allow to set the mtp_property_inherited flag or
1317 * the automatic inheritance of flags will not work */
1318 ent->attr.mtd_attr.irg_add_properties = property_mask & ~mtp_property_inherited;
1320 } /* set_entity_additional_properties */
1322 /* Sets one additional graph property. */
1323 void set_entity_additional_property(ir_entity *ent, mtp_additional_property flag)
1327 assert(is_method_entity(ent));
1329 /* first check, if the graph exists */
1330 irg = get_entity_irg(ent);
1332 set_irg_additional_property(irg, flag);
1334 unsigned mask = ent->attr.mtd_attr.irg_add_properties;
1336 if (mask & mtp_property_inherited)
1337 mask = get_method_additional_properties(get_entity_type(ent));
1339 /* do not allow to set the mtp_property_inherited flag or
1340 * the automatic inheritance of flags will not work */
1341 ent->attr.mtd_attr.irg_add_properties = mask | (flag & ~mtp_property_inherited);
1343 } /* set_entity_additional_property */
1345 /* Returns the class type that this type info entity represents or NULL
1346 if ent is no type info entity. */
1347 ir_type *(get_entity_repr_class)(const ir_entity *ent) {
1348 return _get_entity_repr_class(ent);
1349 } /* get_entity_repr_class */
1351 dbg_info *(get_entity_dbg_info)(const ir_entity *ent) {
1352 return _get_entity_dbg_info(ent);
1353 } /* get_entity_dbg_info */
1355 void (set_entity_dbg_info)(ir_entity *ent, dbg_info *db) {
1356 _set_entity_dbg_info(ent, db);
1357 } /* set_entity_dbg_info */
1359 /* Initialize entity module. */
1360 void firm_init_entity(void)
1362 symconst_symbol sym;
1364 assert(firm_unknown_type && "Call init_type() before firm_init_entity()!");
1365 assert(!unknown_entity && "Call firm_init_entity() only once!");
1367 unknown_entity = new_rd_entity(NULL, firm_unknown_type, new_id_from_str(UNKNOWN_ENTITY_NAME), firm_unknown_type);
1368 set_entity_visibility(unknown_entity, visibility_external_allocated);
1369 set_entity_ld_ident(unknown_entity, get_entity_ident(unknown_entity));
1371 current_ir_graph = get_const_code_irg();
1372 sym.entity_p = unknown_entity;
1373 /* TODO: we need two unknown_entities here, one for code and one for data */
1374 unknown_entity->value = new_SymConst(mode_P_data, sym, symconst_addr_ent);
1375 } /* firm_init_entity */