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;
150 res->variability = variability_uninitialized;
154 if (is_Class_type(owner)) {
155 res->overwrites = NEW_ARR_F(ir_entity *, 0);
156 res->overwrittenby = NEW_ARR_F(ir_entity *, 0);
158 res->overwrites = NULL;
159 res->overwrittenby = NULL;
163 res->nr = get_irp_new_node_nr();
164 #endif /* DEBUG_libfirm */
167 set_entity_dbg_info(res, db);
170 } /* new_rd_entity */
173 new_d_entity(ir_type *owner, ident *name, ir_type *type, dbg_info *db) {
176 assert(is_compound_type(owner));
177 res = new_rd_entity(db, owner, name, type);
178 /* Remember entity in it's owner. */
179 insert_entity_in_owner(res);
181 hook_new_entity(res);
186 new_entity(ir_type *owner, ident *name, ir_type *type) {
187 return new_d_entity(owner, name, type, NULL);
191 * Free entity attributes.
193 * @param ent the entity
195 static void free_entity_attrs(ir_entity *ent) {
197 if (get_type_tpop(get_entity_owner(ent)) == type_class) {
198 DEL_ARR_F(ent->overwrites); ent->overwrites = NULL;
199 DEL_ARR_F(ent->overwrittenby); ent->overwrittenby = NULL;
201 assert(ent->overwrites == NULL);
202 assert(ent->overwrittenby == NULL);
204 if (is_compound_entity(ent)) {
205 if (ent->has_initializer) {
206 /* TODO: free initializers */
208 if (ent->attr.cmpd_attr.val_paths) {
209 for (i = get_compound_ent_n_values(ent) - 1; i >= 0; --i)
210 if (ent->attr.cmpd_attr.val_paths[i]) {
211 /* free_compound_graph_path(ent->attr.cmpd_attr.val_paths[i]) ; * @@@ warum nich? */
212 /* Geht nich: wird mehrfach verwendet!!! ==> mehrfach frei gegeben. */
213 /* DEL_ARR_F(ent->attr.cmpd_attr.val_paths); */
215 ent->attr.cmpd_attr.val_paths = NULL;
217 if (ent->attr.cmpd_attr.values) {
218 /*DEL_ARR_F(ent->attr.cmpd_attr.values)*/;
220 ent->attr.cmpd_attr.values = NULL;
222 } else if (is_method_entity(ent)) {
223 if (ent->attr.mtd_attr.param_access) {
224 DEL_ARR_F(ent->attr.mtd_attr.param_access);
225 ent->attr.mtd_attr.param_access = NULL;
227 if (ent->attr.mtd_attr.param_weight) {
228 DEL_ARR_F(ent->attr.mtd_attr.param_weight);
229 ent->attr.mtd_attr.param_weight = NULL;
232 } /* free_entity_attrs */
235 * Creates a deep copy of an entity.
237 static ir_entity *deep_entity_copy(ir_entity *old)
239 ir_entity *newe = XMALLOC(ir_entity);
242 if (is_compound_entity(old)) {
243 if (old->has_initializer) {
244 /* FIXME: the initializers are NOT copied */
246 newe->attr.cmpd_attr.values = NULL;
247 newe->attr.cmpd_attr.val_paths = NULL;
248 if (old->attr.cmpd_attr.values)
249 newe->attr.cmpd_attr.values = DUP_ARR_F(ir_node *, old->attr.cmpd_attr.values);
251 /* FIXME: the compound graph paths are NOT copied */
252 if (old->attr.cmpd_attr.val_paths)
253 newe->attr.cmpd_attr.val_paths = DUP_ARR_F(compound_graph_path *, old->attr.cmpd_attr.val_paths);
255 } else if (is_method_entity(old)) {
256 /* do NOT copy them, reanalyze. This might be the best solution */
257 newe->attr.mtd_attr.param_access = NULL;
258 newe->attr.mtd_attr.param_weight = NULL;
262 newe->nr = get_irp_new_node_nr();
267 * Copies the entity if the new_owner is different from the
268 * owner of the old entity, else returns the old entity.
271 copy_entity_own(ir_entity *old, ir_type *new_owner) {
273 assert(is_entity(old));
274 assert(is_compound_type(new_owner));
275 assert(get_type_state(new_owner) != layout_fixed);
277 if (old->owner == new_owner)
280 /* create a deep copy so we are safe of aliasing and double-freeing. */
281 newe = deep_entity_copy(old);
282 newe->owner = new_owner;
284 if (is_Class_type(new_owner)) {
285 newe->overwrites = NEW_ARR_F(ir_entity *, 0);
286 newe->overwrittenby = NEW_ARR_F(ir_entity *, 0);
289 insert_entity_in_owner(newe);
291 } /* copy_entity_own */
294 copy_entity_name(ir_entity *old, ident *new_name) {
296 assert(old && old->kind == k_entity);
298 if (old->name == new_name) return old;
299 newe = deep_entity_copy(old);
300 newe->name = new_name;
301 newe->ld_name = NULL;
303 if (is_Class_type(newe->owner)) {
304 newe->overwrites = DUP_ARR_F(ir_entity *, old->overwrites);
305 newe->overwrittenby = DUP_ARR_F(ir_entity *, old->overwrittenby);
307 insert_entity_in_owner(newe);
310 } /* copy_entity_name */
313 free_entity(ir_entity *ent) {
314 assert(ent && ent->kind == k_entity);
315 free_entity_attrs(ent);
320 /* Outputs a unique number for this node */
322 get_entity_nr(const ir_entity *ent) {
323 assert(ent && ent->kind == k_entity);
327 return (long)PTR_TO_INT(ent);
329 } /* get_entity_nr */
332 (get_entity_name)(const ir_entity *ent) {
333 return _get_entity_name(ent);
334 } /* get_entity_name */
337 (get_entity_ident)(const ir_entity *ent) {
338 return _get_entity_ident(ent);
339 } /* get_entity_ident */
342 (set_entity_ident)(ir_entity *ent, ident *id) {
343 _set_entity_ident(ent, id);
344 } /* set_entity_ident */
347 (get_entity_owner)(ir_entity *ent) {
348 return _get_entity_owner(ent);
349 } /* get_entity_owner */
352 set_entity_owner(ir_entity *ent, ir_type *owner) {
353 assert(is_entity(ent));
354 assert(is_compound_type(owner));
356 } /* set_entity_owner */
359 (get_entity_ld_ident)(ir_entity *ent) {
360 return _get_entity_ld_ident(ent);
361 } /* get_entity_ld_ident */
364 (set_entity_ld_ident)(ir_entity *ent, ident *ld_ident) {
365 _set_entity_ld_ident(ent, ld_ident);
366 } /* set_entity_ld_ident */
369 (get_entity_ld_name)(ir_entity *ent) {
370 return _get_entity_ld_name(ent);
371 } /* get_entity_ld_name */
374 (get_entity_type)(ir_entity *ent) {
375 return _get_entity_type(ent);
376 } /* get_entity_type */
379 (set_entity_type)(ir_entity *ent, ir_type *type) {
380 _set_entity_type(ent, type);
381 } /* set_entity_type */
384 (get_entity_allocation)(const ir_entity *ent) {
385 return _get_entity_allocation(ent);
386 } /* get_entity_allocation */
389 (set_entity_allocation)(ir_entity *ent, ir_allocation al) {
390 _set_entity_allocation(ent, al);
391 } /* set_entity_allocation */
393 /* return the name of the visibility */
394 const char *get_allocation_name(ir_allocation al)
396 #define X(a) case a: return #a
398 X(allocation_automatic);
399 X(allocation_parameter);
400 X(allocation_dynamic);
401 X(allocation_static);
402 default: return "BAD VALUE";
405 } /* get_allocation_name */
408 (get_entity_visibility)(const ir_entity *ent) {
409 return _get_entity_visibility(ent);
410 } /* get_entity_visibility */
413 set_entity_visibility(ir_entity *ent, ir_visibility vis) {
414 assert(ent && ent->kind == k_entity);
415 if (vis != visibility_local)
416 assert((ent->allocation == allocation_static) ||
417 (ent->allocation == allocation_automatic));
418 /* @@@ Test that the owner type is not local, but how??
419 && get_class_visibility(get_entity_owner(ent)) != local));*/
420 ent->visibility = vis;
421 } /* set_entity_visibility */
423 /* return the name of the visibility */
424 const char *get_visibility_name(ir_visibility vis)
426 #define X(a) case a: return #a
429 X(visibility_external_visible);
430 X(visibility_external_allocated);
431 default: return "BAD VALUE";
434 } /* get_visibility_name */
437 (get_entity_variability)(const ir_entity *ent) {
438 return _get_entity_variability(ent);
439 } /* get_entity_variability */
442 set_entity_variability(ir_entity *ent, ir_variability var)
444 assert(ent && ent->kind == k_entity);
445 if (var == variability_part_constant)
446 assert(is_Class_type(ent->type) || is_Struct_type(ent->type));
448 if ((is_compound_type(ent->type)) &&
449 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
450 /* Allocate data structures for constant values */
451 ent->attr.cmpd_attr.values = NEW_ARR_F(ir_node *, 0);
452 ent->attr.cmpd_attr.val_paths = NEW_ARR_F(compound_graph_path *, 0);
454 if ((is_atomic_type(ent->type)) &&
455 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
456 /* Set default constant value. */
457 ent->value = new_r_Unknown(get_const_code_irg(), get_type_mode(ent->type));
460 if ((is_compound_type(ent->type)) &&
461 (var == variability_uninitialized) && (ent->variability != variability_uninitialized)) {
462 /* Free data structures for constant values */
463 DEL_ARR_F(ent->attr.cmpd_attr.values); ent->attr.cmpd_attr.values = NULL;
464 DEL_ARR_F(ent->attr.cmpd_attr.val_paths); ent->attr.cmpd_attr.val_paths = NULL;
466 ent->variability = var;
467 } /* set_entity_variability */
469 /* return the name of the variability */
470 const char *get_variability_name(ir_variability var)
472 #define X(a) case a: return #a
474 X(variability_uninitialized);
475 X(variability_initialized);
476 X(variability_part_constant);
477 X(variability_constant);
478 default: return "BAD VALUE";
481 } /* get_variability_name */
484 (get_entity_volatility)(const ir_entity *ent) {
485 return _get_entity_volatility(ent);
486 } /* get_entity_volatility */
489 (set_entity_volatility)(ir_entity *ent, ir_volatility vol) {
490 _set_entity_volatility(ent, vol);
491 } /* set_entity_volatility */
493 /* Return the name of the volatility. */
494 const char *get_volatility_name(ir_volatility var)
496 #define X(a) case a: return #a
498 X(volatility_non_volatile);
499 X(volatility_is_volatile);
500 default: return "BAD VALUE";
503 } /* get_volatility_name */
506 (get_entity_aligned)(const ir_entity *ent) {
507 return _get_entity_aligned(ent);
511 (set_entity_aligned)(ir_entity *ent, ir_align a) {
512 _set_entity_aligned(ent, a);
516 (get_entity_alignment)(const ir_entity *ent) {
517 return _get_entity_alignment(ent);
521 (set_entity_alignment)(ir_entity *ent, unsigned alignment) {
522 _set_entity_alignment(ent, alignment);
525 /* Return the name of the alignment. */
526 const char *get_align_name(ir_align a)
528 #define X(a) case a: return #a
530 X(align_non_aligned);
532 default: return "BAD VALUE";
535 } /* get_align_name */
538 (get_entity_peculiarity)(const ir_entity *ent) {
539 return _get_entity_peculiarity(ent);
540 } /* get_entity_peculiarity */
543 (set_entity_peculiarity)(ir_entity *ent, ir_peculiarity pec) {
544 _set_entity_peculiarity(ent, pec);
545 } /* set_entity_peculiarity */
547 /* Checks if an entity cannot be overridden anymore. */
548 int (is_entity_final)(const ir_entity *ent) {
549 return _is_entity_final(ent);
550 } /* is_entity_final */
552 /* Sets/resets the final flag of an entity. */
553 void (set_entity_final)(ir_entity *ent, int final) {
554 _set_entity_final(ent, final);
555 } /* set_entity_final */
557 /* Checks if an entity is compiler generated */
558 int (is_entity_compiler_generated)(const ir_entity *ent) {
559 return _is_entity_compiler_generated(ent);
560 } /* is_entity_compiler_generated */
562 /* Sets/resets the compiler generated flag */
563 void (set_entity_compiler_generated)(ir_entity *ent, int flag) {
564 _set_entity_compiler_generated(ent, flag);
565 } /* set_entity_compiler_generated */
567 /* Checks if an entity is marked by the backend */
568 int (is_entity_backend_marked)(const ir_entity *ent) {
569 return _is_entity_backend_marked(ent);
570 } /* is_entity_backend_marked */
572 /* Sets/resets the compiler generated flag */
573 void (set_entity_backend_marked)(ir_entity *ent, int flag) {
574 _set_entity_backend_marked(ent, flag);
575 } /* set_entity_backend_marked */
577 ir_entity_usage (get_entity_usage)(const ir_entity *ent) {
578 return _get_entity_usage(ent);
581 void (set_entity_usage)(ir_entity *ent, ir_entity_usage flags) {
582 _set_entity_usage(ent, flags);
585 /* Get the entity's stickyness */
587 (get_entity_stickyness)(const ir_entity *ent) {
588 return _get_entity_stickyness(ent);
589 } /* get_entity_stickyness */
591 /* Set the entity's stickyness */
593 (set_entity_stickyness)(ir_entity *ent, ir_stickyness stickyness) {
594 _set_entity_stickyness(ent, stickyness);
595 } /* set_entity_stickyness */
597 /* Set has no effect for existent entities of type method. */
599 get_atomic_ent_value(ir_entity *ent)
601 assert(ent && is_atomic_entity(ent));
602 assert(ent->variability != variability_uninitialized);
603 return skip_Id(ent->value);
604 } /* get_atomic_ent_value */
607 set_atomic_ent_value(ir_entity *ent, ir_node *val) {
608 assert(is_atomic_entity(ent) && (ent->variability != variability_uninitialized));
609 if (is_Method_type(ent->type) && (ent->peculiarity == peculiarity_existent))
611 assert(is_Dummy(val) || get_irn_mode(val) == get_type_mode(ent->type));
613 } /* set_atomic_ent_value */
615 /* Returns true if the the node is representable as code on
617 int is_irn_const_expression(ir_node *n) {
620 /* we are in danger iff an exception will arise. TODO: be more precisely,
621 * for instance Div. will NOT rise if divisor != 0
623 if (is_binop(n) && !is_fragile_op(n))
624 return is_irn_const_expression(get_binop_left(n)) && is_irn_const_expression(get_binop_right(n));
627 switch (get_irn_opcode(n)) {
634 return is_irn_const_expression(get_irn_n(n, 0));
639 } /* is_irn_const_expression */
642 * Copies a firm subgraph that complies to the restrictions for
643 * constant expressions to current_block in current_ir_graph.
645 ir_node *copy_const_value(dbg_info *dbg, ir_node *n) {
649 /* @@@ GL I think we should implement this using the routines from irgopt for
650 dead node elimination/inlineing. */
653 switch (get_irn_opcode(n)) {
655 nn = new_d_Const_type(dbg, get_Const_tarval(n), get_Const_type(n));
658 nn = new_d_SymConst_type(dbg, get_irn_mode(n), get_SymConst_symbol(n), get_SymConst_kind(n),
659 get_SymConst_value_type(n));
662 nn = new_d_Add(dbg, copy_const_value(dbg, get_Add_left(n)),
663 copy_const_value(dbg, get_Add_right(n)), m); break;
665 nn = new_d_Sub(dbg, copy_const_value(dbg, get_Sub_left(n)),
666 copy_const_value(dbg, get_Sub_right(n)), m); break;
668 nn = new_d_Mul(dbg, copy_const_value(dbg, get_Mul_left(n)),
669 copy_const_value(dbg, get_Mul_right(n)), m); break;
671 nn = new_d_And(dbg, copy_const_value(dbg, get_And_left(n)),
672 copy_const_value(dbg, get_And_right(n)), m); break;
674 nn = new_d_Or(dbg, copy_const_value(dbg, get_Or_left(n)),
675 copy_const_value(dbg, get_Or_right(n)), m); break;
677 nn = new_d_Eor(dbg, copy_const_value(dbg, get_Eor_left(n)),
678 copy_const_value(dbg, get_Eor_right(n)), m); break;
680 nn = new_d_Cast(dbg, copy_const_value(dbg, get_Cast_op(n)), get_Cast_type(n)); break;
682 nn = new_d_Conv(dbg, copy_const_value(dbg, get_Conv_op(n)), m); break;
684 nn = new_Unknown(m); break;
686 assert(0 && "opcode invalid or not implemented");
691 } /* copy_const_value */
693 /** Return the name of the initializer kind. */
694 const char *get_initializer_kind_name(ir_initializer_kind_t ini)
696 #define X(a) case a: return #a
698 X(IR_INITIALIZER_CONST);
699 X(IR_INITIALIZER_TARVAL);
700 X(IR_INITIALIZER_NULL);
701 X(IR_INITIALIZER_COMPOUND);
702 default: return "BAD VALUE";
707 static ir_initializer_t null_initializer = { IR_INITIALIZER_NULL };
709 ir_initializer_t *get_initializer_null(void)
711 return &null_initializer;
714 ir_initializer_t *create_initializer_const(ir_node *value)
716 struct obstack *obst = get_irg_obstack(get_const_code_irg());
718 ir_initializer_t *initializer
719 = obstack_alloc(obst, sizeof(ir_initializer_const_t));
720 initializer->kind = IR_INITIALIZER_CONST;
721 initializer->consti.value = value;
726 ir_initializer_t *create_initializer_tarval(tarval *tv)
728 struct obstack *obst = get_irg_obstack(get_const_code_irg());
730 ir_initializer_t *initializer
731 = obstack_alloc(obst, sizeof(ir_initializer_tarval_t));
732 initializer->kind = IR_INITIALIZER_TARVAL;
733 initializer->tarval.value = tv;
738 ir_initializer_t *create_initializer_compound(unsigned n_entries)
740 struct obstack *obst = get_irg_obstack(get_const_code_irg());
743 size_t size = sizeof(ir_initializer_compound_t)
744 + (n_entries-1) * sizeof(ir_initializer_t*);
746 ir_initializer_t *initializer = obstack_alloc(obst, size);
747 initializer->kind = IR_INITIALIZER_COMPOUND;
748 initializer->compound.n_initializers = n_entries;
750 for(i = 0; i < n_entries; ++i) {
751 initializer->compound.initializers[i] = get_initializer_null();
757 ir_node *get_initializer_const_value(const ir_initializer_t *initializer)
759 assert(initializer->kind == IR_INITIALIZER_CONST);
760 return skip_Id(initializer->consti.value);
763 tarval *get_initializer_tarval_value(const ir_initializer_t *initializer)
765 assert(initializer->kind == IR_INITIALIZER_TARVAL);
766 return initializer->tarval.value;
769 unsigned get_initializer_compound_n_entries(const ir_initializer_t *initializer)
771 assert(initializer->kind == IR_INITIALIZER_COMPOUND);
772 return initializer->compound.n_initializers;
775 void set_initializer_compound_value(ir_initializer_t *initializer,
776 unsigned index, ir_initializer_t *value)
778 assert(initializer->kind == IR_INITIALIZER_COMPOUND);
779 assert(index < initializer->compound.n_initializers);
781 initializer->compound.initializers[index] = value;
784 ir_initializer_t *get_initializer_compound_value(
785 const ir_initializer_t *initializer, unsigned index)
787 assert(initializer->kind == IR_INITIALIZER_COMPOUND);
788 assert(index < initializer->compound.n_initializers);
790 return initializer->compound.initializers[index];
793 ir_initializer_kind_t get_initializer_kind(const ir_initializer_t *initializer)
795 return initializer->kind;
798 static void check_entity_initializer(ir_entity *entity)
804 void set_entity_initializer(ir_entity *entity, ir_initializer_t *initializer)
806 entity->attr.initializer = initializer;
807 entity->has_initializer = 1;
808 check_entity_initializer(entity);
811 int has_entity_initializer(const ir_entity *entity)
813 return entity->has_initializer;
816 ir_initializer_t *get_entity_initializer(const ir_entity *entity)
818 assert(entity->has_initializer);
819 return entity->attr.initializer;
822 /* Creates a new compound graph path. */
823 compound_graph_path *
824 new_compound_graph_path(ir_type *tp, int length) {
825 compound_graph_path *res;
827 assert(is_compound_type(tp));
830 res = xmalloc(sizeof(*res) + (length-1) * sizeof(res->list[0]));
831 memset(res, 0, sizeof(*res) + (length-1) * sizeof(res->list[0]));
832 res->kind = k_ir_compound_graph_path;
837 } /* new_compound_graph_path */
839 /* Frees an graph path object */
840 void free_compound_graph_path (compound_graph_path *gr) {
841 assert(gr && is_compound_graph_path(gr));
844 } /* free_compound_graph_path */
846 /* Returns non-zero if an object is a compound graph path */
847 int is_compound_graph_path(const void *thing) {
848 return (get_kind(thing) == k_ir_compound_graph_path);
849 } /* is_compound_graph_path */
851 /* Checks whether the path up to pos is correct. If the path contains a NULL,
852 * assumes the path is not complete and returns 'true'. */
853 int is_proper_compound_graph_path(compound_graph_path *gr, int pos) {
856 ir_type *owner = gr->tp;
858 for (i = 0; i <= pos; i++) {
859 node = get_compound_graph_path_node(gr, i);
861 /* Path not yet complete. */
863 if (get_entity_owner(node) != owner)
865 owner = get_entity_type(node);
867 if (pos == get_compound_graph_path_length(gr))
868 if (!is_atomic_type(owner))
871 } /* is_proper_compound_graph_path */
873 /* Returns the length of a graph path */
874 int get_compound_graph_path_length(const compound_graph_path *gr) {
875 assert(gr && is_compound_graph_path(gr));
877 } /* get_compound_graph_path_length */
880 get_compound_graph_path_node(const compound_graph_path *gr, int pos) {
881 assert(gr && is_compound_graph_path(gr));
882 assert(pos >= 0 && pos < gr->len);
883 return gr->list[pos].node;
884 } /* get_compound_graph_path_node */
887 set_compound_graph_path_node(compound_graph_path *gr, int pos, ir_entity *node) {
888 assert(gr && is_compound_graph_path(gr));
889 assert(pos >= 0 && pos < gr->len);
890 assert(is_entity(node));
891 gr->list[pos].node = node;
892 assert(is_proper_compound_graph_path(gr, pos));
893 } /* set_compound_graph_path_node */
896 get_compound_graph_path_array_index(const compound_graph_path *gr, int pos) {
897 assert(gr && is_compound_graph_path(gr));
898 assert(pos >= 0 && pos < gr->len);
899 return gr->list[pos].index;
900 } /* get_compound_graph_path_array_index */
903 set_compound_graph_path_array_index(compound_graph_path *gr, int pos, int index) {
904 assert(gr && is_compound_graph_path(gr));
905 assert(pos >= 0 && pos < gr->len);
906 gr->list[pos].index = index;
907 } /* set_compound_graph_path_array_index */
910 get_compound_graph_path_type(const compound_graph_path *gr) {
911 assert(gr && is_compound_graph_path(gr));
915 /* A value of a compound entity is a pair of value and the corresponding path to a member of
918 add_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path) {
919 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
920 assert(is_compound_graph_path(path));
921 ARR_APP1(ir_node *, ent->attr.cmpd_attr.values, val);
922 ARR_APP1(compound_graph_path *, ent->attr.cmpd_attr.val_paths, path);
923 } /* add_compound_ent_value_w_path */
926 set_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path, int pos) {
927 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
928 assert(is_compound_graph_path(path));
929 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
930 ent->attr.cmpd_attr.values[pos] = val;
931 ent->attr.cmpd_attr.val_paths[pos] = path;
932 } /* set_compound_ent_value_w_path */
935 get_compound_ent_n_values(ir_entity *ent) {
936 assert(!ent->has_initializer);
937 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
938 return ARR_LEN(ent->attr.cmpd_attr.values);
939 } /* get_compound_ent_n_values */
942 get_compound_ent_value(ir_entity *ent, int pos) {
943 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
944 assert(!ent->has_initializer);
945 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
946 return skip_Id(ent->attr.cmpd_attr.values[pos]);
947 } /* get_compound_ent_value */
949 compound_graph_path *
950 get_compound_ent_value_path(ir_entity *ent, int pos) {
951 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
952 assert(!ent->has_initializer);
953 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.val_paths));
954 return ent->attr.cmpd_attr.val_paths[pos];
955 } /* get_compound_ent_value_path */
958 * Returns non-zero, if two compound_graph_pathes are equal
960 * @param path1 the first path
961 * @param path2 the second path
963 static int equal_paths(compound_graph_path *path1, compound_graph_path *path2) {
965 int len1 = get_compound_graph_path_length(path1);
966 int len2 = get_compound_graph_path_length(path2);
968 if (len2 != len1) return 0;
970 for (i = 0; i < len1; i++) {
972 ir_entity *node1 = get_compound_graph_path_node(path1, i);
973 ir_entity *node2 = get_compound_graph_path_node(path2, i);
975 if (node1 != node2) return 0;
977 tp = get_entity_owner(node1);
978 if (is_Array_type(tp)) {
979 int index1 = get_compound_graph_path_array_index(path1, i);
980 int index2 = get_compound_graph_path_array_index(path2, i);
981 if (index1 != index2)
989 * Returns the position of a value with the given path.
990 * The path must contain array indices for all array element entities.
992 * @todo This implementation is very slow (O(number of initializers * |path|)
993 * and should be replaced when the new tree oriented
994 * value representation is finally implemented.
996 static int get_compound_ent_pos_by_path(ir_entity *ent, compound_graph_path *path) {
997 int i, n_paths = get_compound_ent_n_values(ent);
999 for (i = 0; i < n_paths; i ++) {
1000 compound_graph_path *gr = get_compound_ent_value_path(ent, i);
1001 if (equal_paths(gr, path))
1005 } /* get_compound_ent_pos_by_path */
1007 /* Returns a constant value given the access path.
1008 * The path must contain array indices for all array element entities. */
1009 ir_node *get_compound_ent_value_by_path(ir_entity *ent, compound_graph_path *path) {
1010 int pos = get_compound_ent_pos_by_path(ent, path);
1012 return get_compound_ent_value(ent, pos);
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 */
1335 (is_entity)(const void *thing) {
1336 return _is_entity(thing);
1339 int is_atomic_entity(ir_entity *ent) {
1340 ir_type *t = get_entity_type(ent);
1341 const tp_op *op = get_type_tpop(t);
1342 return (op == type_primitive || op == type_pointer ||
1343 op == type_enumeration || op == type_method);
1344 } /* is_atomic_entity */
1346 int is_compound_entity(ir_entity *ent) {
1347 ir_type *t = get_entity_type(ent);
1348 const tp_op *op = get_type_tpop(t);
1349 return (op == type_class || op == type_struct ||
1350 op == type_array || op == type_union);
1351 } /* is_compound_entity */
1353 int is_method_entity(ir_entity *ent) {
1354 ir_type *t = get_entity_type(ent);
1355 return is_Method_type(t);
1356 } /* is_method_entity */
1359 * @todo not implemented!!! */
1360 int equal_entity(ir_entity *ent1, ir_entity *ent2) {
1363 fprintf(stderr, " calling unimplemented equal entity!!! \n");
1365 } /* equal_entity */
1368 ir_visited_t (get_entity_visited)(ir_entity *ent) {
1369 return _get_entity_visited(ent);
1370 } /* get_entity_visited */
1372 void (set_entity_visited)(ir_entity *ent, ir_visited_t num) {
1373 _set_entity_visited(ent, num);
1374 } /* set_entity_visited */
1376 /* Sets visited field in ir_entity to entity_visited. */
1377 void (mark_entity_visited)(ir_entity *ent) {
1378 _mark_entity_visited(ent);
1379 } /* mark_entity_visited */
1381 int (entity_visited)(ir_entity *ent) {
1382 return _entity_visited(ent);
1383 } /* entity_visited */
1385 int (entity_not_visited)(ir_entity *ent) {
1386 return _entity_not_visited(ent);
1387 } /* entity_not_visited */
1389 /* Returns the mask of the additional entity properties. */
1390 unsigned get_entity_additional_properties(ir_entity *ent) {
1393 assert(is_method_entity(ent));
1395 /* first check, if the graph has additional properties */
1396 irg = get_entity_irg(ent);
1399 return get_irg_additional_properties(irg);
1401 if (ent->attr.mtd_attr.irg_add_properties & mtp_property_inherited)
1402 return get_method_additional_properties(get_entity_type(ent));
1404 return ent->attr.mtd_attr.irg_add_properties;
1405 } /* get_entity_additional_properties */
1407 /* Sets the mask of the additional graph properties. */
1408 void set_entity_additional_properties(ir_entity *ent, unsigned property_mask)
1412 assert(is_method_entity(ent));
1414 /* first check, if the graph exists */
1415 irg = get_entity_irg(ent);
1417 set_irg_additional_properties(irg, property_mask);
1419 /* do not allow to set the mtp_property_inherited flag or
1420 * the automatic inheritance of flags will not work */
1421 ent->attr.mtd_attr.irg_add_properties = property_mask & ~mtp_property_inherited;
1423 } /* set_entity_additional_properties */
1425 /* Sets one additional graph property. */
1426 void set_entity_additional_property(ir_entity *ent, mtp_additional_property flag)
1430 assert(is_method_entity(ent));
1432 /* first check, if the graph exists */
1433 irg = get_entity_irg(ent);
1435 set_irg_additional_property(irg, flag);
1437 unsigned mask = ent->attr.mtd_attr.irg_add_properties;
1439 if (mask & mtp_property_inherited)
1440 mask = get_method_additional_properties(get_entity_type(ent));
1442 /* do not allow to set the mtp_property_inherited flag or
1443 * the automatic inheritance of flags will not work */
1444 ent->attr.mtd_attr.irg_add_properties = mask | (flag & ~mtp_property_inherited);
1446 } /* set_entity_additional_property */
1448 /* Returns the class type that this type info entity represents or NULL
1449 if ent is no type info entity. */
1450 ir_type *(get_entity_repr_class)(const ir_entity *ent) {
1451 return _get_entity_repr_class(ent);
1452 } /* get_entity_repr_class */
1454 dbg_info *(get_entity_dbg_info)(const ir_entity *ent) {
1455 return _get_entity_dbg_info(ent);
1456 } /* get_entity_dbg_info */
1458 void (set_entity_dbg_info)(ir_entity *ent, dbg_info *db) {
1459 _set_entity_dbg_info(ent, db);
1460 } /* set_entity_dbg_info */
1462 /* Initialize entity module. */
1463 void firm_init_entity(void)
1465 symconst_symbol sym;
1467 assert(firm_unknown_type && "Call init_type() before firm_init_entity()!");
1468 assert(!unknown_entity && "Call firm_init_entity() only once!");
1470 unknown_entity = new_rd_entity(NULL, firm_unknown_type, new_id_from_str(UNKNOWN_ENTITY_NAME), firm_unknown_type);
1471 set_entity_visibility(unknown_entity, visibility_external_allocated);
1472 set_entity_ld_ident(unknown_entity, get_entity_ident(unknown_entity));
1474 current_ir_graph = get_const_code_irg();
1475 sym.entity_p = unknown_entity;
1476 /* TODO: we need two unknown_entities here, one for code and one for data */
1477 unknown_entity->value = new_SymConst(mode_P_data, sym, symconst_addr_ent);
1478 } /* firm_init_entity */