2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Representation of all program known entities.
23 * @author Martin Trapp, Christian Schaefer, Goetz Lindenmaier, Michael Beck
34 #include "firm_common_t.h"
47 #include "irgraph_t.h"
49 #include "callgraph.h"
52 * An interval initializer.
54 typedef struct interval_initializer interval_initializer;
57 * A value initializer.
59 typedef struct value_initializer value_initializer;
61 struct interval_initializer {
62 int first_index; /**< The first index of the initialized interval. */
63 int last_index; /**< The last index of the initialized interval. */
64 interval_initializer *next; /**< Points to the next interval initializer. */
67 struct value_initializer {
68 ir_entity *ent; /**< The initialized entity. */
69 value_initializer *next; /**< Points to the next value initializer. */
72 typedef union initializer {
73 ir_node *value; /**< The value of the initializer. */
74 ir_node **values; /**< The values of an interval. */
75 value_initializer *val_init; /**< Points the the head of the next value initializers. */
76 interval_initializer *int_init; /**< Points to the head of the next value initializers. */
79 /*-----------------------------------------------------------------*/
81 /*-----------------------------------------------------------------*/
83 ir_entity *unknown_entity = NULL;
85 ir_entity *get_unknown_entity(void) { return unknown_entity; }
87 /** The name of the unknown entity. */
88 #define UNKNOWN_ENTITY_NAME "unknown_entity"
90 /*-----------------------------------------------------------------*/
92 /*-----------------------------------------------------------------*/
95 * Add an entity to it's already set owner type.
97 static INLINE void insert_entity_in_owner(ir_entity *ent) {
98 ir_type *owner = ent->owner;
99 switch (get_type_tpop_code(owner)) {
101 add_class_member(owner, ent);
104 add_struct_member(owner, ent);
107 add_union_member(owner, ent);
110 set_array_element_entity(owner, ent);
114 } /* insert_entity_in_owner */
117 * Creates a new entity. This entity is NOT inserted in the owner type.
119 * @param db debug info for this entity
120 * @param owner the owner type of the new entity
121 * @param name the name of the new entity
122 * @param type the type of the new entity
124 * @return the new created entity
126 static INLINE ir_entity *
127 new_rd_entity(dbg_info *db, ir_type *owner, ident *name, ir_type *type)
132 assert(!id_contains_char(name, ' ') && "entity name should not contain spaces");
134 res = xmalloc(sizeof(*res));
135 memset(res, 0, sizeof(*res));
137 res->kind = k_entity;
143 res->allocation = allocation_automatic;
144 res->visibility = visibility_local;
145 res->volatility = volatility_non_volatile;
146 res->align = align_is_aligned;
147 res->stickyness = stickyness_unsticky;
148 res->peculiarity = peculiarity_existent;
149 res->usage = ir_usage_unknown;
151 res->compiler_gen = 0;
152 res->backend_marked = 0;
154 res->offset_bit_remainder = 0;
156 res->repr_class = NULL;
158 if (is_Method_type(type)) {
160 ir_mode *mode = is_Method_type(type) ? mode_P_code : mode_P_data;
162 rem = current_ir_graph;
163 current_ir_graph = get_const_code_irg();
164 res->value = new_SymConst(mode, sym, symconst_addr_ent);
165 current_ir_graph = rem;
166 res->allocation = allocation_static;
167 res->variability = variability_constant;
168 res->attr.mtd_attr.irg_add_properties = mtp_property_inherited;
169 res->attr.mtd_attr.vtable_number = VTABLE_NUM_NOT_SET;
170 res->attr.mtd_attr.param_access = NULL;
171 res->attr.mtd_attr.param_weight = NULL;
172 res->attr.mtd_attr.irg = NULL;
173 } else if (is_compound_type(type)) {
174 res->variability = variability_uninitialized;
176 res->attr.cmpd_attr.values = NULL;
177 res->attr.cmpd_attr.val_paths = NULL;
179 res->variability = variability_uninitialized;
183 if (is_Class_type(owner)) {
184 res->overwrites = NEW_ARR_F(ir_entity *, 0);
185 res->overwrittenby = NEW_ARR_F(ir_entity *, 0);
187 res->overwrites = NULL;
188 res->overwrittenby = NULL;
192 res->nr = get_irp_new_node_nr();
193 #endif /* DEBUG_libfirm */
196 set_entity_dbg_info(res, db);
199 } /* new_rd_entity */
202 new_d_entity(ir_type *owner, ident *name, ir_type *type, dbg_info *db) {
205 assert(is_compound_type(owner));
206 res = new_rd_entity(db, owner, name, type);
207 /* Remember entity in it's owner. */
208 insert_entity_in_owner(res);
210 hook_new_entity(res);
215 new_entity(ir_type *owner, ident *name, ir_type *type) {
216 return new_d_entity(owner, name, type, NULL);
220 * Free entity attributes.
222 * @param ent the entity
224 static void free_entity_attrs(ir_entity *ent) {
226 if (get_type_tpop(get_entity_owner(ent)) == type_class) {
227 DEL_ARR_F(ent->overwrites); ent->overwrites = NULL;
228 DEL_ARR_F(ent->overwrittenby); ent->overwrittenby = NULL;
230 assert(ent->overwrites == NULL);
231 assert(ent->overwrittenby == NULL);
233 if (is_compound_entity(ent)) {
234 if (ent->attr.cmpd_attr.val_paths) {
235 for (i = get_compound_ent_n_values(ent) - 1; i >= 0; --i)
236 if (ent->attr.cmpd_attr.val_paths[i]) {
237 /* free_compound_graph_path(ent->attr.cmpd_attr.val_paths[i]) ; * @@@ warum nich? */
238 /* Geht nich: wird mehrfach verwendet!!! ==> mehrfach frei gegeben. */
239 /* DEL_ARR_F(ent->attr.cmpd_attr.val_paths); */
241 ent->attr.cmpd_attr.val_paths = NULL;
243 /* if (ent->attr.cmpd_attr.values) DEL_ARR_F(ent->attr.cmpd_attr.values); *//* @@@ warum nich? */
244 ent->attr.cmpd_attr.values = NULL;
245 } else if (is_method_entity(ent)) {
246 if (ent->attr.mtd_attr.param_access) {
247 DEL_ARR_F(ent->attr.mtd_attr.param_access);
248 ent->attr.mtd_attr.param_access = NULL;
250 if (ent->attr.mtd_attr.param_weight) {
251 DEL_ARR_F(ent->attr.mtd_attr.param_weight);
252 ent->attr.mtd_attr.param_weight = NULL;
255 } /* free_entity_attrs */
258 * Copies the entity if the new_owner is different from the
259 * owner of the old entity, else returns the old entity.
262 copy_entity_own(ir_entity *old, ir_type *new_owner) {
264 assert(is_entity(old));
265 assert(is_compound_type(new_owner));
266 assert(get_type_state(new_owner) != layout_fixed);
268 if (old->owner == new_owner) return old;
269 newe = xmalloc(sizeof(*newe));
270 memcpy(newe, old, sizeof(*newe));
271 newe->owner = new_owner;
272 if (is_Class_type(new_owner)) {
273 newe->overwrites = NEW_ARR_F(ir_entity *, 0);
274 newe->overwrittenby = NEW_ARR_F(ir_entity *, 0);
277 newe->nr = get_irp_new_node_nr();
280 insert_entity_in_owner(newe);
283 } /* copy_entity_own */
286 copy_entity_name(ir_entity *old, ident *new_name) {
288 assert(old && old->kind == k_entity);
290 if (old->name == new_name) return old;
291 newe = xmalloc(sizeof(*newe));
292 memcpy(newe, old, sizeof(*newe));
293 newe->name = new_name;
294 newe->ld_name = NULL;
295 if (is_Class_type(newe->owner)) {
296 newe->overwrites = DUP_ARR_F(ir_entity *, old->overwrites);
297 newe->overwrittenby = DUP_ARR_F(ir_entity *, old->overwrittenby);
300 newe->nr = get_irp_new_node_nr();
303 insert_entity_in_owner(newe);
306 } /* copy_entity_name */
310 free_entity(ir_entity *ent) {
311 assert(ent && ent->kind == k_entity);
312 free_entity_attrs(ent);
317 /* Outputs a unique number for this node */
319 get_entity_nr(const ir_entity *ent) {
320 assert(ent && ent->kind == k_entity);
324 return (long)PTR_TO_INT(ent);
326 } /* get_entity_nr */
329 (get_entity_name)(const ir_entity *ent) {
330 return _get_entity_name(ent);
331 } /* get_entity_name */
334 (get_entity_ident)(const ir_entity *ent) {
335 return _get_entity_ident(ent);
336 } /* get_entity_ident */
339 (set_entity_ident)(ir_entity *ent, ident *id) {
340 _set_entity_ident(ent, id);
341 } /* set_entity_ident */
344 (get_entity_owner)(ir_entity *ent) {
345 return _get_entity_owner(ent);
346 } /* get_entity_owner */
349 set_entity_owner(ir_entity *ent, ir_type *owner) {
350 assert(is_entity(ent));
351 assert(is_compound_type(owner));
353 } /* set_entity_owner */
356 (get_entity_ld_ident)(ir_entity *ent) {
357 return _get_entity_ld_ident(ent);
358 } /* get_entity_ld_ident */
361 (set_entity_ld_ident)(ir_entity *ent, ident *ld_ident) {
362 _set_entity_ld_ident(ent, ld_ident);
363 } /* set_entity_ld_ident */
366 (get_entity_ld_name)(ir_entity *ent) {
367 return _get_entity_ld_name(ent);
368 } /* get_entity_ld_name */
371 (get_entity_type)(ir_entity *ent) {
372 return _get_entity_type(ent);
373 } /* get_entity_type */
376 (set_entity_type)(ir_entity *ent, ir_type *type) {
377 _set_entity_type(ent, type);
378 } /* set_entity_type */
381 (get_entity_allocation)(const ir_entity *ent) {
382 return _get_entity_allocation(ent);
383 } /* get_entity_allocation */
386 (set_entity_allocation)(ir_entity *ent, ir_allocation al) {
387 _set_entity_allocation(ent, al);
388 } /* set_entity_allocation */
390 /* return the name of the visibility */
391 const char *get_allocation_name(ir_allocation all)
393 #define X(a) case a: return #a
395 X(allocation_automatic);
396 X(allocation_parameter);
397 X(allocation_dynamic);
398 X(allocation_static);
399 default: return "BAD VALUE";
402 } /* get_allocation_name */
405 (get_entity_visibility)(const ir_entity *ent) {
406 return _get_entity_visibility(ent);
407 } /* get_entity_visibility */
410 set_entity_visibility(ir_entity *ent, ir_visibility vis) {
411 assert(ent && ent->kind == k_entity);
412 if (vis != visibility_local)
413 assert((ent->allocation == allocation_static) ||
414 (ent->allocation == allocation_automatic));
415 /* @@@ Test that the owner type is not local, but how??
416 && get_class_visibility(get_entity_owner(ent)) != local));*/
417 ent->visibility = vis;
418 } /* set_entity_visibility */
420 /* return the name of the visibility */
421 const char *get_visibility_name(ir_visibility vis)
423 #define X(a) case a: return #a
426 X(visibility_external_visible);
427 X(visibility_external_allocated);
428 default: return "BAD VALUE";
431 } /* get_visibility_name */
434 (get_entity_variability)(const ir_entity *ent) {
435 return _get_entity_variability(ent);
436 } /* get_entity_variability */
439 set_entity_variability(ir_entity *ent, ir_variability var)
441 assert(ent && ent->kind == k_entity);
442 if (var == variability_part_constant)
443 assert(is_Class_type(ent->type) || is_Struct_type(ent->type));
445 if ((is_compound_type(ent->type)) &&
446 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
447 /* Allocate data structures for constant values */
448 ent->attr.cmpd_attr.values = NEW_ARR_F(ir_node *, 0);
449 ent->attr.cmpd_attr.val_paths = NEW_ARR_F(compound_graph_path *, 0);
451 if ((is_atomic_type(ent->type)) &&
452 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
453 /* Set default constant value. */
454 ent->value = new_rd_Unknown(get_const_code_irg(), get_type_mode(ent->type));
457 if ((is_compound_type(ent->type)) &&
458 (var == variability_uninitialized) && (ent->variability != variability_uninitialized)) {
459 /* Free data structures for constant values */
460 DEL_ARR_F(ent->attr.cmpd_attr.values); ent->attr.cmpd_attr.values = NULL;
461 DEL_ARR_F(ent->attr.cmpd_attr.val_paths); ent->attr.cmpd_attr.val_paths = NULL;
463 ent->variability = var;
464 } /* set_entity_variability */
466 /* return the name of the variability */
467 const char *get_variability_name(ir_variability var)
469 #define X(a) case a: return #a
471 X(variability_uninitialized);
472 X(variability_initialized);
473 X(variability_part_constant);
474 X(variability_constant);
475 default: return "BAD VALUE";
478 } /* get_variability_name */
481 (get_entity_volatility)(const ir_entity *ent) {
482 return _get_entity_volatility(ent);
483 } /* get_entity_volatility */
486 (set_entity_volatility)(ir_entity *ent, ir_volatility vol) {
487 _set_entity_volatility(ent, vol);
488 } /* set_entity_volatility */
490 /* Return the name of the volatility. */
491 const char *get_volatility_name(ir_volatility var)
493 #define X(a) case a: return #a
495 X(volatility_non_volatile);
496 X(volatility_is_volatile);
497 default: return "BAD VALUE";
500 } /* get_volatility_name */
503 (get_entity_align)(const ir_entity *ent) {
504 return _get_entity_align(ent);
505 } /* get_entity_align */
508 (set_entity_align)(ir_entity *ent, ir_align a) {
509 _set_entity_align(ent, a);
510 } /* set_entity_align */
512 /* Return the name of the alignment. */
513 const char *get_align_name(ir_align a)
515 #define X(a) case a: return #a
517 X(align_non_aligned);
519 default: return "BAD VALUE";
522 } /* get_align_name */
525 (get_entity_peculiarity)(const ir_entity *ent) {
526 return _get_entity_peculiarity(ent);
527 } /* get_entity_peculiarity */
530 (set_entity_peculiarity)(ir_entity *ent, ir_peculiarity pec) {
531 _set_entity_peculiarity(ent, pec);
532 } /* set_entity_peculiarity */
534 /* Checks if an entity cannot be overridden anymore. */
535 int (is_entity_final)(const ir_entity *ent) {
536 return _is_entity_final(ent);
537 } /* is_entity_final */
539 /* Sets/resets the final flag of an entity. */
540 void (set_entity_final)(ir_entity *ent, int final) {
541 _set_entity_final(ent, final);
542 } /* set_entity_final */
544 /* Checks if an entity is compiler generated */
545 int (is_entity_compiler_generated)(const ir_entity *ent) {
546 return _is_entity_compiler_generated(ent);
547 } /* is_entity_compiler_generated */
549 /* Sets/resets the compiler generated flag */
550 void (set_entity_compiler_generated)(ir_entity *ent, int flag) {
551 _set_entity_compiler_generated(ent, flag);
552 } /* set_entity_compiler_generated */
554 /* Checks if an entity is marked by the backend */
555 int (is_entity_backend_marked)(const ir_entity *ent) {
556 return _is_entity_backend_marked(ent);
557 } /* is_entity_backend_marked */
559 /* Sets/resets the compiler generated flag */
560 void (set_entity_backend_marked)(ir_entity *ent, int flag) {
561 _set_entity_backend_marked(ent, flag);
562 } /* set_entity_backend_marked */
564 ir_entity_usage (get_entity_usage)(const ir_entity *ent) {
565 return _get_entity_usage(ent);
568 void (set_entity_usage)(ir_entity *ent, ir_entity_usage flags) {
569 _set_entity_usage(ent, flags);
572 /* Get the entity's stickyness */
574 (get_entity_stickyness)(const ir_entity *ent) {
575 return _get_entity_stickyness(ent);
576 } /* get_entity_stickyness */
578 /* Set the entity's stickyness */
580 (set_entity_stickyness)(ir_entity *ent, ir_stickyness stickyness) {
581 _set_entity_stickyness(ent, stickyness);
582 } /* set_entity_stickyness */
584 /* Set has no effect for existent entities of type method. */
586 get_atomic_ent_value(ir_entity *ent)
588 assert(ent && is_atomic_entity(ent));
589 assert(ent->variability != variability_uninitialized);
590 return skip_Id(ent->value);
591 } /* get_atomic_ent_value */
594 set_atomic_ent_value(ir_entity *ent, ir_node *val) {
595 assert(is_atomic_entity(ent) && (ent->variability != variability_uninitialized));
596 if (is_Method_type(ent->type) && (ent->peculiarity == peculiarity_existent))
598 assert(get_irn_mode(val) == get_type_mode(ent->type));
600 } /* set_atomic_ent_value */
602 /* Returns true if the the node is representable as code on
604 int is_irn_const_expression(ir_node *n) {
607 /* we are in danger iff an exception will arise. TODO: be more precisely,
608 * for instance Div. will NOT rise if divisor != 0
610 if (is_binop(n) && !is_fragile_op(n))
611 return is_irn_const_expression(get_binop_left(n)) && is_irn_const_expression(get_binop_right(n));
614 switch (get_irn_opcode(n)) {
621 return is_irn_const_expression(get_irn_n(n, 0));
626 } /* is_irn_const_expression */
629 * Copies a firm subgraph that complies to the restrictions for
630 * constant expressions to current_block in current_ir_graph.
632 ir_node *copy_const_value(dbg_info *dbg, ir_node *n) {
636 /* @@@ GL I think we should implement this using the routines from irgopt for
637 dead node elimination/inlineing. */
640 switch (get_irn_opcode(n)) {
642 nn = new_d_Const_type(dbg, m, get_Const_tarval(n), get_Const_type(n));
645 nn = new_d_SymConst_type(dbg, get_irn_mode(n), get_SymConst_symbol(n), get_SymConst_kind(n),
646 get_SymConst_value_type(n));
649 nn = new_d_Add(dbg, copy_const_value(dbg, get_Add_left(n)),
650 copy_const_value(dbg, get_Add_right(n)), m); break;
652 nn = new_d_Sub(dbg, copy_const_value(dbg, get_Sub_left(n)),
653 copy_const_value(dbg, get_Sub_right(n)), m); break;
655 nn = new_d_Mul(dbg, copy_const_value(dbg, get_Mul_left(n)),
656 copy_const_value(dbg, get_Mul_right(n)), m); break;
658 nn = new_d_And(dbg, copy_const_value(dbg, get_And_left(n)),
659 copy_const_value(dbg, get_And_right(n)), m); break;
661 nn = new_d_Or(dbg, copy_const_value(dbg, get_Or_left(n)),
662 copy_const_value(dbg, get_Or_right(n)), m); break;
664 nn = new_d_Eor(dbg, copy_const_value(dbg, get_Eor_left(n)),
665 copy_const_value(dbg, get_Eor_right(n)), m); break;
667 nn = new_d_Cast(dbg, copy_const_value(dbg, get_Cast_op(n)), get_Cast_type(n)); break;
669 nn = new_d_Conv(dbg, copy_const_value(dbg, get_Conv_op(n)), m); break;
671 nn = new_d_Unknown(m); break;
673 assert(0 && "opcode invalid or not implemented");
678 } /* copy_const_value */
680 static ir_initializer_t null_initializer = { IR_INITIALIZER_NULL };
682 ir_initializer_t *get_initializer_null(void)
684 return &null_initializer;
687 ir_initializer_t *create_initializer_const(ir_node *value)
689 struct obstack *obst = get_irg_obstack(get_const_code_irg());
691 ir_initializer_t *initializer
692 = obstack_alloc(obst, sizeof(ir_initializer_const_t));
693 initializer->kind = IR_INITIALIZER_CONST;
694 initializer->consti.value = value;
699 ir_initializer_t *create_initializer_tarval(tarval *tv)
701 struct obstack *obst = get_irg_obstack(get_const_code_irg());
703 ir_initializer_t *initializer
704 = obstack_alloc(obst, sizeof(ir_initializer_tarval_t));
705 initializer->kind = IR_INITIALIZER_TARVAL;
706 initializer->tarval.value = tv;
711 ir_initializer_t *create_initializer_compound(unsigned n_entries)
713 struct obstack *obst = get_irg_obstack(get_const_code_irg());
716 size_t size = sizeof(ir_initializer_compound_t)
717 + (n_entries-1) * sizeof(ir_initializer_t*);
719 ir_initializer_t *initializer = obstack_alloc(obst, size);
720 initializer->kind = IR_INITIALIZER_COMPOUND;
721 initializer->compound.n_initializers = n_entries;
723 for(i = 0; i < n_entries; ++i) {
724 initializer->compound.initializers[i] = get_initializer_null();
730 ir_node *get_initializer_const_value(const ir_initializer_t *initializer)
732 assert(initializer->kind == IR_INITIALIZER_CONST);
733 return initializer->consti.value;
736 tarval *get_initializer_tarval_value(const ir_initializer_t *initializer)
738 assert(initializer->kind == IR_INITIALIZER_TARVAL);
739 return initializer->tarval.value;
742 unsigned get_initializer_compound_n_entries(const ir_initializer_t *initializer)
744 assert(initializer->kind == IR_INITIALIZER_COMPOUND);
745 return initializer->compound.n_initializers;
748 void set_initializer_compound_value(ir_initializer_t *initializer,
749 unsigned index, ir_initializer_t *value)
751 assert(initializer->kind == IR_INITIALIZER_COMPOUND);
752 assert(index < initializer->compound.n_initializers);
754 initializer->compound.initializers[index] = value;
757 ir_initializer_t *get_initializer_compound_value(
758 const ir_initializer_t *initializer, unsigned index)
760 assert(initializer->kind == IR_INITIALIZER_COMPOUND);
761 assert(index < initializer->compound.n_initializers);
763 return initializer->compound.initializers[index];
766 ir_initializer_kind_t get_initializer_kind(const ir_initializer_t *initializer)
768 return initializer->kind;
771 static void check_entity_initializer(ir_entity *entity)
777 void set_entity_initializer(ir_entity *entity, ir_initializer_t *initializer)
779 entity->attr.initializer = initializer;
780 entity->has_initializer = 1;
781 check_entity_initializer(entity);
784 ir_initializer_t *get_entity_initializer(const ir_entity *entity)
786 assert(entity->has_initializer);
787 return entity->attr.initializer;
790 /* Creates a new compound graph path. */
791 compound_graph_path *
792 new_compound_graph_path(ir_type *tp, int length) {
793 compound_graph_path *res;
795 assert(is_compound_type(tp));
798 res = xmalloc(sizeof(*res) + (length-1) * sizeof(res->list[0]));
799 memset(res, 0, sizeof(*res) + (length-1) * sizeof(res->list[0]));
800 res->kind = k_ir_compound_graph_path;
805 } /* new_compound_graph_path */
807 /* Frees an graph path object */
808 void free_compound_graph_path (compound_graph_path *gr) {
809 assert(gr && is_compound_graph_path(gr));
812 } /* free_compound_graph_path */
814 /* Returns non-zero if an object is a compound graph path */
815 int is_compound_graph_path(const void *thing) {
816 return (get_kind(thing) == k_ir_compound_graph_path);
817 } /* is_compound_graph_path */
819 /* Checks whether the path up to pos is correct. If the path contains a NULL,
820 * assumes the path is not complete and returns 'true'. */
821 int is_proper_compound_graph_path(compound_graph_path *gr, int pos) {
824 ir_type *owner = gr->tp;
826 for (i = 0; i <= pos; i++) {
827 node = get_compound_graph_path_node(gr, i);
829 /* Path not yet complete. */
831 if (get_entity_owner(node) != owner)
833 owner = get_entity_type(node);
835 if (pos == get_compound_graph_path_length(gr))
836 if (!is_atomic_type(owner))
839 } /* is_proper_compound_graph_path */
841 /* Returns the length of a graph path */
842 int get_compound_graph_path_length(const compound_graph_path *gr) {
843 assert(gr && is_compound_graph_path(gr));
845 } /* get_compound_graph_path_length */
848 get_compound_graph_path_node(const compound_graph_path *gr, int pos) {
849 assert(gr && is_compound_graph_path(gr));
850 assert(pos >= 0 && pos < gr->len);
851 return gr->list[pos].node;
852 } /* get_compound_graph_path_node */
855 set_compound_graph_path_node(compound_graph_path *gr, int pos, ir_entity *node) {
856 assert(gr && is_compound_graph_path(gr));
857 assert(pos >= 0 && pos < gr->len);
858 assert(is_entity(node));
859 gr->list[pos].node = node;
860 assert(is_proper_compound_graph_path(gr, pos));
861 } /* set_compound_graph_path_node */
864 get_compound_graph_path_array_index(const compound_graph_path *gr, int pos) {
865 assert(gr && is_compound_graph_path(gr));
866 assert(pos >= 0 && pos < gr->len);
867 return gr->list[pos].index;
868 } /* get_compound_graph_path_array_index */
871 set_compound_graph_path_array_index(compound_graph_path *gr, int pos, int index) {
872 assert(gr && is_compound_graph_path(gr));
873 assert(pos >= 0 && pos < gr->len);
874 gr->list[pos].index = index;
875 } /* set_compound_graph_path_array_index */
878 get_compound_graph_path_type(const compound_graph_path *gr) {
879 assert(gr && is_compound_graph_path(gr));
883 /* A value of a compound entity is a pair of value and the corresponding path to a member of
886 add_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path) {
887 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
888 assert(is_compound_graph_path(path));
889 ARR_APP1(ir_node *, ent->attr.cmpd_attr.values, val);
890 ARR_APP1(compound_graph_path *, ent->attr.cmpd_attr.val_paths, path);
891 } /* add_compound_ent_value_w_path */
894 set_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path, int pos) {
895 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
896 assert(is_compound_graph_path(path));
897 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
898 ent->attr.cmpd_attr.values[pos] = val;
899 ent->attr.cmpd_attr.val_paths[pos] = path;
900 } /* set_compound_ent_value_w_path */
903 get_compound_ent_n_values(ir_entity *ent) {
904 assert(!ent->has_initializer);
905 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
906 return ARR_LEN(ent->attr.cmpd_attr.values);
907 } /* get_compound_ent_n_values */
910 get_compound_ent_value(ir_entity *ent, int pos) {
911 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
912 assert(!ent->has_initializer);
913 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
914 return ent->attr.cmpd_attr.values[pos];
915 } /* get_compound_ent_value */
917 compound_graph_path *
918 get_compound_ent_value_path(ir_entity *ent, int pos) {
919 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
920 assert(!ent->has_initializer);
921 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.val_paths));
922 return ent->attr.cmpd_attr.val_paths[pos];
923 } /* get_compound_ent_value_path */
926 * Returns non-zero, if two compound_graph_pathes are equal
928 * @param path1 the first path
929 * @param path2 the second path
931 static int equal_paths(compound_graph_path *path1, compound_graph_path *path2) {
933 int len1 = get_compound_graph_path_length(path1);
934 int len2 = get_compound_graph_path_length(path2);
936 if (len2 != len1) return 0;
938 for (i = 0; i < len1; i++) {
940 ir_entity *node1 = get_compound_graph_path_node(path1, i);
941 ir_entity *node2 = get_compound_graph_path_node(path2, i);
943 if (node1 != node2) return 0;
945 tp = get_entity_owner(node1);
946 if (is_Array_type(tp)) {
947 int index1 = get_compound_graph_path_array_index(path1, i);
948 int index2 = get_compound_graph_path_array_index(path2, i);
949 if (index1 != index2)
957 * Returns the position of a value with the given path.
958 * The path must contain array indices for all array element entities.
960 * @todo This implementation is very slow (O(number of initializers * |path|)
961 * and should be replaced when the new tree oriented
962 * value representation is finally implemented.
964 static int get_compound_ent_pos_by_path(ir_entity *ent, compound_graph_path *path) {
965 int i, n_paths = get_compound_ent_n_values(ent);
967 for (i = 0; i < n_paths; i ++) {
968 compound_graph_path *gr = get_compound_ent_value_path(ent, i);
969 if (equal_paths(gr, path))
973 } /* get_compound_ent_pos_by_path */
975 /* Returns a constant value given the access path.
976 * The path must contain array indices for all array element entities. */
977 ir_node *get_compound_ent_value_by_path(ir_entity *ent, compound_graph_path *path) {
978 int pos = get_compound_ent_pos_by_path(ent, path);
980 return get_compound_ent_value(ent, pos);
982 } /* get_compound_ent_value_by_path */
986 remove_compound_ent_value(ir_entity *ent, ir_entity *value_ent) {
988 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
990 n = ARR_LEN(ent->attr.cmpd_attr.val_paths);
991 for (i = 0; i < n; ++i) {
992 compound_graph_path *path = ent->attr.cmpd_attr.val_paths[i];
993 if (path->list[path->len-1].node == value_ent) {
994 for (; i < n - 1; ++i) {
995 ent->attr.cmpd_attr.val_paths[i] = ent->attr.cmpd_attr.val_paths[i+1];
996 ent->attr.cmpd_attr.values[i] = ent->attr.cmpd_attr.values[i+1];
998 ARR_SETLEN(ir_entity*, ent->attr.cmpd_attr.val_paths, n - 1);
999 ARR_SETLEN(ir_node*, ent->attr.cmpd_attr.values, n - 1);
1003 } /* remove_compound_ent_value */
1006 add_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member) {
1007 compound_graph_path *path;
1008 ir_type *owner_tp = get_entity_owner(member);
1009 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
1010 path = new_compound_graph_path(get_entity_type(ent), 1);
1011 path->list[0].node = member;
1012 if (is_Array_type(owner_tp)) {
1016 assert(get_array_n_dimensions(owner_tp) == 1 && has_array_lower_bound(owner_tp, 0));
1017 max = get_array_lower_bound_int(owner_tp, 0) -1;
1018 for (i = 0, n = get_compound_ent_n_values(ent); i < n; ++i) {
1019 int index = get_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0);
1024 path->list[0].index = max + 1;
1026 add_compound_ent_value_w_path(ent, val, path);
1027 } /* add_compound_ent_value */
1031 get_compound_ent_value_member(ir_entity *ent, int pos) {
1032 compound_graph_path *path;
1033 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
1034 path = get_compound_ent_value_path(ent, pos);
1036 return get_compound_graph_path_node(path, get_compound_graph_path_length(path)-1);
1037 } /* get_compound_ent_value_member */
1040 set_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member, int pos) {
1041 compound_graph_path *path;
1042 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
1043 path = get_compound_ent_value_path(ent, pos);
1044 set_compound_graph_path_node(path, 0, member);
1045 set_compound_ent_value_w_path(ent, val, path, pos);
1046 } /* set_compound_ent_value */
1049 set_array_entity_values(ir_entity *ent, tarval **values, int num_vals) {
1051 ir_graph *rem = current_ir_graph;
1052 ir_type *arrtp = get_entity_type(ent);
1054 ir_type *elttp = get_array_element_type(arrtp);
1056 assert(is_Array_type(arrtp));
1057 assert(get_array_n_dimensions(arrtp) == 1);
1058 /* One bound is sufficient, the number of constant fields makes the
1060 assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
1061 assert(get_entity_variability(ent) != variability_uninitialized);
1062 current_ir_graph = get_const_code_irg();
1064 for (i = 0; i < num_vals; i++) {
1065 val = new_Const_type(values[i], elttp);
1066 add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
1067 set_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0, i);
1069 current_ir_graph = rem;
1070 } /* set_array_entity_values */
1072 /* Return the overall offset of value at position pos in bytes. */
1073 unsigned get_compound_ent_value_offset_bytes(ir_entity *ent, int pos) {
1074 compound_graph_path *path;
1076 unsigned offset = 0;
1079 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
1081 path = get_compound_ent_value_path(ent, pos);
1082 path_len = get_compound_graph_path_length(path);
1085 for (i = 0; i < path_len; ++i) {
1086 if (is_Array_type(curr_tp)) {
1087 ir_type *elem_type = get_array_element_type(curr_tp);
1088 unsigned size = get_type_size_bytes(elem_type);
1089 unsigned align = get_type_alignment_bytes(elem_type);
1093 if(size % align > 0) {
1094 size += align - (size % align);
1096 idx = get_compound_graph_path_array_index(path, i);
1098 offset += size * idx;
1099 curr_tp = elem_type;
1101 ir_entity *node = get_compound_graph_path_node(path, i);
1102 offset += get_entity_offset(node);
1103 curr_tp = get_entity_type(node);
1108 } /* get_compound_ent_value_offset_bytes */
1110 /* Return the offset in bits from the last byte address. */
1111 unsigned get_compound_ent_value_offset_bit_remainder(ir_entity *ent, int pos) {
1112 compound_graph_path *path;
1114 ir_entity *last_node;
1116 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
1118 path = get_compound_ent_value_path(ent, pos);
1119 path_len = get_compound_graph_path_length(path);
1120 last_node = get_compound_graph_path_node(path, path_len - 1);
1122 if(last_node == NULL)
1125 return get_entity_offset_bits_remainder(last_node);
1126 } /* get_compound_ent_value_offset_bit_remainder */
1129 (get_entity_offset)(const ir_entity *ent) {
1130 return _get_entity_offset(ent);
1131 } /* get_entity_offset */
1134 (set_entity_offset)(ir_entity *ent, int offset) {
1135 _set_entity_offset(ent, offset);
1136 } /* set_entity_offset */
1139 (get_entity_offset_bits_remainder)(const ir_entity *ent) {
1140 return _get_entity_offset_bits_remainder(ent);
1141 } /* get_entity_offset_bits_remainder */
1144 (set_entity_offset_bits_remainder)(ir_entity *ent, unsigned char offset) {
1145 _set_entity_offset_bits_remainder(ent, offset);
1146 } /* set_entity_offset_bits_remainder */
1149 add_entity_overwrites(ir_entity *ent, ir_entity *overwritten) {
1151 ir_type *owner = get_entity_owner(ent);
1152 ir_type *ovw_ovner = get_entity_owner(overwritten);
1153 assert(is_Class_type(owner));
1154 assert(is_Class_type(ovw_ovner));
1155 assert(! is_class_final(ovw_ovner));
1157 ARR_APP1(ir_entity *, ent->overwrites, overwritten);
1158 ARR_APP1(ir_entity *, overwritten->overwrittenby, ent);
1159 } /* add_entity_overwrites */
1162 get_entity_n_overwrites(ir_entity *ent) {
1163 assert(is_Class_type(get_entity_owner(ent)));
1164 return (ARR_LEN(ent->overwrites));
1165 } /* get_entity_n_overwrites */
1168 get_entity_overwrites_index(ir_entity *ent, ir_entity *overwritten) {
1170 assert(is_Class_type(get_entity_owner(ent)));
1171 n = get_entity_n_overwrites(ent);
1172 for (i = 0; i < n; ++i) {
1173 if (get_entity_overwrites(ent, i) == overwritten)
1177 } /* get_entity_overwrites_index */
1180 get_entity_overwrites(ir_entity *ent, int pos) {
1181 assert(is_Class_type(get_entity_owner(ent)));
1182 assert(pos < get_entity_n_overwrites(ent));
1183 return ent->overwrites[pos];
1184 } /* get_entity_overwrites */
1187 set_entity_overwrites(ir_entity *ent, int pos, ir_entity *overwritten) {
1188 assert(is_Class_type(get_entity_owner(ent)));
1189 assert(pos < get_entity_n_overwrites(ent));
1190 ent->overwrites[pos] = overwritten;
1191 } /* set_entity_overwrites */
1194 remove_entity_overwrites(ir_entity *ent, ir_entity *overwritten) {
1196 assert(is_Class_type(get_entity_owner(ent)));
1197 n = ARR_LEN(ent->overwrites);
1198 for (i = 0; i < n; ++i) {
1199 if (ent->overwrites[i] == overwritten) {
1200 for (; i < n - 1; i++)
1201 ent->overwrites[i] = ent->overwrites[i+1];
1202 ARR_SETLEN(ir_entity*, ent->overwrites, n - 1);
1206 } /* remove_entity_overwrites */
1209 add_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites) {
1210 add_entity_overwrites(overwrites, ent);
1211 } /* add_entity_overwrittenby */
1214 get_entity_n_overwrittenby(ir_entity *ent) {
1215 assert(is_Class_type(get_entity_owner(ent)));
1216 return ARR_LEN(ent->overwrittenby);
1217 } /* get_entity_n_overwrittenby */
1220 get_entity_overwrittenby_index(ir_entity *ent, ir_entity *overwrites) {
1222 assert(is_Class_type(get_entity_owner(ent)));
1223 n = get_entity_n_overwrittenby(ent);
1224 for (i = 0; i < n; ++i) {
1225 if (get_entity_overwrittenby(ent, i) == overwrites)
1229 } /* get_entity_overwrittenby_index */
1232 get_entity_overwrittenby(ir_entity *ent, int pos) {
1233 assert(is_Class_type(get_entity_owner(ent)));
1234 assert(pos < get_entity_n_overwrittenby(ent));
1235 return ent->overwrittenby[pos];
1236 } /* get_entity_overwrittenby */
1239 set_entity_overwrittenby(ir_entity *ent, int pos, ir_entity *overwrites) {
1240 assert(is_Class_type(get_entity_owner(ent)));
1241 assert(pos < get_entity_n_overwrittenby(ent));
1242 ent->overwrittenby[pos] = overwrites;
1243 } /* set_entity_overwrittenby */
1245 void remove_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites) {
1247 assert(is_Class_type(get_entity_owner(ent)));
1249 n = ARR_LEN(ent->overwrittenby);
1250 for (i = 0; i < n; ++i) {
1251 if (ent->overwrittenby[i] == overwrites) {
1252 for(; i < n - 1; ++i)
1253 ent->overwrittenby[i] = ent->overwrittenby[i+1];
1254 ARR_SETLEN(ir_entity*, ent->overwrittenby, n - 1);
1258 } /* remove_entity_overwrittenby */
1260 /* A link to store intermediate information */
1262 (get_entity_link)(const ir_entity *ent) {
1263 return _get_entity_link(ent);
1264 } /* get_entity_link */
1267 (set_entity_link)(ir_entity *ent, void *l) {
1268 _set_entity_link(ent, l);
1269 } /* set_entity_link */
1272 (get_entity_irg)(const ir_entity *ent) {
1273 return _get_entity_irg(ent);
1274 } /* get_entity_irg */
1277 set_entity_irg(ir_entity *ent, ir_graph *irg) {
1278 assert(is_method_entity(ent));
1279 /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
1280 * Methode selbst nicht mehr aufgerufen werden kann, die Entität
1281 * aber erhalten bleiben soll? Wandle die Entitaet in description oder
1284 assert((irg && ent->peculiarity == peculiarity_existent) ||
1285 (!irg && (ent->peculiarity == peculiarity_existent)
1286 && (ent -> visibility == visibility_external_allocated)) ||
1287 (!irg && ent->peculiarity == peculiarity_description) ||
1288 (!irg && ent->peculiarity == peculiarity_inherited));
1289 ent->attr.mtd_attr.irg = irg;
1290 } /* set_entity_irg */
1292 unsigned get_entity_vtable_number(const ir_entity *ent) {
1293 assert(is_method_entity((ir_entity *)ent));
1294 return ent->attr.mtd_attr.vtable_number;
1295 } /* get_entity_vtable_number */
1297 void set_entity_vtable_number(ir_entity *ent, unsigned vtable_number) {
1298 assert(is_method_entity(ent));
1299 ent->attr.mtd_attr.vtable_number = vtable_number;
1300 } /* set_entity_vtable_number */
1303 (is_entity)(const void *thing) {
1304 return _is_entity(thing);
1307 int is_atomic_entity(ir_entity *ent) {
1308 ir_type *t = get_entity_type(ent);
1309 const tp_op *op = get_type_tpop(t);
1310 return (op == type_primitive || op == type_pointer ||
1311 op == type_enumeration || op == type_method);
1312 } /* is_atomic_entity */
1314 int is_compound_entity(ir_entity *ent) {
1315 ir_type *t = get_entity_type(ent);
1316 const tp_op *op = get_type_tpop(t);
1317 return (op == type_class || op == type_struct ||
1318 op == type_array || op == type_union);
1319 } /* is_compound_entity */
1321 int is_method_entity(ir_entity *ent) {
1322 ir_type *t = get_entity_type(ent);
1323 return is_Method_type(t);
1324 } /* is_method_entity */
1327 * @todo not implemented!!! */
1328 int equal_entity(ir_entity *ent1, ir_entity *ent2) {
1331 fprintf(stderr, " calling unimplemented equal entity!!! \n");
1333 } /* equal_entity */
1336 unsigned long (get_entity_visited)(ir_entity *ent) {
1337 return _get_entity_visited(ent);
1338 } /* get_entity_visited */
1340 void (set_entity_visited)(ir_entity *ent, unsigned long num) {
1341 _set_entity_visited(ent, num);
1342 } /* set_entity_visited */
1344 /* Sets visited field in ir_entity to entity_visited. */
1345 void (mark_entity_visited)(ir_entity *ent) {
1346 _mark_entity_visited(ent);
1347 } /* mark_entity_visited */
1349 int (entity_visited)(ir_entity *ent) {
1350 return _entity_visited(ent);
1351 } /* entity_visited */
1353 int (entity_not_visited)(ir_entity *ent) {
1354 return _entity_not_visited(ent);
1355 } /* entity_not_visited */
1357 /* Returns the mask of the additional entity properties. */
1358 unsigned get_entity_additional_properties(ir_entity *ent) {
1361 assert(is_method_entity(ent));
1363 /* first check, if the graph has additional properties */
1364 irg = get_entity_irg(ent);
1367 return get_irg_additional_properties(irg);
1369 if (ent->attr.mtd_attr.irg_add_properties & mtp_property_inherited)
1370 return get_method_additional_properties(get_entity_type(ent));
1372 return ent->attr.mtd_attr.irg_add_properties;
1373 } /* get_entity_additional_properties */
1375 /* Sets the mask of the additional graph properties. */
1376 void set_entity_additional_properties(ir_entity *ent, unsigned property_mask)
1380 assert(is_method_entity(ent));
1382 /* first check, if the graph exists */
1383 irg = get_entity_irg(ent);
1385 set_irg_additional_properties(irg, property_mask);
1387 /* do not allow to set the mtp_property_inherited flag or
1388 * the automatic inheritance of flags will not work */
1389 ent->attr.mtd_attr.irg_add_properties = property_mask & ~mtp_property_inherited;
1391 } /* set_entity_additional_properties */
1393 /* Sets one additional graph property. */
1394 void set_entity_additional_property(ir_entity *ent, mtp_additional_property flag)
1398 assert(is_method_entity(ent));
1400 /* first check, if the graph exists */
1401 irg = get_entity_irg(ent);
1403 set_irg_additional_property(irg, flag);
1405 unsigned mask = ent->attr.mtd_attr.irg_add_properties;
1407 if (mask & mtp_property_inherited)
1408 mask = get_method_additional_properties(get_entity_type(ent));
1410 /* do not allow to set the mtp_property_inherited flag or
1411 * the automatic inheritance of flags will not work */
1412 ent->attr.mtd_attr.irg_add_properties = mask | (flag & ~mtp_property_inherited);
1414 } /* set_entity_additional_property */
1416 /* Returns the class type that this type info entity represents or NULL
1417 if ent is no type info entity. */
1418 ir_type *(get_entity_repr_class)(const ir_entity *ent) {
1419 return _get_entity_repr_class(ent);
1420 } /* get_entity_repr_class */
1422 dbg_info *(get_entity_dbg_info)(const ir_entity *ent) {
1423 return _get_entity_dbg_info(ent);
1424 } /* get_entity_dbg_info */
1426 void (set_entity_dbg_info)(ir_entity *ent, dbg_info *db) {
1427 _set_entity_dbg_info(ent, db);
1428 } /* set_entity_dbg_info */
1430 /* Initialize entity module. */
1431 void firm_init_entity(void)
1433 symconst_symbol sym;
1435 assert(firm_unknown_type && "Call init_type() before firm_init_entity()!");
1436 assert(!unknown_entity && "Call firm_init_entity() only once!");
1438 unknown_entity = new_rd_entity(NULL, firm_unknown_type, new_id_from_str(UNKNOWN_ENTITY_NAME), firm_unknown_type);
1439 set_entity_visibility(unknown_entity, visibility_external_allocated);
1440 set_entity_ld_ident(unknown_entity, get_entity_ident(unknown_entity));
1442 current_ir_graph = get_const_code_irg();
1443 sym.entity_p = unknown_entity;
1444 /* TODO: we need two unknown_entities here, one for code and one for data */
1445 unknown_entity->value = new_SymConst(mode_P_data, sym, symconst_addr_ent);
1446 } /* firm_init_entity */