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))
608 } /* set_atomic_ent_value */
610 /* Returns true if the the node is representable as code on
612 int is_irn_const_expression(ir_node *n) {
615 /* we are in danger iff an exception will arise. TODO: be more precisely,
616 * for instance Div. will NOT rise if divisor != 0
618 if (is_binop(n) && !is_fragile_op(n))
619 return is_irn_const_expression(get_binop_left(n)) && is_irn_const_expression(get_binop_right(n));
622 switch (get_irn_opcode(n)) {
629 return is_irn_const_expression(get_irn_n(n, 0));
634 } /* is_irn_const_expression */
637 * Copies a firm subgraph that complies to the restrictions for
638 * constant expressions to current_block in current_ir_graph.
640 ir_node *copy_const_value(dbg_info *dbg, ir_node *n) {
644 /* @@@ GL I think we should implement this using the routines from irgopt for
645 dead node elimination/inlineing. */
648 switch (get_irn_opcode(n)) {
650 nn = new_d_Const_type(dbg, m, get_Const_tarval(n), get_Const_type(n));
653 nn = new_d_SymConst_type(dbg, get_SymConst_symbol(n), get_SymConst_kind(n),
654 get_SymConst_value_type(n));
657 nn = new_d_Add(dbg, copy_const_value(dbg, get_Add_left(n)),
658 copy_const_value(dbg, get_Add_right(n)), m); break;
660 nn = new_d_Sub(dbg, copy_const_value(dbg, get_Sub_left(n)),
661 copy_const_value(dbg, get_Sub_right(n)), m); break;
663 nn = new_d_Mul(dbg, copy_const_value(dbg, get_Mul_left(n)),
664 copy_const_value(dbg, get_Mul_right(n)), m); break;
666 nn = new_d_And(dbg, copy_const_value(dbg, get_And_left(n)),
667 copy_const_value(dbg, get_And_right(n)), m); break;
669 nn = new_d_Or(dbg, copy_const_value(dbg, get_Or_left(n)),
670 copy_const_value(dbg, get_Or_right(n)), m); break;
672 nn = new_d_Eor(dbg, copy_const_value(dbg, get_Eor_left(n)),
673 copy_const_value(dbg, get_Eor_right(n)), m); break;
675 nn = new_d_Cast(dbg, copy_const_value(dbg, get_Cast_op(n)), get_Cast_type(n)); break;
677 nn = new_d_Conv(dbg, copy_const_value(dbg, get_Conv_op(n)), m); break;
679 nn = new_d_Unknown(m); break;
681 assert(0 && "opcode invalid or not implemented");
686 } /* copy_const_value */
688 /* Creates a new compound graph path. */
689 compound_graph_path *
690 new_compound_graph_path(ir_type *tp, int length) {
691 compound_graph_path *res;
693 assert(is_compound_type(tp));
696 res = xmalloc(sizeof(*res) + (length-1) * sizeof(res->list[0]));
697 memset(res, 0, sizeof(*res) + (length-1) * sizeof(res->list[0]));
698 res->kind = k_ir_compound_graph_path;
703 } /* new_compound_graph_path */
705 /* Frees an graph path object */
706 void free_compound_graph_path (compound_graph_path *gr) {
707 assert(gr && is_compound_graph_path(gr));
710 } /* free_compound_graph_path */
712 /* Returns non-zero if an object is a compound graph path */
713 int is_compound_graph_path(const void *thing) {
714 return (get_kind(thing) == k_ir_compound_graph_path);
715 } /* is_compound_graph_path */
717 /* Checks whether the path up to pos is correct. If the path contains a NULL,
718 * assumes the path is not complete and returns 'true'. */
719 int is_proper_compound_graph_path(compound_graph_path *gr, int pos) {
722 ir_type *owner = gr->tp;
724 for (i = 0; i <= pos; i++) {
725 node = get_compound_graph_path_node(gr, i);
727 /* Path not yet complete. */
729 if (get_entity_owner(node) != owner)
731 owner = get_entity_type(node);
733 if (pos == get_compound_graph_path_length(gr))
734 if (!is_atomic_type(owner))
737 } /* is_proper_compound_graph_path */
739 /* Returns the length of a graph path */
740 int get_compound_graph_path_length(const compound_graph_path *gr) {
741 assert(gr && is_compound_graph_path(gr));
743 } /* get_compound_graph_path_length */
746 get_compound_graph_path_node(const compound_graph_path *gr, int pos) {
747 assert(gr && is_compound_graph_path(gr));
748 assert(pos >= 0 && pos < gr->len);
749 return gr->list[pos].node;
750 } /* get_compound_graph_path_node */
753 set_compound_graph_path_node(compound_graph_path *gr, int pos, ir_entity *node) {
754 assert(gr && is_compound_graph_path(gr));
755 assert(pos >= 0 && pos < gr->len);
756 assert(is_entity(node));
757 gr->list[pos].node = node;
758 assert(is_proper_compound_graph_path(gr, pos));
759 } /* set_compound_graph_path_node */
762 get_compound_graph_path_array_index(const compound_graph_path *gr, int pos) {
763 assert(gr && is_compound_graph_path(gr));
764 assert(pos >= 0 && pos < gr->len);
765 return gr->list[pos].index;
766 } /* get_compound_graph_path_array_index */
769 set_compound_graph_path_array_index(compound_graph_path *gr, int pos, int index) {
770 assert(gr && is_compound_graph_path(gr));
771 assert(pos >= 0 && pos < gr->len);
772 gr->list[pos].index = index;
773 } /* set_compound_graph_path_array_index */
775 /* A value of a compound entity is a pair of value and the corresponding path to a member of
778 add_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path) {
779 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
780 assert(is_compound_graph_path(path));
781 ARR_APP1(ir_node *, ent->attr.cmpd_attr.values, val);
782 ARR_APP1(compound_graph_path *, ent->attr.cmpd_attr.val_paths, path);
783 } /* add_compound_ent_value_w_path */
786 set_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path, int pos) {
787 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
788 assert(is_compound_graph_path(path));
789 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
790 ent->attr.cmpd_attr.values[pos] = val;
791 ent->attr.cmpd_attr.val_paths[pos] = path;
792 } /* set_compound_ent_value_w_path */
795 get_compound_ent_n_values(ir_entity *ent) {
796 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
797 return ARR_LEN(ent->attr.cmpd_attr.values);
798 } /* get_compound_ent_n_values */
801 get_compound_ent_value(ir_entity *ent, int pos) {
802 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
803 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
804 return ent->attr.cmpd_attr.values[pos];
805 } /* get_compound_ent_value */
807 compound_graph_path *
808 get_compound_ent_value_path(ir_entity *ent, int pos) {
809 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
810 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.val_paths));
811 return ent->attr.cmpd_attr.val_paths[pos];
812 } /* get_compound_ent_value_path */
815 * Returns non-zero, if two compound_graph_pathes are equal
817 * @param path1 the first path
818 * @param visited_indices
819 * @param path2 the second path
821 static int equal_paths(compound_graph_path *path1, int *visited_indices, compound_graph_path *path2) {
823 int len1 = get_compound_graph_path_length(path1);
824 int len2 = get_compound_graph_path_length(path2);
826 if (len2 != len1) return 0;
828 for (i = 0; i < len1; i++) {
830 ir_entity *node1 = get_compound_graph_path_node(path1, i);
831 ir_entity *node2 = get_compound_graph_path_node(path2, i);
833 if (node1 != node2) return 0;
835 /* FIXME: Strange code. What is it good for? */
836 tp = get_entity_owner(node1);
837 if (is_Array_type(tp)) {
840 /* Compute the index of this node. */
841 assert(get_array_n_dimensions(tp) == 1 && "multidim not implemented");
843 low = get_array_lower_bound_int(tp, 0);
844 if (low + visited_indices[i] < get_compound_graph_path_array_index(path2, i)) {
845 visited_indices[i]++;
848 assert(low + visited_indices[i] == get_compound_graph_path_array_index(path2, i));
855 * Returns the position of a value with the given path.
856 * The path must contain array indices for all array element entities.
858 * @todo This implementation is very slow (O(number of initializers^2) and should
859 * be replaced when the new tree oriented
860 * value representation is finally implemented.
862 static int get_compound_ent_pos_by_path(ir_entity *ent, compound_graph_path *path) {
863 int i, n_paths = get_compound_ent_n_values(ent);
864 int *visited_indices;
865 int path_len = get_compound_graph_path_length(path);
867 NEW_ARR_A(int *, visited_indices, path_len);
868 memset(visited_indices, 0, sizeof(*visited_indices) * path_len);
869 for (i = 0; i < n_paths; i ++) {
870 if (equal_paths(get_compound_ent_value_path(ent, i), visited_indices, path))
877 printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
878 printf("Entity %s : ", get_entity_name(ent));
879 for (j = 0; j < get_compound_graph_path_length(path); ++j) {
880 ir_entity *node = get_compound_graph_path_node(path, j);
881 printf("%s", get_entity_name(node));
882 if (is_Array_type(get_entity_owner(node)))
883 printf("[%d]", get_compound_graph_path_array_index(path, j));
885 printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
889 assert(0 && "path not found");
891 } /* get_compound_ent_pos_by_path */
893 /* Returns a constant value given the access path.
894 * The path must contain array indices for all array element entities. */
895 ir_node *get_compound_ent_value_by_path(ir_entity *ent, compound_graph_path *path) {
896 return get_compound_ent_value(ent, get_compound_ent_pos_by_path(ent, path));
897 } /* get_compound_ent_value_by_path */
901 remove_compound_ent_value(ir_entity *ent, ir_entity *value_ent) {
903 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
904 for (i = 0; i < (ARR_LEN(ent->attr.cmpd_attr.val_paths)); ++i) {
905 compound_graph_path *path = ent->attr.cmpd_attr.val_paths[i];
906 if (path->list[path->len-1].node == value_ent) {
907 for (; i < (ARR_LEN(ent->attr.cmpd_attr.val_paths))-1; ++i) {
908 ent->attr.cmpd_attr.val_paths[i] = ent->attr.cmpd_attr.val_paths[i+1];
909 ent->attr.cmpd_attr.values[i] = ent->attr.cmpd_attr.values[i+1];
911 ARR_SETLEN(ir_entity*, ent->attr.cmpd_attr.val_paths, ARR_LEN(ent->attr.cmpd_attr.val_paths) - 1);
912 ARR_SETLEN(ir_node*, ent->attr.cmpd_attr.values, ARR_LEN(ent->attr.cmpd_attr.values) - 1);
916 } /* remove_compound_ent_value */
919 add_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member) {
920 compound_graph_path *path;
921 ir_type *owner_tp = get_entity_owner(member);
922 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
923 path = new_compound_graph_path(get_entity_type(ent), 1);
924 path->list[0].node = member;
925 if (is_Array_type(owner_tp)) {
929 assert(get_array_n_dimensions(owner_tp) == 1 && has_array_lower_bound(owner_tp, 0));
930 max = get_array_lower_bound_int(owner_tp, 0) -1;
931 for (i = 0, n = get_compound_ent_n_values(ent); i < n; ++i) {
932 int index = get_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0);
937 path->list[0].index = max + 1;
939 add_compound_ent_value_w_path(ent, val, path);
940 } /* add_compound_ent_value */
944 get_compound_ent_value_member(ir_entity *ent, int pos) {
945 compound_graph_path *path;
946 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
947 path = get_compound_ent_value_path(ent, pos);
949 return get_compound_graph_path_node(path, get_compound_graph_path_length(path)-1);
950 } /* get_compound_ent_value_member */
953 set_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member, int pos) {
954 compound_graph_path *path;
955 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
956 path = get_compound_ent_value_path(ent, pos);
957 set_compound_graph_path_node(path, 0, member);
958 set_compound_ent_value_w_path(ent, val, path, pos);
959 } /* set_compound_ent_value */
962 set_array_entity_values(ir_entity *ent, tarval **values, int num_vals) {
964 ir_graph *rem = current_ir_graph;
965 ir_type *arrtp = get_entity_type(ent);
967 ir_type *elttp = get_array_element_type(arrtp);
969 assert(is_Array_type(arrtp));
970 assert(get_array_n_dimensions(arrtp) == 1);
971 /* One bound is sufficient, the number of constant fields makes the
973 assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
974 assert(get_entity_variability(ent) != variability_uninitialized);
975 current_ir_graph = get_const_code_irg();
977 for (i = 0; i < num_vals; i++) {
978 val = new_Const_type(values[i], elttp);
979 add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
980 set_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0, i);
982 current_ir_graph = rem;
983 } /* set_array_entity_values */
985 /* Return the overall offset of value at position pos in bytes. */
986 int get_compound_ent_value_offset_bytes(ir_entity *ent, int pos) {
987 compound_graph_path *path;
992 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
994 path = get_compound_ent_value_path(ent, pos);
995 path_len = get_compound_graph_path_length(path);
998 for (i = 0; i < path_len; ++i) {
999 ir_entity *node = get_compound_graph_path_node(path, i);
1000 ir_type *node_tp = get_entity_type(node);
1002 if (is_Array_type(curr_tp)) {
1003 int size = get_type_size_bits(node_tp);
1004 int align = get_type_alignment_bits(node_tp);
1008 if(size % align > 0) {
1009 size += align - (size % align);
1011 assert(size % 8 == 0);
1013 idx = get_compound_graph_path_array_index(path, i);
1015 offset += size * idx;
1017 offset += get_entity_offset(node);
1023 } /* get_compound_ent_value_offset_bytes */
1025 /* Return the offset in bits from the last byte address. */
1026 int get_compound_ent_value_offset_bit_remainder(ir_entity *ent, int pos) {
1027 compound_graph_path *path;
1029 ir_entity *last_node;
1031 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
1033 path = get_compound_ent_value_path(ent, pos);
1034 path_len = get_compound_graph_path_length(path);
1035 last_node = get_compound_graph_path_node(path, path_len - 1);
1037 return get_entity_offset_bits_remainder(last_node);
1038 } /* get_compound_ent_value_offset_bit_remainder */
1041 (get_entity_offset)(const ir_entity *ent) {
1042 return _get_entity_offset(ent);
1043 } /* get_entity_offset */
1046 (set_entity_offset)(ir_entity *ent, int offset) {
1047 _set_entity_offset(ent, offset);
1048 } /* set_entity_offset */
1051 (get_entity_offset_bits_remainder)(const ir_entity *ent) {
1052 return _get_entity_offset_bits_remainder(ent);
1053 } /* get_entity_offset_bits_remainder */
1056 (set_entity_offset_bits_remainder)(ir_entity *ent, unsigned char offset) {
1057 _set_entity_offset_bits_remainder(ent, offset);
1058 } /* set_entity_offset_bits_remainder */
1061 add_entity_overwrites(ir_entity *ent, ir_entity *overwritten) {
1063 ir_type *owner = get_entity_owner(ent);
1064 ir_type *ovw_ovner = get_entity_owner(overwritten);
1065 assert(is_Class_type(owner));
1066 assert(is_Class_type(ovw_ovner));
1067 assert(! is_class_final(ovw_ovner));
1069 ARR_APP1(ir_entity *, ent->overwrites, overwritten);
1070 ARR_APP1(ir_entity *, overwritten->overwrittenby, ent);
1071 } /* add_entity_overwrites */
1074 get_entity_n_overwrites(ir_entity *ent) {
1075 assert(is_Class_type(get_entity_owner(ent)));
1076 return (ARR_LEN(ent->overwrites));
1077 } /* get_entity_n_overwrites */
1080 get_entity_overwrites_index(ir_entity *ent, ir_entity *overwritten) {
1082 assert(is_Class_type(get_entity_owner(ent)));
1083 for (i = 0; i < get_entity_n_overwrites(ent); i++)
1084 if (get_entity_overwrites(ent, i) == overwritten)
1087 } /* get_entity_overwrites_index */
1090 get_entity_overwrites(ir_entity *ent, int pos) {
1091 assert(is_Class_type(get_entity_owner(ent)));
1092 assert(pos < get_entity_n_overwrites(ent));
1093 return ent->overwrites[pos];
1094 } /* get_entity_overwrites */
1097 set_entity_overwrites(ir_entity *ent, int pos, ir_entity *overwritten) {
1098 assert(is_Class_type(get_entity_owner(ent)));
1099 assert(pos < get_entity_n_overwrites(ent));
1100 ent->overwrites[pos] = overwritten;
1101 } /* set_entity_overwrites */
1104 remove_entity_overwrites(ir_entity *ent, ir_entity *overwritten) {
1106 assert(is_Class_type(get_entity_owner(ent)));
1107 for (i = 0; i < (ARR_LEN (ent->overwrites)); i++)
1108 if (ent->overwrites[i] == overwritten) {
1109 for(; i < (ARR_LEN (ent->overwrites))-1; i++)
1110 ent->overwrites[i] = ent->overwrites[i+1];
1111 ARR_SETLEN(ir_entity*, ent->overwrites, ARR_LEN(ent->overwrites) - 1);
1114 } /* remove_entity_overwrites */
1117 add_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites) {
1118 add_entity_overwrites(overwrites, ent);
1119 } /* add_entity_overwrittenby */
1122 get_entity_n_overwrittenby(ir_entity *ent) {
1123 assert(is_Class_type(get_entity_owner(ent)));
1124 return (ARR_LEN (ent->overwrittenby));
1125 } /* get_entity_n_overwrittenby */
1128 get_entity_overwrittenby_index(ir_entity *ent, ir_entity *overwrites) {
1130 assert(is_Class_type(get_entity_owner(ent)));
1131 for (i = 0; i < get_entity_n_overwrittenby(ent); i++)
1132 if (get_entity_overwrittenby(ent, i) == overwrites)
1135 } /* get_entity_overwrittenby_index */
1138 get_entity_overwrittenby(ir_entity *ent, int pos) {
1139 assert(is_Class_type(get_entity_owner(ent)));
1140 assert(pos < get_entity_n_overwrittenby(ent));
1141 return ent->overwrittenby[pos];
1142 } /* get_entity_overwrittenby */
1145 set_entity_overwrittenby(ir_entity *ent, int pos, ir_entity *overwrites) {
1146 assert(is_Class_type(get_entity_owner(ent)));
1147 assert(pos < get_entity_n_overwrittenby(ent));
1148 ent->overwrittenby[pos] = overwrites;
1149 } /* set_entity_overwrittenby */
1151 void remove_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites) {
1153 assert(is_Class_type(get_entity_owner(ent)));
1154 for (i = 0; i < (ARR_LEN (ent->overwrittenby)); i++)
1155 if (ent->overwrittenby[i] == overwrites) {
1156 for(; i < (ARR_LEN (ent->overwrittenby))-1; i++)
1157 ent->overwrittenby[i] = ent->overwrittenby[i+1];
1158 ARR_SETLEN(ir_entity*, ent->overwrittenby, ARR_LEN(ent->overwrittenby) - 1);
1161 } /* remove_entity_overwrittenby */
1163 /* A link to store intermediate information */
1165 (get_entity_link)(const ir_entity *ent) {
1166 return _get_entity_link(ent);
1167 } /* get_entity_link */
1170 (set_entity_link)(ir_entity *ent, void *l) {
1171 _set_entity_link(ent, l);
1172 } /* set_entity_link */
1175 (get_entity_irg)(const ir_entity *ent) {
1176 return _get_entity_irg(ent);
1177 } /* get_entity_irg */
1180 set_entity_irg(ir_entity *ent, ir_graph *irg) {
1181 assert(is_method_entity(ent));
1182 /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
1183 * Methode selbst nicht mehr aufgerufen werden kann, die Entität
1184 * aber erhalten bleiben soll? Wandle die Entitaet in description oder
1187 assert((irg && ent->peculiarity == peculiarity_existent) ||
1188 (!irg && (ent->peculiarity == peculiarity_existent)
1189 && (ent -> visibility == visibility_external_allocated)) ||
1190 (!irg && ent->peculiarity == peculiarity_description) ||
1191 (!irg && ent->peculiarity == peculiarity_inherited));
1192 ent->attr.mtd_attr.irg = irg;
1193 } /* set_entity_irg */
1195 unsigned get_entity_vtable_number(const ir_entity *ent) {
1196 assert(is_method_entity((ir_entity *)ent));
1197 return ent->attr.mtd_attr.vtable_number;
1198 } /* get_entity_vtable_number */
1200 void set_entity_vtable_number(ir_entity *ent, unsigned vtable_number) {
1201 assert(is_method_entity(ent));
1202 ent->attr.mtd_attr.vtable_number = vtable_number;
1203 } /* set_entity_vtable_number */
1205 /* Returns the section of a method. */
1206 ir_img_section get_method_img_section(const ir_entity *ent) {
1207 assert(is_method_entity((ir_entity *)ent));
1208 return ent->attr.mtd_attr.section;
1209 } /* get_method_img_section */
1211 /* Sets the section of a method. */
1212 void set_method_img_section(ir_entity *ent, ir_img_section section) {
1213 assert(is_method_entity(ent));
1214 ent->attr.mtd_attr.section = section;
1215 } /* set_method_img_section */
1218 (is_entity)(const void *thing) {
1219 return _is_entity(thing);
1222 int is_atomic_entity(ir_entity *ent) {
1223 ir_type *t = get_entity_type(ent);
1224 const tp_op *op = get_type_tpop(t);
1225 return (op == type_primitive || op == type_pointer ||
1226 op == type_enumeration || op == type_method);
1227 } /* is_atomic_entity */
1229 int is_compound_entity(ir_entity *ent) {
1230 ir_type *t = get_entity_type(ent);
1231 const tp_op *op = get_type_tpop(t);
1232 return (op == type_class || op == type_struct ||
1233 op == type_array || op == type_union);
1234 } /* is_compound_entity */
1236 int is_method_entity(ir_entity *ent) {
1237 ir_type *t = get_entity_type(ent);
1238 return is_Method_type(t);
1239 } /* is_method_entity */
1242 * @todo not implemented!!! */
1243 int equal_entity(ir_entity *ent1, ir_entity *ent2) {
1246 fprintf(stderr, " calling unimplemented equal entity!!! \n");
1248 } /* equal_entity */
1251 unsigned long (get_entity_visited)(ir_entity *ent) {
1252 return _get_entity_visited(ent);
1253 } /* get_entity_visited */
1255 void (set_entity_visited)(ir_entity *ent, unsigned long num) {
1256 _set_entity_visited(ent, num);
1257 } /* set_entity_visited */
1259 /* Sets visited field in ir_entity to entity_visited. */
1260 void (mark_entity_visited)(ir_entity *ent) {
1261 _mark_entity_visited(ent);
1262 } /* mark_entity_visited */
1264 int (entity_visited)(ir_entity *ent) {
1265 return _entity_visited(ent);
1266 } /* entity_visited */
1268 int (entity_not_visited)(ir_entity *ent) {
1269 return _entity_not_visited(ent);
1270 } /* entity_not_visited */
1272 /* Returns the mask of the additional entity properties. */
1273 unsigned get_entity_additional_properties(ir_entity *ent) {
1276 assert(is_method_entity(ent));
1278 /* first check, if the graph has additional properties */
1279 irg = get_entity_irg(ent);
1282 return get_irg_additional_properties(irg);
1284 if (ent->attr.mtd_attr.irg_add_properties & mtp_property_inherited)
1285 return get_method_additional_properties(get_entity_type(ent));
1287 return ent->attr.mtd_attr.irg_add_properties;
1288 } /* get_entity_additional_properties */
1290 /* Sets the mask of the additional graph properties. */
1291 void set_entity_additional_properties(ir_entity *ent, unsigned property_mask)
1295 assert(is_method_entity(ent));
1297 /* first check, if the graph exists */
1298 irg = get_entity_irg(ent);
1300 set_irg_additional_properties(irg, property_mask);
1302 /* do not allow to set the mtp_property_inherited flag or
1303 * the automatic inheritance of flags will not work */
1304 ent->attr.mtd_attr.irg_add_properties = property_mask & ~mtp_property_inherited;
1306 } /* set_entity_additional_properties */
1308 /* Sets one additional graph property. */
1309 void set_entity_additional_property(ir_entity *ent, mtp_additional_property flag)
1313 assert(is_method_entity(ent));
1315 /* first check, if the graph exists */
1316 irg = get_entity_irg(ent);
1318 set_irg_additional_property(irg, flag);
1320 unsigned mask = ent->attr.mtd_attr.irg_add_properties;
1322 if (mask & mtp_property_inherited)
1323 mask = get_method_additional_properties(get_entity_type(ent));
1325 /* do not allow to set the mtp_property_inherited flag or
1326 * the automatic inheritance of flags will not work */
1327 ent->attr.mtd_attr.irg_add_properties = mask | (flag & ~mtp_property_inherited);
1329 } /* set_entity_additional_property */
1331 /* Returns the class type that this type info entity represents or NULL
1332 if ent is no type info entity. */
1333 ir_type *(get_entity_repr_class)(const ir_entity *ent) {
1334 return _get_entity_repr_class(ent);
1335 } /* get_entity_repr_class */
1337 dbg_info *(get_entity_dbg_info)(const ir_entity *ent) {
1338 return _get_entity_dbg_info(ent);
1339 } /* get_entity_dbg_info */
1341 void (set_entity_dbg_info)(ir_entity *ent, dbg_info *db) {
1342 _set_entity_dbg_info(ent, db);
1343 } /* set_entity_dbg_info */
1345 /* Initialize entity module. */
1346 void firm_init_entity(void)
1348 symconst_symbol sym;
1350 assert(firm_unknown_type && "Call init_type() before firm_init_entity()!");
1351 assert(!unknown_entity && "Call firm_init_entity() only once!");
1353 unknown_entity = new_rd_entity(NULL, firm_unknown_type, new_id_from_str(UNKNOWN_ENTITY_NAME), firm_unknown_type);
1354 set_entity_visibility(unknown_entity, visibility_external_allocated);
1355 set_entity_ld_ident(unknown_entity, get_entity_ident(unknown_entity));
1357 current_ir_graph = get_const_code_irg();
1358 sym.entity_p = unknown_entity;
1359 unknown_entity->value = new_SymConst(sym, symconst_addr_ent);
1360 } /* firm_init_entity */