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
32 #include "firm_common_t.h"
45 #include "irgraph_t.h"
46 #include "callgraph.h"
49 /*-----------------------------------------------------------------*/
51 /*-----------------------------------------------------------------*/
53 ir_entity *unknown_entity = NULL;
55 ir_entity *get_unknown_entity(void) { return unknown_entity; }
57 /** The name of the unknown entity. */
58 #define UNKNOWN_ENTITY_NAME "unknown_entity"
60 /*-----------------------------------------------------------------*/
62 /*-----------------------------------------------------------------*/
65 * Add an entity to it's already set owner type.
67 static inline void insert_entity_in_owner(ir_entity *ent) {
68 ir_type *owner = ent->owner;
69 switch (get_type_tpop_code(owner)) {
71 add_class_member(owner, ent);
74 add_struct_member(owner, ent);
77 add_union_member(owner, ent);
80 set_array_element_entity(owner, ent);
83 panic("Unsupported type kind");
85 } /* insert_entity_in_owner */
88 * Creates a new entity. This entity is NOT inserted in the owner type.
90 * @param db debug info for this entity
91 * @param owner the owner type of the new entity
92 * @param name the name of the new entity
93 * @param type the type of the new entity
95 * @return the new created entity
97 static inline ir_entity *
98 new_rd_entity(dbg_info *db, ir_type *owner, ident *name, ir_type *type)
103 assert(!id_contains_char(name, ' ') && "entity name should not contain spaces");
105 res = XMALLOCZ(ir_entity);
107 res->kind = k_entity;
113 res->allocation = allocation_automatic;
114 res->visibility = visibility_local;
115 res->volatility = volatility_non_volatile;
116 res->aligned = align_is_aligned;
117 res->stickyness = stickyness_unsticky;
118 res->peculiarity = peculiarity_existent;
119 res->usage = ir_usage_unknown;
121 res->compiler_gen = 0;
122 res->backend_marked = 0;
124 res->offset_bit_remainder = 0;
127 res->repr_class = NULL;
129 if (is_Method_type(type)) {
131 ir_mode *mode = is_Method_type(type) ? mode_P_code : mode_P_data;
133 rem = current_ir_graph;
134 current_ir_graph = get_const_code_irg();
135 res->value = new_SymConst(mode, sym, symconst_addr_ent);
136 current_ir_graph = rem;
137 res->allocation = allocation_static;
138 res->variability = variability_constant;
139 res->attr.mtd_attr.irg_add_properties = mtp_property_inherited;
140 res->attr.mtd_attr.vtable_number = VTABLE_NUM_NOT_SET;
141 res->attr.mtd_attr.param_access = NULL;
142 res->attr.mtd_attr.param_weight = NULL;
143 res->attr.mtd_attr.irg = NULL;
144 } else if (is_compound_type(type)) {
145 res->variability = variability_uninitialized;
147 res->attr.cmpd_attr.values = NULL;
148 res->attr.cmpd_attr.val_paths = NULL;
149 } else if (is_code_type(type)) {
150 res->attr.code_attr.label = (ir_label_t) -1;
152 res->variability = variability_uninitialized;
156 if (is_Class_type(owner)) {
157 res->overwrites = NEW_ARR_F(ir_entity *, 0);
158 res->overwrittenby = NEW_ARR_F(ir_entity *, 0);
160 res->overwrites = NULL;
161 res->overwrittenby = NULL;
165 res->nr = get_irp_new_node_nr();
166 #endif /* DEBUG_libfirm */
169 set_entity_dbg_info(res, db);
172 } /* new_rd_entity */
175 new_d_entity(ir_type *owner, ident *name, ir_type *type, dbg_info *db) {
178 assert(is_compound_type(owner));
179 res = new_rd_entity(db, owner, name, type);
180 /* Remember entity in it's owner. */
181 insert_entity_in_owner(res);
183 hook_new_entity(res);
188 new_entity(ir_type *owner, ident *name, ir_type *type) {
189 return new_d_entity(owner, name, type, NULL);
193 * Free entity attributes.
195 * @param ent the entity
197 static void free_entity_attrs(ir_entity *ent) {
199 if (get_type_tpop(get_entity_owner(ent)) == type_class) {
200 DEL_ARR_F(ent->overwrites); ent->overwrites = NULL;
201 DEL_ARR_F(ent->overwrittenby); ent->overwrittenby = NULL;
203 assert(ent->overwrites == NULL);
204 assert(ent->overwrittenby == NULL);
206 if (is_compound_entity(ent)) {
207 if (ent->has_initializer) {
208 /* TODO: free initializers */
210 if (ent->attr.cmpd_attr.val_paths) {
211 for (i = get_compound_ent_n_values(ent) - 1; i >= 0; --i)
212 if (ent->attr.cmpd_attr.val_paths[i]) {
213 /* free_compound_graph_path(ent->attr.cmpd_attr.val_paths[i]) ; * @@@ warum nich? */
214 /* Geht nich: wird mehrfach verwendet!!! ==> mehrfach frei gegeben. */
215 /* DEL_ARR_F(ent->attr.cmpd_attr.val_paths); */
217 ent->attr.cmpd_attr.val_paths = NULL;
219 if (ent->attr.cmpd_attr.values) {
220 /*DEL_ARR_F(ent->attr.cmpd_attr.values)*/;
222 ent->attr.cmpd_attr.values = NULL;
224 } else if (is_method_entity(ent)) {
225 if (ent->attr.mtd_attr.param_access) {
226 DEL_ARR_F(ent->attr.mtd_attr.param_access);
227 ent->attr.mtd_attr.param_access = NULL;
229 if (ent->attr.mtd_attr.param_weight) {
230 DEL_ARR_F(ent->attr.mtd_attr.param_weight);
231 ent->attr.mtd_attr.param_weight = NULL;
234 } /* free_entity_attrs */
237 * Creates a deep copy of an entity.
239 static ir_entity *deep_entity_copy(ir_entity *old)
241 ir_entity *newe = XMALLOC(ir_entity);
244 if (is_compound_entity(old)) {
245 if (old->has_initializer) {
246 /* FIXME: the initializers are NOT copied */
248 newe->attr.cmpd_attr.values = NULL;
249 newe->attr.cmpd_attr.val_paths = NULL;
250 if (old->attr.cmpd_attr.values)
251 newe->attr.cmpd_attr.values = DUP_ARR_F(ir_node *, old->attr.cmpd_attr.values);
253 /* FIXME: the compound graph paths are NOT copied */
254 if (old->attr.cmpd_attr.val_paths)
255 newe->attr.cmpd_attr.val_paths = DUP_ARR_F(compound_graph_path *, old->attr.cmpd_attr.val_paths);
257 } else if (is_method_entity(old)) {
258 /* do NOT copy them, reanalyze. This might be the best solution */
259 newe->attr.mtd_attr.param_access = NULL;
260 newe->attr.mtd_attr.param_weight = NULL;
264 newe->nr = get_irp_new_node_nr();
269 * Copies the entity if the new_owner is different from the
270 * owner of the old entity, else returns the old entity.
273 copy_entity_own(ir_entity *old, ir_type *new_owner) {
275 assert(is_entity(old));
276 assert(is_compound_type(new_owner));
277 assert(get_type_state(new_owner) != layout_fixed);
279 if (old->owner == new_owner)
282 /* create a deep copy so we are safe of aliasing and double-freeing. */
283 newe = deep_entity_copy(old);
284 newe->owner = new_owner;
286 if (is_Class_type(new_owner)) {
287 newe->overwrites = NEW_ARR_F(ir_entity *, 0);
288 newe->overwrittenby = NEW_ARR_F(ir_entity *, 0);
291 insert_entity_in_owner(newe);
293 } /* copy_entity_own */
296 copy_entity_name(ir_entity *old, ident *new_name) {
298 assert(old && old->kind == k_entity);
300 if (old->name == new_name) return old;
301 newe = deep_entity_copy(old);
302 newe->name = new_name;
303 newe->ld_name = NULL;
305 if (is_Class_type(newe->owner)) {
306 newe->overwrites = DUP_ARR_F(ir_entity *, old->overwrites);
307 newe->overwrittenby = DUP_ARR_F(ir_entity *, old->overwrittenby);
309 insert_entity_in_owner(newe);
312 } /* copy_entity_name */
315 free_entity(ir_entity *ent) {
316 assert(ent && ent->kind == k_entity);
317 free_entity_attrs(ent);
322 /* Outputs a unique number for this node */
324 get_entity_nr(const ir_entity *ent) {
325 assert(ent && ent->kind == k_entity);
329 return (long)PTR_TO_INT(ent);
331 } /* get_entity_nr */
334 (get_entity_name)(const ir_entity *ent) {
335 return _get_entity_name(ent);
336 } /* get_entity_name */
339 (get_entity_ident)(const ir_entity *ent) {
340 return _get_entity_ident(ent);
341 } /* get_entity_ident */
344 (set_entity_ident)(ir_entity *ent, ident *id) {
345 _set_entity_ident(ent, id);
346 } /* set_entity_ident */
349 (get_entity_owner)(ir_entity *ent) {
350 return _get_entity_owner(ent);
351 } /* get_entity_owner */
354 set_entity_owner(ir_entity *ent, ir_type *owner) {
355 assert(is_entity(ent));
356 assert(is_compound_type(owner));
358 } /* set_entity_owner */
361 (get_entity_ld_ident)(ir_entity *ent) {
362 return _get_entity_ld_ident(ent);
363 } /* get_entity_ld_ident */
366 (set_entity_ld_ident)(ir_entity *ent, ident *ld_ident) {
367 _set_entity_ld_ident(ent, ld_ident);
368 } /* set_entity_ld_ident */
371 (get_entity_ld_name)(ir_entity *ent) {
372 return _get_entity_ld_name(ent);
373 } /* get_entity_ld_name */
376 (get_entity_type)(ir_entity *ent) {
377 return _get_entity_type(ent);
378 } /* get_entity_type */
381 (set_entity_type)(ir_entity *ent, ir_type *type) {
382 _set_entity_type(ent, type);
383 } /* set_entity_type */
386 (get_entity_allocation)(const ir_entity *ent) {
387 return _get_entity_allocation(ent);
388 } /* get_entity_allocation */
391 (set_entity_allocation)(ir_entity *ent, ir_allocation al) {
392 _set_entity_allocation(ent, al);
393 } /* set_entity_allocation */
395 /* return the name of the visibility */
396 const char *get_allocation_name(ir_allocation al)
398 #define X(a) case a: return #a
400 X(allocation_automatic);
401 X(allocation_parameter);
402 X(allocation_dynamic);
403 X(allocation_static);
404 default: return "BAD VALUE";
407 } /* get_allocation_name */
410 (get_entity_visibility)(const ir_entity *ent) {
411 return _get_entity_visibility(ent);
412 } /* get_entity_visibility */
415 set_entity_visibility(ir_entity *ent, ir_visibility vis) {
416 assert(ent && ent->kind == k_entity);
417 if (vis != visibility_local)
418 assert((ent->allocation == allocation_static) ||
419 (ent->allocation == allocation_automatic));
420 /* @@@ Test that the owner type is not local, but how??
421 && get_class_visibility(get_entity_owner(ent)) != local));*/
422 ent->visibility = vis;
423 } /* set_entity_visibility */
425 /* return the name of the visibility */
426 const char *get_visibility_name(ir_visibility vis)
428 #define X(a) case a: return #a
431 X(visibility_external_visible);
432 X(visibility_external_allocated);
433 default: return "BAD VALUE";
436 } /* get_visibility_name */
439 (get_entity_variability)(const ir_entity *ent) {
440 return _get_entity_variability(ent);
441 } /* get_entity_variability */
444 set_entity_variability(ir_entity *ent, ir_variability var)
446 assert(ent && ent->kind == k_entity);
447 if (var == variability_part_constant)
448 assert(is_Class_type(ent->type) || is_Struct_type(ent->type));
450 if ((is_compound_type(ent->type)) &&
451 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
452 /* Allocate data structures for constant values */
453 ent->attr.cmpd_attr.values = NEW_ARR_F(ir_node *, 0);
454 ent->attr.cmpd_attr.val_paths = NEW_ARR_F(compound_graph_path *, 0);
456 if ((is_atomic_type(ent->type)) &&
457 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
458 /* Set default constant value. */
459 ent->value = new_r_Unknown(get_const_code_irg(), get_type_mode(ent->type));
462 if ((is_compound_type(ent->type)) &&
463 (var == variability_uninitialized) && (ent->variability != variability_uninitialized)) {
464 /* Free data structures for constant values */
465 DEL_ARR_F(ent->attr.cmpd_attr.values); ent->attr.cmpd_attr.values = NULL;
466 DEL_ARR_F(ent->attr.cmpd_attr.val_paths); ent->attr.cmpd_attr.val_paths = NULL;
468 ent->variability = var;
469 } /* set_entity_variability */
471 /* return the name of the variability */
472 const char *get_variability_name(ir_variability var)
474 #define X(a) case a: return #a
476 X(variability_uninitialized);
477 X(variability_initialized);
478 X(variability_part_constant);
479 X(variability_constant);
480 default: return "BAD VALUE";
483 } /* get_variability_name */
486 (get_entity_volatility)(const ir_entity *ent) {
487 return _get_entity_volatility(ent);
488 } /* get_entity_volatility */
491 (set_entity_volatility)(ir_entity *ent, ir_volatility vol) {
492 _set_entity_volatility(ent, vol);
493 } /* set_entity_volatility */
495 /* Return the name of the volatility. */
496 const char *get_volatility_name(ir_volatility var)
498 #define X(a) case a: return #a
500 X(volatility_non_volatile);
501 X(volatility_is_volatile);
502 default: return "BAD VALUE";
505 } /* get_volatility_name */
508 (get_entity_aligned)(const ir_entity *ent) {
509 return _get_entity_aligned(ent);
513 (set_entity_aligned)(ir_entity *ent, ir_align a) {
514 _set_entity_aligned(ent, a);
518 (get_entity_alignment)(const ir_entity *ent) {
519 return _get_entity_alignment(ent);
523 (set_entity_alignment)(ir_entity *ent, unsigned alignment) {
524 _set_entity_alignment(ent, alignment);
527 /* Return the name of the alignment. */
528 const char *get_align_name(ir_align a)
530 #define X(a) case a: return #a
532 X(align_non_aligned);
534 default: return "BAD VALUE";
537 } /* get_align_name */
540 set_entity_label(ir_entity *ent, ir_label_t label)
542 ent->attr.code_attr.label = label;
545 ir_label_t get_entity_label(const ir_entity *ent)
547 return ent->attr.code_attr.label;
551 (get_entity_peculiarity)(const ir_entity *ent) {
552 return _get_entity_peculiarity(ent);
553 } /* get_entity_peculiarity */
556 (set_entity_peculiarity)(ir_entity *ent, ir_peculiarity pec) {
557 _set_entity_peculiarity(ent, pec);
558 } /* set_entity_peculiarity */
560 /* Checks if an entity cannot be overridden anymore. */
561 int (is_entity_final)(const ir_entity *ent) {
562 return _is_entity_final(ent);
563 } /* is_entity_final */
565 /* Sets/resets the final flag of an entity. */
566 void (set_entity_final)(ir_entity *ent, int final) {
567 _set_entity_final(ent, final);
568 } /* set_entity_final */
570 /* Checks if an entity is compiler generated */
571 int (is_entity_compiler_generated)(const ir_entity *ent) {
572 return _is_entity_compiler_generated(ent);
573 } /* is_entity_compiler_generated */
575 /* Sets/resets the compiler generated flag */
576 void (set_entity_compiler_generated)(ir_entity *ent, int flag) {
577 _set_entity_compiler_generated(ent, flag);
578 } /* set_entity_compiler_generated */
580 /* Checks if an entity is marked by the backend */
581 int (is_entity_backend_marked)(const ir_entity *ent) {
582 return _is_entity_backend_marked(ent);
583 } /* is_entity_backend_marked */
585 /* Sets/resets the compiler generated flag */
586 void (set_entity_backend_marked)(ir_entity *ent, int flag) {
587 _set_entity_backend_marked(ent, flag);
588 } /* set_entity_backend_marked */
590 ir_entity_usage (get_entity_usage)(const ir_entity *ent) {
591 return _get_entity_usage(ent);
594 void (set_entity_usage)(ir_entity *ent, ir_entity_usage flags) {
595 _set_entity_usage(ent, flags);
598 /* Get the entity's stickyness */
600 (get_entity_stickyness)(const ir_entity *ent) {
601 return _get_entity_stickyness(ent);
602 } /* get_entity_stickyness */
604 /* Set the entity's stickyness */
606 (set_entity_stickyness)(ir_entity *ent, ir_stickyness stickyness) {
607 _set_entity_stickyness(ent, stickyness);
608 } /* set_entity_stickyness */
610 /* Set has no effect for existent entities of type method. */
612 get_atomic_ent_value(ir_entity *ent)
614 assert(ent && is_atomic_entity(ent));
615 assert(ent->variability != variability_uninitialized);
616 return skip_Id(ent->value);
617 } /* get_atomic_ent_value */
620 set_atomic_ent_value(ir_entity *ent, ir_node *val) {
621 assert(is_atomic_entity(ent) && (ent->variability != variability_uninitialized));
622 if (is_Method_type(ent->type) && (ent->peculiarity == peculiarity_existent))
624 assert(is_Dummy(val) || get_irn_mode(val) == get_type_mode(ent->type));
626 } /* set_atomic_ent_value */
628 /* Returns true if the the node is representable as code on
630 int is_irn_const_expression(ir_node *n) {
633 /* we are in danger iff an exception will arise. TODO: be more precisely,
634 * for instance Div. will NOT rise if divisor != 0
636 if (is_binop(n) && !is_fragile_op(n))
637 return is_irn_const_expression(get_binop_left(n)) && is_irn_const_expression(get_binop_right(n));
640 switch (get_irn_opcode(n)) {
647 return is_irn_const_expression(get_irn_n(n, 0));
652 } /* is_irn_const_expression */
655 * Copies a firm subgraph that complies to the restrictions for
656 * constant expressions to current_block in current_ir_graph.
658 ir_node *copy_const_value(dbg_info *dbg, ir_node *n) {
662 /* @@@ GL I think we should implement this using the routines from irgopt for
663 dead node elimination/inlineing. */
666 switch (get_irn_opcode(n)) {
668 nn = new_d_Const_type(dbg, get_Const_tarval(n), get_Const_type(n));
671 nn = new_d_SymConst_type(dbg, get_irn_mode(n), get_SymConst_symbol(n), get_SymConst_kind(n),
672 get_SymConst_value_type(n));
675 nn = new_d_Add(dbg, copy_const_value(dbg, get_Add_left(n)),
676 copy_const_value(dbg, get_Add_right(n)), m); break;
678 nn = new_d_Sub(dbg, copy_const_value(dbg, get_Sub_left(n)),
679 copy_const_value(dbg, get_Sub_right(n)), m); break;
681 nn = new_d_Mul(dbg, copy_const_value(dbg, get_Mul_left(n)),
682 copy_const_value(dbg, get_Mul_right(n)), m); break;
684 nn = new_d_And(dbg, copy_const_value(dbg, get_And_left(n)),
685 copy_const_value(dbg, get_And_right(n)), m); break;
687 nn = new_d_Or(dbg, copy_const_value(dbg, get_Or_left(n)),
688 copy_const_value(dbg, get_Or_right(n)), m); break;
690 nn = new_d_Eor(dbg, copy_const_value(dbg, get_Eor_left(n)),
691 copy_const_value(dbg, get_Eor_right(n)), m); break;
693 nn = new_d_Cast(dbg, copy_const_value(dbg, get_Cast_op(n)), get_Cast_type(n)); break;
695 nn = new_d_Conv(dbg, copy_const_value(dbg, get_Conv_op(n)), m); break;
697 nn = new_Unknown(m); break;
699 assert(0 && "opcode invalid or not implemented");
704 } /* copy_const_value */
706 /** Return the name of the initializer kind. */
707 const char *get_initializer_kind_name(ir_initializer_kind_t ini)
709 #define X(a) case a: return #a
711 X(IR_INITIALIZER_CONST);
712 X(IR_INITIALIZER_TARVAL);
713 X(IR_INITIALIZER_NULL);
714 X(IR_INITIALIZER_COMPOUND);
715 default: return "BAD VALUE";
720 static ir_initializer_t null_initializer = { IR_INITIALIZER_NULL };
722 ir_initializer_t *get_initializer_null(void)
724 return &null_initializer;
727 ir_initializer_t *create_initializer_const(ir_node *value)
729 struct obstack *obst = get_irg_obstack(get_const_code_irg());
731 ir_initializer_t *initializer
732 = obstack_alloc(obst, sizeof(ir_initializer_const_t));
733 initializer->kind = IR_INITIALIZER_CONST;
734 initializer->consti.value = value;
739 ir_initializer_t *create_initializer_tarval(tarval *tv)
741 struct obstack *obst = get_irg_obstack(get_const_code_irg());
743 ir_initializer_t *initializer
744 = obstack_alloc(obst, sizeof(ir_initializer_tarval_t));
745 initializer->kind = IR_INITIALIZER_TARVAL;
746 initializer->tarval.value = tv;
751 ir_initializer_t *create_initializer_compound(unsigned n_entries)
753 struct obstack *obst = get_irg_obstack(get_const_code_irg());
756 size_t size = sizeof(ir_initializer_compound_t)
757 + (n_entries-1) * sizeof(ir_initializer_t*);
759 ir_initializer_t *initializer = obstack_alloc(obst, size);
760 initializer->kind = IR_INITIALIZER_COMPOUND;
761 initializer->compound.n_initializers = n_entries;
763 for(i = 0; i < n_entries; ++i) {
764 initializer->compound.initializers[i] = get_initializer_null();
770 ir_node *get_initializer_const_value(const ir_initializer_t *initializer)
772 assert(initializer->kind == IR_INITIALIZER_CONST);
773 return skip_Id(initializer->consti.value);
776 tarval *get_initializer_tarval_value(const ir_initializer_t *initializer)
778 assert(initializer->kind == IR_INITIALIZER_TARVAL);
779 return initializer->tarval.value;
782 unsigned get_initializer_compound_n_entries(const ir_initializer_t *initializer)
784 assert(initializer->kind == IR_INITIALIZER_COMPOUND);
785 return initializer->compound.n_initializers;
788 void set_initializer_compound_value(ir_initializer_t *initializer,
789 unsigned index, ir_initializer_t *value)
791 assert(initializer->kind == IR_INITIALIZER_COMPOUND);
792 assert(index < initializer->compound.n_initializers);
794 initializer->compound.initializers[index] = value;
797 ir_initializer_t *get_initializer_compound_value(
798 const ir_initializer_t *initializer, unsigned index)
800 assert(initializer->kind == IR_INITIALIZER_COMPOUND);
801 assert(index < initializer->compound.n_initializers);
803 return initializer->compound.initializers[index];
806 ir_initializer_kind_t get_initializer_kind(const ir_initializer_t *initializer)
808 return initializer->kind;
811 static void check_entity_initializer(ir_entity *entity)
817 void set_entity_initializer(ir_entity *entity, ir_initializer_t *initializer)
819 entity->attr.initializer = initializer;
820 entity->has_initializer = 1;
821 check_entity_initializer(entity);
824 int has_entity_initializer(const ir_entity *entity)
826 return entity->has_initializer;
829 ir_initializer_t *get_entity_initializer(const ir_entity *entity)
831 assert(entity->has_initializer);
832 return entity->attr.initializer;
835 /* Creates a new compound graph path. */
836 compound_graph_path *
837 new_compound_graph_path(ir_type *tp, int length) {
838 compound_graph_path *res;
840 assert(is_compound_type(tp));
843 res = xmalloc(sizeof(*res) + (length-1) * sizeof(res->list[0]));
844 memset(res, 0, sizeof(*res) + (length-1) * sizeof(res->list[0]));
845 res->kind = k_ir_compound_graph_path;
850 } /* new_compound_graph_path */
852 /* Frees an graph path object */
853 void free_compound_graph_path (compound_graph_path *gr) {
854 assert(gr && is_compound_graph_path(gr));
857 } /* free_compound_graph_path */
859 /* Returns non-zero if an object is a compound graph path */
860 int is_compound_graph_path(const void *thing) {
861 return (get_kind(thing) == k_ir_compound_graph_path);
862 } /* is_compound_graph_path */
864 /* Checks whether the path up to pos is correct. If the path contains a NULL,
865 * assumes the path is not complete and returns 'true'. */
866 int is_proper_compound_graph_path(compound_graph_path *gr, int pos) {
869 ir_type *owner = gr->tp;
871 for (i = 0; i <= pos; i++) {
872 node = get_compound_graph_path_node(gr, i);
874 /* Path not yet complete. */
876 if (get_entity_owner(node) != owner)
878 owner = get_entity_type(node);
880 if (pos == get_compound_graph_path_length(gr))
881 if (!is_atomic_type(owner))
884 } /* is_proper_compound_graph_path */
886 /* Returns the length of a graph path */
887 int get_compound_graph_path_length(const compound_graph_path *gr) {
888 assert(gr && is_compound_graph_path(gr));
890 } /* get_compound_graph_path_length */
893 get_compound_graph_path_node(const compound_graph_path *gr, int pos) {
894 assert(gr && is_compound_graph_path(gr));
895 assert(pos >= 0 && pos < gr->len);
896 return gr->list[pos].node;
897 } /* get_compound_graph_path_node */
900 set_compound_graph_path_node(compound_graph_path *gr, int pos, ir_entity *node) {
901 assert(gr && is_compound_graph_path(gr));
902 assert(pos >= 0 && pos < gr->len);
903 assert(is_entity(node));
904 gr->list[pos].node = node;
905 assert(is_proper_compound_graph_path(gr, pos));
906 } /* set_compound_graph_path_node */
909 get_compound_graph_path_array_index(const compound_graph_path *gr, int pos) {
910 assert(gr && is_compound_graph_path(gr));
911 assert(pos >= 0 && pos < gr->len);
912 return gr->list[pos].index;
913 } /* get_compound_graph_path_array_index */
916 set_compound_graph_path_array_index(compound_graph_path *gr, int pos, int index) {
917 assert(gr && is_compound_graph_path(gr));
918 assert(pos >= 0 && pos < gr->len);
919 gr->list[pos].index = index;
920 } /* set_compound_graph_path_array_index */
923 get_compound_graph_path_type(const compound_graph_path *gr) {
924 assert(gr && is_compound_graph_path(gr));
928 /* A value of a compound entity is a pair of value and the corresponding path to a member of
931 add_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path) {
932 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
933 assert(is_compound_graph_path(path));
934 ARR_APP1(ir_node *, ent->attr.cmpd_attr.values, val);
935 ARR_APP1(compound_graph_path *, ent->attr.cmpd_attr.val_paths, path);
936 } /* add_compound_ent_value_w_path */
939 set_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path, int pos) {
940 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
941 assert(is_compound_graph_path(path));
942 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
943 ent->attr.cmpd_attr.values[pos] = val;
944 ent->attr.cmpd_attr.val_paths[pos] = path;
945 } /* set_compound_ent_value_w_path */
948 get_compound_ent_n_values(ir_entity *ent) {
949 assert(!ent->has_initializer);
950 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
951 return ARR_LEN(ent->attr.cmpd_attr.values);
952 } /* get_compound_ent_n_values */
955 get_compound_ent_value(ir_entity *ent, int pos) {
956 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
957 assert(!ent->has_initializer);
958 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
959 return skip_Id(ent->attr.cmpd_attr.values[pos]);
960 } /* get_compound_ent_value */
962 compound_graph_path *
963 get_compound_ent_value_path(ir_entity *ent, int pos) {
964 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
965 assert(!ent->has_initializer);
966 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.val_paths));
967 return ent->attr.cmpd_attr.val_paths[pos];
968 } /* get_compound_ent_value_path */
971 * Returns non-zero, if two compound_graph_pathes are equal
973 * @param path1 the first path
974 * @param path2 the second path
976 static int equal_paths(compound_graph_path *path1, compound_graph_path *path2) {
978 int len1 = get_compound_graph_path_length(path1);
979 int len2 = get_compound_graph_path_length(path2);
981 if (len2 != len1) return 0;
983 for (i = 0; i < len1; i++) {
985 ir_entity *node1 = get_compound_graph_path_node(path1, i);
986 ir_entity *node2 = get_compound_graph_path_node(path2, i);
988 if (node1 != node2) return 0;
990 tp = get_entity_owner(node1);
991 if (is_Array_type(tp)) {
992 int index1 = get_compound_graph_path_array_index(path1, i);
993 int index2 = get_compound_graph_path_array_index(path2, i);
994 if (index1 != index2)
1002 * Returns the position of a value with the given path.
1003 * The path must contain array indices for all array element entities.
1005 * @todo This implementation is very slow (O(number of initializers * |path|)
1006 * and should be replaced when the new tree oriented
1007 * value representation is finally implemented.
1009 static int get_compound_ent_pos_by_path(ir_entity *ent, compound_graph_path *path) {
1010 int i, n_paths = get_compound_ent_n_values(ent);
1012 for (i = 0; i < n_paths; i ++) {
1013 compound_graph_path *gr = get_compound_ent_value_path(ent, i);
1014 if (equal_paths(gr, path))
1018 } /* get_compound_ent_pos_by_path */
1020 /* Returns a constant value given the access path.
1021 * The path must contain array indices for all array element entities. */
1022 ir_node *get_compound_ent_value_by_path(ir_entity *ent, compound_graph_path *path) {
1023 int pos = get_compound_ent_pos_by_path(ent, path);
1025 return get_compound_ent_value(ent, pos);
1027 } /* get_compound_ent_value_by_path */
1031 remove_compound_ent_value(ir_entity *ent, ir_entity *value_ent) {
1033 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
1035 n = ARR_LEN(ent->attr.cmpd_attr.val_paths);
1036 for (i = 0; i < n; ++i) {
1037 compound_graph_path *path = ent->attr.cmpd_attr.val_paths[i];
1038 if (path->list[path->len-1].node == value_ent) {
1039 for (; i < n - 1; ++i) {
1040 ent->attr.cmpd_attr.val_paths[i] = ent->attr.cmpd_attr.val_paths[i+1];
1041 ent->attr.cmpd_attr.values[i] = ent->attr.cmpd_attr.values[i+1];
1043 ARR_SETLEN(ir_entity*, ent->attr.cmpd_attr.val_paths, n - 1);
1044 ARR_SETLEN(ir_node*, ent->attr.cmpd_attr.values, n - 1);
1048 } /* remove_compound_ent_value */
1051 add_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member) {
1052 compound_graph_path *path;
1053 ir_type *owner_tp = get_entity_owner(member);
1054 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
1055 path = new_compound_graph_path(get_entity_type(ent), 1);
1056 path->list[0].node = member;
1057 if (is_Array_type(owner_tp)) {
1061 assert(get_array_n_dimensions(owner_tp) == 1 && has_array_lower_bound(owner_tp, 0));
1062 max = get_array_lower_bound_int(owner_tp, 0) -1;
1063 for (i = 0, n = get_compound_ent_n_values(ent); i < n; ++i) {
1064 int index = get_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0);
1069 path->list[0].index = max + 1;
1071 add_compound_ent_value_w_path(ent, val, path);
1072 } /* add_compound_ent_value */
1076 get_compound_ent_value_member(ir_entity *ent, int pos) {
1077 compound_graph_path *path;
1078 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
1079 path = get_compound_ent_value_path(ent, pos);
1081 return get_compound_graph_path_node(path, get_compound_graph_path_length(path)-1);
1082 } /* get_compound_ent_value_member */
1085 set_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member, int pos) {
1086 compound_graph_path *path;
1087 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
1088 path = get_compound_ent_value_path(ent, pos);
1089 set_compound_graph_path_node(path, 0, member);
1090 set_compound_ent_value_w_path(ent, val, path, pos);
1091 } /* set_compound_ent_value */
1094 set_array_entity_values(ir_entity *ent, tarval **values, int num_vals) {
1096 ir_graph *rem = current_ir_graph;
1097 ir_type *arrtp = get_entity_type(ent);
1099 ir_type *elttp = get_array_element_type(arrtp);
1101 assert(is_Array_type(arrtp));
1102 assert(get_array_n_dimensions(arrtp) == 1);
1103 /* One bound is sufficient, the number of constant fields makes the
1105 assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
1106 assert(get_entity_variability(ent) != variability_uninitialized);
1107 current_ir_graph = get_const_code_irg();
1109 for (i = 0; i < num_vals; i++) {
1110 val = new_Const_type(values[i], elttp);
1111 add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
1112 set_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0, i);
1114 current_ir_graph = rem;
1115 } /* set_array_entity_values */
1117 /* Return the overall offset of value at position pos in bytes. */
1118 unsigned get_compound_ent_value_offset_bytes(ir_entity *ent, int pos) {
1119 compound_graph_path *path;
1121 unsigned offset = 0;
1124 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
1126 path = get_compound_ent_value_path(ent, pos);
1127 path_len = get_compound_graph_path_length(path);
1130 for (i = 0; i < path_len; ++i) {
1131 if (is_Array_type(curr_tp)) {
1132 ir_type *elem_type = get_array_element_type(curr_tp);
1133 unsigned size = get_type_size_bytes(elem_type);
1134 unsigned align = get_type_alignment_bytes(elem_type);
1138 if(size % align > 0) {
1139 size += align - (size % align);
1141 idx = get_compound_graph_path_array_index(path, i);
1143 offset += size * idx;
1144 curr_tp = elem_type;
1146 ir_entity *node = get_compound_graph_path_node(path, i);
1147 offset += get_entity_offset(node);
1148 curr_tp = get_entity_type(node);
1153 } /* get_compound_ent_value_offset_bytes */
1155 /* Return the offset in bits from the last byte address. */
1156 unsigned get_compound_ent_value_offset_bit_remainder(ir_entity *ent, int pos) {
1157 compound_graph_path *path;
1159 ir_entity *last_node;
1161 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
1163 path = get_compound_ent_value_path(ent, pos);
1164 path_len = get_compound_graph_path_length(path);
1165 last_node = get_compound_graph_path_node(path, path_len - 1);
1167 if(last_node == NULL)
1170 return get_entity_offset_bits_remainder(last_node);
1171 } /* get_compound_ent_value_offset_bit_remainder */
1174 (get_entity_offset)(const ir_entity *ent) {
1175 return _get_entity_offset(ent);
1176 } /* get_entity_offset */
1179 (set_entity_offset)(ir_entity *ent, int offset) {
1180 _set_entity_offset(ent, offset);
1181 } /* set_entity_offset */
1184 (get_entity_offset_bits_remainder)(const ir_entity *ent) {
1185 return _get_entity_offset_bits_remainder(ent);
1186 } /* get_entity_offset_bits_remainder */
1189 (set_entity_offset_bits_remainder)(ir_entity *ent, unsigned char offset) {
1190 _set_entity_offset_bits_remainder(ent, offset);
1191 } /* set_entity_offset_bits_remainder */
1194 add_entity_overwrites(ir_entity *ent, ir_entity *overwritten) {
1196 ir_type *owner = get_entity_owner(ent);
1197 ir_type *ovw_ovner = get_entity_owner(overwritten);
1198 assert(is_Class_type(owner));
1199 assert(is_Class_type(ovw_ovner));
1200 assert(! is_class_final(ovw_ovner));
1202 ARR_APP1(ir_entity *, ent->overwrites, overwritten);
1203 ARR_APP1(ir_entity *, overwritten->overwrittenby, ent);
1204 } /* add_entity_overwrites */
1207 get_entity_n_overwrites(ir_entity *ent) {
1208 assert(is_Class_type(get_entity_owner(ent)));
1209 return (ARR_LEN(ent->overwrites));
1210 } /* get_entity_n_overwrites */
1213 get_entity_overwrites_index(ir_entity *ent, ir_entity *overwritten) {
1215 assert(is_Class_type(get_entity_owner(ent)));
1216 n = get_entity_n_overwrites(ent);
1217 for (i = 0; i < n; ++i) {
1218 if (get_entity_overwrites(ent, i) == overwritten)
1222 } /* get_entity_overwrites_index */
1225 get_entity_overwrites(ir_entity *ent, int pos) {
1226 assert(is_Class_type(get_entity_owner(ent)));
1227 assert(pos < get_entity_n_overwrites(ent));
1228 return ent->overwrites[pos];
1229 } /* get_entity_overwrites */
1232 set_entity_overwrites(ir_entity *ent, int pos, ir_entity *overwritten) {
1233 assert(is_Class_type(get_entity_owner(ent)));
1234 assert(pos < get_entity_n_overwrites(ent));
1235 ent->overwrites[pos] = overwritten;
1236 } /* set_entity_overwrites */
1239 remove_entity_overwrites(ir_entity *ent, ir_entity *overwritten) {
1241 assert(is_Class_type(get_entity_owner(ent)));
1242 n = ARR_LEN(ent->overwrites);
1243 for (i = 0; i < n; ++i) {
1244 if (ent->overwrites[i] == overwritten) {
1245 for (; i < n - 1; i++)
1246 ent->overwrites[i] = ent->overwrites[i+1];
1247 ARR_SETLEN(ir_entity*, ent->overwrites, n - 1);
1251 } /* remove_entity_overwrites */
1254 add_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites) {
1255 add_entity_overwrites(overwrites, ent);
1256 } /* add_entity_overwrittenby */
1259 get_entity_n_overwrittenby(ir_entity *ent) {
1260 assert(is_Class_type(get_entity_owner(ent)));
1261 return ARR_LEN(ent->overwrittenby);
1262 } /* get_entity_n_overwrittenby */
1265 get_entity_overwrittenby_index(ir_entity *ent, ir_entity *overwrites) {
1267 assert(is_Class_type(get_entity_owner(ent)));
1268 n = get_entity_n_overwrittenby(ent);
1269 for (i = 0; i < n; ++i) {
1270 if (get_entity_overwrittenby(ent, i) == overwrites)
1274 } /* get_entity_overwrittenby_index */
1277 get_entity_overwrittenby(ir_entity *ent, int pos) {
1278 assert(is_Class_type(get_entity_owner(ent)));
1279 assert(pos < get_entity_n_overwrittenby(ent));
1280 return ent->overwrittenby[pos];
1281 } /* get_entity_overwrittenby */
1284 set_entity_overwrittenby(ir_entity *ent, int pos, ir_entity *overwrites) {
1285 assert(is_Class_type(get_entity_owner(ent)));
1286 assert(pos < get_entity_n_overwrittenby(ent));
1287 ent->overwrittenby[pos] = overwrites;
1288 } /* set_entity_overwrittenby */
1290 void remove_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites) {
1292 assert(is_Class_type(get_entity_owner(ent)));
1294 n = ARR_LEN(ent->overwrittenby);
1295 for (i = 0; i < n; ++i) {
1296 if (ent->overwrittenby[i] == overwrites) {
1297 for(; i < n - 1; ++i)
1298 ent->overwrittenby[i] = ent->overwrittenby[i+1];
1299 ARR_SETLEN(ir_entity*, ent->overwrittenby, n - 1);
1303 } /* remove_entity_overwrittenby */
1305 /* A link to store intermediate information */
1307 (get_entity_link)(const ir_entity *ent) {
1308 return _get_entity_link(ent);
1309 } /* get_entity_link */
1312 (set_entity_link)(ir_entity *ent, void *l) {
1313 _set_entity_link(ent, l);
1314 } /* set_entity_link */
1317 (get_entity_irg)(const ir_entity *ent) {
1318 return _get_entity_irg(ent);
1319 } /* get_entity_irg */
1322 set_entity_irg(ir_entity *ent, ir_graph *irg) {
1323 assert(is_method_entity(ent));
1324 /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
1325 * Methode selbst nicht mehr aufgerufen werden kann, die Entität
1326 * aber erhalten bleiben soll? Wandle die Entitaet in description oder
1329 assert((irg && ent->peculiarity == peculiarity_existent) ||
1330 (!irg && (ent->peculiarity == peculiarity_existent)
1331 && (ent -> visibility == visibility_external_allocated)) ||
1332 (!irg && ent->peculiarity == peculiarity_description) ||
1333 (!irg && ent->peculiarity == peculiarity_inherited));
1334 ent->attr.mtd_attr.irg = irg;
1335 } /* set_entity_irg */
1337 unsigned get_entity_vtable_number(const ir_entity *ent) {
1338 assert(is_method_entity((ir_entity *)ent));
1339 return ent->attr.mtd_attr.vtable_number;
1340 } /* get_entity_vtable_number */
1342 void set_entity_vtable_number(ir_entity *ent, unsigned vtable_number) {
1343 assert(is_method_entity(ent));
1344 ent->attr.mtd_attr.vtable_number = vtable_number;
1345 } /* set_entity_vtable_number */
1348 (is_entity)(const void *thing) {
1349 return _is_entity(thing);
1352 int is_atomic_entity(ir_entity *ent) {
1353 ir_type *t = get_entity_type(ent);
1354 const tp_op *op = get_type_tpop(t);
1355 return (op == type_primitive || op == type_pointer ||
1356 op == type_enumeration || op == type_method);
1357 } /* is_atomic_entity */
1359 int is_compound_entity(ir_entity *ent) {
1360 ir_type *t = get_entity_type(ent);
1361 const tp_op *op = get_type_tpop(t);
1362 return (op == type_class || op == type_struct ||
1363 op == type_array || op == type_union);
1364 } /* is_compound_entity */
1366 int is_method_entity(ir_entity *ent) {
1367 ir_type *t = get_entity_type(ent);
1368 return is_Method_type(t);
1369 } /* is_method_entity */
1372 * @todo not implemented!!! */
1373 int equal_entity(ir_entity *ent1, ir_entity *ent2) {
1376 fprintf(stderr, " calling unimplemented equal entity!!! \n");
1378 } /* equal_entity */
1381 ir_visited_t (get_entity_visited)(ir_entity *ent) {
1382 return _get_entity_visited(ent);
1383 } /* get_entity_visited */
1385 void (set_entity_visited)(ir_entity *ent, ir_visited_t num) {
1386 _set_entity_visited(ent, num);
1387 } /* set_entity_visited */
1389 /* Sets visited field in ir_entity to entity_visited. */
1390 void (mark_entity_visited)(ir_entity *ent) {
1391 _mark_entity_visited(ent);
1392 } /* mark_entity_visited */
1394 int (entity_visited)(ir_entity *ent) {
1395 return _entity_visited(ent);
1396 } /* entity_visited */
1398 int (entity_not_visited)(ir_entity *ent) {
1399 return _entity_not_visited(ent);
1400 } /* entity_not_visited */
1402 /* Returns the mask of the additional entity properties. */
1403 unsigned get_entity_additional_properties(ir_entity *ent) {
1406 assert(is_method_entity(ent));
1408 /* first check, if the graph has additional properties */
1409 irg = get_entity_irg(ent);
1412 return get_irg_additional_properties(irg);
1414 if (ent->attr.mtd_attr.irg_add_properties & mtp_property_inherited)
1415 return get_method_additional_properties(get_entity_type(ent));
1417 return ent->attr.mtd_attr.irg_add_properties;
1418 } /* get_entity_additional_properties */
1420 /* Sets the mask of the additional graph properties. */
1421 void set_entity_additional_properties(ir_entity *ent, unsigned property_mask)
1425 assert(is_method_entity(ent));
1427 /* first check, if the graph exists */
1428 irg = get_entity_irg(ent);
1430 set_irg_additional_properties(irg, property_mask);
1432 /* do not allow to set the mtp_property_inherited flag or
1433 * the automatic inheritance of flags will not work */
1434 ent->attr.mtd_attr.irg_add_properties = property_mask & ~mtp_property_inherited;
1436 } /* set_entity_additional_properties */
1438 /* Sets one additional graph property. */
1439 void set_entity_additional_property(ir_entity *ent, mtp_additional_property flag)
1443 assert(is_method_entity(ent));
1445 /* first check, if the graph exists */
1446 irg = get_entity_irg(ent);
1448 set_irg_additional_property(irg, flag);
1450 unsigned mask = ent->attr.mtd_attr.irg_add_properties;
1452 if (mask & mtp_property_inherited)
1453 mask = get_method_additional_properties(get_entity_type(ent));
1455 /* do not allow to set the mtp_property_inherited flag or
1456 * the automatic inheritance of flags will not work */
1457 ent->attr.mtd_attr.irg_add_properties = mask | (flag & ~mtp_property_inherited);
1459 } /* set_entity_additional_property */
1461 /* Returns the class type that this type info entity represents or NULL
1462 if ent is no type info entity. */
1463 ir_type *(get_entity_repr_class)(const ir_entity *ent) {
1464 return _get_entity_repr_class(ent);
1465 } /* get_entity_repr_class */
1467 dbg_info *(get_entity_dbg_info)(const ir_entity *ent) {
1468 return _get_entity_dbg_info(ent);
1469 } /* get_entity_dbg_info */
1471 void (set_entity_dbg_info)(ir_entity *ent, dbg_info *db) {
1472 _set_entity_dbg_info(ent, db);
1473 } /* set_entity_dbg_info */
1475 /* Initialize entity module. */
1476 void firm_init_entity(void)
1478 symconst_symbol sym;
1480 assert(firm_unknown_type && "Call init_type() before firm_init_entity()!");
1481 assert(!unknown_entity && "Call firm_init_entity() only once!");
1483 unknown_entity = new_rd_entity(NULL, firm_unknown_type, new_id_from_str(UNKNOWN_ENTITY_NAME), firm_unknown_type);
1484 set_entity_visibility(unknown_entity, visibility_external_allocated);
1485 set_entity_ld_ident(unknown_entity, get_entity_ident(unknown_entity));
1487 current_ir_graph = get_const_code_irg();
1488 sym.entity_p = unknown_entity;
1489 /* TODO: we need two unknown_entities here, one for code and one for data */
1490 unknown_entity->value = new_SymConst(mode_P_data, sym, symconst_addr_ent);
1491 } /* firm_init_entity */