2 * Copyright (C) 1995-2007 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"
43 /* All this is needed to build the constant node for methods: */
47 #include "irdump.h" /* for output if errors occur. */
49 #include "callgraph.h" /* for dumping debug output */
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->address_taken = ir_address_taken_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)) {
161 rem = current_ir_graph;
162 current_ir_graph = get_const_code_irg();
163 res->value = new_SymConst(sym, symconst_addr_ent);
164 current_ir_graph = rem;
165 res->allocation = allocation_static;
166 res->variability = variability_constant;
167 res->attr.mtd_attr.irg_add_properties = mtp_property_inherited;
168 res->attr.mtd_attr.vtable_number = VTABLE_NUM_NOT_SET;
169 res->attr.mtd_attr.param_access = NULL;
170 res->attr.mtd_attr.param_weight = NULL;
171 res->attr.mtd_attr.irg = NULL;
172 res->attr.mtd_attr.section = section_text;
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 copy_entity_own(ir_entity *old, ir_type *new_owner) {
260 assert(is_entity(old));
261 assert(is_compound_type(new_owner));
263 if (old->owner == new_owner) return old;
264 newe = xmalloc(sizeof(*newe));
265 memcpy(newe, old, sizeof(*newe));
266 newe->owner = new_owner;
267 if (is_Class_type(new_owner)) {
268 newe->overwrites = NEW_ARR_F(ir_entity *, 0);
269 newe->overwrittenby = NEW_ARR_F(ir_entity *, 0);
272 newe->nr = get_irp_new_node_nr();
275 insert_entity_in_owner(newe);
278 } /* copy_entity_own */
281 copy_entity_name(ir_entity *old, ident *new_name) {
283 assert(old && old->kind == k_entity);
285 if (old->name == new_name) return old;
286 newe = xmalloc(sizeof(*newe));
287 memcpy(newe, old, sizeof(*newe));
288 newe->name = new_name;
289 newe->ld_name = NULL;
290 if (is_Class_type(newe->owner)) {
291 newe->overwrites = DUP_ARR_F(ir_entity *, old->overwrites);
292 newe->overwrittenby = DUP_ARR_F(ir_entity *, old->overwrittenby);
295 newe->nr = get_irp_new_node_nr();
298 insert_entity_in_owner(newe);
301 } /* copy_entity_name */
305 free_entity(ir_entity *ent) {
306 assert(ent && ent->kind == k_entity);
307 free_entity_attrs(ent);
312 /* Outputs a unique number for this node */
314 get_entity_nr(const ir_entity *ent) {
315 assert(ent && ent->kind == k_entity);
319 return (long)PTR_TO_INT(ent);
321 } /* get_entity_nr */
324 (get_entity_name)(const ir_entity *ent) {
325 return _get_entity_name(ent);
326 } /* get_entity_name */
329 (get_entity_ident)(const ir_entity *ent) {
330 return _get_entity_ident(ent);
331 } /* get_entity_ident */
334 (set_entity_ident)(ir_entity *ent, ident *id) {
335 _set_entity_ident(ent, id);
336 } /* set_entity_ident */
339 (get_entity_owner)(ir_entity *ent) {
340 return _get_entity_owner(ent);
341 } /* get_entity_owner */
344 set_entity_owner(ir_entity *ent, ir_type *owner) {
345 assert(is_entity(ent));
346 assert(is_compound_type(owner));
348 } /* set_entity_owner */
351 (get_entity_ld_ident)(ir_entity *ent) {
352 return _get_entity_ld_ident(ent);
353 } /* get_entity_ld_ident */
356 (set_entity_ld_ident)(ir_entity *ent, ident *ld_ident) {
357 _set_entity_ld_ident(ent, ld_ident);
358 } /* set_entity_ld_ident */
361 (get_entity_ld_name)(ir_entity *ent) {
362 return _get_entity_ld_name(ent);
363 } /* get_entity_ld_name */
366 (get_entity_type)(ir_entity *ent) {
367 return _get_entity_type(ent);
368 } /* get_entity_type */
371 (set_entity_type)(ir_entity *ent, ir_type *type) {
372 _set_entity_type(ent, type);
373 } /* set_entity_type */
376 (get_entity_allocation)(const ir_entity *ent) {
377 return _get_entity_allocation(ent);
378 } /* get_entity_allocation */
381 (set_entity_allocation)(ir_entity *ent, ir_allocation al) {
382 _set_entity_allocation(ent, al);
383 } /* set_entity_allocation */
385 /* return the name of the visibility */
386 const char *get_allocation_name(ir_allocation all)
388 #define X(a) case a: return #a
390 X(allocation_automatic);
391 X(allocation_parameter);
392 X(allocation_dynamic);
393 X(allocation_static);
394 default: return "BAD VALUE";
397 } /* get_allocation_name */
400 (get_entity_visibility)(const ir_entity *ent) {
401 return _get_entity_visibility(ent);
402 } /* get_entity_visibility */
405 set_entity_visibility(ir_entity *ent, ir_visibility vis) {
406 assert(ent && ent->kind == k_entity);
407 if (vis != visibility_local)
408 assert((ent->allocation == allocation_static) ||
409 (ent->allocation == allocation_automatic));
410 /* @@@ Test that the owner type is not local, but how??
411 && get_class_visibility(get_entity_owner(ent)) != local));*/
412 ent->visibility = vis;
413 } /* set_entity_visibility */
415 /* return the name of the visibility */
416 const char *get_visibility_name(ir_visibility vis)
418 #define X(a) case a: return #a
421 X(visibility_external_visible);
422 X(visibility_external_allocated);
423 default: return "BAD VALUE";
426 } /* get_visibility_name */
429 (get_entity_variability)(const ir_entity *ent) {
430 return _get_entity_variability(ent);
431 } /* get_entity_variability */
434 set_entity_variability(ir_entity *ent, ir_variability var)
436 assert(ent && ent->kind == k_entity);
437 if (var == variability_part_constant)
438 assert(is_Class_type(ent->type) || is_Struct_type(ent->type));
440 if ((is_compound_type(ent->type)) &&
441 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
442 /* Allocate data structures for constant values */
443 ent->attr.cmpd_attr.values = NEW_ARR_F(ir_node *, 0);
444 ent->attr.cmpd_attr.val_paths = NEW_ARR_F(compound_graph_path *, 0);
446 if ((is_atomic_type(ent->type)) &&
447 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
448 /* Set default constant value. */
449 ent->value = new_rd_Unknown(get_const_code_irg(), get_type_mode(ent->type));
452 if ((is_compound_type(ent->type)) &&
453 (var == variability_uninitialized) && (ent->variability != variability_uninitialized)) {
454 /* Free data structures for constant values */
455 DEL_ARR_F(ent->attr.cmpd_attr.values); ent->attr.cmpd_attr.values = NULL;
456 DEL_ARR_F(ent->attr.cmpd_attr.val_paths); ent->attr.cmpd_attr.val_paths = NULL;
458 ent->variability = var;
459 } /* set_entity_variability */
461 /* return the name of the variability */
462 const char *get_variability_name(ir_variability var)
464 #define X(a) case a: return #a
466 X(variability_uninitialized);
467 X(variability_initialized);
468 X(variability_part_constant);
469 X(variability_constant);
470 default: return "BAD VALUE";
473 } /* get_variability_name */
476 (get_entity_volatility)(const ir_entity *ent) {
477 return _get_entity_volatility(ent);
478 } /* get_entity_volatility */
481 (set_entity_volatility)(ir_entity *ent, ir_volatility vol) {
482 _set_entity_volatility(ent, vol);
483 } /* set_entity_volatility */
485 /* Return the name of the volatility. */
486 const char *get_volatility_name(ir_volatility var)
488 #define X(a) case a: return #a
490 X(volatility_non_volatile);
491 X(volatility_is_volatile);
492 default: return "BAD VALUE";
495 } /* get_volatility_name */
498 (get_entity_align)(const ir_entity *ent) {
499 return _get_entity_align(ent);
500 } /* get_entity_align */
503 (set_entity_align)(ir_entity *ent, ir_align a) {
504 _set_entity_align(ent, a);
505 } /* set_entity_align */
507 /* Return the name of the alignment. */
508 const char *get_align_name(ir_align a)
510 #define X(a) case a: return #a
512 X(align_non_aligned);
514 default: return "BAD VALUE";
517 } /* get_align_name */
520 (get_entity_peculiarity)(const ir_entity *ent) {
521 return _get_entity_peculiarity(ent);
522 } /* get_entity_peculiarity */
525 (set_entity_peculiarity)(ir_entity *ent, ir_peculiarity pec) {
526 _set_entity_peculiarity(ent, pec);
527 } /* set_entity_peculiarity */
529 /* Checks if an entity cannot be overridden anymore. */
530 int (is_entity_final)(const ir_entity *ent) {
531 return _is_entity_final(ent);
532 } /* is_entity_final */
534 /* Sets/resets the final flag of an entity. */
535 void (set_entity_final)(ir_entity *ent, int final) {
536 _set_entity_final(ent, final);
537 } /* set_entity_final */
539 /* Checks if an entity is compiler generated */
540 int (is_entity_compiler_generated)(const ir_entity *ent) {
541 return _is_entity_compiler_generated(ent);
542 } /* is_entity_compiler_generated */
544 /* Sets/resets the compiler generated flag */
545 void (set_entity_compiler_generated)(ir_entity *ent, int flag) {
546 _set_entity_compiler_generated(ent, flag);
547 } /* set_entity_compiler_generated */
549 /* Checks if an entity is marked by the backend */
550 int (is_entity_backend_marked)(const ir_entity *ent) {
551 return _is_entity_backend_marked(ent);
552 } /* is_entity_backend_marked */
554 /* Sets/resets the compiler generated flag */
555 void (set_entity_backend_marked)(ir_entity *ent, int flag) {
556 _set_entity_backend_marked(ent, flag);
557 } /* set_entity_backend_marked */
559 /* Checks if the address of an entity was taken. */
560 ir_address_taken_state (get_entity_address_taken)(const ir_entity *ent) {
561 return _get_entity_address_taken(ent);
562 } /* is_entity_address_taken */
564 /* Sets/resets the address taken flag. */
565 void (set_entity_address_taken)(ir_entity *ent, ir_address_taken_state flag) {
566 _set_entity_address_taken(ent, flag);
567 } /* set_entity_address_taken */
569 /* Return the name of the address_taken state. */
570 const char *get_address_taken_state_name(ir_address_taken_state state) {
571 #define X(a) case a: return #a
573 X(ir_address_not_taken);
574 X(ir_address_taken_unknown);
576 default: return "BAD VALUE";
579 } /* get_address_taken_state_name */
581 /* Get the entity's stickyness */
583 (get_entity_stickyness)(const ir_entity *ent) {
584 return _get_entity_stickyness(ent);
585 } /* get_entity_stickyness */
587 /* Set the entity's stickyness */
589 (set_entity_stickyness)(ir_entity *ent, ir_stickyness stickyness) {
590 _set_entity_stickyness(ent, stickyness);
591 } /* set_entity_stickyness */
593 /* Set has no effect for existent entities of type method. */
595 get_atomic_ent_value(ir_entity *ent)
597 assert(ent && is_atomic_entity(ent));
598 assert(ent->variability != variability_uninitialized);
599 return skip_Id(ent->value);
600 } /* get_atomic_ent_value */
603 set_atomic_ent_value(ir_entity *ent, ir_node *val) {
604 assert(is_atomic_entity(ent) && (ent->variability != variability_uninitialized));
605 if (is_Method_type(ent->type) && (ent->peculiarity == peculiarity_existent))
607 assert(get_irn_mode(val) == get_type_mode(ent->type));
609 } /* set_atomic_ent_value */
611 /* Returns true if the the node is representable as code on
613 int is_irn_const_expression(ir_node *n) {
616 /* we are in danger iff an exception will arise. TODO: be more precisely,
617 * for instance Div. will NOT rise if divisor != 0
619 if (is_binop(n) && !is_fragile_op(n))
620 return is_irn_const_expression(get_binop_left(n)) && is_irn_const_expression(get_binop_right(n));
623 switch (get_irn_opcode(n)) {
630 return is_irn_const_expression(get_irn_n(n, 0));
635 } /* is_irn_const_expression */
638 * Copies a firm subgraph that complies to the restrictions for
639 * constant expressions to current_block in current_ir_graph.
641 ir_node *copy_const_value(dbg_info *dbg, ir_node *n) {
645 /* @@@ GL I think we should implement this using the routines from irgopt for
646 dead node elimination/inlineing. */
649 switch (get_irn_opcode(n)) {
651 nn = new_d_Const_type(dbg, m, get_Const_tarval(n), get_Const_type(n));
654 nn = new_d_SymConst_type(dbg, get_SymConst_symbol(n), get_SymConst_kind(n),
655 get_SymConst_value_type(n));
658 nn = new_d_Add(dbg, copy_const_value(dbg, get_Add_left(n)),
659 copy_const_value(dbg, get_Add_right(n)), m); break;
661 nn = new_d_Sub(dbg, copy_const_value(dbg, get_Sub_left(n)),
662 copy_const_value(dbg, get_Sub_right(n)), m); break;
664 nn = new_d_Mul(dbg, copy_const_value(dbg, get_Mul_left(n)),
665 copy_const_value(dbg, get_Mul_right(n)), m); break;
667 nn = new_d_And(dbg, copy_const_value(dbg, get_And_left(n)),
668 copy_const_value(dbg, get_And_right(n)), m); break;
670 nn = new_d_Or(dbg, copy_const_value(dbg, get_Or_left(n)),
671 copy_const_value(dbg, get_Or_right(n)), m); break;
673 nn = new_d_Eor(dbg, copy_const_value(dbg, get_Eor_left(n)),
674 copy_const_value(dbg, get_Eor_right(n)), m); break;
676 nn = new_d_Cast(dbg, copy_const_value(dbg, get_Cast_op(n)), get_Cast_type(n)); break;
678 nn = new_d_Conv(dbg, copy_const_value(dbg, get_Conv_op(n)), m); break;
680 nn = new_d_Unknown(m); break;
682 assert(0 && "opcode invalid or not implemented");
687 } /* copy_const_value */
689 /* Creates a new compound graph path. */
690 compound_graph_path *
691 new_compound_graph_path(ir_type *tp, int length) {
692 compound_graph_path *res;
694 assert(is_compound_type(tp));
697 res = xmalloc(sizeof(*res) + (length-1) * sizeof(res->list[0]));
698 memset(res, 0, sizeof(*res) + (length-1) * sizeof(res->list[0]));
699 res->kind = k_ir_compound_graph_path;
704 } /* new_compound_graph_path */
706 /* Frees an graph path object */
707 void free_compound_graph_path (compound_graph_path *gr) {
708 assert(gr && is_compound_graph_path(gr));
711 } /* free_compound_graph_path */
713 /* Returns non-zero if an object is a compound graph path */
714 int is_compound_graph_path(const void *thing) {
715 return (get_kind(thing) == k_ir_compound_graph_path);
716 } /* is_compound_graph_path */
718 /* Checks whether the path up to pos is correct. If the path contains a NULL,
719 * assumes the path is not complete and returns 'true'. */
720 int is_proper_compound_graph_path(compound_graph_path *gr, int pos) {
723 ir_type *owner = gr->tp;
725 for (i = 0; i <= pos; i++) {
726 node = get_compound_graph_path_node(gr, i);
728 /* Path not yet complete. */
730 if (get_entity_owner(node) != owner)
732 owner = get_entity_type(node);
734 if (pos == get_compound_graph_path_length(gr))
735 if (!is_atomic_type(owner))
738 } /* is_proper_compound_graph_path */
740 /* Returns the length of a graph path */
741 int get_compound_graph_path_length(const compound_graph_path *gr) {
742 assert(gr && is_compound_graph_path(gr));
744 } /* get_compound_graph_path_length */
747 get_compound_graph_path_node(const compound_graph_path *gr, int pos) {
748 assert(gr && is_compound_graph_path(gr));
749 assert(pos >= 0 && pos < gr->len);
750 return gr->list[pos].node;
751 } /* get_compound_graph_path_node */
754 set_compound_graph_path_node(compound_graph_path *gr, int pos, ir_entity *node) {
755 assert(gr && is_compound_graph_path(gr));
756 assert(pos >= 0 && pos < gr->len);
757 assert(is_entity(node));
758 gr->list[pos].node = node;
759 assert(is_proper_compound_graph_path(gr, pos));
760 } /* set_compound_graph_path_node */
763 get_compound_graph_path_array_index(const compound_graph_path *gr, int pos) {
764 assert(gr && is_compound_graph_path(gr));
765 assert(pos >= 0 && pos < gr->len);
766 return gr->list[pos].index;
767 } /* get_compound_graph_path_array_index */
770 set_compound_graph_path_array_index(compound_graph_path *gr, int pos, int index) {
771 assert(gr && is_compound_graph_path(gr));
772 assert(pos >= 0 && pos < gr->len);
773 gr->list[pos].index = index;
774 } /* set_compound_graph_path_array_index */
776 /* A value of a compound entity is a pair of value and the corresponding path to a member of
779 add_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path) {
780 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
781 assert(is_compound_graph_path(path));
782 ARR_APP1(ir_node *, ent->attr.cmpd_attr.values, val);
783 ARR_APP1(compound_graph_path *, ent->attr.cmpd_attr.val_paths, path);
784 } /* add_compound_ent_value_w_path */
787 set_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path, int pos) {
788 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
789 assert(is_compound_graph_path(path));
790 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
791 ent->attr.cmpd_attr.values[pos] = val;
792 ent->attr.cmpd_attr.val_paths[pos] = path;
793 } /* set_compound_ent_value_w_path */
796 get_compound_ent_n_values(ir_entity *ent) {
797 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
798 return ARR_LEN(ent->attr.cmpd_attr.values);
799 } /* get_compound_ent_n_values */
802 get_compound_ent_value(ir_entity *ent, int pos) {
803 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
804 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
805 return ent->attr.cmpd_attr.values[pos];
806 } /* get_compound_ent_value */
808 compound_graph_path *
809 get_compound_ent_value_path(ir_entity *ent, int pos) {
810 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
811 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.val_paths));
812 return ent->attr.cmpd_attr.val_paths[pos];
813 } /* get_compound_ent_value_path */
816 * Returns non-zero, if two compound_graph_pathes are equal
818 * @param path1 the first path
819 * @param visited_indices
820 * @param path2 the second path
822 static int equal_paths(compound_graph_path *path1, int *visited_indices, compound_graph_path *path2) {
824 int len1 = get_compound_graph_path_length(path1);
825 int len2 = get_compound_graph_path_length(path2);
827 if (len2 != len1) return 0;
829 for (i = 0; i < len1; i++) {
831 ir_entity *node1 = get_compound_graph_path_node(path1, i);
832 ir_entity *node2 = get_compound_graph_path_node(path2, i);
834 if (node1 != node2) return 0;
836 /* FIXME: Strange code. What is it good for? */
837 tp = get_entity_owner(node1);
838 if (is_Array_type(tp)) {
841 /* Compute the index of this node. */
842 assert(get_array_n_dimensions(tp) == 1 && "multidim not implemented");
844 low = get_array_lower_bound_int(tp, 0);
845 if (low + visited_indices[i] < get_compound_graph_path_array_index(path2, i)) {
846 visited_indices[i]++;
849 assert(low + visited_indices[i] == get_compound_graph_path_array_index(path2, i));
856 * Returns the position of a value with the given path.
857 * The path must contain array indices for all array element entities.
859 * @todo This implementation is very slow (O(number of initializers^2) and should
860 * be replaced when the new tree oriented
861 * value representation is finally implemented.
863 static int get_compound_ent_pos_by_path(ir_entity *ent, compound_graph_path *path) {
864 int i, n_paths = get_compound_ent_n_values(ent);
865 int *visited_indices;
866 int path_len = get_compound_graph_path_length(path);
868 NEW_ARR_A(int *, visited_indices, path_len);
869 memset(visited_indices, 0, sizeof(*visited_indices) * path_len);
870 for (i = 0; i < n_paths; i ++) {
871 if (equal_paths(get_compound_ent_value_path(ent, i), visited_indices, path))
878 printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
879 printf("Entity %s : ", get_entity_name(ent));
880 for (j = 0; j < get_compound_graph_path_length(path); ++j) {
881 ir_entity *node = get_compound_graph_path_node(path, j);
882 printf("%s", get_entity_name(node));
883 if (is_Array_type(get_entity_owner(node)))
884 printf("[%d]", get_compound_graph_path_array_index(path, j));
886 printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
890 assert(0 && "path not found");
892 } /* get_compound_ent_pos_by_path */
894 /* Returns a constant value given the access path.
895 * The path must contain array indices for all array element entities. */
896 ir_node *get_compound_ent_value_by_path(ir_entity *ent, compound_graph_path *path) {
897 return get_compound_ent_value(ent, get_compound_ent_pos_by_path(ent, path));
898 } /* get_compound_ent_value_by_path */
902 remove_compound_ent_value(ir_entity *ent, ir_entity *value_ent) {
904 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
906 n = ARR_LEN(ent->attr.cmpd_attr.val_paths);
907 for (i = 0; i < n; ++i) {
908 compound_graph_path *path = ent->attr.cmpd_attr.val_paths[i];
909 if (path->list[path->len-1].node == value_ent) {
910 for (; i < n - 1; ++i) {
911 ent->attr.cmpd_attr.val_paths[i] = ent->attr.cmpd_attr.val_paths[i+1];
912 ent->attr.cmpd_attr.values[i] = ent->attr.cmpd_attr.values[i+1];
914 ARR_SETLEN(ir_entity*, ent->attr.cmpd_attr.val_paths, n - 1);
915 ARR_SETLEN(ir_node*, ent->attr.cmpd_attr.values, n - 1);
919 } /* remove_compound_ent_value */
922 add_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member) {
923 compound_graph_path *path;
924 ir_type *owner_tp = get_entity_owner(member);
925 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
926 path = new_compound_graph_path(get_entity_type(ent), 1);
927 path->list[0].node = member;
928 if (is_Array_type(owner_tp)) {
932 assert(get_array_n_dimensions(owner_tp) == 1 && has_array_lower_bound(owner_tp, 0));
933 max = get_array_lower_bound_int(owner_tp, 0) -1;
934 for (i = 0, n = get_compound_ent_n_values(ent); i < n; ++i) {
935 int index = get_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0);
940 path->list[0].index = max + 1;
942 add_compound_ent_value_w_path(ent, val, path);
943 } /* add_compound_ent_value */
947 get_compound_ent_value_member(ir_entity *ent, int pos) {
948 compound_graph_path *path;
949 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
950 path = get_compound_ent_value_path(ent, pos);
952 return get_compound_graph_path_node(path, get_compound_graph_path_length(path)-1);
953 } /* get_compound_ent_value_member */
956 set_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member, int pos) {
957 compound_graph_path *path;
958 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
959 path = get_compound_ent_value_path(ent, pos);
960 set_compound_graph_path_node(path, 0, member);
961 set_compound_ent_value_w_path(ent, val, path, pos);
962 } /* set_compound_ent_value */
965 set_array_entity_values(ir_entity *ent, tarval **values, int num_vals) {
967 ir_graph *rem = current_ir_graph;
968 ir_type *arrtp = get_entity_type(ent);
970 ir_type *elttp = get_array_element_type(arrtp);
972 assert(is_Array_type(arrtp));
973 assert(get_array_n_dimensions(arrtp) == 1);
974 /* One bound is sufficient, the number of constant fields makes the
976 assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
977 assert(get_entity_variability(ent) != variability_uninitialized);
978 current_ir_graph = get_const_code_irg();
980 for (i = 0; i < num_vals; i++) {
981 val = new_Const_type(values[i], elttp);
982 add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
983 set_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0, i);
985 current_ir_graph = rem;
986 } /* set_array_entity_values */
988 /* Return the overall offset of value at position pos in bytes. */
989 unsigned get_compound_ent_value_offset_bytes(ir_entity *ent, int pos) {
990 compound_graph_path *path;
995 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
997 path = get_compound_ent_value_path(ent, pos);
998 path_len = get_compound_graph_path_length(path);
1001 for (i = 0; i < path_len; ++i) {
1002 if (is_Array_type(curr_tp)) {
1003 ir_type *elem_type = get_array_element_type(curr_tp);
1004 unsigned size = get_type_size_bytes(elem_type);
1005 unsigned align = get_type_alignment_bytes(elem_type);
1009 if(size % align > 0) {
1010 size += align - (size % align);
1012 idx = get_compound_graph_path_array_index(path, i);
1014 offset += size * idx;
1015 curr_tp = elem_type;
1017 ir_entity *node = get_compound_graph_path_node(path, i);
1018 offset += get_entity_offset(node);
1019 curr_tp = get_entity_type(node);
1024 } /* get_compound_ent_value_offset_bytes */
1026 /* Return the offset in bits from the last byte address. */
1027 unsigned get_compound_ent_value_offset_bit_remainder(ir_entity *ent, int pos) {
1028 compound_graph_path *path;
1030 ir_entity *last_node;
1032 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
1034 path = get_compound_ent_value_path(ent, pos);
1035 path_len = get_compound_graph_path_length(path);
1036 last_node = get_compound_graph_path_node(path, path_len - 1);
1038 if(last_node == NULL)
1041 return get_entity_offset_bits_remainder(last_node);
1042 } /* get_compound_ent_value_offset_bit_remainder */
1045 (get_entity_offset)(const ir_entity *ent) {
1046 return _get_entity_offset(ent);
1047 } /* get_entity_offset */
1050 (set_entity_offset)(ir_entity *ent, int offset) {
1051 _set_entity_offset(ent, offset);
1052 } /* set_entity_offset */
1055 (get_entity_offset_bits_remainder)(const ir_entity *ent) {
1056 return _get_entity_offset_bits_remainder(ent);
1057 } /* get_entity_offset_bits_remainder */
1060 (set_entity_offset_bits_remainder)(ir_entity *ent, unsigned char offset) {
1061 _set_entity_offset_bits_remainder(ent, offset);
1062 } /* set_entity_offset_bits_remainder */
1065 add_entity_overwrites(ir_entity *ent, ir_entity *overwritten) {
1067 ir_type *owner = get_entity_owner(ent);
1068 ir_type *ovw_ovner = get_entity_owner(overwritten);
1069 assert(is_Class_type(owner));
1070 assert(is_Class_type(ovw_ovner));
1071 assert(! is_class_final(ovw_ovner));
1073 ARR_APP1(ir_entity *, ent->overwrites, overwritten);
1074 ARR_APP1(ir_entity *, overwritten->overwrittenby, ent);
1075 } /* add_entity_overwrites */
1078 get_entity_n_overwrites(ir_entity *ent) {
1079 assert(is_Class_type(get_entity_owner(ent)));
1080 return (ARR_LEN(ent->overwrites));
1081 } /* get_entity_n_overwrites */
1084 get_entity_overwrites_index(ir_entity *ent, ir_entity *overwritten) {
1086 assert(is_Class_type(get_entity_owner(ent)));
1087 n = get_entity_n_overwrites(ent);
1088 for (i = 0; i < n; ++i) {
1089 if (get_entity_overwrites(ent, i) == overwritten)
1093 } /* get_entity_overwrites_index */
1096 get_entity_overwrites(ir_entity *ent, int pos) {
1097 assert(is_Class_type(get_entity_owner(ent)));
1098 assert(pos < get_entity_n_overwrites(ent));
1099 return ent->overwrites[pos];
1100 } /* get_entity_overwrites */
1103 set_entity_overwrites(ir_entity *ent, int pos, ir_entity *overwritten) {
1104 assert(is_Class_type(get_entity_owner(ent)));
1105 assert(pos < get_entity_n_overwrites(ent));
1106 ent->overwrites[pos] = overwritten;
1107 } /* set_entity_overwrites */
1110 remove_entity_overwrites(ir_entity *ent, ir_entity *overwritten) {
1112 assert(is_Class_type(get_entity_owner(ent)));
1113 n = ARR_LEN(ent->overwrites);
1114 for (i = 0; i < n; ++i) {
1115 if (ent->overwrites[i] == overwritten) {
1116 for (; i < n - 1; i++)
1117 ent->overwrites[i] = ent->overwrites[i+1];
1118 ARR_SETLEN(ir_entity*, ent->overwrites, n - 1);
1122 } /* remove_entity_overwrites */
1125 add_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites) {
1126 add_entity_overwrites(overwrites, ent);
1127 } /* add_entity_overwrittenby */
1130 get_entity_n_overwrittenby(ir_entity *ent) {
1131 assert(is_Class_type(get_entity_owner(ent)));
1132 return ARR_LEN(ent->overwrittenby);
1133 } /* get_entity_n_overwrittenby */
1136 get_entity_overwrittenby_index(ir_entity *ent, ir_entity *overwrites) {
1138 assert(is_Class_type(get_entity_owner(ent)));
1139 n = get_entity_n_overwrittenby(ent);
1140 for (i = 0; i < n; ++i) {
1141 if (get_entity_overwrittenby(ent, i) == overwrites)
1145 } /* get_entity_overwrittenby_index */
1148 get_entity_overwrittenby(ir_entity *ent, int pos) {
1149 assert(is_Class_type(get_entity_owner(ent)));
1150 assert(pos < get_entity_n_overwrittenby(ent));
1151 return ent->overwrittenby[pos];
1152 } /* get_entity_overwrittenby */
1155 set_entity_overwrittenby(ir_entity *ent, int pos, ir_entity *overwrites) {
1156 assert(is_Class_type(get_entity_owner(ent)));
1157 assert(pos < get_entity_n_overwrittenby(ent));
1158 ent->overwrittenby[pos] = overwrites;
1159 } /* set_entity_overwrittenby */
1161 void remove_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites) {
1163 assert(is_Class_type(get_entity_owner(ent)));
1165 n = ARR_LEN(ent->overwrittenby);
1166 for (i = 0; i < n; ++i) {
1167 if (ent->overwrittenby[i] == overwrites) {
1168 for(; i < n - 1; ++i)
1169 ent->overwrittenby[i] = ent->overwrittenby[i+1];
1170 ARR_SETLEN(ir_entity*, ent->overwrittenby, n - 1);
1174 } /* remove_entity_overwrittenby */
1176 /* A link to store intermediate information */
1178 (get_entity_link)(const ir_entity *ent) {
1179 return _get_entity_link(ent);
1180 } /* get_entity_link */
1183 (set_entity_link)(ir_entity *ent, void *l) {
1184 _set_entity_link(ent, l);
1185 } /* set_entity_link */
1188 (get_entity_irg)(const ir_entity *ent) {
1189 return _get_entity_irg(ent);
1190 } /* get_entity_irg */
1193 set_entity_irg(ir_entity *ent, ir_graph *irg) {
1194 assert(is_method_entity(ent));
1195 /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
1196 * Methode selbst nicht mehr aufgerufen werden kann, die Entität
1197 * aber erhalten bleiben soll? Wandle die Entitaet in description oder
1200 assert((irg && ent->peculiarity == peculiarity_existent) ||
1201 (!irg && (ent->peculiarity == peculiarity_existent)
1202 && (ent -> visibility == visibility_external_allocated)) ||
1203 (!irg && ent->peculiarity == peculiarity_description) ||
1204 (!irg && ent->peculiarity == peculiarity_inherited));
1205 ent->attr.mtd_attr.irg = irg;
1206 } /* set_entity_irg */
1208 unsigned get_entity_vtable_number(const ir_entity *ent) {
1209 assert(is_method_entity((ir_entity *)ent));
1210 return ent->attr.mtd_attr.vtable_number;
1211 } /* get_entity_vtable_number */
1213 void set_entity_vtable_number(ir_entity *ent, unsigned vtable_number) {
1214 assert(is_method_entity(ent));
1215 ent->attr.mtd_attr.vtable_number = vtable_number;
1216 } /* set_entity_vtable_number */
1218 /* Returns the section of a method. */
1219 ir_img_section get_method_img_section(const ir_entity *ent) {
1220 assert(is_method_entity((ir_entity *)ent));
1221 return ent->attr.mtd_attr.section;
1222 } /* get_method_img_section */
1224 /* Sets the section of a method. */
1225 void set_method_img_section(ir_entity *ent, ir_img_section section) {
1226 assert(is_method_entity(ent));
1227 ent->attr.mtd_attr.section = section;
1228 } /* set_method_img_section */
1231 (is_entity)(const void *thing) {
1232 return _is_entity(thing);
1235 int is_atomic_entity(ir_entity *ent) {
1236 ir_type *t = get_entity_type(ent);
1237 const tp_op *op = get_type_tpop(t);
1238 return (op == type_primitive || op == type_pointer ||
1239 op == type_enumeration || op == type_method);
1240 } /* is_atomic_entity */
1242 int is_compound_entity(ir_entity *ent) {
1243 ir_type *t = get_entity_type(ent);
1244 const tp_op *op = get_type_tpop(t);
1245 return (op == type_class || op == type_struct ||
1246 op == type_array || op == type_union);
1247 } /* is_compound_entity */
1249 int is_method_entity(ir_entity *ent) {
1250 ir_type *t = get_entity_type(ent);
1251 return is_Method_type(t);
1252 } /* is_method_entity */
1255 * @todo not implemented!!! */
1256 int equal_entity(ir_entity *ent1, ir_entity *ent2) {
1259 fprintf(stderr, " calling unimplemented equal entity!!! \n");
1261 } /* equal_entity */
1264 unsigned long (get_entity_visited)(ir_entity *ent) {
1265 return _get_entity_visited(ent);
1266 } /* get_entity_visited */
1268 void (set_entity_visited)(ir_entity *ent, unsigned long num) {
1269 _set_entity_visited(ent, num);
1270 } /* set_entity_visited */
1272 /* Sets visited field in ir_entity to entity_visited. */
1273 void (mark_entity_visited)(ir_entity *ent) {
1274 _mark_entity_visited(ent);
1275 } /* mark_entity_visited */
1277 int (entity_visited)(ir_entity *ent) {
1278 return _entity_visited(ent);
1279 } /* entity_visited */
1281 int (entity_not_visited)(ir_entity *ent) {
1282 return _entity_not_visited(ent);
1283 } /* entity_not_visited */
1285 /* Returns the mask of the additional entity properties. */
1286 unsigned get_entity_additional_properties(ir_entity *ent) {
1289 assert(is_method_entity(ent));
1291 /* first check, if the graph has additional properties */
1292 irg = get_entity_irg(ent);
1295 return get_irg_additional_properties(irg);
1297 if (ent->attr.mtd_attr.irg_add_properties & mtp_property_inherited)
1298 return get_method_additional_properties(get_entity_type(ent));
1300 return ent->attr.mtd_attr.irg_add_properties;
1301 } /* get_entity_additional_properties */
1303 /* Sets the mask of the additional graph properties. */
1304 void set_entity_additional_properties(ir_entity *ent, unsigned property_mask)
1308 assert(is_method_entity(ent));
1310 /* first check, if the graph exists */
1311 irg = get_entity_irg(ent);
1313 set_irg_additional_properties(irg, property_mask);
1315 /* do not allow to set the mtp_property_inherited flag or
1316 * the automatic inheritance of flags will not work */
1317 ent->attr.mtd_attr.irg_add_properties = property_mask & ~mtp_property_inherited;
1319 } /* set_entity_additional_properties */
1321 /* Sets one additional graph property. */
1322 void set_entity_additional_property(ir_entity *ent, mtp_additional_property flag)
1326 assert(is_method_entity(ent));
1328 /* first check, if the graph exists */
1329 irg = get_entity_irg(ent);
1331 set_irg_additional_property(irg, flag);
1333 unsigned mask = ent->attr.mtd_attr.irg_add_properties;
1335 if (mask & mtp_property_inherited)
1336 mask = get_method_additional_properties(get_entity_type(ent));
1338 /* do not allow to set the mtp_property_inherited flag or
1339 * the automatic inheritance of flags will not work */
1340 ent->attr.mtd_attr.irg_add_properties = mask | (flag & ~mtp_property_inherited);
1342 } /* set_entity_additional_property */
1344 /* Returns the class type that this type info entity represents or NULL
1345 if ent is no type info entity. */
1346 ir_type *(get_entity_repr_class)(const ir_entity *ent) {
1347 return _get_entity_repr_class(ent);
1348 } /* get_entity_repr_class */
1350 dbg_info *(get_entity_dbg_info)(const ir_entity *ent) {
1351 return _get_entity_dbg_info(ent);
1352 } /* get_entity_dbg_info */
1354 void (set_entity_dbg_info)(ir_entity *ent, dbg_info *db) {
1355 _set_entity_dbg_info(ent, db);
1356 } /* set_entity_dbg_info */
1358 /* Initialize entity module. */
1359 void firm_init_entity(void)
1361 symconst_symbol sym;
1363 assert(firm_unknown_type && "Call init_type() before firm_init_entity()!");
1364 assert(!unknown_entity && "Call firm_init_entity() only once!");
1366 unknown_entity = new_rd_entity(NULL, firm_unknown_type, new_id_from_str(UNKNOWN_ENTITY_NAME), firm_unknown_type);
1367 set_entity_visibility(unknown_entity, visibility_external_allocated);
1368 set_entity_ld_ident(unknown_entity, get_entity_ident(unknown_entity));
1370 current_ir_graph = get_const_code_irg();
1371 sym.entity_p = unknown_entity;
1372 unknown_entity->value = new_SymConst(sym, symconst_addr_ent);
1373 } /* firm_init_entity */