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 * Copies the entity if the new_owner is different from the
260 * owner of the old entity, else returns the old entity.
263 copy_entity_own(ir_entity *old, ir_type *new_owner) {
265 assert(is_entity(old));
266 assert(is_compound_type(new_owner));
267 assert(get_type_state(new_owner) != layout_fixed);
269 if (old->owner == new_owner) return old;
270 newe = xmalloc(sizeof(*newe));
271 memcpy(newe, old, sizeof(*newe));
272 newe->owner = new_owner;
273 if (is_Class_type(new_owner)) {
274 newe->overwrites = NEW_ARR_F(ir_entity *, 0);
275 newe->overwrittenby = NEW_ARR_F(ir_entity *, 0);
278 newe->nr = get_irp_new_node_nr();
281 insert_entity_in_owner(newe);
284 } /* copy_entity_own */
287 copy_entity_name(ir_entity *old, ident *new_name) {
289 assert(old && old->kind == k_entity);
291 if (old->name == new_name) return old;
292 newe = xmalloc(sizeof(*newe));
293 memcpy(newe, old, sizeof(*newe));
294 newe->name = new_name;
295 newe->ld_name = NULL;
296 if (is_Class_type(newe->owner)) {
297 newe->overwrites = DUP_ARR_F(ir_entity *, old->overwrites);
298 newe->overwrittenby = DUP_ARR_F(ir_entity *, old->overwrittenby);
301 newe->nr = get_irp_new_node_nr();
304 insert_entity_in_owner(newe);
307 } /* copy_entity_name */
311 free_entity(ir_entity *ent) {
312 assert(ent && ent->kind == k_entity);
313 free_entity_attrs(ent);
318 /* Outputs a unique number for this node */
320 get_entity_nr(const ir_entity *ent) {
321 assert(ent && ent->kind == k_entity);
325 return (long)PTR_TO_INT(ent);
327 } /* get_entity_nr */
330 (get_entity_name)(const ir_entity *ent) {
331 return _get_entity_name(ent);
332 } /* get_entity_name */
335 (get_entity_ident)(const ir_entity *ent) {
336 return _get_entity_ident(ent);
337 } /* get_entity_ident */
340 (set_entity_ident)(ir_entity *ent, ident *id) {
341 _set_entity_ident(ent, id);
342 } /* set_entity_ident */
345 (get_entity_owner)(ir_entity *ent) {
346 return _get_entity_owner(ent);
347 } /* get_entity_owner */
350 set_entity_owner(ir_entity *ent, ir_type *owner) {
351 assert(is_entity(ent));
352 assert(is_compound_type(owner));
354 } /* set_entity_owner */
357 (get_entity_ld_ident)(ir_entity *ent) {
358 return _get_entity_ld_ident(ent);
359 } /* get_entity_ld_ident */
362 (set_entity_ld_ident)(ir_entity *ent, ident *ld_ident) {
363 _set_entity_ld_ident(ent, ld_ident);
364 } /* set_entity_ld_ident */
367 (get_entity_ld_name)(ir_entity *ent) {
368 return _get_entity_ld_name(ent);
369 } /* get_entity_ld_name */
372 (get_entity_type)(ir_entity *ent) {
373 return _get_entity_type(ent);
374 } /* get_entity_type */
377 (set_entity_type)(ir_entity *ent, ir_type *type) {
378 _set_entity_type(ent, type);
379 } /* set_entity_type */
382 (get_entity_allocation)(const ir_entity *ent) {
383 return _get_entity_allocation(ent);
384 } /* get_entity_allocation */
387 (set_entity_allocation)(ir_entity *ent, ir_allocation al) {
388 _set_entity_allocation(ent, al);
389 } /* set_entity_allocation */
391 /* return the name of the visibility */
392 const char *get_allocation_name(ir_allocation all)
394 #define X(a) case a: return #a
396 X(allocation_automatic);
397 X(allocation_parameter);
398 X(allocation_dynamic);
399 X(allocation_static);
400 default: return "BAD VALUE";
403 } /* get_allocation_name */
406 (get_entity_visibility)(const ir_entity *ent) {
407 return _get_entity_visibility(ent);
408 } /* get_entity_visibility */
411 set_entity_visibility(ir_entity *ent, ir_visibility vis) {
412 assert(ent && ent->kind == k_entity);
413 if (vis != visibility_local)
414 assert((ent->allocation == allocation_static) ||
415 (ent->allocation == allocation_automatic));
416 /* @@@ Test that the owner type is not local, but how??
417 && get_class_visibility(get_entity_owner(ent)) != local));*/
418 ent->visibility = vis;
419 } /* set_entity_visibility */
421 /* return the name of the visibility */
422 const char *get_visibility_name(ir_visibility vis)
424 #define X(a) case a: return #a
427 X(visibility_external_visible);
428 X(visibility_external_allocated);
429 default: return "BAD VALUE";
432 } /* get_visibility_name */
435 (get_entity_variability)(const ir_entity *ent) {
436 return _get_entity_variability(ent);
437 } /* get_entity_variability */
440 set_entity_variability(ir_entity *ent, ir_variability var)
442 assert(ent && ent->kind == k_entity);
443 if (var == variability_part_constant)
444 assert(is_Class_type(ent->type) || is_Struct_type(ent->type));
446 if ((is_compound_type(ent->type)) &&
447 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
448 /* Allocate data structures for constant values */
449 ent->attr.cmpd_attr.values = NEW_ARR_F(ir_node *, 0);
450 ent->attr.cmpd_attr.val_paths = NEW_ARR_F(compound_graph_path *, 0);
452 if ((is_atomic_type(ent->type)) &&
453 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
454 /* Set default constant value. */
455 ent->value = new_rd_Unknown(get_const_code_irg(), get_type_mode(ent->type));
458 if ((is_compound_type(ent->type)) &&
459 (var == variability_uninitialized) && (ent->variability != variability_uninitialized)) {
460 /* Free data structures for constant values */
461 DEL_ARR_F(ent->attr.cmpd_attr.values); ent->attr.cmpd_attr.values = NULL;
462 DEL_ARR_F(ent->attr.cmpd_attr.val_paths); ent->attr.cmpd_attr.val_paths = NULL;
464 ent->variability = var;
465 } /* set_entity_variability */
467 /* return the name of the variability */
468 const char *get_variability_name(ir_variability var)
470 #define X(a) case a: return #a
472 X(variability_uninitialized);
473 X(variability_initialized);
474 X(variability_part_constant);
475 X(variability_constant);
476 default: return "BAD VALUE";
479 } /* get_variability_name */
482 (get_entity_volatility)(const ir_entity *ent) {
483 return _get_entity_volatility(ent);
484 } /* get_entity_volatility */
487 (set_entity_volatility)(ir_entity *ent, ir_volatility vol) {
488 _set_entity_volatility(ent, vol);
489 } /* set_entity_volatility */
491 /* Return the name of the volatility. */
492 const char *get_volatility_name(ir_volatility var)
494 #define X(a) case a: return #a
496 X(volatility_non_volatile);
497 X(volatility_is_volatile);
498 default: return "BAD VALUE";
501 } /* get_volatility_name */
504 (get_entity_align)(const ir_entity *ent) {
505 return _get_entity_align(ent);
506 } /* get_entity_align */
509 (set_entity_align)(ir_entity *ent, ir_align a) {
510 _set_entity_align(ent, a);
511 } /* set_entity_align */
513 /* Return the name of the alignment. */
514 const char *get_align_name(ir_align a)
516 #define X(a) case a: return #a
518 X(align_non_aligned);
520 default: return "BAD VALUE";
523 } /* get_align_name */
526 (get_entity_peculiarity)(const ir_entity *ent) {
527 return _get_entity_peculiarity(ent);
528 } /* get_entity_peculiarity */
531 (set_entity_peculiarity)(ir_entity *ent, ir_peculiarity pec) {
532 _set_entity_peculiarity(ent, pec);
533 } /* set_entity_peculiarity */
535 /* Checks if an entity cannot be overridden anymore. */
536 int (is_entity_final)(const ir_entity *ent) {
537 return _is_entity_final(ent);
538 } /* is_entity_final */
540 /* Sets/resets the final flag of an entity. */
541 void (set_entity_final)(ir_entity *ent, int final) {
542 _set_entity_final(ent, final);
543 } /* set_entity_final */
545 /* Checks if an entity is compiler generated */
546 int (is_entity_compiler_generated)(const ir_entity *ent) {
547 return _is_entity_compiler_generated(ent);
548 } /* is_entity_compiler_generated */
550 /* Sets/resets the compiler generated flag */
551 void (set_entity_compiler_generated)(ir_entity *ent, int flag) {
552 _set_entity_compiler_generated(ent, flag);
553 } /* set_entity_compiler_generated */
555 /* Checks if an entity is marked by the backend */
556 int (is_entity_backend_marked)(const ir_entity *ent) {
557 return _is_entity_backend_marked(ent);
558 } /* is_entity_backend_marked */
560 /* Sets/resets the compiler generated flag */
561 void (set_entity_backend_marked)(ir_entity *ent, int flag) {
562 _set_entity_backend_marked(ent, flag);
563 } /* set_entity_backend_marked */
565 /* Checks if the address of an entity was taken. */
566 ir_address_taken_state (get_entity_address_taken)(const ir_entity *ent) {
567 return _get_entity_address_taken(ent);
568 } /* is_entity_address_taken */
570 /* Sets/resets the address taken flag. */
571 void (set_entity_address_taken)(ir_entity *ent, ir_address_taken_state flag) {
572 _set_entity_address_taken(ent, flag);
573 } /* set_entity_address_taken */
575 /* Return the name of the address_taken state. */
576 const char *get_address_taken_state_name(ir_address_taken_state state) {
577 #define X(a) case a: return #a
579 X(ir_address_not_taken);
580 X(ir_address_taken_unknown);
582 default: return "BAD VALUE";
585 } /* get_address_taken_state_name */
587 /* Get the entity's stickyness */
589 (get_entity_stickyness)(const ir_entity *ent) {
590 return _get_entity_stickyness(ent);
591 } /* get_entity_stickyness */
593 /* Set the entity's stickyness */
595 (set_entity_stickyness)(ir_entity *ent, ir_stickyness stickyness) {
596 _set_entity_stickyness(ent, stickyness);
597 } /* set_entity_stickyness */
599 /* Set has no effect for existent entities of type method. */
601 get_atomic_ent_value(ir_entity *ent)
603 assert(ent && is_atomic_entity(ent));
604 assert(ent->variability != variability_uninitialized);
605 return skip_Id(ent->value);
606 } /* get_atomic_ent_value */
609 set_atomic_ent_value(ir_entity *ent, ir_node *val) {
610 assert(is_atomic_entity(ent) && (ent->variability != variability_uninitialized));
611 if (is_Method_type(ent->type) && (ent->peculiarity == peculiarity_existent))
613 assert(get_irn_mode(val) == get_type_mode(ent->type));
615 } /* set_atomic_ent_value */
617 /* Returns true if the the node is representable as code on
619 int is_irn_const_expression(ir_node *n) {
622 /* we are in danger iff an exception will arise. TODO: be more precisely,
623 * for instance Div. will NOT rise if divisor != 0
625 if (is_binop(n) && !is_fragile_op(n))
626 return is_irn_const_expression(get_binop_left(n)) && is_irn_const_expression(get_binop_right(n));
629 switch (get_irn_opcode(n)) {
636 return is_irn_const_expression(get_irn_n(n, 0));
641 } /* is_irn_const_expression */
644 * Copies a firm subgraph that complies to the restrictions for
645 * constant expressions to current_block in current_ir_graph.
647 ir_node *copy_const_value(dbg_info *dbg, ir_node *n) {
651 /* @@@ GL I think we should implement this using the routines from irgopt for
652 dead node elimination/inlineing. */
655 switch (get_irn_opcode(n)) {
657 nn = new_d_Const_type(dbg, m, get_Const_tarval(n), get_Const_type(n));
660 nn = new_d_SymConst_type(dbg, get_irn_mode(n), get_SymConst_symbol(n), get_SymConst_kind(n),
661 get_SymConst_value_type(n));
664 nn = new_d_Add(dbg, copy_const_value(dbg, get_Add_left(n)),
665 copy_const_value(dbg, get_Add_right(n)), m); break;
667 nn = new_d_Sub(dbg, copy_const_value(dbg, get_Sub_left(n)),
668 copy_const_value(dbg, get_Sub_right(n)), m); break;
670 nn = new_d_Mul(dbg, copy_const_value(dbg, get_Mul_left(n)),
671 copy_const_value(dbg, get_Mul_right(n)), m); break;
673 nn = new_d_And(dbg, copy_const_value(dbg, get_And_left(n)),
674 copy_const_value(dbg, get_And_right(n)), m); break;
676 nn = new_d_Or(dbg, copy_const_value(dbg, get_Or_left(n)),
677 copy_const_value(dbg, get_Or_right(n)), m); break;
679 nn = new_d_Eor(dbg, copy_const_value(dbg, get_Eor_left(n)),
680 copy_const_value(dbg, get_Eor_right(n)), m); break;
682 nn = new_d_Cast(dbg, copy_const_value(dbg, get_Cast_op(n)), get_Cast_type(n)); break;
684 nn = new_d_Conv(dbg, copy_const_value(dbg, get_Conv_op(n)), m); break;
686 nn = new_d_Unknown(m); break;
688 assert(0 && "opcode invalid or not implemented");
693 } /* copy_const_value */
695 static ir_initializer_t null_initializer = { IR_INITIALIZER_NULL };
697 ir_initializer_t *get_initializer_null(void)
699 return &null_initializer;
702 ir_initializer_t *create_initializer_const(ir_node *value)
704 struct obstack *obst = get_irg_obstack(get_const_code_irg());
706 ir_initializer_t *initializer
707 = obstack_alloc(obst, sizeof(ir_initializer_const_t));
708 initializer->kind = IR_INITIALIZER_CONST;
709 initializer->consti.value = value;
714 ir_initializer_t *create_initializer_tarval(tarval *tv)
716 struct obstack *obst = get_irg_obstack(get_const_code_irg());
718 ir_initializer_t *initializer
719 = obstack_alloc(obst, sizeof(ir_initializer_tarval_t));
720 initializer->kind = IR_INITIALIZER_TARVAL;
721 initializer->tarval.value = tv;
726 ir_initializer_t *create_initializer_compound(unsigned n_entries)
728 struct obstack *obst = get_irg_obstack(get_const_code_irg());
731 size_t size = sizeof(ir_initializer_compound_t)
732 + (n_entries-1) * sizeof(ir_initializer_t*);
734 ir_initializer_t *initializer = obstack_alloc(obst, size);
735 initializer->kind = IR_INITIALIZER_COMPOUND;
736 initializer->compound.n_initializers = n_entries;
738 for(i = 0; i < n_entries; ++i) {
739 initializer->compound.initializers[i] = get_initializer_null();
745 ir_node *get_initializer_const_value(const ir_initializer_t *initializer)
747 assert(initializer->kind == IR_INITIALIZER_CONST);
748 return initializer->consti.value;
751 tarval *get_initializer_tarval_value(const ir_initializer_t *initializer)
753 assert(initializer->kind == IR_INITIALIZER_TARVAL);
754 return initializer->tarval.value;
757 unsigned get_initializer_compound_n_entries(const ir_initializer_t *initializer)
759 assert(initializer->kind == IR_INITIALIZER_COMPOUND);
760 return initializer->compound.n_initializers;
763 void set_initializer_compound_value(ir_initializer_t *initializer,
764 unsigned index, ir_initializer_t *value)
766 assert(initializer->kind == IR_INITIALIZER_COMPOUND);
767 assert(index < initializer->compound.n_initializers);
769 initializer->compound.initializers[index] = value;
772 ir_initializer_t *get_initializer_compound_value(
773 const ir_initializer_t *initializer, unsigned index)
775 assert(initializer->kind == IR_INITIALIZER_COMPOUND);
776 assert(index < initializer->compound.n_initializers);
778 return initializer->compound.initializers[index];
781 ir_initializer_kind_t get_initializer_kind(const ir_initializer_t *initializer)
783 return initializer->kind;
786 static void check_entity_initializer(ir_entity *entity)
792 void set_entity_initializer(ir_entity *entity, ir_initializer_t *initializer)
794 entity->attr.initializer = initializer;
795 entity->has_initializer = 1;
796 check_entity_initializer(entity);
799 ir_initializer_t *get_entity_initializer(const ir_entity *entity)
801 assert(entity->has_initializer);
802 return entity->attr.initializer;
805 /* Creates a new compound graph path. */
806 compound_graph_path *
807 new_compound_graph_path(ir_type *tp, int length) {
808 compound_graph_path *res;
810 assert(is_compound_type(tp));
813 res = xmalloc(sizeof(*res) + (length-1) * sizeof(res->list[0]));
814 memset(res, 0, sizeof(*res) + (length-1) * sizeof(res->list[0]));
815 res->kind = k_ir_compound_graph_path;
820 } /* new_compound_graph_path */
822 /* Frees an graph path object */
823 void free_compound_graph_path (compound_graph_path *gr) {
824 assert(gr && is_compound_graph_path(gr));
827 } /* free_compound_graph_path */
829 /* Returns non-zero if an object is a compound graph path */
830 int is_compound_graph_path(const void *thing) {
831 return (get_kind(thing) == k_ir_compound_graph_path);
832 } /* is_compound_graph_path */
834 /* Checks whether the path up to pos is correct. If the path contains a NULL,
835 * assumes the path is not complete and returns 'true'. */
836 int is_proper_compound_graph_path(compound_graph_path *gr, int pos) {
839 ir_type *owner = gr->tp;
841 for (i = 0; i <= pos; i++) {
842 node = get_compound_graph_path_node(gr, i);
844 /* Path not yet complete. */
846 if (get_entity_owner(node) != owner)
848 owner = get_entity_type(node);
850 if (pos == get_compound_graph_path_length(gr))
851 if (!is_atomic_type(owner))
854 } /* is_proper_compound_graph_path */
856 /* Returns the length of a graph path */
857 int get_compound_graph_path_length(const compound_graph_path *gr) {
858 assert(gr && is_compound_graph_path(gr));
860 } /* get_compound_graph_path_length */
863 get_compound_graph_path_node(const compound_graph_path *gr, int pos) {
864 assert(gr && is_compound_graph_path(gr));
865 assert(pos >= 0 && pos < gr->len);
866 return gr->list[pos].node;
867 } /* get_compound_graph_path_node */
870 set_compound_graph_path_node(compound_graph_path *gr, int pos, ir_entity *node) {
871 assert(gr && is_compound_graph_path(gr));
872 assert(pos >= 0 && pos < gr->len);
873 assert(is_entity(node));
874 gr->list[pos].node = node;
875 assert(is_proper_compound_graph_path(gr, pos));
876 } /* set_compound_graph_path_node */
879 get_compound_graph_path_array_index(const compound_graph_path *gr, int pos) {
880 assert(gr && is_compound_graph_path(gr));
881 assert(pos >= 0 && pos < gr->len);
882 return gr->list[pos].index;
883 } /* get_compound_graph_path_array_index */
886 set_compound_graph_path_array_index(compound_graph_path *gr, int pos, int index) {
887 assert(gr && is_compound_graph_path(gr));
888 assert(pos >= 0 && pos < gr->len);
889 gr->list[pos].index = index;
890 } /* set_compound_graph_path_array_index */
892 /* A value of a compound entity is a pair of value and the corresponding path to a member of
895 add_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path) {
896 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
897 assert(is_compound_graph_path(path));
898 ARR_APP1(ir_node *, ent->attr.cmpd_attr.values, val);
899 ARR_APP1(compound_graph_path *, ent->attr.cmpd_attr.val_paths, path);
900 } /* add_compound_ent_value_w_path */
903 set_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path, int pos) {
904 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
905 assert(is_compound_graph_path(path));
906 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
907 ent->attr.cmpd_attr.values[pos] = val;
908 ent->attr.cmpd_attr.val_paths[pos] = path;
909 } /* set_compound_ent_value_w_path */
912 get_compound_ent_n_values(ir_entity *ent) {
913 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
914 return ARR_LEN(ent->attr.cmpd_attr.values);
915 } /* get_compound_ent_n_values */
918 get_compound_ent_value(ir_entity *ent, int pos) {
919 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
920 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
921 return ent->attr.cmpd_attr.values[pos];
922 } /* get_compound_ent_value */
924 compound_graph_path *
925 get_compound_ent_value_path(ir_entity *ent, int pos) {
926 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
927 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.val_paths));
928 return ent->attr.cmpd_attr.val_paths[pos];
929 } /* get_compound_ent_value_path */
932 * Returns non-zero, if two compound_graph_pathes are equal
934 * @param path1 the first path
935 * @param visited_indices
936 * @param path2 the second path
938 static int equal_paths(compound_graph_path *path1, int *visited_indices, compound_graph_path *path2) {
940 int len1 = get_compound_graph_path_length(path1);
941 int len2 = get_compound_graph_path_length(path2);
943 if (len2 != len1) return 0;
945 for (i = 0; i < len1; i++) {
947 ir_entity *node1 = get_compound_graph_path_node(path1, i);
948 ir_entity *node2 = get_compound_graph_path_node(path2, i);
950 if (node1 != node2) return 0;
952 /* FIXME: Strange code. What is it good for? */
953 tp = get_entity_owner(node1);
954 if (is_Array_type(tp)) {
957 /* Compute the index of this node. */
958 assert(get_array_n_dimensions(tp) == 1 && "multidim not implemented");
960 low = get_array_lower_bound_int(tp, 0);
961 if (low + visited_indices[i] < get_compound_graph_path_array_index(path2, i)) {
962 visited_indices[i]++;
965 assert(low + visited_indices[i] == get_compound_graph_path_array_index(path2, i));
972 * Returns the position of a value with the given path.
973 * The path must contain array indices for all array element entities.
975 * @todo This implementation is very slow (O(number of initializers^2) and should
976 * be replaced when the new tree oriented
977 * value representation is finally implemented.
979 static int get_compound_ent_pos_by_path(ir_entity *ent, compound_graph_path *path) {
980 int i, n_paths = get_compound_ent_n_values(ent);
981 int *visited_indices;
982 int path_len = get_compound_graph_path_length(path);
984 NEW_ARR_A(int *, visited_indices, path_len);
985 memset(visited_indices, 0, sizeof(*visited_indices) * path_len);
986 for (i = 0; i < n_paths; i ++) {
987 if (equal_paths(get_compound_ent_value_path(ent, i), visited_indices, path))
994 printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
995 printf("Entity %s : ", get_entity_name(ent));
996 for (j = 0; j < get_compound_graph_path_length(path); ++j) {
997 ir_entity *node = get_compound_graph_path_node(path, j);
998 printf("%s", get_entity_name(node));
999 if (is_Array_type(get_entity_owner(node)))
1000 printf("[%d]", get_compound_graph_path_array_index(path, j));
1002 printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
1006 assert(0 && "path not found");
1008 } /* get_compound_ent_pos_by_path */
1010 /* Returns a constant value given the access path.
1011 * The path must contain array indices for all array element entities. */
1012 ir_node *get_compound_ent_value_by_path(ir_entity *ent, compound_graph_path *path) {
1013 return get_compound_ent_value(ent, get_compound_ent_pos_by_path(ent, path));
1014 } /* get_compound_ent_value_by_path */
1018 remove_compound_ent_value(ir_entity *ent, ir_entity *value_ent) {
1020 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
1022 n = ARR_LEN(ent->attr.cmpd_attr.val_paths);
1023 for (i = 0; i < n; ++i) {
1024 compound_graph_path *path = ent->attr.cmpd_attr.val_paths[i];
1025 if (path->list[path->len-1].node == value_ent) {
1026 for (; i < n - 1; ++i) {
1027 ent->attr.cmpd_attr.val_paths[i] = ent->attr.cmpd_attr.val_paths[i+1];
1028 ent->attr.cmpd_attr.values[i] = ent->attr.cmpd_attr.values[i+1];
1030 ARR_SETLEN(ir_entity*, ent->attr.cmpd_attr.val_paths, n - 1);
1031 ARR_SETLEN(ir_node*, ent->attr.cmpd_attr.values, n - 1);
1035 } /* remove_compound_ent_value */
1038 add_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member) {
1039 compound_graph_path *path;
1040 ir_type *owner_tp = get_entity_owner(member);
1041 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
1042 path = new_compound_graph_path(get_entity_type(ent), 1);
1043 path->list[0].node = member;
1044 if (is_Array_type(owner_tp)) {
1048 assert(get_array_n_dimensions(owner_tp) == 1 && has_array_lower_bound(owner_tp, 0));
1049 max = get_array_lower_bound_int(owner_tp, 0) -1;
1050 for (i = 0, n = get_compound_ent_n_values(ent); i < n; ++i) {
1051 int index = get_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0);
1056 path->list[0].index = max + 1;
1058 add_compound_ent_value_w_path(ent, val, path);
1059 } /* add_compound_ent_value */
1063 get_compound_ent_value_member(ir_entity *ent, int pos) {
1064 compound_graph_path *path;
1065 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
1066 path = get_compound_ent_value_path(ent, pos);
1068 return get_compound_graph_path_node(path, get_compound_graph_path_length(path)-1);
1069 } /* get_compound_ent_value_member */
1072 set_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member, int pos) {
1073 compound_graph_path *path;
1074 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
1075 path = get_compound_ent_value_path(ent, pos);
1076 set_compound_graph_path_node(path, 0, member);
1077 set_compound_ent_value_w_path(ent, val, path, pos);
1078 } /* set_compound_ent_value */
1081 set_array_entity_values(ir_entity *ent, tarval **values, int num_vals) {
1083 ir_graph *rem = current_ir_graph;
1084 ir_type *arrtp = get_entity_type(ent);
1086 ir_type *elttp = get_array_element_type(arrtp);
1088 assert(is_Array_type(arrtp));
1089 assert(get_array_n_dimensions(arrtp) == 1);
1090 /* One bound is sufficient, the number of constant fields makes the
1092 assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
1093 assert(get_entity_variability(ent) != variability_uninitialized);
1094 current_ir_graph = get_const_code_irg();
1096 for (i = 0; i < num_vals; i++) {
1097 val = new_Const_type(values[i], elttp);
1098 add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
1099 set_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0, i);
1101 current_ir_graph = rem;
1102 } /* set_array_entity_values */
1104 /* Return the overall offset of value at position pos in bytes. */
1105 unsigned get_compound_ent_value_offset_bytes(ir_entity *ent, int pos) {
1106 compound_graph_path *path;
1108 unsigned offset = 0;
1111 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
1113 path = get_compound_ent_value_path(ent, pos);
1114 path_len = get_compound_graph_path_length(path);
1117 for (i = 0; i < path_len; ++i) {
1118 if (is_Array_type(curr_tp)) {
1119 ir_type *elem_type = get_array_element_type(curr_tp);
1120 unsigned size = get_type_size_bytes(elem_type);
1121 unsigned align = get_type_alignment_bytes(elem_type);
1125 if(size % align > 0) {
1126 size += align - (size % align);
1128 idx = get_compound_graph_path_array_index(path, i);
1130 offset += size * idx;
1131 curr_tp = elem_type;
1133 ir_entity *node = get_compound_graph_path_node(path, i);
1134 offset += get_entity_offset(node);
1135 curr_tp = get_entity_type(node);
1140 } /* get_compound_ent_value_offset_bytes */
1142 /* Return the offset in bits from the last byte address. */
1143 unsigned get_compound_ent_value_offset_bit_remainder(ir_entity *ent, int pos) {
1144 compound_graph_path *path;
1146 ir_entity *last_node;
1148 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
1150 path = get_compound_ent_value_path(ent, pos);
1151 path_len = get_compound_graph_path_length(path);
1152 last_node = get_compound_graph_path_node(path, path_len - 1);
1154 if(last_node == NULL)
1157 return get_entity_offset_bits_remainder(last_node);
1158 } /* get_compound_ent_value_offset_bit_remainder */
1161 (get_entity_offset)(const ir_entity *ent) {
1162 return _get_entity_offset(ent);
1163 } /* get_entity_offset */
1166 (set_entity_offset)(ir_entity *ent, int offset) {
1167 _set_entity_offset(ent, offset);
1168 } /* set_entity_offset */
1171 (get_entity_offset_bits_remainder)(const ir_entity *ent) {
1172 return _get_entity_offset_bits_remainder(ent);
1173 } /* get_entity_offset_bits_remainder */
1176 (set_entity_offset_bits_remainder)(ir_entity *ent, unsigned char offset) {
1177 _set_entity_offset_bits_remainder(ent, offset);
1178 } /* set_entity_offset_bits_remainder */
1181 add_entity_overwrites(ir_entity *ent, ir_entity *overwritten) {
1183 ir_type *owner = get_entity_owner(ent);
1184 ir_type *ovw_ovner = get_entity_owner(overwritten);
1185 assert(is_Class_type(owner));
1186 assert(is_Class_type(ovw_ovner));
1187 assert(! is_class_final(ovw_ovner));
1189 ARR_APP1(ir_entity *, ent->overwrites, overwritten);
1190 ARR_APP1(ir_entity *, overwritten->overwrittenby, ent);
1191 } /* add_entity_overwrites */
1194 get_entity_n_overwrites(ir_entity *ent) {
1195 assert(is_Class_type(get_entity_owner(ent)));
1196 return (ARR_LEN(ent->overwrites));
1197 } /* get_entity_n_overwrites */
1200 get_entity_overwrites_index(ir_entity *ent, ir_entity *overwritten) {
1202 assert(is_Class_type(get_entity_owner(ent)));
1203 n = get_entity_n_overwrites(ent);
1204 for (i = 0; i < n; ++i) {
1205 if (get_entity_overwrites(ent, i) == overwritten)
1209 } /* get_entity_overwrites_index */
1212 get_entity_overwrites(ir_entity *ent, int pos) {
1213 assert(is_Class_type(get_entity_owner(ent)));
1214 assert(pos < get_entity_n_overwrites(ent));
1215 return ent->overwrites[pos];
1216 } /* get_entity_overwrites */
1219 set_entity_overwrites(ir_entity *ent, int pos, ir_entity *overwritten) {
1220 assert(is_Class_type(get_entity_owner(ent)));
1221 assert(pos < get_entity_n_overwrites(ent));
1222 ent->overwrites[pos] = overwritten;
1223 } /* set_entity_overwrites */
1226 remove_entity_overwrites(ir_entity *ent, ir_entity *overwritten) {
1228 assert(is_Class_type(get_entity_owner(ent)));
1229 n = ARR_LEN(ent->overwrites);
1230 for (i = 0; i < n; ++i) {
1231 if (ent->overwrites[i] == overwritten) {
1232 for (; i < n - 1; i++)
1233 ent->overwrites[i] = ent->overwrites[i+1];
1234 ARR_SETLEN(ir_entity*, ent->overwrites, n - 1);
1238 } /* remove_entity_overwrites */
1241 add_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites) {
1242 add_entity_overwrites(overwrites, ent);
1243 } /* add_entity_overwrittenby */
1246 get_entity_n_overwrittenby(ir_entity *ent) {
1247 assert(is_Class_type(get_entity_owner(ent)));
1248 return ARR_LEN(ent->overwrittenby);
1249 } /* get_entity_n_overwrittenby */
1252 get_entity_overwrittenby_index(ir_entity *ent, ir_entity *overwrites) {
1254 assert(is_Class_type(get_entity_owner(ent)));
1255 n = get_entity_n_overwrittenby(ent);
1256 for (i = 0; i < n; ++i) {
1257 if (get_entity_overwrittenby(ent, i) == overwrites)
1261 } /* get_entity_overwrittenby_index */
1264 get_entity_overwrittenby(ir_entity *ent, int pos) {
1265 assert(is_Class_type(get_entity_owner(ent)));
1266 assert(pos < get_entity_n_overwrittenby(ent));
1267 return ent->overwrittenby[pos];
1268 } /* get_entity_overwrittenby */
1271 set_entity_overwrittenby(ir_entity *ent, int pos, ir_entity *overwrites) {
1272 assert(is_Class_type(get_entity_owner(ent)));
1273 assert(pos < get_entity_n_overwrittenby(ent));
1274 ent->overwrittenby[pos] = overwrites;
1275 } /* set_entity_overwrittenby */
1277 void remove_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites) {
1279 assert(is_Class_type(get_entity_owner(ent)));
1281 n = ARR_LEN(ent->overwrittenby);
1282 for (i = 0; i < n; ++i) {
1283 if (ent->overwrittenby[i] == overwrites) {
1284 for(; i < n - 1; ++i)
1285 ent->overwrittenby[i] = ent->overwrittenby[i+1];
1286 ARR_SETLEN(ir_entity*, ent->overwrittenby, n - 1);
1290 } /* remove_entity_overwrittenby */
1292 /* A link to store intermediate information */
1294 (get_entity_link)(const ir_entity *ent) {
1295 return _get_entity_link(ent);
1296 } /* get_entity_link */
1299 (set_entity_link)(ir_entity *ent, void *l) {
1300 _set_entity_link(ent, l);
1301 } /* set_entity_link */
1304 (get_entity_irg)(const ir_entity *ent) {
1305 return _get_entity_irg(ent);
1306 } /* get_entity_irg */
1309 set_entity_irg(ir_entity *ent, ir_graph *irg) {
1310 assert(is_method_entity(ent));
1311 /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
1312 * Methode selbst nicht mehr aufgerufen werden kann, die Entität
1313 * aber erhalten bleiben soll? Wandle die Entitaet in description oder
1316 assert((irg && ent->peculiarity == peculiarity_existent) ||
1317 (!irg && (ent->peculiarity == peculiarity_existent)
1318 && (ent -> visibility == visibility_external_allocated)) ||
1319 (!irg && ent->peculiarity == peculiarity_description) ||
1320 (!irg && ent->peculiarity == peculiarity_inherited));
1321 ent->attr.mtd_attr.irg = irg;
1322 } /* set_entity_irg */
1324 unsigned get_entity_vtable_number(const ir_entity *ent) {
1325 assert(is_method_entity((ir_entity *)ent));
1326 return ent->attr.mtd_attr.vtable_number;
1327 } /* get_entity_vtable_number */
1329 void set_entity_vtable_number(ir_entity *ent, unsigned vtable_number) {
1330 assert(is_method_entity(ent));
1331 ent->attr.mtd_attr.vtable_number = vtable_number;
1332 } /* set_entity_vtable_number */
1334 /* Returns the section of a method. */
1335 ir_img_section get_method_img_section(const ir_entity *ent) {
1336 assert(is_method_entity((ir_entity *)ent));
1337 return ent->attr.mtd_attr.section;
1338 } /* get_method_img_section */
1340 /* Sets the section of a method. */
1341 void set_method_img_section(ir_entity *ent, ir_img_section section) {
1342 assert(is_method_entity(ent));
1343 ent->attr.mtd_attr.section = section;
1344 } /* set_method_img_section */
1347 (is_entity)(const void *thing) {
1348 return _is_entity(thing);
1351 int is_atomic_entity(ir_entity *ent) {
1352 ir_type *t = get_entity_type(ent);
1353 const tp_op *op = get_type_tpop(t);
1354 return (op == type_primitive || op == type_pointer ||
1355 op == type_enumeration || op == type_method);
1356 } /* is_atomic_entity */
1358 int is_compound_entity(ir_entity *ent) {
1359 ir_type *t = get_entity_type(ent);
1360 const tp_op *op = get_type_tpop(t);
1361 return (op == type_class || op == type_struct ||
1362 op == type_array || op == type_union);
1363 } /* is_compound_entity */
1365 int is_method_entity(ir_entity *ent) {
1366 ir_type *t = get_entity_type(ent);
1367 return is_Method_type(t);
1368 } /* is_method_entity */
1371 * @todo not implemented!!! */
1372 int equal_entity(ir_entity *ent1, ir_entity *ent2) {
1375 fprintf(stderr, " calling unimplemented equal entity!!! \n");
1377 } /* equal_entity */
1380 unsigned long (get_entity_visited)(ir_entity *ent) {
1381 return _get_entity_visited(ent);
1382 } /* get_entity_visited */
1384 void (set_entity_visited)(ir_entity *ent, unsigned long num) {
1385 _set_entity_visited(ent, num);
1386 } /* set_entity_visited */
1388 /* Sets visited field in ir_entity to entity_visited. */
1389 void (mark_entity_visited)(ir_entity *ent) {
1390 _mark_entity_visited(ent);
1391 } /* mark_entity_visited */
1393 int (entity_visited)(ir_entity *ent) {
1394 return _entity_visited(ent);
1395 } /* entity_visited */
1397 int (entity_not_visited)(ir_entity *ent) {
1398 return _entity_not_visited(ent);
1399 } /* entity_not_visited */
1401 /* Returns the mask of the additional entity properties. */
1402 unsigned get_entity_additional_properties(ir_entity *ent) {
1405 assert(is_method_entity(ent));
1407 /* first check, if the graph has additional properties */
1408 irg = get_entity_irg(ent);
1411 return get_irg_additional_properties(irg);
1413 if (ent->attr.mtd_attr.irg_add_properties & mtp_property_inherited)
1414 return get_method_additional_properties(get_entity_type(ent));
1416 return ent->attr.mtd_attr.irg_add_properties;
1417 } /* get_entity_additional_properties */
1419 /* Sets the mask of the additional graph properties. */
1420 void set_entity_additional_properties(ir_entity *ent, unsigned property_mask)
1424 assert(is_method_entity(ent));
1426 /* first check, if the graph exists */
1427 irg = get_entity_irg(ent);
1429 set_irg_additional_properties(irg, property_mask);
1431 /* do not allow to set the mtp_property_inherited flag or
1432 * the automatic inheritance of flags will not work */
1433 ent->attr.mtd_attr.irg_add_properties = property_mask & ~mtp_property_inherited;
1435 } /* set_entity_additional_properties */
1437 /* Sets one additional graph property. */
1438 void set_entity_additional_property(ir_entity *ent, mtp_additional_property flag)
1442 assert(is_method_entity(ent));
1444 /* first check, if the graph exists */
1445 irg = get_entity_irg(ent);
1447 set_irg_additional_property(irg, flag);
1449 unsigned mask = ent->attr.mtd_attr.irg_add_properties;
1451 if (mask & mtp_property_inherited)
1452 mask = get_method_additional_properties(get_entity_type(ent));
1454 /* do not allow to set the mtp_property_inherited flag or
1455 * the automatic inheritance of flags will not work */
1456 ent->attr.mtd_attr.irg_add_properties = mask | (flag & ~mtp_property_inherited);
1458 } /* set_entity_additional_property */
1460 /* Returns the class type that this type info entity represents or NULL
1461 if ent is no type info entity. */
1462 ir_type *(get_entity_repr_class)(const ir_entity *ent) {
1463 return _get_entity_repr_class(ent);
1464 } /* get_entity_repr_class */
1466 dbg_info *(get_entity_dbg_info)(const ir_entity *ent) {
1467 return _get_entity_dbg_info(ent);
1468 } /* get_entity_dbg_info */
1470 void (set_entity_dbg_info)(ir_entity *ent, dbg_info *db) {
1471 _set_entity_dbg_info(ent, db);
1472 } /* set_entity_dbg_info */
1474 /* Initialize entity module. */
1475 void firm_init_entity(void)
1477 symconst_symbol sym;
1479 assert(firm_unknown_type && "Call init_type() before firm_init_entity()!");
1480 assert(!unknown_entity && "Call firm_init_entity() only once!");
1482 unknown_entity = new_rd_entity(NULL, firm_unknown_type, new_id_from_str(UNKNOWN_ENTITY_NAME), firm_unknown_type);
1483 set_entity_visibility(unknown_entity, visibility_external_allocated);
1484 set_entity_ld_ident(unknown_entity, get_entity_ident(unknown_entity));
1486 current_ir_graph = get_const_code_irg();
1487 sym.entity_p = unknown_entity;
1488 /* TODO: we need two unknown_entities here, one for code and one for data */
1489 unknown_entity->value = new_SymConst(mode_P_data, sym, symconst_addr_ent);
1490 } /* firm_init_entity */