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"
47 #include "irgraph_t.h"
49 #include "callgraph.h"
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 static ir_initializer_t null_initializer = { IR_INITIALIZER_NULL };
692 ir_initializer_t *get_initializer_null(void)
694 return &null_initializer;
697 ir_initializer_t *create_initializer_const(ir_node *value)
699 struct obstack *obst = get_irg_obstack(get_const_code_irg());
701 ir_initializer_t *initializer
702 = obstack_alloc(obst, sizeof(ir_initializer_const_t));
703 initializer->kind = IR_INITIALIZER_CONST;
704 initializer->consti.value = value;
709 ir_initializer_t *create_initializer_tarval(tarval *tv)
711 struct obstack *obst = get_irg_obstack(get_const_code_irg());
713 ir_initializer_t *initializer
714 = obstack_alloc(obst, sizeof(ir_initializer_tarval_t));
715 initializer->kind = IR_INITIALIZER_TARVAL;
716 initializer->tarval.value = tv;
721 ir_initializer_t *create_initializer_compound(unsigned n_entries)
723 struct obstack *obst = get_irg_obstack(get_const_code_irg());
726 size_t size = sizeof(ir_initializer_compound_t)
727 + (n_entries-1) * sizeof(ir_initializer_t*);
729 ir_initializer_t *initializer = obstack_alloc(obst, size);
730 initializer->kind = IR_INITIALIZER_COMPOUND;
731 initializer->compound.n_initializers = n_entries;
733 for(i = 0; i < n_entries; ++i) {
734 initializer->compound.initializers[i] = get_initializer_null();
740 ir_node *get_initializer_const_value(const ir_initializer_t *initializer)
742 assert(initializer->kind == IR_INITIALIZER_CONST);
743 return initializer->consti.value;
746 tarval *get_initializer_tarval_value(const ir_initializer_t *initializer)
748 assert(initializer->kind == IR_INITIALIZER_TARVAL);
749 return initializer->tarval.value;
752 unsigned get_initializer_compound_n_entries(const ir_initializer_t *initializer)
754 assert(initializer->kind == IR_INITIALIZER_COMPOUND);
755 return initializer->compound.n_initializers;
758 void set_initializer_compound_value(ir_initializer_t *initializer,
759 unsigned index, ir_initializer_t *value)
761 assert(initializer->kind == IR_INITIALIZER_COMPOUND);
762 assert(index < initializer->compound.n_initializers);
764 initializer->compound.initializers[index] = value;
767 ir_initializer_t *get_initializer_compound_value(
768 const ir_initializer_t *initializer, unsigned index)
770 assert(initializer->kind == IR_INITIALIZER_COMPOUND);
771 assert(index < initializer->compound.n_initializers);
773 return initializer->compound.initializers[index];
776 ir_initializer_kind_t get_initializer_kind(const ir_initializer_t *initializer)
778 return initializer->kind;
781 static void check_entity_initializer(ir_entity *entity)
787 void set_entity_initializer(ir_entity *entity, ir_initializer_t *initializer)
789 entity->attr.initializer = initializer;
790 entity->has_initializer = 1;
791 check_entity_initializer(entity);
794 ir_initializer_t *get_entity_initializer(const ir_entity *entity)
796 assert(entity->has_initializer);
797 return entity->attr.initializer;
800 /* Creates a new compound graph path. */
801 compound_graph_path *
802 new_compound_graph_path(ir_type *tp, int length) {
803 compound_graph_path *res;
805 assert(is_compound_type(tp));
808 res = xmalloc(sizeof(*res) + (length-1) * sizeof(res->list[0]));
809 memset(res, 0, sizeof(*res) + (length-1) * sizeof(res->list[0]));
810 res->kind = k_ir_compound_graph_path;
815 } /* new_compound_graph_path */
817 /* Frees an graph path object */
818 void free_compound_graph_path (compound_graph_path *gr) {
819 assert(gr && is_compound_graph_path(gr));
822 } /* free_compound_graph_path */
824 /* Returns non-zero if an object is a compound graph path */
825 int is_compound_graph_path(const void *thing) {
826 return (get_kind(thing) == k_ir_compound_graph_path);
827 } /* is_compound_graph_path */
829 /* Checks whether the path up to pos is correct. If the path contains a NULL,
830 * assumes the path is not complete and returns 'true'. */
831 int is_proper_compound_graph_path(compound_graph_path *gr, int pos) {
834 ir_type *owner = gr->tp;
836 for (i = 0; i <= pos; i++) {
837 node = get_compound_graph_path_node(gr, i);
839 /* Path not yet complete. */
841 if (get_entity_owner(node) != owner)
843 owner = get_entity_type(node);
845 if (pos == get_compound_graph_path_length(gr))
846 if (!is_atomic_type(owner))
849 } /* is_proper_compound_graph_path */
851 /* Returns the length of a graph path */
852 int get_compound_graph_path_length(const compound_graph_path *gr) {
853 assert(gr && is_compound_graph_path(gr));
855 } /* get_compound_graph_path_length */
858 get_compound_graph_path_node(const compound_graph_path *gr, int pos) {
859 assert(gr && is_compound_graph_path(gr));
860 assert(pos >= 0 && pos < gr->len);
861 return gr->list[pos].node;
862 } /* get_compound_graph_path_node */
865 set_compound_graph_path_node(compound_graph_path *gr, int pos, ir_entity *node) {
866 assert(gr && is_compound_graph_path(gr));
867 assert(pos >= 0 && pos < gr->len);
868 assert(is_entity(node));
869 gr->list[pos].node = node;
870 assert(is_proper_compound_graph_path(gr, pos));
871 } /* set_compound_graph_path_node */
874 get_compound_graph_path_array_index(const compound_graph_path *gr, int pos) {
875 assert(gr && is_compound_graph_path(gr));
876 assert(pos >= 0 && pos < gr->len);
877 return gr->list[pos].index;
878 } /* get_compound_graph_path_array_index */
881 set_compound_graph_path_array_index(compound_graph_path *gr, int pos, int index) {
882 assert(gr && is_compound_graph_path(gr));
883 assert(pos >= 0 && pos < gr->len);
884 gr->list[pos].index = index;
885 } /* set_compound_graph_path_array_index */
887 /* A value of a compound entity is a pair of value and the corresponding path to a member of
890 add_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path) {
891 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
892 assert(is_compound_graph_path(path));
893 ARR_APP1(ir_node *, ent->attr.cmpd_attr.values, val);
894 ARR_APP1(compound_graph_path *, ent->attr.cmpd_attr.val_paths, path);
895 } /* add_compound_ent_value_w_path */
898 set_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path, int pos) {
899 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
900 assert(is_compound_graph_path(path));
901 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
902 ent->attr.cmpd_attr.values[pos] = val;
903 ent->attr.cmpd_attr.val_paths[pos] = path;
904 } /* set_compound_ent_value_w_path */
907 get_compound_ent_n_values(ir_entity *ent) {
908 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
909 return ARR_LEN(ent->attr.cmpd_attr.values);
910 } /* get_compound_ent_n_values */
913 get_compound_ent_value(ir_entity *ent, int pos) {
914 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
915 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
916 return ent->attr.cmpd_attr.values[pos];
917 } /* get_compound_ent_value */
919 compound_graph_path *
920 get_compound_ent_value_path(ir_entity *ent, int pos) {
921 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
922 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.val_paths));
923 return ent->attr.cmpd_attr.val_paths[pos];
924 } /* get_compound_ent_value_path */
927 * Returns non-zero, if two compound_graph_pathes are equal
929 * @param path1 the first path
930 * @param visited_indices
931 * @param path2 the second path
933 static int equal_paths(compound_graph_path *path1, int *visited_indices, compound_graph_path *path2) {
935 int len1 = get_compound_graph_path_length(path1);
936 int len2 = get_compound_graph_path_length(path2);
938 if (len2 != len1) return 0;
940 for (i = 0; i < len1; i++) {
942 ir_entity *node1 = get_compound_graph_path_node(path1, i);
943 ir_entity *node2 = get_compound_graph_path_node(path2, i);
945 if (node1 != node2) return 0;
947 /* FIXME: Strange code. What is it good for? */
948 tp = get_entity_owner(node1);
949 if (is_Array_type(tp)) {
952 /* Compute the index of this node. */
953 assert(get_array_n_dimensions(tp) == 1 && "multidim not implemented");
955 low = get_array_lower_bound_int(tp, 0);
956 if (low + visited_indices[i] < get_compound_graph_path_array_index(path2, i)) {
957 visited_indices[i]++;
960 assert(low + visited_indices[i] == get_compound_graph_path_array_index(path2, i));
967 * Returns the position of a value with the given path.
968 * The path must contain array indices for all array element entities.
970 * @todo This implementation is very slow (O(number of initializers^2) and should
971 * be replaced when the new tree oriented
972 * value representation is finally implemented.
974 static int get_compound_ent_pos_by_path(ir_entity *ent, compound_graph_path *path) {
975 int i, n_paths = get_compound_ent_n_values(ent);
976 int *visited_indices;
977 int path_len = get_compound_graph_path_length(path);
979 NEW_ARR_A(int *, visited_indices, path_len);
980 memset(visited_indices, 0, sizeof(*visited_indices) * path_len);
981 for (i = 0; i < n_paths; i ++) {
982 if (equal_paths(get_compound_ent_value_path(ent, i), visited_indices, path))
989 printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
990 printf("Entity %s : ", get_entity_name(ent));
991 for (j = 0; j < get_compound_graph_path_length(path); ++j) {
992 ir_entity *node = get_compound_graph_path_node(path, j);
993 printf("%s", get_entity_name(node));
994 if (is_Array_type(get_entity_owner(node)))
995 printf("[%d]", get_compound_graph_path_array_index(path, j));
997 printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
1001 assert(0 && "path not found");
1003 } /* get_compound_ent_pos_by_path */
1005 /* Returns a constant value given the access path.
1006 * The path must contain array indices for all array element entities. */
1007 ir_node *get_compound_ent_value_by_path(ir_entity *ent, compound_graph_path *path) {
1008 return get_compound_ent_value(ent, get_compound_ent_pos_by_path(ent, path));
1009 } /* get_compound_ent_value_by_path */
1013 remove_compound_ent_value(ir_entity *ent, ir_entity *value_ent) {
1015 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
1017 n = ARR_LEN(ent->attr.cmpd_attr.val_paths);
1018 for (i = 0; i < n; ++i) {
1019 compound_graph_path *path = ent->attr.cmpd_attr.val_paths[i];
1020 if (path->list[path->len-1].node == value_ent) {
1021 for (; i < n - 1; ++i) {
1022 ent->attr.cmpd_attr.val_paths[i] = ent->attr.cmpd_attr.val_paths[i+1];
1023 ent->attr.cmpd_attr.values[i] = ent->attr.cmpd_attr.values[i+1];
1025 ARR_SETLEN(ir_entity*, ent->attr.cmpd_attr.val_paths, n - 1);
1026 ARR_SETLEN(ir_node*, ent->attr.cmpd_attr.values, n - 1);
1030 } /* remove_compound_ent_value */
1033 add_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member) {
1034 compound_graph_path *path;
1035 ir_type *owner_tp = get_entity_owner(member);
1036 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
1037 path = new_compound_graph_path(get_entity_type(ent), 1);
1038 path->list[0].node = member;
1039 if (is_Array_type(owner_tp)) {
1043 assert(get_array_n_dimensions(owner_tp) == 1 && has_array_lower_bound(owner_tp, 0));
1044 max = get_array_lower_bound_int(owner_tp, 0) -1;
1045 for (i = 0, n = get_compound_ent_n_values(ent); i < n; ++i) {
1046 int index = get_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0);
1051 path->list[0].index = max + 1;
1053 add_compound_ent_value_w_path(ent, val, path);
1054 } /* add_compound_ent_value */
1058 get_compound_ent_value_member(ir_entity *ent, int pos) {
1059 compound_graph_path *path;
1060 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
1061 path = get_compound_ent_value_path(ent, pos);
1063 return get_compound_graph_path_node(path, get_compound_graph_path_length(path)-1);
1064 } /* get_compound_ent_value_member */
1067 set_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member, int pos) {
1068 compound_graph_path *path;
1069 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
1070 path = get_compound_ent_value_path(ent, pos);
1071 set_compound_graph_path_node(path, 0, member);
1072 set_compound_ent_value_w_path(ent, val, path, pos);
1073 } /* set_compound_ent_value */
1076 set_array_entity_values(ir_entity *ent, tarval **values, int num_vals) {
1078 ir_graph *rem = current_ir_graph;
1079 ir_type *arrtp = get_entity_type(ent);
1081 ir_type *elttp = get_array_element_type(arrtp);
1083 assert(is_Array_type(arrtp));
1084 assert(get_array_n_dimensions(arrtp) == 1);
1085 /* One bound is sufficient, the number of constant fields makes the
1087 assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
1088 assert(get_entity_variability(ent) != variability_uninitialized);
1089 current_ir_graph = get_const_code_irg();
1091 for (i = 0; i < num_vals; i++) {
1092 val = new_Const_type(values[i], elttp);
1093 add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
1094 set_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0, i);
1096 current_ir_graph = rem;
1097 } /* set_array_entity_values */
1099 /* Return the overall offset of value at position pos in bytes. */
1100 unsigned get_compound_ent_value_offset_bytes(ir_entity *ent, int pos) {
1101 compound_graph_path *path;
1103 unsigned offset = 0;
1106 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
1108 path = get_compound_ent_value_path(ent, pos);
1109 path_len = get_compound_graph_path_length(path);
1112 for (i = 0; i < path_len; ++i) {
1113 if (is_Array_type(curr_tp)) {
1114 ir_type *elem_type = get_array_element_type(curr_tp);
1115 unsigned size = get_type_size_bytes(elem_type);
1116 unsigned align = get_type_alignment_bytes(elem_type);
1120 if(size % align > 0) {
1121 size += align - (size % align);
1123 idx = get_compound_graph_path_array_index(path, i);
1125 offset += size * idx;
1126 curr_tp = elem_type;
1128 ir_entity *node = get_compound_graph_path_node(path, i);
1129 offset += get_entity_offset(node);
1130 curr_tp = get_entity_type(node);
1135 } /* get_compound_ent_value_offset_bytes */
1137 /* Return the offset in bits from the last byte address. */
1138 unsigned get_compound_ent_value_offset_bit_remainder(ir_entity *ent, int pos) {
1139 compound_graph_path *path;
1141 ir_entity *last_node;
1143 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
1145 path = get_compound_ent_value_path(ent, pos);
1146 path_len = get_compound_graph_path_length(path);
1147 last_node = get_compound_graph_path_node(path, path_len - 1);
1149 if(last_node == NULL)
1152 return get_entity_offset_bits_remainder(last_node);
1153 } /* get_compound_ent_value_offset_bit_remainder */
1156 (get_entity_offset)(const ir_entity *ent) {
1157 return _get_entity_offset(ent);
1158 } /* get_entity_offset */
1161 (set_entity_offset)(ir_entity *ent, int offset) {
1162 _set_entity_offset(ent, offset);
1163 } /* set_entity_offset */
1166 (get_entity_offset_bits_remainder)(const ir_entity *ent) {
1167 return _get_entity_offset_bits_remainder(ent);
1168 } /* get_entity_offset_bits_remainder */
1171 (set_entity_offset_bits_remainder)(ir_entity *ent, unsigned char offset) {
1172 _set_entity_offset_bits_remainder(ent, offset);
1173 } /* set_entity_offset_bits_remainder */
1176 add_entity_overwrites(ir_entity *ent, ir_entity *overwritten) {
1178 ir_type *owner = get_entity_owner(ent);
1179 ir_type *ovw_ovner = get_entity_owner(overwritten);
1180 assert(is_Class_type(owner));
1181 assert(is_Class_type(ovw_ovner));
1182 assert(! is_class_final(ovw_ovner));
1184 ARR_APP1(ir_entity *, ent->overwrites, overwritten);
1185 ARR_APP1(ir_entity *, overwritten->overwrittenby, ent);
1186 } /* add_entity_overwrites */
1189 get_entity_n_overwrites(ir_entity *ent) {
1190 assert(is_Class_type(get_entity_owner(ent)));
1191 return (ARR_LEN(ent->overwrites));
1192 } /* get_entity_n_overwrites */
1195 get_entity_overwrites_index(ir_entity *ent, ir_entity *overwritten) {
1197 assert(is_Class_type(get_entity_owner(ent)));
1198 n = get_entity_n_overwrites(ent);
1199 for (i = 0; i < n; ++i) {
1200 if (get_entity_overwrites(ent, i) == overwritten)
1204 } /* get_entity_overwrites_index */
1207 get_entity_overwrites(ir_entity *ent, int pos) {
1208 assert(is_Class_type(get_entity_owner(ent)));
1209 assert(pos < get_entity_n_overwrites(ent));
1210 return ent->overwrites[pos];
1211 } /* get_entity_overwrites */
1214 set_entity_overwrites(ir_entity *ent, int pos, ir_entity *overwritten) {
1215 assert(is_Class_type(get_entity_owner(ent)));
1216 assert(pos < get_entity_n_overwrites(ent));
1217 ent->overwrites[pos] = overwritten;
1218 } /* set_entity_overwrites */
1221 remove_entity_overwrites(ir_entity *ent, ir_entity *overwritten) {
1223 assert(is_Class_type(get_entity_owner(ent)));
1224 n = ARR_LEN(ent->overwrites);
1225 for (i = 0; i < n; ++i) {
1226 if (ent->overwrites[i] == overwritten) {
1227 for (; i < n - 1; i++)
1228 ent->overwrites[i] = ent->overwrites[i+1];
1229 ARR_SETLEN(ir_entity*, ent->overwrites, n - 1);
1233 } /* remove_entity_overwrites */
1236 add_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites) {
1237 add_entity_overwrites(overwrites, ent);
1238 } /* add_entity_overwrittenby */
1241 get_entity_n_overwrittenby(ir_entity *ent) {
1242 assert(is_Class_type(get_entity_owner(ent)));
1243 return ARR_LEN(ent->overwrittenby);
1244 } /* get_entity_n_overwrittenby */
1247 get_entity_overwrittenby_index(ir_entity *ent, ir_entity *overwrites) {
1249 assert(is_Class_type(get_entity_owner(ent)));
1250 n = get_entity_n_overwrittenby(ent);
1251 for (i = 0; i < n; ++i) {
1252 if (get_entity_overwrittenby(ent, i) == overwrites)
1256 } /* get_entity_overwrittenby_index */
1259 get_entity_overwrittenby(ir_entity *ent, int pos) {
1260 assert(is_Class_type(get_entity_owner(ent)));
1261 assert(pos < get_entity_n_overwrittenby(ent));
1262 return ent->overwrittenby[pos];
1263 } /* get_entity_overwrittenby */
1266 set_entity_overwrittenby(ir_entity *ent, int pos, ir_entity *overwrites) {
1267 assert(is_Class_type(get_entity_owner(ent)));
1268 assert(pos < get_entity_n_overwrittenby(ent));
1269 ent->overwrittenby[pos] = overwrites;
1270 } /* set_entity_overwrittenby */
1272 void remove_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites) {
1274 assert(is_Class_type(get_entity_owner(ent)));
1276 n = ARR_LEN(ent->overwrittenby);
1277 for (i = 0; i < n; ++i) {
1278 if (ent->overwrittenby[i] == overwrites) {
1279 for(; i < n - 1; ++i)
1280 ent->overwrittenby[i] = ent->overwrittenby[i+1];
1281 ARR_SETLEN(ir_entity*, ent->overwrittenby, n - 1);
1285 } /* remove_entity_overwrittenby */
1287 /* A link to store intermediate information */
1289 (get_entity_link)(const ir_entity *ent) {
1290 return _get_entity_link(ent);
1291 } /* get_entity_link */
1294 (set_entity_link)(ir_entity *ent, void *l) {
1295 _set_entity_link(ent, l);
1296 } /* set_entity_link */
1299 (get_entity_irg)(const ir_entity *ent) {
1300 return _get_entity_irg(ent);
1301 } /* get_entity_irg */
1304 set_entity_irg(ir_entity *ent, ir_graph *irg) {
1305 assert(is_method_entity(ent));
1306 /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
1307 * Methode selbst nicht mehr aufgerufen werden kann, die Entität
1308 * aber erhalten bleiben soll? Wandle die Entitaet in description oder
1311 assert((irg && ent->peculiarity == peculiarity_existent) ||
1312 (!irg && (ent->peculiarity == peculiarity_existent)
1313 && (ent -> visibility == visibility_external_allocated)) ||
1314 (!irg && ent->peculiarity == peculiarity_description) ||
1315 (!irg && ent->peculiarity == peculiarity_inherited));
1316 ent->attr.mtd_attr.irg = irg;
1317 } /* set_entity_irg */
1319 unsigned get_entity_vtable_number(const ir_entity *ent) {
1320 assert(is_method_entity((ir_entity *)ent));
1321 return ent->attr.mtd_attr.vtable_number;
1322 } /* get_entity_vtable_number */
1324 void set_entity_vtable_number(ir_entity *ent, unsigned vtable_number) {
1325 assert(is_method_entity(ent));
1326 ent->attr.mtd_attr.vtable_number = vtable_number;
1327 } /* set_entity_vtable_number */
1329 /* Returns the section of a method. */
1330 ir_img_section get_method_img_section(const ir_entity *ent) {
1331 assert(is_method_entity((ir_entity *)ent));
1332 return ent->attr.mtd_attr.section;
1333 } /* get_method_img_section */
1335 /* Sets the section of a method. */
1336 void set_method_img_section(ir_entity *ent, ir_img_section section) {
1337 assert(is_method_entity(ent));
1338 ent->attr.mtd_attr.section = section;
1339 } /* set_method_img_section */
1342 (is_entity)(const void *thing) {
1343 return _is_entity(thing);
1346 int is_atomic_entity(ir_entity *ent) {
1347 ir_type *t = get_entity_type(ent);
1348 const tp_op *op = get_type_tpop(t);
1349 return (op == type_primitive || op == type_pointer ||
1350 op == type_enumeration || op == type_method);
1351 } /* is_atomic_entity */
1353 int is_compound_entity(ir_entity *ent) {
1354 ir_type *t = get_entity_type(ent);
1355 const tp_op *op = get_type_tpop(t);
1356 return (op == type_class || op == type_struct ||
1357 op == type_array || op == type_union);
1358 } /* is_compound_entity */
1360 int is_method_entity(ir_entity *ent) {
1361 ir_type *t = get_entity_type(ent);
1362 return is_Method_type(t);
1363 } /* is_method_entity */
1366 * @todo not implemented!!! */
1367 int equal_entity(ir_entity *ent1, ir_entity *ent2) {
1370 fprintf(stderr, " calling unimplemented equal entity!!! \n");
1372 } /* equal_entity */
1375 unsigned long (get_entity_visited)(ir_entity *ent) {
1376 return _get_entity_visited(ent);
1377 } /* get_entity_visited */
1379 void (set_entity_visited)(ir_entity *ent, unsigned long num) {
1380 _set_entity_visited(ent, num);
1381 } /* set_entity_visited */
1383 /* Sets visited field in ir_entity to entity_visited. */
1384 void (mark_entity_visited)(ir_entity *ent) {
1385 _mark_entity_visited(ent);
1386 } /* mark_entity_visited */
1388 int (entity_visited)(ir_entity *ent) {
1389 return _entity_visited(ent);
1390 } /* entity_visited */
1392 int (entity_not_visited)(ir_entity *ent) {
1393 return _entity_not_visited(ent);
1394 } /* entity_not_visited */
1396 /* Returns the mask of the additional entity properties. */
1397 unsigned get_entity_additional_properties(ir_entity *ent) {
1400 assert(is_method_entity(ent));
1402 /* first check, if the graph has additional properties */
1403 irg = get_entity_irg(ent);
1406 return get_irg_additional_properties(irg);
1408 if (ent->attr.mtd_attr.irg_add_properties & mtp_property_inherited)
1409 return get_method_additional_properties(get_entity_type(ent));
1411 return ent->attr.mtd_attr.irg_add_properties;
1412 } /* get_entity_additional_properties */
1414 /* Sets the mask of the additional graph properties. */
1415 void set_entity_additional_properties(ir_entity *ent, unsigned property_mask)
1419 assert(is_method_entity(ent));
1421 /* first check, if the graph exists */
1422 irg = get_entity_irg(ent);
1424 set_irg_additional_properties(irg, property_mask);
1426 /* do not allow to set the mtp_property_inherited flag or
1427 * the automatic inheritance of flags will not work */
1428 ent->attr.mtd_attr.irg_add_properties = property_mask & ~mtp_property_inherited;
1430 } /* set_entity_additional_properties */
1432 /* Sets one additional graph property. */
1433 void set_entity_additional_property(ir_entity *ent, mtp_additional_property flag)
1437 assert(is_method_entity(ent));
1439 /* first check, if the graph exists */
1440 irg = get_entity_irg(ent);
1442 set_irg_additional_property(irg, flag);
1444 unsigned mask = ent->attr.mtd_attr.irg_add_properties;
1446 if (mask & mtp_property_inherited)
1447 mask = get_method_additional_properties(get_entity_type(ent));
1449 /* do not allow to set the mtp_property_inherited flag or
1450 * the automatic inheritance of flags will not work */
1451 ent->attr.mtd_attr.irg_add_properties = mask | (flag & ~mtp_property_inherited);
1453 } /* set_entity_additional_property */
1455 /* Returns the class type that this type info entity represents or NULL
1456 if ent is no type info entity. */
1457 ir_type *(get_entity_repr_class)(const ir_entity *ent) {
1458 return _get_entity_repr_class(ent);
1459 } /* get_entity_repr_class */
1461 dbg_info *(get_entity_dbg_info)(const ir_entity *ent) {
1462 return _get_entity_dbg_info(ent);
1463 } /* get_entity_dbg_info */
1465 void (set_entity_dbg_info)(ir_entity *ent, dbg_info *db) {
1466 _set_entity_dbg_info(ent, db);
1467 } /* set_entity_dbg_info */
1469 /* Initialize entity module. */
1470 void firm_init_entity(void)
1472 symconst_symbol sym;
1474 assert(firm_unknown_type && "Call init_type() before firm_init_entity()!");
1475 assert(!unknown_entity && "Call firm_init_entity() only once!");
1477 unknown_entity = new_rd_entity(NULL, firm_unknown_type, new_id_from_str(UNKNOWN_ENTITY_NAME), firm_unknown_type);
1478 set_entity_visibility(unknown_entity, visibility_external_allocated);
1479 set_entity_ld_ident(unknown_entity, get_entity_ident(unknown_entity));
1481 current_ir_graph = get_const_code_irg();
1482 sym.entity_p = unknown_entity;
1483 /* TODO: we need two unknown_entities here, one for code and one for data */
1484 unknown_entity->value = new_SymConst(mode_P_data, sym, symconst_addr_ent);
1485 } /* firm_init_entity */