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"
50 * An interval initializer.
52 typedef struct interval_initializer interval_initializer;
55 * A value initializer.
57 typedef struct value_initializer value_initializer;
59 struct interval_initializer {
60 int first_index; /**< The first index of the initialized interval. */
61 int last_index; /**< The last index of the initialized interval. */
62 interval_initializer *next; /**< Points to the next interval initializer. */
65 struct value_initializer {
66 ir_entity *ent; /**< The initialized entity. */
67 value_initializer *next; /**< Points to the next value initializer. */
70 typedef union initializer {
71 ir_node *value; /**< The value of the initializer. */
72 ir_node **values; /**< The values of an interval. */
73 value_initializer *val_init; /**< Points the the head of the next value initializers. */
74 interval_initializer *int_init; /**< Points to the head of the next value initializers. */
77 /*-----------------------------------------------------------------*/
79 /*-----------------------------------------------------------------*/
81 ir_entity *unknown_entity = NULL;
83 ir_entity *get_unknown_entity(void) { return unknown_entity; }
85 /** The name of the unknown entity. */
86 #define UNKNOWN_ENTITY_NAME "unknown_entity"
88 /*-----------------------------------------------------------------*/
90 /*-----------------------------------------------------------------*/
93 * Add an entity to it's already set owner type.
95 static inline void insert_entity_in_owner(ir_entity *ent) {
96 ir_type *owner = ent->owner;
97 switch (get_type_tpop_code(owner)) {
99 add_class_member(owner, ent);
102 add_struct_member(owner, ent);
105 add_union_member(owner, ent);
108 set_array_element_entity(owner, ent);
111 panic("Unsupported type kind");
113 } /* insert_entity_in_owner */
116 * Creates a new entity. This entity is NOT inserted in the owner type.
118 * @param db debug info for this entity
119 * @param owner the owner type of the new entity
120 * @param name the name of the new entity
121 * @param type the type of the new entity
123 * @return the new created entity
125 static inline ir_entity *
126 new_rd_entity(dbg_info *db, ir_type *owner, ident *name, ir_type *type)
131 assert(!id_contains_char(name, ' ') && "entity name should not contain spaces");
133 res = XMALLOCZ(ir_entity);
135 res->kind = k_entity;
141 res->allocation = allocation_automatic;
142 res->visibility = visibility_local;
143 res->volatility = volatility_non_volatile;
144 res->align = align_is_aligned;
145 res->stickyness = stickyness_unsticky;
146 res->peculiarity = peculiarity_existent;
147 res->usage = ir_usage_unknown;
149 res->compiler_gen = 0;
150 res->backend_marked = 0;
152 res->offset_bit_remainder = 0;
154 res->repr_class = NULL;
156 if (is_Method_type(type)) {
158 ir_mode *mode = is_Method_type(type) ? mode_P_code : mode_P_data;
160 rem = current_ir_graph;
161 current_ir_graph = get_const_code_irg();
162 res->value = new_SymConst(mode, sym, symconst_addr_ent);
163 current_ir_graph = rem;
164 res->allocation = allocation_static;
165 res->variability = variability_constant;
166 res->attr.mtd_attr.irg_add_properties = mtp_property_inherited;
167 res->attr.mtd_attr.vtable_number = VTABLE_NUM_NOT_SET;
168 res->attr.mtd_attr.param_access = NULL;
169 res->attr.mtd_attr.param_weight = NULL;
170 res->attr.mtd_attr.irg = NULL;
171 } else if (is_compound_type(type)) {
172 res->variability = variability_uninitialized;
174 res->attr.cmpd_attr.values = NULL;
175 res->attr.cmpd_attr.val_paths = NULL;
177 res->variability = variability_uninitialized;
181 if (is_Class_type(owner)) {
182 res->overwrites = NEW_ARR_F(ir_entity *, 0);
183 res->overwrittenby = NEW_ARR_F(ir_entity *, 0);
185 res->overwrites = NULL;
186 res->overwrittenby = NULL;
190 res->nr = get_irp_new_node_nr();
191 #endif /* DEBUG_libfirm */
194 set_entity_dbg_info(res, db);
197 } /* new_rd_entity */
200 new_d_entity(ir_type *owner, ident *name, ir_type *type, dbg_info *db) {
203 assert(is_compound_type(owner));
204 res = new_rd_entity(db, owner, name, type);
205 /* Remember entity in it's owner. */
206 insert_entity_in_owner(res);
208 hook_new_entity(res);
213 new_entity(ir_type *owner, ident *name, ir_type *type) {
214 return new_d_entity(owner, name, type, NULL);
218 * Free entity attributes.
220 * @param ent the entity
222 static void free_entity_attrs(ir_entity *ent) {
224 if (get_type_tpop(get_entity_owner(ent)) == type_class) {
225 DEL_ARR_F(ent->overwrites); ent->overwrites = NULL;
226 DEL_ARR_F(ent->overwrittenby); ent->overwrittenby = NULL;
228 assert(ent->overwrites == NULL);
229 assert(ent->overwrittenby == NULL);
231 if (is_compound_entity(ent)) {
232 if (ent->attr.cmpd_attr.val_paths) {
233 for (i = get_compound_ent_n_values(ent) - 1; i >= 0; --i)
234 if (ent->attr.cmpd_attr.val_paths[i]) {
235 /* free_compound_graph_path(ent->attr.cmpd_attr.val_paths[i]) ; * @@@ warum nich? */
236 /* Geht nich: wird mehrfach verwendet!!! ==> mehrfach frei gegeben. */
237 /* DEL_ARR_F(ent->attr.cmpd_attr.val_paths); */
239 ent->attr.cmpd_attr.val_paths = NULL;
241 /* if (ent->attr.cmpd_attr.values) DEL_ARR_F(ent->attr.cmpd_attr.values); *//* @@@ warum nich? */
242 ent->attr.cmpd_attr.values = NULL;
243 } else if (is_method_entity(ent)) {
244 if (ent->attr.mtd_attr.param_access) {
245 DEL_ARR_F(ent->attr.mtd_attr.param_access);
246 ent->attr.mtd_attr.param_access = NULL;
248 if (ent->attr.mtd_attr.param_weight) {
249 DEL_ARR_F(ent->attr.mtd_attr.param_weight);
250 ent->attr.mtd_attr.param_weight = NULL;
253 } /* free_entity_attrs */
256 * Copies the entity if the new_owner is different from the
257 * owner of the old entity, else returns the old entity.
260 copy_entity_own(ir_entity *old, ir_type *new_owner) {
262 assert(is_entity(old));
263 assert(is_compound_type(new_owner));
264 assert(get_type_state(new_owner) != layout_fixed);
266 if (old->owner == new_owner) return old;
267 newe = XMALLOC(ir_entity);
268 memcpy(newe, old, sizeof(*newe));
269 newe->owner = new_owner;
270 if (is_Class_type(new_owner)) {
271 newe->overwrites = NEW_ARR_F(ir_entity *, 0);
272 newe->overwrittenby = NEW_ARR_F(ir_entity *, 0);
275 newe->nr = get_irp_new_node_nr();
278 insert_entity_in_owner(newe);
281 } /* copy_entity_own */
284 copy_entity_name(ir_entity *old, ident *new_name) {
286 assert(old && old->kind == k_entity);
288 if (old->name == new_name) return old;
289 newe = XMALLOC(ir_entity);
290 memcpy(newe, old, sizeof(*newe));
291 newe->name = new_name;
292 newe->ld_name = NULL;
293 if (is_Class_type(newe->owner)) {
294 newe->overwrites = DUP_ARR_F(ir_entity *, old->overwrites);
295 newe->overwrittenby = DUP_ARR_F(ir_entity *, old->overwrittenby);
298 newe->nr = get_irp_new_node_nr();
301 insert_entity_in_owner(newe);
304 } /* copy_entity_name */
308 free_entity(ir_entity *ent) {
309 assert(ent && ent->kind == k_entity);
310 free_entity_attrs(ent);
315 /* Outputs a unique number for this node */
317 get_entity_nr(const ir_entity *ent) {
318 assert(ent && ent->kind == k_entity);
322 return (long)PTR_TO_INT(ent);
324 } /* get_entity_nr */
327 (get_entity_name)(const ir_entity *ent) {
328 return _get_entity_name(ent);
329 } /* get_entity_name */
332 (get_entity_ident)(const ir_entity *ent) {
333 return _get_entity_ident(ent);
334 } /* get_entity_ident */
337 (set_entity_ident)(ir_entity *ent, ident *id) {
338 _set_entity_ident(ent, id);
339 } /* set_entity_ident */
342 (get_entity_owner)(ir_entity *ent) {
343 return _get_entity_owner(ent);
344 } /* get_entity_owner */
347 set_entity_owner(ir_entity *ent, ir_type *owner) {
348 assert(is_entity(ent));
349 assert(is_compound_type(owner));
351 } /* set_entity_owner */
354 (get_entity_ld_ident)(ir_entity *ent) {
355 return _get_entity_ld_ident(ent);
356 } /* get_entity_ld_ident */
359 (set_entity_ld_ident)(ir_entity *ent, ident *ld_ident) {
360 _set_entity_ld_ident(ent, ld_ident);
361 } /* set_entity_ld_ident */
364 (get_entity_ld_name)(ir_entity *ent) {
365 return _get_entity_ld_name(ent);
366 } /* get_entity_ld_name */
369 (get_entity_type)(ir_entity *ent) {
370 return _get_entity_type(ent);
371 } /* get_entity_type */
374 (set_entity_type)(ir_entity *ent, ir_type *type) {
375 _set_entity_type(ent, type);
376 } /* set_entity_type */
379 (get_entity_allocation)(const ir_entity *ent) {
380 return _get_entity_allocation(ent);
381 } /* get_entity_allocation */
384 (set_entity_allocation)(ir_entity *ent, ir_allocation al) {
385 _set_entity_allocation(ent, al);
386 } /* set_entity_allocation */
388 /* return the name of the visibility */
389 const char *get_allocation_name(ir_allocation all)
391 #define X(a) case a: return #a
393 X(allocation_automatic);
394 X(allocation_parameter);
395 X(allocation_dynamic);
396 X(allocation_static);
397 default: return "BAD VALUE";
400 } /* get_allocation_name */
403 (get_entity_visibility)(const ir_entity *ent) {
404 return _get_entity_visibility(ent);
405 } /* get_entity_visibility */
408 set_entity_visibility(ir_entity *ent, ir_visibility vis) {
409 assert(ent && ent->kind == k_entity);
410 if (vis != visibility_local)
411 assert((ent->allocation == allocation_static) ||
412 (ent->allocation == allocation_automatic));
413 /* @@@ Test that the owner type is not local, but how??
414 && get_class_visibility(get_entity_owner(ent)) != local));*/
415 ent->visibility = vis;
416 } /* set_entity_visibility */
418 /* return the name of the visibility */
419 const char *get_visibility_name(ir_visibility vis)
421 #define X(a) case a: return #a
424 X(visibility_external_visible);
425 X(visibility_external_allocated);
426 default: return "BAD VALUE";
429 } /* get_visibility_name */
432 (get_entity_variability)(const ir_entity *ent) {
433 return _get_entity_variability(ent);
434 } /* get_entity_variability */
437 set_entity_variability(ir_entity *ent, ir_variability var)
439 assert(ent && ent->kind == k_entity);
440 if (var == variability_part_constant)
441 assert(is_Class_type(ent->type) || is_Struct_type(ent->type));
443 if ((is_compound_type(ent->type)) &&
444 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
445 /* Allocate data structures for constant values */
446 ent->attr.cmpd_attr.values = NEW_ARR_F(ir_node *, 0);
447 ent->attr.cmpd_attr.val_paths = NEW_ARR_F(compound_graph_path *, 0);
449 if ((is_atomic_type(ent->type)) &&
450 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
451 /* Set default constant value. */
452 ent->value = new_rd_Unknown(get_const_code_irg(), get_type_mode(ent->type));
455 if ((is_compound_type(ent->type)) &&
456 (var == variability_uninitialized) && (ent->variability != variability_uninitialized)) {
457 /* Free data structures for constant values */
458 DEL_ARR_F(ent->attr.cmpd_attr.values); ent->attr.cmpd_attr.values = NULL;
459 DEL_ARR_F(ent->attr.cmpd_attr.val_paths); ent->attr.cmpd_attr.val_paths = NULL;
461 ent->variability = var;
462 } /* set_entity_variability */
464 /* return the name of the variability */
465 const char *get_variability_name(ir_variability var)
467 #define X(a) case a: return #a
469 X(variability_uninitialized);
470 X(variability_initialized);
471 X(variability_part_constant);
472 X(variability_constant);
473 default: return "BAD VALUE";
476 } /* get_variability_name */
479 (get_entity_volatility)(const ir_entity *ent) {
480 return _get_entity_volatility(ent);
481 } /* get_entity_volatility */
484 (set_entity_volatility)(ir_entity *ent, ir_volatility vol) {
485 _set_entity_volatility(ent, vol);
486 } /* set_entity_volatility */
488 /* Return the name of the volatility. */
489 const char *get_volatility_name(ir_volatility var)
491 #define X(a) case a: return #a
493 X(volatility_non_volatile);
494 X(volatility_is_volatile);
495 default: return "BAD VALUE";
498 } /* get_volatility_name */
501 (get_entity_align)(const ir_entity *ent) {
502 return _get_entity_align(ent);
503 } /* get_entity_align */
506 (set_entity_align)(ir_entity *ent, ir_align a) {
507 _set_entity_align(ent, a);
508 } /* set_entity_align */
510 /* Return the name of the alignment. */
511 const char *get_align_name(ir_align a)
513 #define X(a) case a: return #a
515 X(align_non_aligned);
517 default: return "BAD VALUE";
520 } /* get_align_name */
523 (get_entity_peculiarity)(const ir_entity *ent) {
524 return _get_entity_peculiarity(ent);
525 } /* get_entity_peculiarity */
528 (set_entity_peculiarity)(ir_entity *ent, ir_peculiarity pec) {
529 _set_entity_peculiarity(ent, pec);
530 } /* set_entity_peculiarity */
532 /* Checks if an entity cannot be overridden anymore. */
533 int (is_entity_final)(const ir_entity *ent) {
534 return _is_entity_final(ent);
535 } /* is_entity_final */
537 /* Sets/resets the final flag of an entity. */
538 void (set_entity_final)(ir_entity *ent, int final) {
539 _set_entity_final(ent, final);
540 } /* set_entity_final */
542 /* Checks if an entity is compiler generated */
543 int (is_entity_compiler_generated)(const ir_entity *ent) {
544 return _is_entity_compiler_generated(ent);
545 } /* is_entity_compiler_generated */
547 /* Sets/resets the compiler generated flag */
548 void (set_entity_compiler_generated)(ir_entity *ent, int flag) {
549 _set_entity_compiler_generated(ent, flag);
550 } /* set_entity_compiler_generated */
552 /* Checks if an entity is marked by the backend */
553 int (is_entity_backend_marked)(const ir_entity *ent) {
554 return _is_entity_backend_marked(ent);
555 } /* is_entity_backend_marked */
557 /* Sets/resets the compiler generated flag */
558 void (set_entity_backend_marked)(ir_entity *ent, int flag) {
559 _set_entity_backend_marked(ent, flag);
560 } /* set_entity_backend_marked */
562 ir_entity_usage (get_entity_usage)(const ir_entity *ent) {
563 return _get_entity_usage(ent);
566 void (set_entity_usage)(ir_entity *ent, ir_entity_usage flags) {
567 _set_entity_usage(ent, flags);
570 /* Get the entity's stickyness */
572 (get_entity_stickyness)(const ir_entity *ent) {
573 return _get_entity_stickyness(ent);
574 } /* get_entity_stickyness */
576 /* Set the entity's stickyness */
578 (set_entity_stickyness)(ir_entity *ent, ir_stickyness stickyness) {
579 _set_entity_stickyness(ent, stickyness);
580 } /* set_entity_stickyness */
582 /* Set has no effect for existent entities of type method. */
584 get_atomic_ent_value(ir_entity *ent)
586 assert(ent && is_atomic_entity(ent));
587 assert(ent->variability != variability_uninitialized);
588 return skip_Id(ent->value);
589 } /* get_atomic_ent_value */
592 set_atomic_ent_value(ir_entity *ent, ir_node *val) {
593 assert(is_atomic_entity(ent) && (ent->variability != variability_uninitialized));
594 if (is_Method_type(ent->type) && (ent->peculiarity == peculiarity_existent))
596 assert(get_irn_mode(val) == get_type_mode(ent->type));
598 } /* set_atomic_ent_value */
600 /* Returns true if the the node is representable as code on
602 int is_irn_const_expression(ir_node *n) {
605 /* we are in danger iff an exception will arise. TODO: be more precisely,
606 * for instance Div. will NOT rise if divisor != 0
608 if (is_binop(n) && !is_fragile_op(n))
609 return is_irn_const_expression(get_binop_left(n)) && is_irn_const_expression(get_binop_right(n));
612 switch (get_irn_opcode(n)) {
619 return is_irn_const_expression(get_irn_n(n, 0));
624 } /* is_irn_const_expression */
627 * Copies a firm subgraph that complies to the restrictions for
628 * constant expressions to current_block in current_ir_graph.
630 ir_node *copy_const_value(dbg_info *dbg, ir_node *n) {
634 /* @@@ GL I think we should implement this using the routines from irgopt for
635 dead node elimination/inlineing. */
638 switch (get_irn_opcode(n)) {
640 nn = new_d_Const_type(dbg, m, get_Const_tarval(n), get_Const_type(n));
643 nn = new_d_SymConst_type(dbg, get_irn_mode(n), get_SymConst_symbol(n), get_SymConst_kind(n),
644 get_SymConst_value_type(n));
647 nn = new_d_Add(dbg, copy_const_value(dbg, get_Add_left(n)),
648 copy_const_value(dbg, get_Add_right(n)), m); break;
650 nn = new_d_Sub(dbg, copy_const_value(dbg, get_Sub_left(n)),
651 copy_const_value(dbg, get_Sub_right(n)), m); break;
653 nn = new_d_Mul(dbg, copy_const_value(dbg, get_Mul_left(n)),
654 copy_const_value(dbg, get_Mul_right(n)), m); break;
656 nn = new_d_And(dbg, copy_const_value(dbg, get_And_left(n)),
657 copy_const_value(dbg, get_And_right(n)), m); break;
659 nn = new_d_Or(dbg, copy_const_value(dbg, get_Or_left(n)),
660 copy_const_value(dbg, get_Or_right(n)), m); break;
662 nn = new_d_Eor(dbg, copy_const_value(dbg, get_Eor_left(n)),
663 copy_const_value(dbg, get_Eor_right(n)), m); break;
665 nn = new_d_Cast(dbg, copy_const_value(dbg, get_Cast_op(n)), get_Cast_type(n)); break;
667 nn = new_d_Conv(dbg, copy_const_value(dbg, get_Conv_op(n)), m); break;
669 nn = new_d_Unknown(m); break;
671 assert(0 && "opcode invalid or not implemented");
676 } /* copy_const_value */
678 static ir_initializer_t null_initializer = { IR_INITIALIZER_NULL };
680 ir_initializer_t *get_initializer_null(void)
682 return &null_initializer;
685 ir_initializer_t *create_initializer_const(ir_node *value)
687 struct obstack *obst = get_irg_obstack(get_const_code_irg());
689 ir_initializer_t *initializer
690 = obstack_alloc(obst, sizeof(ir_initializer_const_t));
691 initializer->kind = IR_INITIALIZER_CONST;
692 initializer->consti.value = value;
697 ir_initializer_t *create_initializer_tarval(tarval *tv)
699 struct obstack *obst = get_irg_obstack(get_const_code_irg());
701 ir_initializer_t *initializer
702 = obstack_alloc(obst, sizeof(ir_initializer_tarval_t));
703 initializer->kind = IR_INITIALIZER_TARVAL;
704 initializer->tarval.value = tv;
709 ir_initializer_t *create_initializer_compound(unsigned n_entries)
711 struct obstack *obst = get_irg_obstack(get_const_code_irg());
714 size_t size = sizeof(ir_initializer_compound_t)
715 + (n_entries-1) * sizeof(ir_initializer_t*);
717 ir_initializer_t *initializer = obstack_alloc(obst, size);
718 initializer->kind = IR_INITIALIZER_COMPOUND;
719 initializer->compound.n_initializers = n_entries;
721 for(i = 0; i < n_entries; ++i) {
722 initializer->compound.initializers[i] = get_initializer_null();
728 ir_node *get_initializer_const_value(const ir_initializer_t *initializer)
730 assert(initializer->kind == IR_INITIALIZER_CONST);
731 return initializer->consti.value;
734 tarval *get_initializer_tarval_value(const ir_initializer_t *initializer)
736 assert(initializer->kind == IR_INITIALIZER_TARVAL);
737 return initializer->tarval.value;
740 unsigned get_initializer_compound_n_entries(const ir_initializer_t *initializer)
742 assert(initializer->kind == IR_INITIALIZER_COMPOUND);
743 return initializer->compound.n_initializers;
746 void set_initializer_compound_value(ir_initializer_t *initializer,
747 unsigned index, ir_initializer_t *value)
749 assert(initializer->kind == IR_INITIALIZER_COMPOUND);
750 assert(index < initializer->compound.n_initializers);
752 initializer->compound.initializers[index] = value;
755 ir_initializer_t *get_initializer_compound_value(
756 const ir_initializer_t *initializer, unsigned index)
758 assert(initializer->kind == IR_INITIALIZER_COMPOUND);
759 assert(index < initializer->compound.n_initializers);
761 return initializer->compound.initializers[index];
764 ir_initializer_kind_t get_initializer_kind(const ir_initializer_t *initializer)
766 return initializer->kind;
769 static void check_entity_initializer(ir_entity *entity)
775 void set_entity_initializer(ir_entity *entity, ir_initializer_t *initializer)
777 entity->attr.initializer = initializer;
778 entity->has_initializer = 1;
779 check_entity_initializer(entity);
782 ir_initializer_t *get_entity_initializer(const ir_entity *entity)
784 assert(entity->has_initializer);
785 return entity->attr.initializer;
788 /* Creates a new compound graph path. */
789 compound_graph_path *
790 new_compound_graph_path(ir_type *tp, int length) {
791 compound_graph_path *res;
793 assert(is_compound_type(tp));
796 res = xmalloc(sizeof(*res) + (length-1) * sizeof(res->list[0]));
797 memset(res, 0, sizeof(*res) + (length-1) * sizeof(res->list[0]));
798 res->kind = k_ir_compound_graph_path;
803 } /* new_compound_graph_path */
805 /* Frees an graph path object */
806 void free_compound_graph_path (compound_graph_path *gr) {
807 assert(gr && is_compound_graph_path(gr));
810 } /* free_compound_graph_path */
812 /* Returns non-zero if an object is a compound graph path */
813 int is_compound_graph_path(const void *thing) {
814 return (get_kind(thing) == k_ir_compound_graph_path);
815 } /* is_compound_graph_path */
817 /* Checks whether the path up to pos is correct. If the path contains a NULL,
818 * assumes the path is not complete and returns 'true'. */
819 int is_proper_compound_graph_path(compound_graph_path *gr, int pos) {
822 ir_type *owner = gr->tp;
824 for (i = 0; i <= pos; i++) {
825 node = get_compound_graph_path_node(gr, i);
827 /* Path not yet complete. */
829 if (get_entity_owner(node) != owner)
831 owner = get_entity_type(node);
833 if (pos == get_compound_graph_path_length(gr))
834 if (!is_atomic_type(owner))
837 } /* is_proper_compound_graph_path */
839 /* Returns the length of a graph path */
840 int get_compound_graph_path_length(const compound_graph_path *gr) {
841 assert(gr && is_compound_graph_path(gr));
843 } /* get_compound_graph_path_length */
846 get_compound_graph_path_node(const compound_graph_path *gr, int pos) {
847 assert(gr && is_compound_graph_path(gr));
848 assert(pos >= 0 && pos < gr->len);
849 return gr->list[pos].node;
850 } /* get_compound_graph_path_node */
853 set_compound_graph_path_node(compound_graph_path *gr, int pos, ir_entity *node) {
854 assert(gr && is_compound_graph_path(gr));
855 assert(pos >= 0 && pos < gr->len);
856 assert(is_entity(node));
857 gr->list[pos].node = node;
858 assert(is_proper_compound_graph_path(gr, pos));
859 } /* set_compound_graph_path_node */
862 get_compound_graph_path_array_index(const compound_graph_path *gr, int pos) {
863 assert(gr && is_compound_graph_path(gr));
864 assert(pos >= 0 && pos < gr->len);
865 return gr->list[pos].index;
866 } /* get_compound_graph_path_array_index */
869 set_compound_graph_path_array_index(compound_graph_path *gr, int pos, int index) {
870 assert(gr && is_compound_graph_path(gr));
871 assert(pos >= 0 && pos < gr->len);
872 gr->list[pos].index = index;
873 } /* set_compound_graph_path_array_index */
876 get_compound_graph_path_type(const compound_graph_path *gr) {
877 assert(gr && is_compound_graph_path(gr));
881 /* A value of a compound entity is a pair of value and the corresponding path to a member of
884 add_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path) {
885 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
886 assert(is_compound_graph_path(path));
887 ARR_APP1(ir_node *, ent->attr.cmpd_attr.values, val);
888 ARR_APP1(compound_graph_path *, ent->attr.cmpd_attr.val_paths, path);
889 } /* add_compound_ent_value_w_path */
892 set_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path, int pos) {
893 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
894 assert(is_compound_graph_path(path));
895 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
896 ent->attr.cmpd_attr.values[pos] = val;
897 ent->attr.cmpd_attr.val_paths[pos] = path;
898 } /* set_compound_ent_value_w_path */
901 get_compound_ent_n_values(ir_entity *ent) {
902 assert(!ent->has_initializer);
903 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
904 return ARR_LEN(ent->attr.cmpd_attr.values);
905 } /* get_compound_ent_n_values */
908 get_compound_ent_value(ir_entity *ent, int pos) {
909 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
910 assert(!ent->has_initializer);
911 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
912 return ent->attr.cmpd_attr.values[pos];
913 } /* get_compound_ent_value */
915 compound_graph_path *
916 get_compound_ent_value_path(ir_entity *ent, int pos) {
917 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
918 assert(!ent->has_initializer);
919 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.val_paths));
920 return ent->attr.cmpd_attr.val_paths[pos];
921 } /* get_compound_ent_value_path */
924 * Returns non-zero, if two compound_graph_pathes are equal
926 * @param path1 the first path
927 * @param path2 the second path
929 static int equal_paths(compound_graph_path *path1, compound_graph_path *path2) {
931 int len1 = get_compound_graph_path_length(path1);
932 int len2 = get_compound_graph_path_length(path2);
934 if (len2 != len1) return 0;
936 for (i = 0; i < len1; i++) {
938 ir_entity *node1 = get_compound_graph_path_node(path1, i);
939 ir_entity *node2 = get_compound_graph_path_node(path2, i);
941 if (node1 != node2) return 0;
943 tp = get_entity_owner(node1);
944 if (is_Array_type(tp)) {
945 int index1 = get_compound_graph_path_array_index(path1, i);
946 int index2 = get_compound_graph_path_array_index(path2, i);
947 if (index1 != index2)
955 * Returns the position of a value with the given path.
956 * The path must contain array indices for all array element entities.
958 * @todo This implementation is very slow (O(number of initializers * |path|)
959 * and should be replaced when the new tree oriented
960 * value representation is finally implemented.
962 static int get_compound_ent_pos_by_path(ir_entity *ent, compound_graph_path *path) {
963 int i, n_paths = get_compound_ent_n_values(ent);
965 for (i = 0; i < n_paths; i ++) {
966 compound_graph_path *gr = get_compound_ent_value_path(ent, i);
967 if (equal_paths(gr, path))
971 } /* get_compound_ent_pos_by_path */
973 /* Returns a constant value given the access path.
974 * The path must contain array indices for all array element entities. */
975 ir_node *get_compound_ent_value_by_path(ir_entity *ent, compound_graph_path *path) {
976 int pos = get_compound_ent_pos_by_path(ent, path);
978 return get_compound_ent_value(ent, pos);
980 } /* get_compound_ent_value_by_path */
984 remove_compound_ent_value(ir_entity *ent, ir_entity *value_ent) {
986 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
988 n = ARR_LEN(ent->attr.cmpd_attr.val_paths);
989 for (i = 0; i < n; ++i) {
990 compound_graph_path *path = ent->attr.cmpd_attr.val_paths[i];
991 if (path->list[path->len-1].node == value_ent) {
992 for (; i < n - 1; ++i) {
993 ent->attr.cmpd_attr.val_paths[i] = ent->attr.cmpd_attr.val_paths[i+1];
994 ent->attr.cmpd_attr.values[i] = ent->attr.cmpd_attr.values[i+1];
996 ARR_SETLEN(ir_entity*, ent->attr.cmpd_attr.val_paths, n - 1);
997 ARR_SETLEN(ir_node*, ent->attr.cmpd_attr.values, n - 1);
1001 } /* remove_compound_ent_value */
1004 add_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member) {
1005 compound_graph_path *path;
1006 ir_type *owner_tp = get_entity_owner(member);
1007 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
1008 path = new_compound_graph_path(get_entity_type(ent), 1);
1009 path->list[0].node = member;
1010 if (is_Array_type(owner_tp)) {
1014 assert(get_array_n_dimensions(owner_tp) == 1 && has_array_lower_bound(owner_tp, 0));
1015 max = get_array_lower_bound_int(owner_tp, 0) -1;
1016 for (i = 0, n = get_compound_ent_n_values(ent); i < n; ++i) {
1017 int index = get_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0);
1022 path->list[0].index = max + 1;
1024 add_compound_ent_value_w_path(ent, val, path);
1025 } /* add_compound_ent_value */
1029 get_compound_ent_value_member(ir_entity *ent, int pos) {
1030 compound_graph_path *path;
1031 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
1032 path = get_compound_ent_value_path(ent, pos);
1034 return get_compound_graph_path_node(path, get_compound_graph_path_length(path)-1);
1035 } /* get_compound_ent_value_member */
1038 set_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member, int pos) {
1039 compound_graph_path *path;
1040 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
1041 path = get_compound_ent_value_path(ent, pos);
1042 set_compound_graph_path_node(path, 0, member);
1043 set_compound_ent_value_w_path(ent, val, path, pos);
1044 } /* set_compound_ent_value */
1047 set_array_entity_values(ir_entity *ent, tarval **values, int num_vals) {
1049 ir_graph *rem = current_ir_graph;
1050 ir_type *arrtp = get_entity_type(ent);
1052 ir_type *elttp = get_array_element_type(arrtp);
1054 assert(is_Array_type(arrtp));
1055 assert(get_array_n_dimensions(arrtp) == 1);
1056 /* One bound is sufficient, the number of constant fields makes the
1058 assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
1059 assert(get_entity_variability(ent) != variability_uninitialized);
1060 current_ir_graph = get_const_code_irg();
1062 for (i = 0; i < num_vals; i++) {
1063 val = new_Const_type(values[i], elttp);
1064 add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
1065 set_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0, i);
1067 current_ir_graph = rem;
1068 } /* set_array_entity_values */
1070 /* Return the overall offset of value at position pos in bytes. */
1071 unsigned get_compound_ent_value_offset_bytes(ir_entity *ent, int pos) {
1072 compound_graph_path *path;
1074 unsigned offset = 0;
1077 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
1079 path = get_compound_ent_value_path(ent, pos);
1080 path_len = get_compound_graph_path_length(path);
1083 for (i = 0; i < path_len; ++i) {
1084 if (is_Array_type(curr_tp)) {
1085 ir_type *elem_type = get_array_element_type(curr_tp);
1086 unsigned size = get_type_size_bytes(elem_type);
1087 unsigned align = get_type_alignment_bytes(elem_type);
1091 if(size % align > 0) {
1092 size += align - (size % align);
1094 idx = get_compound_graph_path_array_index(path, i);
1096 offset += size * idx;
1097 curr_tp = elem_type;
1099 ir_entity *node = get_compound_graph_path_node(path, i);
1100 offset += get_entity_offset(node);
1101 curr_tp = get_entity_type(node);
1106 } /* get_compound_ent_value_offset_bytes */
1108 /* Return the offset in bits from the last byte address. */
1109 unsigned get_compound_ent_value_offset_bit_remainder(ir_entity *ent, int pos) {
1110 compound_graph_path *path;
1112 ir_entity *last_node;
1114 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
1116 path = get_compound_ent_value_path(ent, pos);
1117 path_len = get_compound_graph_path_length(path);
1118 last_node = get_compound_graph_path_node(path, path_len - 1);
1120 if(last_node == NULL)
1123 return get_entity_offset_bits_remainder(last_node);
1124 } /* get_compound_ent_value_offset_bit_remainder */
1127 (get_entity_offset)(const ir_entity *ent) {
1128 return _get_entity_offset(ent);
1129 } /* get_entity_offset */
1132 (set_entity_offset)(ir_entity *ent, int offset) {
1133 _set_entity_offset(ent, offset);
1134 } /* set_entity_offset */
1137 (get_entity_offset_bits_remainder)(const ir_entity *ent) {
1138 return _get_entity_offset_bits_remainder(ent);
1139 } /* get_entity_offset_bits_remainder */
1142 (set_entity_offset_bits_remainder)(ir_entity *ent, unsigned char offset) {
1143 _set_entity_offset_bits_remainder(ent, offset);
1144 } /* set_entity_offset_bits_remainder */
1147 add_entity_overwrites(ir_entity *ent, ir_entity *overwritten) {
1149 ir_type *owner = get_entity_owner(ent);
1150 ir_type *ovw_ovner = get_entity_owner(overwritten);
1151 assert(is_Class_type(owner));
1152 assert(is_Class_type(ovw_ovner));
1153 assert(! is_class_final(ovw_ovner));
1155 ARR_APP1(ir_entity *, ent->overwrites, overwritten);
1156 ARR_APP1(ir_entity *, overwritten->overwrittenby, ent);
1157 } /* add_entity_overwrites */
1160 get_entity_n_overwrites(ir_entity *ent) {
1161 assert(is_Class_type(get_entity_owner(ent)));
1162 return (ARR_LEN(ent->overwrites));
1163 } /* get_entity_n_overwrites */
1166 get_entity_overwrites_index(ir_entity *ent, ir_entity *overwritten) {
1168 assert(is_Class_type(get_entity_owner(ent)));
1169 n = get_entity_n_overwrites(ent);
1170 for (i = 0; i < n; ++i) {
1171 if (get_entity_overwrites(ent, i) == overwritten)
1175 } /* get_entity_overwrites_index */
1178 get_entity_overwrites(ir_entity *ent, int pos) {
1179 assert(is_Class_type(get_entity_owner(ent)));
1180 assert(pos < get_entity_n_overwrites(ent));
1181 return ent->overwrites[pos];
1182 } /* get_entity_overwrites */
1185 set_entity_overwrites(ir_entity *ent, int pos, ir_entity *overwritten) {
1186 assert(is_Class_type(get_entity_owner(ent)));
1187 assert(pos < get_entity_n_overwrites(ent));
1188 ent->overwrites[pos] = overwritten;
1189 } /* set_entity_overwrites */
1192 remove_entity_overwrites(ir_entity *ent, ir_entity *overwritten) {
1194 assert(is_Class_type(get_entity_owner(ent)));
1195 n = ARR_LEN(ent->overwrites);
1196 for (i = 0; i < n; ++i) {
1197 if (ent->overwrites[i] == overwritten) {
1198 for (; i < n - 1; i++)
1199 ent->overwrites[i] = ent->overwrites[i+1];
1200 ARR_SETLEN(ir_entity*, ent->overwrites, n - 1);
1204 } /* remove_entity_overwrites */
1207 add_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites) {
1208 add_entity_overwrites(overwrites, ent);
1209 } /* add_entity_overwrittenby */
1212 get_entity_n_overwrittenby(ir_entity *ent) {
1213 assert(is_Class_type(get_entity_owner(ent)));
1214 return ARR_LEN(ent->overwrittenby);
1215 } /* get_entity_n_overwrittenby */
1218 get_entity_overwrittenby_index(ir_entity *ent, ir_entity *overwrites) {
1220 assert(is_Class_type(get_entity_owner(ent)));
1221 n = get_entity_n_overwrittenby(ent);
1222 for (i = 0; i < n; ++i) {
1223 if (get_entity_overwrittenby(ent, i) == overwrites)
1227 } /* get_entity_overwrittenby_index */
1230 get_entity_overwrittenby(ir_entity *ent, int pos) {
1231 assert(is_Class_type(get_entity_owner(ent)));
1232 assert(pos < get_entity_n_overwrittenby(ent));
1233 return ent->overwrittenby[pos];
1234 } /* get_entity_overwrittenby */
1237 set_entity_overwrittenby(ir_entity *ent, int pos, ir_entity *overwrites) {
1238 assert(is_Class_type(get_entity_owner(ent)));
1239 assert(pos < get_entity_n_overwrittenby(ent));
1240 ent->overwrittenby[pos] = overwrites;
1241 } /* set_entity_overwrittenby */
1243 void remove_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites) {
1245 assert(is_Class_type(get_entity_owner(ent)));
1247 n = ARR_LEN(ent->overwrittenby);
1248 for (i = 0; i < n; ++i) {
1249 if (ent->overwrittenby[i] == overwrites) {
1250 for(; i < n - 1; ++i)
1251 ent->overwrittenby[i] = ent->overwrittenby[i+1];
1252 ARR_SETLEN(ir_entity*, ent->overwrittenby, n - 1);
1256 } /* remove_entity_overwrittenby */
1258 /* A link to store intermediate information */
1260 (get_entity_link)(const ir_entity *ent) {
1261 return _get_entity_link(ent);
1262 } /* get_entity_link */
1265 (set_entity_link)(ir_entity *ent, void *l) {
1266 _set_entity_link(ent, l);
1267 } /* set_entity_link */
1270 (get_entity_irg)(const ir_entity *ent) {
1271 return _get_entity_irg(ent);
1272 } /* get_entity_irg */
1275 set_entity_irg(ir_entity *ent, ir_graph *irg) {
1276 assert(is_method_entity(ent));
1277 /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
1278 * Methode selbst nicht mehr aufgerufen werden kann, die Entität
1279 * aber erhalten bleiben soll? Wandle die Entitaet in description oder
1282 assert((irg && ent->peculiarity == peculiarity_existent) ||
1283 (!irg && (ent->peculiarity == peculiarity_existent)
1284 && (ent -> visibility == visibility_external_allocated)) ||
1285 (!irg && ent->peculiarity == peculiarity_description) ||
1286 (!irg && ent->peculiarity == peculiarity_inherited));
1287 ent->attr.mtd_attr.irg = irg;
1288 } /* set_entity_irg */
1290 unsigned get_entity_vtable_number(const ir_entity *ent) {
1291 assert(is_method_entity((ir_entity *)ent));
1292 return ent->attr.mtd_attr.vtable_number;
1293 } /* get_entity_vtable_number */
1295 void set_entity_vtable_number(ir_entity *ent, unsigned vtable_number) {
1296 assert(is_method_entity(ent));
1297 ent->attr.mtd_attr.vtable_number = vtable_number;
1298 } /* set_entity_vtable_number */
1301 (is_entity)(const void *thing) {
1302 return _is_entity(thing);
1305 int is_atomic_entity(ir_entity *ent) {
1306 ir_type *t = get_entity_type(ent);
1307 const tp_op *op = get_type_tpop(t);
1308 return (op == type_primitive || op == type_pointer ||
1309 op == type_enumeration || op == type_method);
1310 } /* is_atomic_entity */
1312 int is_compound_entity(ir_entity *ent) {
1313 ir_type *t = get_entity_type(ent);
1314 const tp_op *op = get_type_tpop(t);
1315 return (op == type_class || op == type_struct ||
1316 op == type_array || op == type_union);
1317 } /* is_compound_entity */
1319 int is_method_entity(ir_entity *ent) {
1320 ir_type *t = get_entity_type(ent);
1321 return is_Method_type(t);
1322 } /* is_method_entity */
1325 * @todo not implemented!!! */
1326 int equal_entity(ir_entity *ent1, ir_entity *ent2) {
1329 fprintf(stderr, " calling unimplemented equal entity!!! \n");
1331 } /* equal_entity */
1334 ir_visited_t (get_entity_visited)(ir_entity *ent) {
1335 return _get_entity_visited(ent);
1336 } /* get_entity_visited */
1338 void (set_entity_visited)(ir_entity *ent, ir_visited_t num) {
1339 _set_entity_visited(ent, num);
1340 } /* set_entity_visited */
1342 /* Sets visited field in ir_entity to entity_visited. */
1343 void (mark_entity_visited)(ir_entity *ent) {
1344 _mark_entity_visited(ent);
1345 } /* mark_entity_visited */
1347 int (entity_visited)(ir_entity *ent) {
1348 return _entity_visited(ent);
1349 } /* entity_visited */
1351 int (entity_not_visited)(ir_entity *ent) {
1352 return _entity_not_visited(ent);
1353 } /* entity_not_visited */
1355 /* Returns the mask of the additional entity properties. */
1356 unsigned get_entity_additional_properties(ir_entity *ent) {
1359 assert(is_method_entity(ent));
1361 /* first check, if the graph has additional properties */
1362 irg = get_entity_irg(ent);
1365 return get_irg_additional_properties(irg);
1367 if (ent->attr.mtd_attr.irg_add_properties & mtp_property_inherited)
1368 return get_method_additional_properties(get_entity_type(ent));
1370 return ent->attr.mtd_attr.irg_add_properties;
1371 } /* get_entity_additional_properties */
1373 /* Sets the mask of the additional graph properties. */
1374 void set_entity_additional_properties(ir_entity *ent, unsigned property_mask)
1378 assert(is_method_entity(ent));
1380 /* first check, if the graph exists */
1381 irg = get_entity_irg(ent);
1383 set_irg_additional_properties(irg, property_mask);
1385 /* do not allow to set the mtp_property_inherited flag or
1386 * the automatic inheritance of flags will not work */
1387 ent->attr.mtd_attr.irg_add_properties = property_mask & ~mtp_property_inherited;
1389 } /* set_entity_additional_properties */
1391 /* Sets one additional graph property. */
1392 void set_entity_additional_property(ir_entity *ent, mtp_additional_property flag)
1396 assert(is_method_entity(ent));
1398 /* first check, if the graph exists */
1399 irg = get_entity_irg(ent);
1401 set_irg_additional_property(irg, flag);
1403 unsigned mask = ent->attr.mtd_attr.irg_add_properties;
1405 if (mask & mtp_property_inherited)
1406 mask = get_method_additional_properties(get_entity_type(ent));
1408 /* do not allow to set the mtp_property_inherited flag or
1409 * the automatic inheritance of flags will not work */
1410 ent->attr.mtd_attr.irg_add_properties = mask | (flag & ~mtp_property_inherited);
1412 } /* set_entity_additional_property */
1414 /* Returns the class type that this type info entity represents or NULL
1415 if ent is no type info entity. */
1416 ir_type *(get_entity_repr_class)(const ir_entity *ent) {
1417 return _get_entity_repr_class(ent);
1418 } /* get_entity_repr_class */
1420 dbg_info *(get_entity_dbg_info)(const ir_entity *ent) {
1421 return _get_entity_dbg_info(ent);
1422 } /* get_entity_dbg_info */
1424 void (set_entity_dbg_info)(ir_entity *ent, dbg_info *db) {
1425 _set_entity_dbg_info(ent, db);
1426 } /* set_entity_dbg_info */
1428 /* Initialize entity module. */
1429 void firm_init_entity(void)
1431 symconst_symbol sym;
1433 assert(firm_unknown_type && "Call init_type() before firm_init_entity()!");
1434 assert(!unknown_entity && "Call firm_init_entity() only once!");
1436 unknown_entity = new_rd_entity(NULL, firm_unknown_type, new_id_from_str(UNKNOWN_ENTITY_NAME), firm_unknown_type);
1437 set_entity_visibility(unknown_entity, visibility_external_allocated);
1438 set_entity_ld_ident(unknown_entity, get_entity_ident(unknown_entity));
1440 current_ir_graph = get_const_code_irg();
1441 sym.entity_p = unknown_entity;
1442 /* TODO: we need two unknown_entities here, one for code and one for data */
1443 unknown_entity->value = new_SymConst(mode_P_data, sym, symconst_addr_ent);
1444 } /* firm_init_entity */