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"
48 #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);
113 panic("Unsupported type kind");
115 } /* insert_entity_in_owner */
118 * Creates a new entity. This entity is NOT inserted in the owner type.
120 * @param db debug info for this entity
121 * @param owner the owner type of the new entity
122 * @param name the name of the new entity
123 * @param type the type of the new entity
125 * @return the new created entity
127 static INLINE ir_entity *
128 new_rd_entity(dbg_info *db, ir_type *owner, ident *name, ir_type *type)
133 assert(!id_contains_char(name, ' ') && "entity name should not contain spaces");
135 res = xmalloc(sizeof(*res));
136 memset(res, 0, sizeof(*res));
138 res->kind = k_entity;
144 res->allocation = allocation_automatic;
145 res->visibility = visibility_local;
146 res->volatility = volatility_non_volatile;
147 res->align = align_is_aligned;
148 res->stickyness = stickyness_unsticky;
149 res->peculiarity = peculiarity_existent;
150 res->usage = ir_usage_unknown;
152 res->compiler_gen = 0;
153 res->backend_marked = 0;
155 res->offset_bit_remainder = 0;
157 res->repr_class = NULL;
159 if (is_Method_type(type)) {
161 ir_mode *mode = is_Method_type(type) ? mode_P_code : mode_P_data;
163 rem = current_ir_graph;
164 current_ir_graph = get_const_code_irg();
165 res->value = new_SymConst(mode, sym, symconst_addr_ent);
166 current_ir_graph = rem;
167 res->allocation = allocation_static;
168 res->variability = variability_constant;
169 res->attr.mtd_attr.irg_add_properties = mtp_property_inherited;
170 res->attr.mtd_attr.vtable_number = VTABLE_NUM_NOT_SET;
171 res->attr.mtd_attr.param_access = NULL;
172 res->attr.mtd_attr.param_weight = NULL;
173 res->attr.mtd_attr.irg = NULL;
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 ir_entity_usage (get_entity_usage)(const ir_entity *ent) {
566 return _get_entity_usage(ent);
569 void (set_entity_usage)(ir_entity *ent, ir_entity_usage flags) {
570 _set_entity_usage(ent, flags);
573 /* Get the entity's stickyness */
575 (get_entity_stickyness)(const ir_entity *ent) {
576 return _get_entity_stickyness(ent);
577 } /* get_entity_stickyness */
579 /* Set the entity's stickyness */
581 (set_entity_stickyness)(ir_entity *ent, ir_stickyness stickyness) {
582 _set_entity_stickyness(ent, stickyness);
583 } /* set_entity_stickyness */
585 /* Set has no effect for existent entities of type method. */
587 get_atomic_ent_value(ir_entity *ent)
589 assert(ent && is_atomic_entity(ent));
590 assert(ent->variability != variability_uninitialized);
591 return skip_Id(ent->value);
592 } /* get_atomic_ent_value */
595 set_atomic_ent_value(ir_entity *ent, ir_node *val) {
596 assert(is_atomic_entity(ent) && (ent->variability != variability_uninitialized));
597 if (is_Method_type(ent->type) && (ent->peculiarity == peculiarity_existent))
599 assert(get_irn_mode(val) == get_type_mode(ent->type));
601 } /* set_atomic_ent_value */
603 /* Returns true if the the node is representable as code on
605 int is_irn_const_expression(ir_node *n) {
608 /* we are in danger iff an exception will arise. TODO: be more precisely,
609 * for instance Div. will NOT rise if divisor != 0
611 if (is_binop(n) && !is_fragile_op(n))
612 return is_irn_const_expression(get_binop_left(n)) && is_irn_const_expression(get_binop_right(n));
615 switch (get_irn_opcode(n)) {
622 return is_irn_const_expression(get_irn_n(n, 0));
627 } /* is_irn_const_expression */
630 * Copies a firm subgraph that complies to the restrictions for
631 * constant expressions to current_block in current_ir_graph.
633 ir_node *copy_const_value(dbg_info *dbg, ir_node *n) {
637 /* @@@ GL I think we should implement this using the routines from irgopt for
638 dead node elimination/inlineing. */
641 switch (get_irn_opcode(n)) {
643 nn = new_d_Const_type(dbg, m, get_Const_tarval(n), get_Const_type(n));
646 nn = new_d_SymConst_type(dbg, get_irn_mode(n), get_SymConst_symbol(n), get_SymConst_kind(n),
647 get_SymConst_value_type(n));
650 nn = new_d_Add(dbg, copy_const_value(dbg, get_Add_left(n)),
651 copy_const_value(dbg, get_Add_right(n)), m); break;
653 nn = new_d_Sub(dbg, copy_const_value(dbg, get_Sub_left(n)),
654 copy_const_value(dbg, get_Sub_right(n)), m); break;
656 nn = new_d_Mul(dbg, copy_const_value(dbg, get_Mul_left(n)),
657 copy_const_value(dbg, get_Mul_right(n)), m); break;
659 nn = new_d_And(dbg, copy_const_value(dbg, get_And_left(n)),
660 copy_const_value(dbg, get_And_right(n)), m); break;
662 nn = new_d_Or(dbg, copy_const_value(dbg, get_Or_left(n)),
663 copy_const_value(dbg, get_Or_right(n)), m); break;
665 nn = new_d_Eor(dbg, copy_const_value(dbg, get_Eor_left(n)),
666 copy_const_value(dbg, get_Eor_right(n)), m); break;
668 nn = new_d_Cast(dbg, copy_const_value(dbg, get_Cast_op(n)), get_Cast_type(n)); break;
670 nn = new_d_Conv(dbg, copy_const_value(dbg, get_Conv_op(n)), m); break;
672 nn = new_d_Unknown(m); break;
674 assert(0 && "opcode invalid or not implemented");
679 } /* copy_const_value */
681 static ir_initializer_t null_initializer = { IR_INITIALIZER_NULL };
683 ir_initializer_t *get_initializer_null(void)
685 return &null_initializer;
688 ir_initializer_t *create_initializer_const(ir_node *value)
690 struct obstack *obst = get_irg_obstack(get_const_code_irg());
692 ir_initializer_t *initializer
693 = obstack_alloc(obst, sizeof(ir_initializer_const_t));
694 initializer->kind = IR_INITIALIZER_CONST;
695 initializer->consti.value = value;
700 ir_initializer_t *create_initializer_tarval(tarval *tv)
702 struct obstack *obst = get_irg_obstack(get_const_code_irg());
704 ir_initializer_t *initializer
705 = obstack_alloc(obst, sizeof(ir_initializer_tarval_t));
706 initializer->kind = IR_INITIALIZER_TARVAL;
707 initializer->tarval.value = tv;
712 ir_initializer_t *create_initializer_compound(unsigned n_entries)
714 struct obstack *obst = get_irg_obstack(get_const_code_irg());
717 size_t size = sizeof(ir_initializer_compound_t)
718 + (n_entries-1) * sizeof(ir_initializer_t*);
720 ir_initializer_t *initializer = obstack_alloc(obst, size);
721 initializer->kind = IR_INITIALIZER_COMPOUND;
722 initializer->compound.n_initializers = n_entries;
724 for(i = 0; i < n_entries; ++i) {
725 initializer->compound.initializers[i] = get_initializer_null();
731 ir_node *get_initializer_const_value(const ir_initializer_t *initializer)
733 assert(initializer->kind == IR_INITIALIZER_CONST);
734 return initializer->consti.value;
737 tarval *get_initializer_tarval_value(const ir_initializer_t *initializer)
739 assert(initializer->kind == IR_INITIALIZER_TARVAL);
740 return initializer->tarval.value;
743 unsigned get_initializer_compound_n_entries(const ir_initializer_t *initializer)
745 assert(initializer->kind == IR_INITIALIZER_COMPOUND);
746 return initializer->compound.n_initializers;
749 void set_initializer_compound_value(ir_initializer_t *initializer,
750 unsigned index, ir_initializer_t *value)
752 assert(initializer->kind == IR_INITIALIZER_COMPOUND);
753 assert(index < initializer->compound.n_initializers);
755 initializer->compound.initializers[index] = value;
758 ir_initializer_t *get_initializer_compound_value(
759 const ir_initializer_t *initializer, unsigned index)
761 assert(initializer->kind == IR_INITIALIZER_COMPOUND);
762 assert(index < initializer->compound.n_initializers);
764 return initializer->compound.initializers[index];
767 ir_initializer_kind_t get_initializer_kind(const ir_initializer_t *initializer)
769 return initializer->kind;
772 static void check_entity_initializer(ir_entity *entity)
778 void set_entity_initializer(ir_entity *entity, ir_initializer_t *initializer)
780 entity->attr.initializer = initializer;
781 entity->has_initializer = 1;
782 check_entity_initializer(entity);
785 ir_initializer_t *get_entity_initializer(const ir_entity *entity)
787 assert(entity->has_initializer);
788 return entity->attr.initializer;
791 /* Creates a new compound graph path. */
792 compound_graph_path *
793 new_compound_graph_path(ir_type *tp, int length) {
794 compound_graph_path *res;
796 assert(is_compound_type(tp));
799 res = xmalloc(sizeof(*res) + (length-1) * sizeof(res->list[0]));
800 memset(res, 0, sizeof(*res) + (length-1) * sizeof(res->list[0]));
801 res->kind = k_ir_compound_graph_path;
806 } /* new_compound_graph_path */
808 /* Frees an graph path object */
809 void free_compound_graph_path (compound_graph_path *gr) {
810 assert(gr && is_compound_graph_path(gr));
813 } /* free_compound_graph_path */
815 /* Returns non-zero if an object is a compound graph path */
816 int is_compound_graph_path(const void *thing) {
817 return (get_kind(thing) == k_ir_compound_graph_path);
818 } /* is_compound_graph_path */
820 /* Checks whether the path up to pos is correct. If the path contains a NULL,
821 * assumes the path is not complete and returns 'true'. */
822 int is_proper_compound_graph_path(compound_graph_path *gr, int pos) {
825 ir_type *owner = gr->tp;
827 for (i = 0; i <= pos; i++) {
828 node = get_compound_graph_path_node(gr, i);
830 /* Path not yet complete. */
832 if (get_entity_owner(node) != owner)
834 owner = get_entity_type(node);
836 if (pos == get_compound_graph_path_length(gr))
837 if (!is_atomic_type(owner))
840 } /* is_proper_compound_graph_path */
842 /* Returns the length of a graph path */
843 int get_compound_graph_path_length(const compound_graph_path *gr) {
844 assert(gr && is_compound_graph_path(gr));
846 } /* get_compound_graph_path_length */
849 get_compound_graph_path_node(const compound_graph_path *gr, int pos) {
850 assert(gr && is_compound_graph_path(gr));
851 assert(pos >= 0 && pos < gr->len);
852 return gr->list[pos].node;
853 } /* get_compound_graph_path_node */
856 set_compound_graph_path_node(compound_graph_path *gr, int pos, ir_entity *node) {
857 assert(gr && is_compound_graph_path(gr));
858 assert(pos >= 0 && pos < gr->len);
859 assert(is_entity(node));
860 gr->list[pos].node = node;
861 assert(is_proper_compound_graph_path(gr, pos));
862 } /* set_compound_graph_path_node */
865 get_compound_graph_path_array_index(const compound_graph_path *gr, int pos) {
866 assert(gr && is_compound_graph_path(gr));
867 assert(pos >= 0 && pos < gr->len);
868 return gr->list[pos].index;
869 } /* get_compound_graph_path_array_index */
872 set_compound_graph_path_array_index(compound_graph_path *gr, int pos, int index) {
873 assert(gr && is_compound_graph_path(gr));
874 assert(pos >= 0 && pos < gr->len);
875 gr->list[pos].index = index;
876 } /* set_compound_graph_path_array_index */
879 get_compound_graph_path_type(const compound_graph_path *gr) {
880 assert(gr && is_compound_graph_path(gr));
884 /* A value of a compound entity is a pair of value and the corresponding path to a member of
887 add_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path) {
888 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
889 assert(is_compound_graph_path(path));
890 ARR_APP1(ir_node *, ent->attr.cmpd_attr.values, val);
891 ARR_APP1(compound_graph_path *, ent->attr.cmpd_attr.val_paths, path);
892 } /* add_compound_ent_value_w_path */
895 set_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path, int pos) {
896 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
897 assert(is_compound_graph_path(path));
898 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
899 ent->attr.cmpd_attr.values[pos] = val;
900 ent->attr.cmpd_attr.val_paths[pos] = path;
901 } /* set_compound_ent_value_w_path */
904 get_compound_ent_n_values(ir_entity *ent) {
905 assert(!ent->has_initializer);
906 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
907 return ARR_LEN(ent->attr.cmpd_attr.values);
908 } /* get_compound_ent_n_values */
911 get_compound_ent_value(ir_entity *ent, int pos) {
912 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
913 assert(!ent->has_initializer);
914 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
915 return ent->attr.cmpd_attr.values[pos];
916 } /* get_compound_ent_value */
918 compound_graph_path *
919 get_compound_ent_value_path(ir_entity *ent, int pos) {
920 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
921 assert(!ent->has_initializer);
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 path2 the second path
932 static int equal_paths(compound_graph_path *path1, compound_graph_path *path2) {
934 int len1 = get_compound_graph_path_length(path1);
935 int len2 = get_compound_graph_path_length(path2);
937 if (len2 != len1) return 0;
939 for (i = 0; i < len1; i++) {
941 ir_entity *node1 = get_compound_graph_path_node(path1, i);
942 ir_entity *node2 = get_compound_graph_path_node(path2, i);
944 if (node1 != node2) return 0;
946 tp = get_entity_owner(node1);
947 if (is_Array_type(tp)) {
948 int index1 = get_compound_graph_path_array_index(path1, i);
949 int index2 = get_compound_graph_path_array_index(path2, i);
950 if (index1 != index2)
958 * Returns the position of a value with the given path.
959 * The path must contain array indices for all array element entities.
961 * @todo This implementation is very slow (O(number of initializers * |path|)
962 * and should be replaced when the new tree oriented
963 * value representation is finally implemented.
965 static int get_compound_ent_pos_by_path(ir_entity *ent, compound_graph_path *path) {
966 int i, n_paths = get_compound_ent_n_values(ent);
968 for (i = 0; i < n_paths; i ++) {
969 compound_graph_path *gr = get_compound_ent_value_path(ent, i);
970 if (equal_paths(gr, path))
974 } /* get_compound_ent_pos_by_path */
976 /* Returns a constant value given the access path.
977 * The path must contain array indices for all array element entities. */
978 ir_node *get_compound_ent_value_by_path(ir_entity *ent, compound_graph_path *path) {
979 int pos = get_compound_ent_pos_by_path(ent, path);
981 return get_compound_ent_value(ent, pos);
983 } /* get_compound_ent_value_by_path */
987 remove_compound_ent_value(ir_entity *ent, ir_entity *value_ent) {
989 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
991 n = ARR_LEN(ent->attr.cmpd_attr.val_paths);
992 for (i = 0; i < n; ++i) {
993 compound_graph_path *path = ent->attr.cmpd_attr.val_paths[i];
994 if (path->list[path->len-1].node == value_ent) {
995 for (; i < n - 1; ++i) {
996 ent->attr.cmpd_attr.val_paths[i] = ent->attr.cmpd_attr.val_paths[i+1];
997 ent->attr.cmpd_attr.values[i] = ent->attr.cmpd_attr.values[i+1];
999 ARR_SETLEN(ir_entity*, ent->attr.cmpd_attr.val_paths, n - 1);
1000 ARR_SETLEN(ir_node*, ent->attr.cmpd_attr.values, n - 1);
1004 } /* remove_compound_ent_value */
1007 add_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member) {
1008 compound_graph_path *path;
1009 ir_type *owner_tp = get_entity_owner(member);
1010 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
1011 path = new_compound_graph_path(get_entity_type(ent), 1);
1012 path->list[0].node = member;
1013 if (is_Array_type(owner_tp)) {
1017 assert(get_array_n_dimensions(owner_tp) == 1 && has_array_lower_bound(owner_tp, 0));
1018 max = get_array_lower_bound_int(owner_tp, 0) -1;
1019 for (i = 0, n = get_compound_ent_n_values(ent); i < n; ++i) {
1020 int index = get_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0);
1025 path->list[0].index = max + 1;
1027 add_compound_ent_value_w_path(ent, val, path);
1028 } /* add_compound_ent_value */
1032 get_compound_ent_value_member(ir_entity *ent, int pos) {
1033 compound_graph_path *path;
1034 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
1035 path = get_compound_ent_value_path(ent, pos);
1037 return get_compound_graph_path_node(path, get_compound_graph_path_length(path)-1);
1038 } /* get_compound_ent_value_member */
1041 set_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member, int pos) {
1042 compound_graph_path *path;
1043 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
1044 path = get_compound_ent_value_path(ent, pos);
1045 set_compound_graph_path_node(path, 0, member);
1046 set_compound_ent_value_w_path(ent, val, path, pos);
1047 } /* set_compound_ent_value */
1050 set_array_entity_values(ir_entity *ent, tarval **values, int num_vals) {
1052 ir_graph *rem = current_ir_graph;
1053 ir_type *arrtp = get_entity_type(ent);
1055 ir_type *elttp = get_array_element_type(arrtp);
1057 assert(is_Array_type(arrtp));
1058 assert(get_array_n_dimensions(arrtp) == 1);
1059 /* One bound is sufficient, the number of constant fields makes the
1061 assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
1062 assert(get_entity_variability(ent) != variability_uninitialized);
1063 current_ir_graph = get_const_code_irg();
1065 for (i = 0; i < num_vals; i++) {
1066 val = new_Const_type(values[i], elttp);
1067 add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
1068 set_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0, i);
1070 current_ir_graph = rem;
1071 } /* set_array_entity_values */
1073 /* Return the overall offset of value at position pos in bytes. */
1074 unsigned get_compound_ent_value_offset_bytes(ir_entity *ent, int pos) {
1075 compound_graph_path *path;
1077 unsigned offset = 0;
1080 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
1082 path = get_compound_ent_value_path(ent, pos);
1083 path_len = get_compound_graph_path_length(path);
1086 for (i = 0; i < path_len; ++i) {
1087 if (is_Array_type(curr_tp)) {
1088 ir_type *elem_type = get_array_element_type(curr_tp);
1089 unsigned size = get_type_size_bytes(elem_type);
1090 unsigned align = get_type_alignment_bytes(elem_type);
1094 if(size % align > 0) {
1095 size += align - (size % align);
1097 idx = get_compound_graph_path_array_index(path, i);
1099 offset += size * idx;
1100 curr_tp = elem_type;
1102 ir_entity *node = get_compound_graph_path_node(path, i);
1103 offset += get_entity_offset(node);
1104 curr_tp = get_entity_type(node);
1109 } /* get_compound_ent_value_offset_bytes */
1111 /* Return the offset in bits from the last byte address. */
1112 unsigned get_compound_ent_value_offset_bit_remainder(ir_entity *ent, int pos) {
1113 compound_graph_path *path;
1115 ir_entity *last_node;
1117 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
1119 path = get_compound_ent_value_path(ent, pos);
1120 path_len = get_compound_graph_path_length(path);
1121 last_node = get_compound_graph_path_node(path, path_len - 1);
1123 if(last_node == NULL)
1126 return get_entity_offset_bits_remainder(last_node);
1127 } /* get_compound_ent_value_offset_bit_remainder */
1130 (get_entity_offset)(const ir_entity *ent) {
1131 return _get_entity_offset(ent);
1132 } /* get_entity_offset */
1135 (set_entity_offset)(ir_entity *ent, int offset) {
1136 _set_entity_offset(ent, offset);
1137 } /* set_entity_offset */
1140 (get_entity_offset_bits_remainder)(const ir_entity *ent) {
1141 return _get_entity_offset_bits_remainder(ent);
1142 } /* get_entity_offset_bits_remainder */
1145 (set_entity_offset_bits_remainder)(ir_entity *ent, unsigned char offset) {
1146 _set_entity_offset_bits_remainder(ent, offset);
1147 } /* set_entity_offset_bits_remainder */
1150 add_entity_overwrites(ir_entity *ent, ir_entity *overwritten) {
1152 ir_type *owner = get_entity_owner(ent);
1153 ir_type *ovw_ovner = get_entity_owner(overwritten);
1154 assert(is_Class_type(owner));
1155 assert(is_Class_type(ovw_ovner));
1156 assert(! is_class_final(ovw_ovner));
1158 ARR_APP1(ir_entity *, ent->overwrites, overwritten);
1159 ARR_APP1(ir_entity *, overwritten->overwrittenby, ent);
1160 } /* add_entity_overwrites */
1163 get_entity_n_overwrites(ir_entity *ent) {
1164 assert(is_Class_type(get_entity_owner(ent)));
1165 return (ARR_LEN(ent->overwrites));
1166 } /* get_entity_n_overwrites */
1169 get_entity_overwrites_index(ir_entity *ent, ir_entity *overwritten) {
1171 assert(is_Class_type(get_entity_owner(ent)));
1172 n = get_entity_n_overwrites(ent);
1173 for (i = 0; i < n; ++i) {
1174 if (get_entity_overwrites(ent, i) == overwritten)
1178 } /* get_entity_overwrites_index */
1181 get_entity_overwrites(ir_entity *ent, int pos) {
1182 assert(is_Class_type(get_entity_owner(ent)));
1183 assert(pos < get_entity_n_overwrites(ent));
1184 return ent->overwrites[pos];
1185 } /* get_entity_overwrites */
1188 set_entity_overwrites(ir_entity *ent, int pos, ir_entity *overwritten) {
1189 assert(is_Class_type(get_entity_owner(ent)));
1190 assert(pos < get_entity_n_overwrites(ent));
1191 ent->overwrites[pos] = overwritten;
1192 } /* set_entity_overwrites */
1195 remove_entity_overwrites(ir_entity *ent, ir_entity *overwritten) {
1197 assert(is_Class_type(get_entity_owner(ent)));
1198 n = ARR_LEN(ent->overwrites);
1199 for (i = 0; i < n; ++i) {
1200 if (ent->overwrites[i] == overwritten) {
1201 for (; i < n - 1; i++)
1202 ent->overwrites[i] = ent->overwrites[i+1];
1203 ARR_SETLEN(ir_entity*, ent->overwrites, n - 1);
1207 } /* remove_entity_overwrites */
1210 add_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites) {
1211 add_entity_overwrites(overwrites, ent);
1212 } /* add_entity_overwrittenby */
1215 get_entity_n_overwrittenby(ir_entity *ent) {
1216 assert(is_Class_type(get_entity_owner(ent)));
1217 return ARR_LEN(ent->overwrittenby);
1218 } /* get_entity_n_overwrittenby */
1221 get_entity_overwrittenby_index(ir_entity *ent, ir_entity *overwrites) {
1223 assert(is_Class_type(get_entity_owner(ent)));
1224 n = get_entity_n_overwrittenby(ent);
1225 for (i = 0; i < n; ++i) {
1226 if (get_entity_overwrittenby(ent, i) == overwrites)
1230 } /* get_entity_overwrittenby_index */
1233 get_entity_overwrittenby(ir_entity *ent, int pos) {
1234 assert(is_Class_type(get_entity_owner(ent)));
1235 assert(pos < get_entity_n_overwrittenby(ent));
1236 return ent->overwrittenby[pos];
1237 } /* get_entity_overwrittenby */
1240 set_entity_overwrittenby(ir_entity *ent, int pos, ir_entity *overwrites) {
1241 assert(is_Class_type(get_entity_owner(ent)));
1242 assert(pos < get_entity_n_overwrittenby(ent));
1243 ent->overwrittenby[pos] = overwrites;
1244 } /* set_entity_overwrittenby */
1246 void remove_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites) {
1248 assert(is_Class_type(get_entity_owner(ent)));
1250 n = ARR_LEN(ent->overwrittenby);
1251 for (i = 0; i < n; ++i) {
1252 if (ent->overwrittenby[i] == overwrites) {
1253 for(; i < n - 1; ++i)
1254 ent->overwrittenby[i] = ent->overwrittenby[i+1];
1255 ARR_SETLEN(ir_entity*, ent->overwrittenby, n - 1);
1259 } /* remove_entity_overwrittenby */
1261 /* A link to store intermediate information */
1263 (get_entity_link)(const ir_entity *ent) {
1264 return _get_entity_link(ent);
1265 } /* get_entity_link */
1268 (set_entity_link)(ir_entity *ent, void *l) {
1269 _set_entity_link(ent, l);
1270 } /* set_entity_link */
1273 (get_entity_irg)(const ir_entity *ent) {
1274 return _get_entity_irg(ent);
1275 } /* get_entity_irg */
1278 set_entity_irg(ir_entity *ent, ir_graph *irg) {
1279 assert(is_method_entity(ent));
1280 /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
1281 * Methode selbst nicht mehr aufgerufen werden kann, die Entität
1282 * aber erhalten bleiben soll? Wandle die Entitaet in description oder
1285 assert((irg && ent->peculiarity == peculiarity_existent) ||
1286 (!irg && (ent->peculiarity == peculiarity_existent)
1287 && (ent -> visibility == visibility_external_allocated)) ||
1288 (!irg && ent->peculiarity == peculiarity_description) ||
1289 (!irg && ent->peculiarity == peculiarity_inherited));
1290 ent->attr.mtd_attr.irg = irg;
1291 } /* set_entity_irg */
1293 unsigned get_entity_vtable_number(const ir_entity *ent) {
1294 assert(is_method_entity((ir_entity *)ent));
1295 return ent->attr.mtd_attr.vtable_number;
1296 } /* get_entity_vtable_number */
1298 void set_entity_vtable_number(ir_entity *ent, unsigned vtable_number) {
1299 assert(is_method_entity(ent));
1300 ent->attr.mtd_attr.vtable_number = vtable_number;
1301 } /* set_entity_vtable_number */
1304 (is_entity)(const void *thing) {
1305 return _is_entity(thing);
1308 int is_atomic_entity(ir_entity *ent) {
1309 ir_type *t = get_entity_type(ent);
1310 const tp_op *op = get_type_tpop(t);
1311 return (op == type_primitive || op == type_pointer ||
1312 op == type_enumeration || op == type_method);
1313 } /* is_atomic_entity */
1315 int is_compound_entity(ir_entity *ent) {
1316 ir_type *t = get_entity_type(ent);
1317 const tp_op *op = get_type_tpop(t);
1318 return (op == type_class || op == type_struct ||
1319 op == type_array || op == type_union);
1320 } /* is_compound_entity */
1322 int is_method_entity(ir_entity *ent) {
1323 ir_type *t = get_entity_type(ent);
1324 return is_Method_type(t);
1325 } /* is_method_entity */
1328 * @todo not implemented!!! */
1329 int equal_entity(ir_entity *ent1, ir_entity *ent2) {
1332 fprintf(stderr, " calling unimplemented equal entity!!! \n");
1334 } /* equal_entity */
1337 ir_visited_t (get_entity_visited)(ir_entity *ent) {
1338 return _get_entity_visited(ent);
1339 } /* get_entity_visited */
1341 void (set_entity_visited)(ir_entity *ent, ir_visited_t num) {
1342 _set_entity_visited(ent, num);
1343 } /* set_entity_visited */
1345 /* Sets visited field in ir_entity to entity_visited. */
1346 void (mark_entity_visited)(ir_entity *ent) {
1347 _mark_entity_visited(ent);
1348 } /* mark_entity_visited */
1350 int (entity_visited)(ir_entity *ent) {
1351 return _entity_visited(ent);
1352 } /* entity_visited */
1354 int (entity_not_visited)(ir_entity *ent) {
1355 return _entity_not_visited(ent);
1356 } /* entity_not_visited */
1358 /* Returns the mask of the additional entity properties. */
1359 unsigned get_entity_additional_properties(ir_entity *ent) {
1362 assert(is_method_entity(ent));
1364 /* first check, if the graph has additional properties */
1365 irg = get_entity_irg(ent);
1368 return get_irg_additional_properties(irg);
1370 if (ent->attr.mtd_attr.irg_add_properties & mtp_property_inherited)
1371 return get_method_additional_properties(get_entity_type(ent));
1373 return ent->attr.mtd_attr.irg_add_properties;
1374 } /* get_entity_additional_properties */
1376 /* Sets the mask of the additional graph properties. */
1377 void set_entity_additional_properties(ir_entity *ent, unsigned property_mask)
1381 assert(is_method_entity(ent));
1383 /* first check, if the graph exists */
1384 irg = get_entity_irg(ent);
1386 set_irg_additional_properties(irg, property_mask);
1388 /* do not allow to set the mtp_property_inherited flag or
1389 * the automatic inheritance of flags will not work */
1390 ent->attr.mtd_attr.irg_add_properties = property_mask & ~mtp_property_inherited;
1392 } /* set_entity_additional_properties */
1394 /* Sets one additional graph property. */
1395 void set_entity_additional_property(ir_entity *ent, mtp_additional_property flag)
1399 assert(is_method_entity(ent));
1401 /* first check, if the graph exists */
1402 irg = get_entity_irg(ent);
1404 set_irg_additional_property(irg, flag);
1406 unsigned mask = ent->attr.mtd_attr.irg_add_properties;
1408 if (mask & mtp_property_inherited)
1409 mask = get_method_additional_properties(get_entity_type(ent));
1411 /* do not allow to set the mtp_property_inherited flag or
1412 * the automatic inheritance of flags will not work */
1413 ent->attr.mtd_attr.irg_add_properties = mask | (flag & ~mtp_property_inherited);
1415 } /* set_entity_additional_property */
1417 /* Returns the class type that this type info entity represents or NULL
1418 if ent is no type info entity. */
1419 ir_type *(get_entity_repr_class)(const ir_entity *ent) {
1420 return _get_entity_repr_class(ent);
1421 } /* get_entity_repr_class */
1423 dbg_info *(get_entity_dbg_info)(const ir_entity *ent) {
1424 return _get_entity_dbg_info(ent);
1425 } /* get_entity_dbg_info */
1427 void (set_entity_dbg_info)(ir_entity *ent, dbg_info *db) {
1428 _set_entity_dbg_info(ent, db);
1429 } /* set_entity_dbg_info */
1431 /* Initialize entity module. */
1432 void firm_init_entity(void)
1434 symconst_symbol sym;
1436 assert(firm_unknown_type && "Call init_type() before firm_init_entity()!");
1437 assert(!unknown_entity && "Call firm_init_entity() only once!");
1439 unknown_entity = new_rd_entity(NULL, firm_unknown_type, new_id_from_str(UNKNOWN_ENTITY_NAME), firm_unknown_type);
1440 set_entity_visibility(unknown_entity, visibility_external_allocated);
1441 set_entity_ld_ident(unknown_entity, get_entity_ident(unknown_entity));
1443 current_ir_graph = get_const_code_irg();
1444 sym.entity_p = unknown_entity;
1445 /* TODO: we need two unknown_entities here, one for code and one for data */
1446 unknown_entity->value = new_SymConst(mode_P_data, sym, symconst_addr_ent);
1447 } /* firm_init_entity */