2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Representation of all program known entities.
23 * @author Martin Trapp, Christian Schaefer, Goetz Lindenmaier, Michael Beck
32 #include "firm_common_t.h"
45 #include "irgraph_t.h"
46 #include "callgraph.h"
49 /*-----------------------------------------------------------------*/
51 /*-----------------------------------------------------------------*/
53 ir_entity *unknown_entity = NULL;
55 ir_entity *get_unknown_entity(void) { return unknown_entity; }
57 /** The name of the unknown entity. */
58 #define UNKNOWN_ENTITY_NAME "unknown_entity"
60 /*-----------------------------------------------------------------*/
62 /*-----------------------------------------------------------------*/
65 * Add an entity to it's already set owner type.
67 static inline void insert_entity_in_owner(ir_entity *ent) {
68 ir_type *owner = ent->owner;
69 switch (get_type_tpop_code(owner)) {
71 add_class_member(owner, ent);
74 add_struct_member(owner, ent);
77 add_union_member(owner, ent);
80 set_array_element_entity(owner, ent);
83 panic("Unsupported type kind");
85 } /* insert_entity_in_owner */
88 * Creates a new entity. This entity is NOT inserted in the owner type.
90 * @param db debug info for this entity
91 * @param owner the owner type of the new entity
92 * @param name the name of the new entity
93 * @param type the type of the new entity
95 * @return the new created entity
97 static inline ir_entity *
98 new_rd_entity(dbg_info *db, ir_type *owner, ident *name, ir_type *type)
103 assert(!id_contains_char(name, ' ') && "entity name should not contain spaces");
105 res = XMALLOCZ(ir_entity);
107 res->kind = k_entity;
113 res->allocation = allocation_automatic;
114 res->visibility = visibility_local;
115 res->volatility = volatility_non_volatile;
116 res->align = align_is_aligned;
117 res->stickyness = stickyness_unsticky;
118 res->peculiarity = peculiarity_existent;
119 res->usage = ir_usage_unknown;
121 res->compiler_gen = 0;
122 res->backend_marked = 0;
124 res->offset_bit_remainder = 0;
126 res->repr_class = NULL;
128 if (is_Method_type(type)) {
130 ir_mode *mode = is_Method_type(type) ? mode_P_code : mode_P_data;
132 rem = current_ir_graph;
133 current_ir_graph = get_const_code_irg();
134 res->value = new_SymConst(mode, sym, symconst_addr_ent);
135 current_ir_graph = rem;
136 res->allocation = allocation_static;
137 res->variability = variability_constant;
138 res->attr.mtd_attr.irg_add_properties = mtp_property_inherited;
139 res->attr.mtd_attr.vtable_number = VTABLE_NUM_NOT_SET;
140 res->attr.mtd_attr.param_access = NULL;
141 res->attr.mtd_attr.param_weight = NULL;
142 res->attr.mtd_attr.irg = NULL;
143 } else if (is_compound_type(type)) {
144 res->variability = variability_uninitialized;
146 res->attr.cmpd_attr.values = NULL;
147 res->attr.cmpd_attr.val_paths = NULL;
149 res->variability = variability_uninitialized;
153 if (is_Class_type(owner)) {
154 res->overwrites = NEW_ARR_F(ir_entity *, 0);
155 res->overwrittenby = NEW_ARR_F(ir_entity *, 0);
157 res->overwrites = NULL;
158 res->overwrittenby = NULL;
162 res->nr = get_irp_new_node_nr();
163 #endif /* DEBUG_libfirm */
166 set_entity_dbg_info(res, db);
169 } /* new_rd_entity */
172 new_d_entity(ir_type *owner, ident *name, ir_type *type, dbg_info *db) {
175 assert(is_compound_type(owner));
176 res = new_rd_entity(db, owner, name, type);
177 /* Remember entity in it's owner. */
178 insert_entity_in_owner(res);
180 hook_new_entity(res);
185 new_entity(ir_type *owner, ident *name, ir_type *type) {
186 return new_d_entity(owner, name, type, NULL);
190 * Free entity attributes.
192 * @param ent the entity
194 static void free_entity_attrs(ir_entity *ent) {
196 if (get_type_tpop(get_entity_owner(ent)) == type_class) {
197 DEL_ARR_F(ent->overwrites); ent->overwrites = NULL;
198 DEL_ARR_F(ent->overwrittenby); ent->overwrittenby = NULL;
200 assert(ent->overwrites == NULL);
201 assert(ent->overwrittenby == NULL);
203 if (is_compound_entity(ent)) {
204 if (ent->has_initializer) {
205 /* TODO: free initializers */
207 if (ent->attr.cmpd_attr.val_paths) {
208 for (i = get_compound_ent_n_values(ent) - 1; i >= 0; --i)
209 if (ent->attr.cmpd_attr.val_paths[i]) {
210 /* free_compound_graph_path(ent->attr.cmpd_attr.val_paths[i]) ; * @@@ warum nich? */
211 /* Geht nich: wird mehrfach verwendet!!! ==> mehrfach frei gegeben. */
212 DEL_ARR_F(ent->attr.cmpd_attr.val_paths);
214 ent->attr.cmpd_attr.val_paths = NULL;
216 if (ent->attr.cmpd_attr.values)
217 DEL_ARR_F(ent->attr.cmpd_attr.values);
218 ent->attr.cmpd_attr.values = NULL;
220 } else if (is_method_entity(ent)) {
221 if (ent->attr.mtd_attr.param_access) {
222 DEL_ARR_F(ent->attr.mtd_attr.param_access);
223 ent->attr.mtd_attr.param_access = NULL;
225 if (ent->attr.mtd_attr.param_weight) {
226 DEL_ARR_F(ent->attr.mtd_attr.param_weight);
227 ent->attr.mtd_attr.param_weight = NULL;
230 } /* free_entity_attrs */
233 * Copies the entity if the new_owner is different from the
234 * owner of the old entity, else returns the old entity.
237 copy_entity_own(ir_entity *old, ir_type *new_owner) {
239 assert(is_entity(old));
240 assert(is_compound_type(new_owner));
241 assert(get_type_state(new_owner) != layout_fixed);
243 if (old->owner == new_owner) return old;
244 newe = XMALLOC(ir_entity);
245 memcpy(newe, old, sizeof(*newe));
246 newe->owner = new_owner;
247 if (is_Class_type(new_owner)) {
248 newe->overwrites = NEW_ARR_F(ir_entity *, 0);
249 newe->overwrittenby = NEW_ARR_F(ir_entity *, 0);
252 newe->nr = get_irp_new_node_nr();
255 insert_entity_in_owner(newe);
258 } /* copy_entity_own */
261 copy_entity_name(ir_entity *old, ident *new_name) {
263 assert(old && old->kind == k_entity);
265 if (old->name == new_name) return old;
266 newe = XMALLOC(ir_entity);
267 memcpy(newe, old, sizeof(*newe));
268 newe->name = new_name;
269 newe->ld_name = NULL;
270 if (is_Class_type(newe->owner)) {
271 newe->overwrites = DUP_ARR_F(ir_entity *, old->overwrites);
272 newe->overwrittenby = DUP_ARR_F(ir_entity *, old->overwrittenby);
275 newe->nr = get_irp_new_node_nr();
278 insert_entity_in_owner(newe);
281 } /* copy_entity_name */
285 free_entity(ir_entity *ent) {
286 assert(ent && ent->kind == k_entity);
287 free_entity_attrs(ent);
292 /* Outputs a unique number for this node */
294 get_entity_nr(const ir_entity *ent) {
295 assert(ent && ent->kind == k_entity);
299 return (long)PTR_TO_INT(ent);
301 } /* get_entity_nr */
304 (get_entity_name)(const ir_entity *ent) {
305 return _get_entity_name(ent);
306 } /* get_entity_name */
309 (get_entity_ident)(const ir_entity *ent) {
310 return _get_entity_ident(ent);
311 } /* get_entity_ident */
314 (set_entity_ident)(ir_entity *ent, ident *id) {
315 _set_entity_ident(ent, id);
316 } /* set_entity_ident */
319 (get_entity_owner)(ir_entity *ent) {
320 return _get_entity_owner(ent);
321 } /* get_entity_owner */
324 set_entity_owner(ir_entity *ent, ir_type *owner) {
325 assert(is_entity(ent));
326 assert(is_compound_type(owner));
328 } /* set_entity_owner */
331 (get_entity_ld_ident)(ir_entity *ent) {
332 return _get_entity_ld_ident(ent);
333 } /* get_entity_ld_ident */
336 (set_entity_ld_ident)(ir_entity *ent, ident *ld_ident) {
337 _set_entity_ld_ident(ent, ld_ident);
338 } /* set_entity_ld_ident */
341 (get_entity_ld_name)(ir_entity *ent) {
342 return _get_entity_ld_name(ent);
343 } /* get_entity_ld_name */
346 (get_entity_type)(ir_entity *ent) {
347 return _get_entity_type(ent);
348 } /* get_entity_type */
351 (set_entity_type)(ir_entity *ent, ir_type *type) {
352 _set_entity_type(ent, type);
353 } /* set_entity_type */
356 (get_entity_allocation)(const ir_entity *ent) {
357 return _get_entity_allocation(ent);
358 } /* get_entity_allocation */
361 (set_entity_allocation)(ir_entity *ent, ir_allocation al) {
362 _set_entity_allocation(ent, al);
363 } /* set_entity_allocation */
365 /* return the name of the visibility */
366 const char *get_allocation_name(ir_allocation all)
368 #define X(a) case a: return #a
370 X(allocation_automatic);
371 X(allocation_parameter);
372 X(allocation_dynamic);
373 X(allocation_static);
374 default: return "BAD VALUE";
377 } /* get_allocation_name */
380 (get_entity_visibility)(const ir_entity *ent) {
381 return _get_entity_visibility(ent);
382 } /* get_entity_visibility */
385 set_entity_visibility(ir_entity *ent, ir_visibility vis) {
386 assert(ent && ent->kind == k_entity);
387 if (vis != visibility_local)
388 assert((ent->allocation == allocation_static) ||
389 (ent->allocation == allocation_automatic));
390 /* @@@ Test that the owner type is not local, but how??
391 && get_class_visibility(get_entity_owner(ent)) != local));*/
392 ent->visibility = vis;
393 } /* set_entity_visibility */
395 /* return the name of the visibility */
396 const char *get_visibility_name(ir_visibility vis)
398 #define X(a) case a: return #a
401 X(visibility_external_visible);
402 X(visibility_external_allocated);
403 default: return "BAD VALUE";
406 } /* get_visibility_name */
409 (get_entity_variability)(const ir_entity *ent) {
410 return _get_entity_variability(ent);
411 } /* get_entity_variability */
414 set_entity_variability(ir_entity *ent, ir_variability var)
416 assert(ent && ent->kind == k_entity);
417 if (var == variability_part_constant)
418 assert(is_Class_type(ent->type) || is_Struct_type(ent->type));
420 if ((is_compound_type(ent->type)) &&
421 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
422 /* Allocate data structures for constant values */
423 ent->attr.cmpd_attr.values = NEW_ARR_F(ir_node *, 0);
424 ent->attr.cmpd_attr.val_paths = NEW_ARR_F(compound_graph_path *, 0);
426 if ((is_atomic_type(ent->type)) &&
427 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
428 /* Set default constant value. */
429 ent->value = new_rd_Unknown(get_const_code_irg(), get_type_mode(ent->type));
432 if ((is_compound_type(ent->type)) &&
433 (var == variability_uninitialized) && (ent->variability != variability_uninitialized)) {
434 /* Free data structures for constant values */
435 DEL_ARR_F(ent->attr.cmpd_attr.values); ent->attr.cmpd_attr.values = NULL;
436 DEL_ARR_F(ent->attr.cmpd_attr.val_paths); ent->attr.cmpd_attr.val_paths = NULL;
438 ent->variability = var;
439 } /* set_entity_variability */
441 /* return the name of the variability */
442 const char *get_variability_name(ir_variability var)
444 #define X(a) case a: return #a
446 X(variability_uninitialized);
447 X(variability_initialized);
448 X(variability_part_constant);
449 X(variability_constant);
450 default: return "BAD VALUE";
453 } /* get_variability_name */
456 (get_entity_volatility)(const ir_entity *ent) {
457 return _get_entity_volatility(ent);
458 } /* get_entity_volatility */
461 (set_entity_volatility)(ir_entity *ent, ir_volatility vol) {
462 _set_entity_volatility(ent, vol);
463 } /* set_entity_volatility */
465 /* Return the name of the volatility. */
466 const char *get_volatility_name(ir_volatility var)
468 #define X(a) case a: return #a
470 X(volatility_non_volatile);
471 X(volatility_is_volatile);
472 default: return "BAD VALUE";
475 } /* get_volatility_name */
478 (get_entity_align)(const ir_entity *ent) {
479 return _get_entity_align(ent);
480 } /* get_entity_align */
483 (set_entity_align)(ir_entity *ent, ir_align a) {
484 _set_entity_align(ent, a);
485 } /* set_entity_align */
487 /* Return the name of the alignment. */
488 const char *get_align_name(ir_align a)
490 #define X(a) case a: return #a
492 X(align_non_aligned);
494 default: return "BAD VALUE";
497 } /* get_align_name */
500 (get_entity_peculiarity)(const ir_entity *ent) {
501 return _get_entity_peculiarity(ent);
502 } /* get_entity_peculiarity */
505 (set_entity_peculiarity)(ir_entity *ent, ir_peculiarity pec) {
506 _set_entity_peculiarity(ent, pec);
507 } /* set_entity_peculiarity */
509 /* Checks if an entity cannot be overridden anymore. */
510 int (is_entity_final)(const ir_entity *ent) {
511 return _is_entity_final(ent);
512 } /* is_entity_final */
514 /* Sets/resets the final flag of an entity. */
515 void (set_entity_final)(ir_entity *ent, int final) {
516 _set_entity_final(ent, final);
517 } /* set_entity_final */
519 /* Checks if an entity is compiler generated */
520 int (is_entity_compiler_generated)(const ir_entity *ent) {
521 return _is_entity_compiler_generated(ent);
522 } /* is_entity_compiler_generated */
524 /* Sets/resets the compiler generated flag */
525 void (set_entity_compiler_generated)(ir_entity *ent, int flag) {
526 _set_entity_compiler_generated(ent, flag);
527 } /* set_entity_compiler_generated */
529 /* Checks if an entity is marked by the backend */
530 int (is_entity_backend_marked)(const ir_entity *ent) {
531 return _is_entity_backend_marked(ent);
532 } /* is_entity_backend_marked */
534 /* Sets/resets the compiler generated flag */
535 void (set_entity_backend_marked)(ir_entity *ent, int flag) {
536 _set_entity_backend_marked(ent, flag);
537 } /* set_entity_backend_marked */
539 ir_entity_usage (get_entity_usage)(const ir_entity *ent) {
540 return _get_entity_usage(ent);
543 void (set_entity_usage)(ir_entity *ent, ir_entity_usage flags) {
544 _set_entity_usage(ent, flags);
547 /* Get the entity's stickyness */
549 (get_entity_stickyness)(const ir_entity *ent) {
550 return _get_entity_stickyness(ent);
551 } /* get_entity_stickyness */
553 /* Set the entity's stickyness */
555 (set_entity_stickyness)(ir_entity *ent, ir_stickyness stickyness) {
556 _set_entity_stickyness(ent, stickyness);
557 } /* set_entity_stickyness */
559 /* Set has no effect for existent entities of type method. */
561 get_atomic_ent_value(ir_entity *ent)
563 assert(ent && is_atomic_entity(ent));
564 assert(ent->variability != variability_uninitialized);
565 return skip_Id(ent->value);
566 } /* get_atomic_ent_value */
569 set_atomic_ent_value(ir_entity *ent, ir_node *val) {
570 assert(is_atomic_entity(ent) && (ent->variability != variability_uninitialized));
571 if (is_Method_type(ent->type) && (ent->peculiarity == peculiarity_existent))
573 assert(get_irn_mode(val) == get_type_mode(ent->type));
575 } /* set_atomic_ent_value */
577 /* Returns true if the the node is representable as code on
579 int is_irn_const_expression(ir_node *n) {
582 /* we are in danger iff an exception will arise. TODO: be more precisely,
583 * for instance Div. will NOT rise if divisor != 0
585 if (is_binop(n) && !is_fragile_op(n))
586 return is_irn_const_expression(get_binop_left(n)) && is_irn_const_expression(get_binop_right(n));
589 switch (get_irn_opcode(n)) {
596 return is_irn_const_expression(get_irn_n(n, 0));
601 } /* is_irn_const_expression */
604 * Copies a firm subgraph that complies to the restrictions for
605 * constant expressions to current_block in current_ir_graph.
607 ir_node *copy_const_value(dbg_info *dbg, ir_node *n) {
611 /* @@@ GL I think we should implement this using the routines from irgopt for
612 dead node elimination/inlineing. */
615 switch (get_irn_opcode(n)) {
617 nn = new_d_Const_type(dbg, m, get_Const_tarval(n), get_Const_type(n));
620 nn = new_d_SymConst_type(dbg, get_irn_mode(n), get_SymConst_symbol(n), get_SymConst_kind(n),
621 get_SymConst_value_type(n));
624 nn = new_d_Add(dbg, copy_const_value(dbg, get_Add_left(n)),
625 copy_const_value(dbg, get_Add_right(n)), m); break;
627 nn = new_d_Sub(dbg, copy_const_value(dbg, get_Sub_left(n)),
628 copy_const_value(dbg, get_Sub_right(n)), m); break;
630 nn = new_d_Mul(dbg, copy_const_value(dbg, get_Mul_left(n)),
631 copy_const_value(dbg, get_Mul_right(n)), m); break;
633 nn = new_d_And(dbg, copy_const_value(dbg, get_And_left(n)),
634 copy_const_value(dbg, get_And_right(n)), m); break;
636 nn = new_d_Or(dbg, copy_const_value(dbg, get_Or_left(n)),
637 copy_const_value(dbg, get_Or_right(n)), m); break;
639 nn = new_d_Eor(dbg, copy_const_value(dbg, get_Eor_left(n)),
640 copy_const_value(dbg, get_Eor_right(n)), m); break;
642 nn = new_d_Cast(dbg, copy_const_value(dbg, get_Cast_op(n)), get_Cast_type(n)); break;
644 nn = new_d_Conv(dbg, copy_const_value(dbg, get_Conv_op(n)), m); break;
646 nn = new_d_Unknown(m); break;
648 assert(0 && "opcode invalid or not implemented");
653 } /* copy_const_value */
655 static ir_initializer_t null_initializer = { IR_INITIALIZER_NULL };
657 ir_initializer_t *get_initializer_null(void)
659 return &null_initializer;
662 ir_initializer_t *create_initializer_const(ir_node *value)
664 struct obstack *obst = get_irg_obstack(get_const_code_irg());
666 ir_initializer_t *initializer
667 = obstack_alloc(obst, sizeof(ir_initializer_const_t));
668 initializer->kind = IR_INITIALIZER_CONST;
669 initializer->consti.value = value;
674 ir_initializer_t *create_initializer_tarval(tarval *tv)
676 struct obstack *obst = get_irg_obstack(get_const_code_irg());
678 ir_initializer_t *initializer
679 = obstack_alloc(obst, sizeof(ir_initializer_tarval_t));
680 initializer->kind = IR_INITIALIZER_TARVAL;
681 initializer->tarval.value = tv;
686 ir_initializer_t *create_initializer_compound(unsigned n_entries)
688 struct obstack *obst = get_irg_obstack(get_const_code_irg());
691 size_t size = sizeof(ir_initializer_compound_t)
692 + (n_entries-1) * sizeof(ir_initializer_t*);
694 ir_initializer_t *initializer = obstack_alloc(obst, size);
695 initializer->kind = IR_INITIALIZER_COMPOUND;
696 initializer->compound.n_initializers = n_entries;
698 for(i = 0; i < n_entries; ++i) {
699 initializer->compound.initializers[i] = get_initializer_null();
705 ir_node *get_initializer_const_value(const ir_initializer_t *initializer)
707 assert(initializer->kind == IR_INITIALIZER_CONST);
708 return initializer->consti.value;
711 tarval *get_initializer_tarval_value(const ir_initializer_t *initializer)
713 assert(initializer->kind == IR_INITIALIZER_TARVAL);
714 return initializer->tarval.value;
717 unsigned get_initializer_compound_n_entries(const ir_initializer_t *initializer)
719 assert(initializer->kind == IR_INITIALIZER_COMPOUND);
720 return initializer->compound.n_initializers;
723 void set_initializer_compound_value(ir_initializer_t *initializer,
724 unsigned index, ir_initializer_t *value)
726 assert(initializer->kind == IR_INITIALIZER_COMPOUND);
727 assert(index < initializer->compound.n_initializers);
729 initializer->compound.initializers[index] = value;
732 ir_initializer_t *get_initializer_compound_value(
733 const ir_initializer_t *initializer, unsigned index)
735 assert(initializer->kind == IR_INITIALIZER_COMPOUND);
736 assert(index < initializer->compound.n_initializers);
738 return initializer->compound.initializers[index];
741 ir_initializer_kind_t get_initializer_kind(const ir_initializer_t *initializer)
743 return initializer->kind;
746 static void check_entity_initializer(ir_entity *entity)
752 void set_entity_initializer(ir_entity *entity, ir_initializer_t *initializer)
754 entity->attr.initializer = initializer;
755 entity->has_initializer = 1;
756 check_entity_initializer(entity);
759 ir_initializer_t *get_entity_initializer(const ir_entity *entity)
761 assert(entity->has_initializer);
762 return entity->attr.initializer;
765 /* Creates a new compound graph path. */
766 compound_graph_path *
767 new_compound_graph_path(ir_type *tp, int length) {
768 compound_graph_path *res;
770 assert(is_compound_type(tp));
773 res = xmalloc(sizeof(*res) + (length-1) * sizeof(res->list[0]));
774 memset(res, 0, sizeof(*res) + (length-1) * sizeof(res->list[0]));
775 res->kind = k_ir_compound_graph_path;
780 } /* new_compound_graph_path */
782 /* Frees an graph path object */
783 void free_compound_graph_path (compound_graph_path *gr) {
784 assert(gr && is_compound_graph_path(gr));
787 } /* free_compound_graph_path */
789 /* Returns non-zero if an object is a compound graph path */
790 int is_compound_graph_path(const void *thing) {
791 return (get_kind(thing) == k_ir_compound_graph_path);
792 } /* is_compound_graph_path */
794 /* Checks whether the path up to pos is correct. If the path contains a NULL,
795 * assumes the path is not complete and returns 'true'. */
796 int is_proper_compound_graph_path(compound_graph_path *gr, int pos) {
799 ir_type *owner = gr->tp;
801 for (i = 0; i <= pos; i++) {
802 node = get_compound_graph_path_node(gr, i);
804 /* Path not yet complete. */
806 if (get_entity_owner(node) != owner)
808 owner = get_entity_type(node);
810 if (pos == get_compound_graph_path_length(gr))
811 if (!is_atomic_type(owner))
814 } /* is_proper_compound_graph_path */
816 /* Returns the length of a graph path */
817 int get_compound_graph_path_length(const compound_graph_path *gr) {
818 assert(gr && is_compound_graph_path(gr));
820 } /* get_compound_graph_path_length */
823 get_compound_graph_path_node(const compound_graph_path *gr, int pos) {
824 assert(gr && is_compound_graph_path(gr));
825 assert(pos >= 0 && pos < gr->len);
826 return gr->list[pos].node;
827 } /* get_compound_graph_path_node */
830 set_compound_graph_path_node(compound_graph_path *gr, int pos, ir_entity *node) {
831 assert(gr && is_compound_graph_path(gr));
832 assert(pos >= 0 && pos < gr->len);
833 assert(is_entity(node));
834 gr->list[pos].node = node;
835 assert(is_proper_compound_graph_path(gr, pos));
836 } /* set_compound_graph_path_node */
839 get_compound_graph_path_array_index(const compound_graph_path *gr, int pos) {
840 assert(gr && is_compound_graph_path(gr));
841 assert(pos >= 0 && pos < gr->len);
842 return gr->list[pos].index;
843 } /* get_compound_graph_path_array_index */
846 set_compound_graph_path_array_index(compound_graph_path *gr, int pos, int index) {
847 assert(gr && is_compound_graph_path(gr));
848 assert(pos >= 0 && pos < gr->len);
849 gr->list[pos].index = index;
850 } /* set_compound_graph_path_array_index */
853 get_compound_graph_path_type(const compound_graph_path *gr) {
854 assert(gr && is_compound_graph_path(gr));
858 /* A value of a compound entity is a pair of value and the corresponding path to a member of
861 add_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path) {
862 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
863 assert(is_compound_graph_path(path));
864 ARR_APP1(ir_node *, ent->attr.cmpd_attr.values, val);
865 ARR_APP1(compound_graph_path *, ent->attr.cmpd_attr.val_paths, path);
866 } /* add_compound_ent_value_w_path */
869 set_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path, int pos) {
870 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
871 assert(is_compound_graph_path(path));
872 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
873 ent->attr.cmpd_attr.values[pos] = val;
874 ent->attr.cmpd_attr.val_paths[pos] = path;
875 } /* set_compound_ent_value_w_path */
878 get_compound_ent_n_values(ir_entity *ent) {
879 assert(!ent->has_initializer);
880 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
881 return ARR_LEN(ent->attr.cmpd_attr.values);
882 } /* get_compound_ent_n_values */
885 get_compound_ent_value(ir_entity *ent, int pos) {
886 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
887 assert(!ent->has_initializer);
888 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
889 return ent->attr.cmpd_attr.values[pos];
890 } /* get_compound_ent_value */
892 compound_graph_path *
893 get_compound_ent_value_path(ir_entity *ent, int pos) {
894 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
895 assert(!ent->has_initializer);
896 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.val_paths));
897 return ent->attr.cmpd_attr.val_paths[pos];
898 } /* get_compound_ent_value_path */
901 * Returns non-zero, if two compound_graph_pathes are equal
903 * @param path1 the first path
904 * @param path2 the second path
906 static int equal_paths(compound_graph_path *path1, compound_graph_path *path2) {
908 int len1 = get_compound_graph_path_length(path1);
909 int len2 = get_compound_graph_path_length(path2);
911 if (len2 != len1) return 0;
913 for (i = 0; i < len1; i++) {
915 ir_entity *node1 = get_compound_graph_path_node(path1, i);
916 ir_entity *node2 = get_compound_graph_path_node(path2, i);
918 if (node1 != node2) return 0;
920 tp = get_entity_owner(node1);
921 if (is_Array_type(tp)) {
922 int index1 = get_compound_graph_path_array_index(path1, i);
923 int index2 = get_compound_graph_path_array_index(path2, i);
924 if (index1 != index2)
932 * Returns the position of a value with the given path.
933 * The path must contain array indices for all array element entities.
935 * @todo This implementation is very slow (O(number of initializers * |path|)
936 * and should be replaced when the new tree oriented
937 * value representation is finally implemented.
939 static int get_compound_ent_pos_by_path(ir_entity *ent, compound_graph_path *path) {
940 int i, n_paths = get_compound_ent_n_values(ent);
942 for (i = 0; i < n_paths; i ++) {
943 compound_graph_path *gr = get_compound_ent_value_path(ent, i);
944 if (equal_paths(gr, path))
948 } /* get_compound_ent_pos_by_path */
950 /* Returns a constant value given the access path.
951 * The path must contain array indices for all array element entities. */
952 ir_node *get_compound_ent_value_by_path(ir_entity *ent, compound_graph_path *path) {
953 int pos = get_compound_ent_pos_by_path(ent, path);
955 return get_compound_ent_value(ent, pos);
957 } /* get_compound_ent_value_by_path */
961 remove_compound_ent_value(ir_entity *ent, ir_entity *value_ent) {
963 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
965 n = ARR_LEN(ent->attr.cmpd_attr.val_paths);
966 for (i = 0; i < n; ++i) {
967 compound_graph_path *path = ent->attr.cmpd_attr.val_paths[i];
968 if (path->list[path->len-1].node == value_ent) {
969 for (; i < n - 1; ++i) {
970 ent->attr.cmpd_attr.val_paths[i] = ent->attr.cmpd_attr.val_paths[i+1];
971 ent->attr.cmpd_attr.values[i] = ent->attr.cmpd_attr.values[i+1];
973 ARR_SETLEN(ir_entity*, ent->attr.cmpd_attr.val_paths, n - 1);
974 ARR_SETLEN(ir_node*, ent->attr.cmpd_attr.values, n - 1);
978 } /* remove_compound_ent_value */
981 add_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member) {
982 compound_graph_path *path;
983 ir_type *owner_tp = get_entity_owner(member);
984 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
985 path = new_compound_graph_path(get_entity_type(ent), 1);
986 path->list[0].node = member;
987 if (is_Array_type(owner_tp)) {
991 assert(get_array_n_dimensions(owner_tp) == 1 && has_array_lower_bound(owner_tp, 0));
992 max = get_array_lower_bound_int(owner_tp, 0) -1;
993 for (i = 0, n = get_compound_ent_n_values(ent); i < n; ++i) {
994 int index = get_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0);
999 path->list[0].index = max + 1;
1001 add_compound_ent_value_w_path(ent, val, path);
1002 } /* add_compound_ent_value */
1006 get_compound_ent_value_member(ir_entity *ent, int pos) {
1007 compound_graph_path *path;
1008 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
1009 path = get_compound_ent_value_path(ent, pos);
1011 return get_compound_graph_path_node(path, get_compound_graph_path_length(path)-1);
1012 } /* get_compound_ent_value_member */
1015 set_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member, int pos) {
1016 compound_graph_path *path;
1017 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
1018 path = get_compound_ent_value_path(ent, pos);
1019 set_compound_graph_path_node(path, 0, member);
1020 set_compound_ent_value_w_path(ent, val, path, pos);
1021 } /* set_compound_ent_value */
1024 set_array_entity_values(ir_entity *ent, tarval **values, int num_vals) {
1026 ir_graph *rem = current_ir_graph;
1027 ir_type *arrtp = get_entity_type(ent);
1029 ir_type *elttp = get_array_element_type(arrtp);
1031 assert(is_Array_type(arrtp));
1032 assert(get_array_n_dimensions(arrtp) == 1);
1033 /* One bound is sufficient, the number of constant fields makes the
1035 assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
1036 assert(get_entity_variability(ent) != variability_uninitialized);
1037 current_ir_graph = get_const_code_irg();
1039 for (i = 0; i < num_vals; i++) {
1040 val = new_Const_type(values[i], elttp);
1041 add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
1042 set_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0, i);
1044 current_ir_graph = rem;
1045 } /* set_array_entity_values */
1047 /* Return the overall offset of value at position pos in bytes. */
1048 unsigned get_compound_ent_value_offset_bytes(ir_entity *ent, int pos) {
1049 compound_graph_path *path;
1051 unsigned offset = 0;
1054 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
1056 path = get_compound_ent_value_path(ent, pos);
1057 path_len = get_compound_graph_path_length(path);
1060 for (i = 0; i < path_len; ++i) {
1061 if (is_Array_type(curr_tp)) {
1062 ir_type *elem_type = get_array_element_type(curr_tp);
1063 unsigned size = get_type_size_bytes(elem_type);
1064 unsigned align = get_type_alignment_bytes(elem_type);
1068 if(size % align > 0) {
1069 size += align - (size % align);
1071 idx = get_compound_graph_path_array_index(path, i);
1073 offset += size * idx;
1074 curr_tp = elem_type;
1076 ir_entity *node = get_compound_graph_path_node(path, i);
1077 offset += get_entity_offset(node);
1078 curr_tp = get_entity_type(node);
1083 } /* get_compound_ent_value_offset_bytes */
1085 /* Return the offset in bits from the last byte address. */
1086 unsigned get_compound_ent_value_offset_bit_remainder(ir_entity *ent, int pos) {
1087 compound_graph_path *path;
1089 ir_entity *last_node;
1091 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
1093 path = get_compound_ent_value_path(ent, pos);
1094 path_len = get_compound_graph_path_length(path);
1095 last_node = get_compound_graph_path_node(path, path_len - 1);
1097 if(last_node == NULL)
1100 return get_entity_offset_bits_remainder(last_node);
1101 } /* get_compound_ent_value_offset_bit_remainder */
1104 (get_entity_offset)(const ir_entity *ent) {
1105 return _get_entity_offset(ent);
1106 } /* get_entity_offset */
1109 (set_entity_offset)(ir_entity *ent, int offset) {
1110 _set_entity_offset(ent, offset);
1111 } /* set_entity_offset */
1114 (get_entity_offset_bits_remainder)(const ir_entity *ent) {
1115 return _get_entity_offset_bits_remainder(ent);
1116 } /* get_entity_offset_bits_remainder */
1119 (set_entity_offset_bits_remainder)(ir_entity *ent, unsigned char offset) {
1120 _set_entity_offset_bits_remainder(ent, offset);
1121 } /* set_entity_offset_bits_remainder */
1124 add_entity_overwrites(ir_entity *ent, ir_entity *overwritten) {
1126 ir_type *owner = get_entity_owner(ent);
1127 ir_type *ovw_ovner = get_entity_owner(overwritten);
1128 assert(is_Class_type(owner));
1129 assert(is_Class_type(ovw_ovner));
1130 assert(! is_class_final(ovw_ovner));
1132 ARR_APP1(ir_entity *, ent->overwrites, overwritten);
1133 ARR_APP1(ir_entity *, overwritten->overwrittenby, ent);
1134 } /* add_entity_overwrites */
1137 get_entity_n_overwrites(ir_entity *ent) {
1138 assert(is_Class_type(get_entity_owner(ent)));
1139 return (ARR_LEN(ent->overwrites));
1140 } /* get_entity_n_overwrites */
1143 get_entity_overwrites_index(ir_entity *ent, ir_entity *overwritten) {
1145 assert(is_Class_type(get_entity_owner(ent)));
1146 n = get_entity_n_overwrites(ent);
1147 for (i = 0; i < n; ++i) {
1148 if (get_entity_overwrites(ent, i) == overwritten)
1152 } /* get_entity_overwrites_index */
1155 get_entity_overwrites(ir_entity *ent, int pos) {
1156 assert(is_Class_type(get_entity_owner(ent)));
1157 assert(pos < get_entity_n_overwrites(ent));
1158 return ent->overwrites[pos];
1159 } /* get_entity_overwrites */
1162 set_entity_overwrites(ir_entity *ent, int pos, ir_entity *overwritten) {
1163 assert(is_Class_type(get_entity_owner(ent)));
1164 assert(pos < get_entity_n_overwrites(ent));
1165 ent->overwrites[pos] = overwritten;
1166 } /* set_entity_overwrites */
1169 remove_entity_overwrites(ir_entity *ent, ir_entity *overwritten) {
1171 assert(is_Class_type(get_entity_owner(ent)));
1172 n = ARR_LEN(ent->overwrites);
1173 for (i = 0; i < n; ++i) {
1174 if (ent->overwrites[i] == overwritten) {
1175 for (; i < n - 1; i++)
1176 ent->overwrites[i] = ent->overwrites[i+1];
1177 ARR_SETLEN(ir_entity*, ent->overwrites, n - 1);
1181 } /* remove_entity_overwrites */
1184 add_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites) {
1185 add_entity_overwrites(overwrites, ent);
1186 } /* add_entity_overwrittenby */
1189 get_entity_n_overwrittenby(ir_entity *ent) {
1190 assert(is_Class_type(get_entity_owner(ent)));
1191 return ARR_LEN(ent->overwrittenby);
1192 } /* get_entity_n_overwrittenby */
1195 get_entity_overwrittenby_index(ir_entity *ent, ir_entity *overwrites) {
1197 assert(is_Class_type(get_entity_owner(ent)));
1198 n = get_entity_n_overwrittenby(ent);
1199 for (i = 0; i < n; ++i) {
1200 if (get_entity_overwrittenby(ent, i) == overwrites)
1204 } /* get_entity_overwrittenby_index */
1207 get_entity_overwrittenby(ir_entity *ent, int pos) {
1208 assert(is_Class_type(get_entity_owner(ent)));
1209 assert(pos < get_entity_n_overwrittenby(ent));
1210 return ent->overwrittenby[pos];
1211 } /* get_entity_overwrittenby */
1214 set_entity_overwrittenby(ir_entity *ent, int pos, ir_entity *overwrites) {
1215 assert(is_Class_type(get_entity_owner(ent)));
1216 assert(pos < get_entity_n_overwrittenby(ent));
1217 ent->overwrittenby[pos] = overwrites;
1218 } /* set_entity_overwrittenby */
1220 void remove_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites) {
1222 assert(is_Class_type(get_entity_owner(ent)));
1224 n = ARR_LEN(ent->overwrittenby);
1225 for (i = 0; i < n; ++i) {
1226 if (ent->overwrittenby[i] == overwrites) {
1227 for(; i < n - 1; ++i)
1228 ent->overwrittenby[i] = ent->overwrittenby[i+1];
1229 ARR_SETLEN(ir_entity*, ent->overwrittenby, n - 1);
1233 } /* remove_entity_overwrittenby */
1235 /* A link to store intermediate information */
1237 (get_entity_link)(const ir_entity *ent) {
1238 return _get_entity_link(ent);
1239 } /* get_entity_link */
1242 (set_entity_link)(ir_entity *ent, void *l) {
1243 _set_entity_link(ent, l);
1244 } /* set_entity_link */
1247 (get_entity_irg)(const ir_entity *ent) {
1248 return _get_entity_irg(ent);
1249 } /* get_entity_irg */
1252 set_entity_irg(ir_entity *ent, ir_graph *irg) {
1253 assert(is_method_entity(ent));
1254 /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
1255 * Methode selbst nicht mehr aufgerufen werden kann, die Entität
1256 * aber erhalten bleiben soll? Wandle die Entitaet in description oder
1259 assert((irg && ent->peculiarity == peculiarity_existent) ||
1260 (!irg && (ent->peculiarity == peculiarity_existent)
1261 && (ent -> visibility == visibility_external_allocated)) ||
1262 (!irg && ent->peculiarity == peculiarity_description) ||
1263 (!irg && ent->peculiarity == peculiarity_inherited));
1264 ent->attr.mtd_attr.irg = irg;
1265 } /* set_entity_irg */
1267 unsigned get_entity_vtable_number(const ir_entity *ent) {
1268 assert(is_method_entity((ir_entity *)ent));
1269 return ent->attr.mtd_attr.vtable_number;
1270 } /* get_entity_vtable_number */
1272 void set_entity_vtable_number(ir_entity *ent, unsigned vtable_number) {
1273 assert(is_method_entity(ent));
1274 ent->attr.mtd_attr.vtable_number = vtable_number;
1275 } /* set_entity_vtable_number */
1278 (is_entity)(const void *thing) {
1279 return _is_entity(thing);
1282 int is_atomic_entity(ir_entity *ent) {
1283 ir_type *t = get_entity_type(ent);
1284 const tp_op *op = get_type_tpop(t);
1285 return (op == type_primitive || op == type_pointer ||
1286 op == type_enumeration || op == type_method);
1287 } /* is_atomic_entity */
1289 int is_compound_entity(ir_entity *ent) {
1290 ir_type *t = get_entity_type(ent);
1291 const tp_op *op = get_type_tpop(t);
1292 return (op == type_class || op == type_struct ||
1293 op == type_array || op == type_union);
1294 } /* is_compound_entity */
1296 int is_method_entity(ir_entity *ent) {
1297 ir_type *t = get_entity_type(ent);
1298 return is_Method_type(t);
1299 } /* is_method_entity */
1302 * @todo not implemented!!! */
1303 int equal_entity(ir_entity *ent1, ir_entity *ent2) {
1306 fprintf(stderr, " calling unimplemented equal entity!!! \n");
1308 } /* equal_entity */
1311 ir_visited_t (get_entity_visited)(ir_entity *ent) {
1312 return _get_entity_visited(ent);
1313 } /* get_entity_visited */
1315 void (set_entity_visited)(ir_entity *ent, ir_visited_t num) {
1316 _set_entity_visited(ent, num);
1317 } /* set_entity_visited */
1319 /* Sets visited field in ir_entity to entity_visited. */
1320 void (mark_entity_visited)(ir_entity *ent) {
1321 _mark_entity_visited(ent);
1322 } /* mark_entity_visited */
1324 int (entity_visited)(ir_entity *ent) {
1325 return _entity_visited(ent);
1326 } /* entity_visited */
1328 int (entity_not_visited)(ir_entity *ent) {
1329 return _entity_not_visited(ent);
1330 } /* entity_not_visited */
1332 /* Returns the mask of the additional entity properties. */
1333 unsigned get_entity_additional_properties(ir_entity *ent) {
1336 assert(is_method_entity(ent));
1338 /* first check, if the graph has additional properties */
1339 irg = get_entity_irg(ent);
1342 return get_irg_additional_properties(irg);
1344 if (ent->attr.mtd_attr.irg_add_properties & mtp_property_inherited)
1345 return get_method_additional_properties(get_entity_type(ent));
1347 return ent->attr.mtd_attr.irg_add_properties;
1348 } /* get_entity_additional_properties */
1350 /* Sets the mask of the additional graph properties. */
1351 void set_entity_additional_properties(ir_entity *ent, unsigned property_mask)
1355 assert(is_method_entity(ent));
1357 /* first check, if the graph exists */
1358 irg = get_entity_irg(ent);
1360 set_irg_additional_properties(irg, property_mask);
1362 /* do not allow to set the mtp_property_inherited flag or
1363 * the automatic inheritance of flags will not work */
1364 ent->attr.mtd_attr.irg_add_properties = property_mask & ~mtp_property_inherited;
1366 } /* set_entity_additional_properties */
1368 /* Sets one additional graph property. */
1369 void set_entity_additional_property(ir_entity *ent, mtp_additional_property flag)
1373 assert(is_method_entity(ent));
1375 /* first check, if the graph exists */
1376 irg = get_entity_irg(ent);
1378 set_irg_additional_property(irg, flag);
1380 unsigned mask = ent->attr.mtd_attr.irg_add_properties;
1382 if (mask & mtp_property_inherited)
1383 mask = get_method_additional_properties(get_entity_type(ent));
1385 /* do not allow to set the mtp_property_inherited flag or
1386 * the automatic inheritance of flags will not work */
1387 ent->attr.mtd_attr.irg_add_properties = mask | (flag & ~mtp_property_inherited);
1389 } /* set_entity_additional_property */
1391 /* Returns the class type that this type info entity represents or NULL
1392 if ent is no type info entity. */
1393 ir_type *(get_entity_repr_class)(const ir_entity *ent) {
1394 return _get_entity_repr_class(ent);
1395 } /* get_entity_repr_class */
1397 dbg_info *(get_entity_dbg_info)(const ir_entity *ent) {
1398 return _get_entity_dbg_info(ent);
1399 } /* get_entity_dbg_info */
1401 void (set_entity_dbg_info)(ir_entity *ent, dbg_info *db) {
1402 _set_entity_dbg_info(ent, db);
1403 } /* set_entity_dbg_info */
1405 /* Initialize entity module. */
1406 void firm_init_entity(void)
1408 symconst_symbol sym;
1410 assert(firm_unknown_type && "Call init_type() before firm_init_entity()!");
1411 assert(!unknown_entity && "Call firm_init_entity() only once!");
1413 unknown_entity = new_rd_entity(NULL, firm_unknown_type, new_id_from_str(UNKNOWN_ENTITY_NAME), firm_unknown_type);
1414 set_entity_visibility(unknown_entity, visibility_external_allocated);
1415 set_entity_ld_ident(unknown_entity, get_entity_ident(unknown_entity));
1417 current_ir_graph = get_const_code_irg();
1418 sym.entity_p = unknown_entity;
1419 /* TODO: we need two unknown_entities here, one for code and one for data */
1420 unknown_entity->value = new_SymConst(mode_P_data, sym, symconst_addr_ent);
1421 } /* firm_init_entity */