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)*/;
219 ent->attr.cmpd_attr.values = NULL;
221 } else if (is_method_entity(ent)) {
222 if (ent->attr.mtd_attr.param_access) {
223 DEL_ARR_F(ent->attr.mtd_attr.param_access);
224 ent->attr.mtd_attr.param_access = NULL;
226 if (ent->attr.mtd_attr.param_weight) {
227 DEL_ARR_F(ent->attr.mtd_attr.param_weight);
228 ent->attr.mtd_attr.param_weight = NULL;
231 } /* free_entity_attrs */
234 * Creates a deep copy of an entity.
236 static ir_entity *deep_entity_copy(ir_entity *old)
238 ir_entity *newe = XMALLOC(ir_entity);
241 if (is_compound_entity(old)) {
242 if (old->has_initializer) {
243 /* FIXME: the initializers are NOT copied */
245 newe->attr.cmpd_attr.values = NULL;
246 newe->attr.cmpd_attr.val_paths = NULL;
247 if (old->attr.cmpd_attr.values)
248 newe->attr.cmpd_attr.values = DUP_ARR_F(ir_node *, old->attr.cmpd_attr.values);
250 /* FIXME: the compound graph paths are NOT copied */
251 if (old->attr.cmpd_attr.val_paths)
252 newe->attr.cmpd_attr.val_paths = DUP_ARR_F(compound_graph_path *, old->attr.cmpd_attr.val_paths);
254 } else if (is_method_entity(old)) {
255 /* do NOT copy them, reanalyze. This might be the best solution */
256 newe->attr.mtd_attr.param_access = NULL;
257 newe->attr.mtd_attr.param_weight = NULL;
261 newe->nr = get_irp_new_node_nr();
266 * Copies the entity if the new_owner is different from the
267 * owner of the old entity, else returns the old entity.
270 copy_entity_own(ir_entity *old, ir_type *new_owner) {
272 assert(is_entity(old));
273 assert(is_compound_type(new_owner));
274 assert(get_type_state(new_owner) != layout_fixed);
276 if (old->owner == new_owner)
279 /* create a deep copy so we are safe of aliasing and double-freeing. */
280 newe = deep_entity_copy(old);
281 newe->owner = new_owner;
283 if (is_Class_type(new_owner)) {
284 newe->overwrites = NEW_ARR_F(ir_entity *, 0);
285 newe->overwrittenby = NEW_ARR_F(ir_entity *, 0);
288 insert_entity_in_owner(newe);
290 } /* copy_entity_own */
293 copy_entity_name(ir_entity *old, ident *new_name) {
295 assert(old && old->kind == k_entity);
297 if (old->name == new_name) return old;
298 newe = deep_entity_copy(old);
299 newe->name = new_name;
300 newe->ld_name = NULL;
302 if (is_Class_type(newe->owner)) {
303 newe->overwrites = DUP_ARR_F(ir_entity *, old->overwrites);
304 newe->overwrittenby = DUP_ARR_F(ir_entity *, old->overwrittenby);
306 insert_entity_in_owner(newe);
309 } /* copy_entity_name */
312 free_entity(ir_entity *ent) {
313 assert(ent && ent->kind == k_entity);
314 free_entity_attrs(ent);
319 /* Outputs a unique number for this node */
321 get_entity_nr(const ir_entity *ent) {
322 assert(ent && ent->kind == k_entity);
326 return (long)PTR_TO_INT(ent);
328 } /* get_entity_nr */
331 (get_entity_name)(const ir_entity *ent) {
332 return _get_entity_name(ent);
333 } /* get_entity_name */
336 (get_entity_ident)(const ir_entity *ent) {
337 return _get_entity_ident(ent);
338 } /* get_entity_ident */
341 (set_entity_ident)(ir_entity *ent, ident *id) {
342 _set_entity_ident(ent, id);
343 } /* set_entity_ident */
346 (get_entity_owner)(ir_entity *ent) {
347 return _get_entity_owner(ent);
348 } /* get_entity_owner */
351 set_entity_owner(ir_entity *ent, ir_type *owner) {
352 assert(is_entity(ent));
353 assert(is_compound_type(owner));
355 } /* set_entity_owner */
358 (get_entity_ld_ident)(ir_entity *ent) {
359 return _get_entity_ld_ident(ent);
360 } /* get_entity_ld_ident */
363 (set_entity_ld_ident)(ir_entity *ent, ident *ld_ident) {
364 _set_entity_ld_ident(ent, ld_ident);
365 } /* set_entity_ld_ident */
368 (get_entity_ld_name)(ir_entity *ent) {
369 return _get_entity_ld_name(ent);
370 } /* get_entity_ld_name */
373 (get_entity_type)(ir_entity *ent) {
374 return _get_entity_type(ent);
375 } /* get_entity_type */
378 (set_entity_type)(ir_entity *ent, ir_type *type) {
379 _set_entity_type(ent, type);
380 } /* set_entity_type */
383 (get_entity_allocation)(const ir_entity *ent) {
384 return _get_entity_allocation(ent);
385 } /* get_entity_allocation */
388 (set_entity_allocation)(ir_entity *ent, ir_allocation al) {
389 _set_entity_allocation(ent, al);
390 } /* set_entity_allocation */
392 /* return the name of the visibility */
393 const char *get_allocation_name(ir_allocation all)
395 #define X(a) case a: return #a
397 X(allocation_automatic);
398 X(allocation_parameter);
399 X(allocation_dynamic);
400 X(allocation_static);
401 default: return "BAD VALUE";
404 } /* get_allocation_name */
407 (get_entity_visibility)(const ir_entity *ent) {
408 return _get_entity_visibility(ent);
409 } /* get_entity_visibility */
412 set_entity_visibility(ir_entity *ent, ir_visibility vis) {
413 assert(ent && ent->kind == k_entity);
414 if (vis != visibility_local)
415 assert((ent->allocation == allocation_static) ||
416 (ent->allocation == allocation_automatic));
417 /* @@@ Test that the owner type is not local, but how??
418 && get_class_visibility(get_entity_owner(ent)) != local));*/
419 ent->visibility = vis;
420 } /* set_entity_visibility */
422 /* return the name of the visibility */
423 const char *get_visibility_name(ir_visibility vis)
425 #define X(a) case a: return #a
428 X(visibility_external_visible);
429 X(visibility_external_allocated);
430 default: return "BAD VALUE";
433 } /* get_visibility_name */
436 (get_entity_variability)(const ir_entity *ent) {
437 return _get_entity_variability(ent);
438 } /* get_entity_variability */
441 set_entity_variability(ir_entity *ent, ir_variability var)
443 assert(ent && ent->kind == k_entity);
444 if (var == variability_part_constant)
445 assert(is_Class_type(ent->type) || is_Struct_type(ent->type));
447 if ((is_compound_type(ent->type)) &&
448 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
449 /* Allocate data structures for constant values */
450 ent->attr.cmpd_attr.values = NEW_ARR_F(ir_node *, 0);
451 ent->attr.cmpd_attr.val_paths = NEW_ARR_F(compound_graph_path *, 0);
453 if ((is_atomic_type(ent->type)) &&
454 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
455 /* Set default constant value. */
456 ent->value = new_rd_Unknown(get_const_code_irg(), get_type_mode(ent->type));
459 if ((is_compound_type(ent->type)) &&
460 (var == variability_uninitialized) && (ent->variability != variability_uninitialized)) {
461 /* Free data structures for constant values */
462 DEL_ARR_F(ent->attr.cmpd_attr.values); ent->attr.cmpd_attr.values = NULL;
463 DEL_ARR_F(ent->attr.cmpd_attr.val_paths); ent->attr.cmpd_attr.val_paths = NULL;
465 ent->variability = var;
466 } /* set_entity_variability */
468 /* return the name of the variability */
469 const char *get_variability_name(ir_variability var)
471 #define X(a) case a: return #a
473 X(variability_uninitialized);
474 X(variability_initialized);
475 X(variability_part_constant);
476 X(variability_constant);
477 default: return "BAD VALUE";
480 } /* get_variability_name */
483 (get_entity_volatility)(const ir_entity *ent) {
484 return _get_entity_volatility(ent);
485 } /* get_entity_volatility */
488 (set_entity_volatility)(ir_entity *ent, ir_volatility vol) {
489 _set_entity_volatility(ent, vol);
490 } /* set_entity_volatility */
492 /* Return the name of the volatility. */
493 const char *get_volatility_name(ir_volatility var)
495 #define X(a) case a: return #a
497 X(volatility_non_volatile);
498 X(volatility_is_volatile);
499 default: return "BAD VALUE";
502 } /* get_volatility_name */
505 (get_entity_align)(const ir_entity *ent) {
506 return _get_entity_align(ent);
507 } /* get_entity_align */
510 (set_entity_align)(ir_entity *ent, ir_align a) {
511 _set_entity_align(ent, a);
512 } /* set_entity_align */
514 /* Return the name of the alignment. */
515 const char *get_align_name(ir_align a)
517 #define X(a) case a: return #a
519 X(align_non_aligned);
521 default: return "BAD VALUE";
524 } /* get_align_name */
527 (get_entity_peculiarity)(const ir_entity *ent) {
528 return _get_entity_peculiarity(ent);
529 } /* get_entity_peculiarity */
532 (set_entity_peculiarity)(ir_entity *ent, ir_peculiarity pec) {
533 _set_entity_peculiarity(ent, pec);
534 } /* set_entity_peculiarity */
536 /* Checks if an entity cannot be overridden anymore. */
537 int (is_entity_final)(const ir_entity *ent) {
538 return _is_entity_final(ent);
539 } /* is_entity_final */
541 /* Sets/resets the final flag of an entity. */
542 void (set_entity_final)(ir_entity *ent, int final) {
543 _set_entity_final(ent, final);
544 } /* set_entity_final */
546 /* Checks if an entity is compiler generated */
547 int (is_entity_compiler_generated)(const ir_entity *ent) {
548 return _is_entity_compiler_generated(ent);
549 } /* is_entity_compiler_generated */
551 /* Sets/resets the compiler generated flag */
552 void (set_entity_compiler_generated)(ir_entity *ent, int flag) {
553 _set_entity_compiler_generated(ent, flag);
554 } /* set_entity_compiler_generated */
556 /* Checks if an entity is marked by the backend */
557 int (is_entity_backend_marked)(const ir_entity *ent) {
558 return _is_entity_backend_marked(ent);
559 } /* is_entity_backend_marked */
561 /* Sets/resets the compiler generated flag */
562 void (set_entity_backend_marked)(ir_entity *ent, int flag) {
563 _set_entity_backend_marked(ent, flag);
564 } /* set_entity_backend_marked */
566 ir_entity_usage (get_entity_usage)(const ir_entity *ent) {
567 return _get_entity_usage(ent);
570 void (set_entity_usage)(ir_entity *ent, ir_entity_usage flags) {
571 _set_entity_usage(ent, flags);
574 /* Get the entity's stickyness */
576 (get_entity_stickyness)(const ir_entity *ent) {
577 return _get_entity_stickyness(ent);
578 } /* get_entity_stickyness */
580 /* Set the entity's stickyness */
582 (set_entity_stickyness)(ir_entity *ent, ir_stickyness stickyness) {
583 _set_entity_stickyness(ent, stickyness);
584 } /* set_entity_stickyness */
586 /* Set has no effect for existent entities of type method. */
588 get_atomic_ent_value(ir_entity *ent)
590 assert(ent && is_atomic_entity(ent));
591 assert(ent->variability != variability_uninitialized);
592 return skip_Id(ent->value);
593 } /* get_atomic_ent_value */
596 set_atomic_ent_value(ir_entity *ent, ir_node *val) {
597 assert(is_atomic_entity(ent) && (ent->variability != variability_uninitialized));
598 if (is_Method_type(ent->type) && (ent->peculiarity == peculiarity_existent))
600 assert(get_irn_mode(val) == get_type_mode(ent->type));
602 } /* set_atomic_ent_value */
604 /* Returns true if the the node is representable as code on
606 int is_irn_const_expression(ir_node *n) {
609 /* we are in danger iff an exception will arise. TODO: be more precisely,
610 * for instance Div. will NOT rise if divisor != 0
612 if (is_binop(n) && !is_fragile_op(n))
613 return is_irn_const_expression(get_binop_left(n)) && is_irn_const_expression(get_binop_right(n));
616 switch (get_irn_opcode(n)) {
623 return is_irn_const_expression(get_irn_n(n, 0));
628 } /* is_irn_const_expression */
631 * Copies a firm subgraph that complies to the restrictions for
632 * constant expressions to current_block in current_ir_graph.
634 ir_node *copy_const_value(dbg_info *dbg, ir_node *n) {
638 /* @@@ GL I think we should implement this using the routines from irgopt for
639 dead node elimination/inlineing. */
642 switch (get_irn_opcode(n)) {
644 nn = new_d_Const_type(dbg, get_Const_tarval(n), get_Const_type(n));
647 nn = new_d_SymConst_type(dbg, get_irn_mode(n), get_SymConst_symbol(n), get_SymConst_kind(n),
648 get_SymConst_value_type(n));
651 nn = new_d_Add(dbg, copy_const_value(dbg, get_Add_left(n)),
652 copy_const_value(dbg, get_Add_right(n)), m); break;
654 nn = new_d_Sub(dbg, copy_const_value(dbg, get_Sub_left(n)),
655 copy_const_value(dbg, get_Sub_right(n)), m); break;
657 nn = new_d_Mul(dbg, copy_const_value(dbg, get_Mul_left(n)),
658 copy_const_value(dbg, get_Mul_right(n)), m); break;
660 nn = new_d_And(dbg, copy_const_value(dbg, get_And_left(n)),
661 copy_const_value(dbg, get_And_right(n)), m); break;
663 nn = new_d_Or(dbg, copy_const_value(dbg, get_Or_left(n)),
664 copy_const_value(dbg, get_Or_right(n)), m); break;
666 nn = new_d_Eor(dbg, copy_const_value(dbg, get_Eor_left(n)),
667 copy_const_value(dbg, get_Eor_right(n)), m); break;
669 nn = new_d_Cast(dbg, copy_const_value(dbg, get_Cast_op(n)), get_Cast_type(n)); break;
671 nn = new_d_Conv(dbg, copy_const_value(dbg, get_Conv_op(n)), m); break;
673 nn = new_d_Unknown(m); break;
675 assert(0 && "opcode invalid or not implemented");
680 } /* copy_const_value */
682 static ir_initializer_t null_initializer = { IR_INITIALIZER_NULL };
684 ir_initializer_t *get_initializer_null(void)
686 return &null_initializer;
689 ir_initializer_t *create_initializer_const(ir_node *value)
691 struct obstack *obst = get_irg_obstack(get_const_code_irg());
693 ir_initializer_t *initializer
694 = obstack_alloc(obst, sizeof(ir_initializer_const_t));
695 initializer->kind = IR_INITIALIZER_CONST;
696 initializer->consti.value = value;
701 ir_initializer_t *create_initializer_tarval(tarval *tv)
703 struct obstack *obst = get_irg_obstack(get_const_code_irg());
705 ir_initializer_t *initializer
706 = obstack_alloc(obst, sizeof(ir_initializer_tarval_t));
707 initializer->kind = IR_INITIALIZER_TARVAL;
708 initializer->tarval.value = tv;
713 ir_initializer_t *create_initializer_compound(unsigned n_entries)
715 struct obstack *obst = get_irg_obstack(get_const_code_irg());
718 size_t size = sizeof(ir_initializer_compound_t)
719 + (n_entries-1) * sizeof(ir_initializer_t*);
721 ir_initializer_t *initializer = obstack_alloc(obst, size);
722 initializer->kind = IR_INITIALIZER_COMPOUND;
723 initializer->compound.n_initializers = n_entries;
725 for(i = 0; i < n_entries; ++i) {
726 initializer->compound.initializers[i] = get_initializer_null();
732 ir_node *get_initializer_const_value(const ir_initializer_t *initializer)
734 assert(initializer->kind == IR_INITIALIZER_CONST);
735 return initializer->consti.value;
738 tarval *get_initializer_tarval_value(const ir_initializer_t *initializer)
740 assert(initializer->kind == IR_INITIALIZER_TARVAL);
741 return initializer->tarval.value;
744 unsigned get_initializer_compound_n_entries(const ir_initializer_t *initializer)
746 assert(initializer->kind == IR_INITIALIZER_COMPOUND);
747 return initializer->compound.n_initializers;
750 void set_initializer_compound_value(ir_initializer_t *initializer,
751 unsigned index, ir_initializer_t *value)
753 assert(initializer->kind == IR_INITIALIZER_COMPOUND);
754 assert(index < initializer->compound.n_initializers);
756 initializer->compound.initializers[index] = value;
759 ir_initializer_t *get_initializer_compound_value(
760 const ir_initializer_t *initializer, unsigned index)
762 assert(initializer->kind == IR_INITIALIZER_COMPOUND);
763 assert(index < initializer->compound.n_initializers);
765 return initializer->compound.initializers[index];
768 ir_initializer_kind_t get_initializer_kind(const ir_initializer_t *initializer)
770 return initializer->kind;
773 static void check_entity_initializer(ir_entity *entity)
779 void set_entity_initializer(ir_entity *entity, ir_initializer_t *initializer)
781 entity->attr.initializer = initializer;
782 entity->has_initializer = 1;
783 check_entity_initializer(entity);
786 ir_initializer_t *get_entity_initializer(const ir_entity *entity)
788 assert(entity->has_initializer);
789 return entity->attr.initializer;
792 /* Creates a new compound graph path. */
793 compound_graph_path *
794 new_compound_graph_path(ir_type *tp, int length) {
795 compound_graph_path *res;
797 assert(is_compound_type(tp));
800 res = xmalloc(sizeof(*res) + (length-1) * sizeof(res->list[0]));
801 memset(res, 0, sizeof(*res) + (length-1) * sizeof(res->list[0]));
802 res->kind = k_ir_compound_graph_path;
807 } /* new_compound_graph_path */
809 /* Frees an graph path object */
810 void free_compound_graph_path (compound_graph_path *gr) {
811 assert(gr && is_compound_graph_path(gr));
814 } /* free_compound_graph_path */
816 /* Returns non-zero if an object is a compound graph path */
817 int is_compound_graph_path(const void *thing) {
818 return (get_kind(thing) == k_ir_compound_graph_path);
819 } /* is_compound_graph_path */
821 /* Checks whether the path up to pos is correct. If the path contains a NULL,
822 * assumes the path is not complete and returns 'true'. */
823 int is_proper_compound_graph_path(compound_graph_path *gr, int pos) {
826 ir_type *owner = gr->tp;
828 for (i = 0; i <= pos; i++) {
829 node = get_compound_graph_path_node(gr, i);
831 /* Path not yet complete. */
833 if (get_entity_owner(node) != owner)
835 owner = get_entity_type(node);
837 if (pos == get_compound_graph_path_length(gr))
838 if (!is_atomic_type(owner))
841 } /* is_proper_compound_graph_path */
843 /* Returns the length of a graph path */
844 int get_compound_graph_path_length(const compound_graph_path *gr) {
845 assert(gr && is_compound_graph_path(gr));
847 } /* get_compound_graph_path_length */
850 get_compound_graph_path_node(const compound_graph_path *gr, int pos) {
851 assert(gr && is_compound_graph_path(gr));
852 assert(pos >= 0 && pos < gr->len);
853 return gr->list[pos].node;
854 } /* get_compound_graph_path_node */
857 set_compound_graph_path_node(compound_graph_path *gr, int pos, ir_entity *node) {
858 assert(gr && is_compound_graph_path(gr));
859 assert(pos >= 0 && pos < gr->len);
860 assert(is_entity(node));
861 gr->list[pos].node = node;
862 assert(is_proper_compound_graph_path(gr, pos));
863 } /* set_compound_graph_path_node */
866 get_compound_graph_path_array_index(const compound_graph_path *gr, int pos) {
867 assert(gr && is_compound_graph_path(gr));
868 assert(pos >= 0 && pos < gr->len);
869 return gr->list[pos].index;
870 } /* get_compound_graph_path_array_index */
873 set_compound_graph_path_array_index(compound_graph_path *gr, int pos, int index) {
874 assert(gr && is_compound_graph_path(gr));
875 assert(pos >= 0 && pos < gr->len);
876 gr->list[pos].index = index;
877 } /* set_compound_graph_path_array_index */
880 get_compound_graph_path_type(const compound_graph_path *gr) {
881 assert(gr && is_compound_graph_path(gr));
885 /* A value of a compound entity is a pair of value and the corresponding path to a member of
888 add_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path) {
889 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
890 assert(is_compound_graph_path(path));
891 ARR_APP1(ir_node *, ent->attr.cmpd_attr.values, val);
892 ARR_APP1(compound_graph_path *, ent->attr.cmpd_attr.val_paths, path);
893 } /* add_compound_ent_value_w_path */
896 set_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path, int pos) {
897 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
898 assert(is_compound_graph_path(path));
899 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
900 ent->attr.cmpd_attr.values[pos] = val;
901 ent->attr.cmpd_attr.val_paths[pos] = path;
902 } /* set_compound_ent_value_w_path */
905 get_compound_ent_n_values(ir_entity *ent) {
906 assert(!ent->has_initializer);
907 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
908 return ARR_LEN(ent->attr.cmpd_attr.values);
909 } /* get_compound_ent_n_values */
912 get_compound_ent_value(ir_entity *ent, int pos) {
913 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
914 assert(!ent->has_initializer);
915 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
916 return ent->attr.cmpd_attr.values[pos];
917 } /* get_compound_ent_value */
919 compound_graph_path *
920 get_compound_ent_value_path(ir_entity *ent, int pos) {
921 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
922 assert(!ent->has_initializer);
923 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.val_paths));
924 return ent->attr.cmpd_attr.val_paths[pos];
925 } /* get_compound_ent_value_path */
928 * Returns non-zero, if two compound_graph_pathes are equal
930 * @param path1 the first path
931 * @param path2 the second path
933 static int equal_paths(compound_graph_path *path1, compound_graph_path *path2) {
935 int len1 = get_compound_graph_path_length(path1);
936 int len2 = get_compound_graph_path_length(path2);
938 if (len2 != len1) return 0;
940 for (i = 0; i < len1; i++) {
942 ir_entity *node1 = get_compound_graph_path_node(path1, i);
943 ir_entity *node2 = get_compound_graph_path_node(path2, i);
945 if (node1 != node2) return 0;
947 tp = get_entity_owner(node1);
948 if (is_Array_type(tp)) {
949 int index1 = get_compound_graph_path_array_index(path1, i);
950 int index2 = get_compound_graph_path_array_index(path2, i);
951 if (index1 != index2)
959 * Returns the position of a value with the given path.
960 * The path must contain array indices for all array element entities.
962 * @todo This implementation is very slow (O(number of initializers * |path|)
963 * and should be replaced when the new tree oriented
964 * value representation is finally implemented.
966 static int get_compound_ent_pos_by_path(ir_entity *ent, compound_graph_path *path) {
967 int i, n_paths = get_compound_ent_n_values(ent);
969 for (i = 0; i < n_paths; i ++) {
970 compound_graph_path *gr = get_compound_ent_value_path(ent, i);
971 if (equal_paths(gr, path))
975 } /* get_compound_ent_pos_by_path */
977 /* Returns a constant value given the access path.
978 * The path must contain array indices for all array element entities. */
979 ir_node *get_compound_ent_value_by_path(ir_entity *ent, compound_graph_path *path) {
980 int pos = get_compound_ent_pos_by_path(ent, path);
982 return get_compound_ent_value(ent, pos);
984 } /* get_compound_ent_value_by_path */
988 remove_compound_ent_value(ir_entity *ent, ir_entity *value_ent) {
990 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
992 n = ARR_LEN(ent->attr.cmpd_attr.val_paths);
993 for (i = 0; i < n; ++i) {
994 compound_graph_path *path = ent->attr.cmpd_attr.val_paths[i];
995 if (path->list[path->len-1].node == value_ent) {
996 for (; i < n - 1; ++i) {
997 ent->attr.cmpd_attr.val_paths[i] = ent->attr.cmpd_attr.val_paths[i+1];
998 ent->attr.cmpd_attr.values[i] = ent->attr.cmpd_attr.values[i+1];
1000 ARR_SETLEN(ir_entity*, ent->attr.cmpd_attr.val_paths, n - 1);
1001 ARR_SETLEN(ir_node*, ent->attr.cmpd_attr.values, n - 1);
1005 } /* remove_compound_ent_value */
1008 add_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member) {
1009 compound_graph_path *path;
1010 ir_type *owner_tp = get_entity_owner(member);
1011 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
1012 path = new_compound_graph_path(get_entity_type(ent), 1);
1013 path->list[0].node = member;
1014 if (is_Array_type(owner_tp)) {
1018 assert(get_array_n_dimensions(owner_tp) == 1 && has_array_lower_bound(owner_tp, 0));
1019 max = get_array_lower_bound_int(owner_tp, 0) -1;
1020 for (i = 0, n = get_compound_ent_n_values(ent); i < n; ++i) {
1021 int index = get_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0);
1026 path->list[0].index = max + 1;
1028 add_compound_ent_value_w_path(ent, val, path);
1029 } /* add_compound_ent_value */
1033 get_compound_ent_value_member(ir_entity *ent, int pos) {
1034 compound_graph_path *path;
1035 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
1036 path = get_compound_ent_value_path(ent, pos);
1038 return get_compound_graph_path_node(path, get_compound_graph_path_length(path)-1);
1039 } /* get_compound_ent_value_member */
1042 set_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member, int pos) {
1043 compound_graph_path *path;
1044 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
1045 path = get_compound_ent_value_path(ent, pos);
1046 set_compound_graph_path_node(path, 0, member);
1047 set_compound_ent_value_w_path(ent, val, path, pos);
1048 } /* set_compound_ent_value */
1051 set_array_entity_values(ir_entity *ent, tarval **values, int num_vals) {
1053 ir_graph *rem = current_ir_graph;
1054 ir_type *arrtp = get_entity_type(ent);
1056 ir_type *elttp = get_array_element_type(arrtp);
1058 assert(is_Array_type(arrtp));
1059 assert(get_array_n_dimensions(arrtp) == 1);
1060 /* One bound is sufficient, the number of constant fields makes the
1062 assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
1063 assert(get_entity_variability(ent) != variability_uninitialized);
1064 current_ir_graph = get_const_code_irg();
1066 for (i = 0; i < num_vals; i++) {
1067 val = new_Const_type(values[i], elttp);
1068 add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
1069 set_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0, i);
1071 current_ir_graph = rem;
1072 } /* set_array_entity_values */
1074 /* Return the overall offset of value at position pos in bytes. */
1075 unsigned get_compound_ent_value_offset_bytes(ir_entity *ent, int pos) {
1076 compound_graph_path *path;
1078 unsigned offset = 0;
1081 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
1083 path = get_compound_ent_value_path(ent, pos);
1084 path_len = get_compound_graph_path_length(path);
1087 for (i = 0; i < path_len; ++i) {
1088 if (is_Array_type(curr_tp)) {
1089 ir_type *elem_type = get_array_element_type(curr_tp);
1090 unsigned size = get_type_size_bytes(elem_type);
1091 unsigned align = get_type_alignment_bytes(elem_type);
1095 if(size % align > 0) {
1096 size += align - (size % align);
1098 idx = get_compound_graph_path_array_index(path, i);
1100 offset += size * idx;
1101 curr_tp = elem_type;
1103 ir_entity *node = get_compound_graph_path_node(path, i);
1104 offset += get_entity_offset(node);
1105 curr_tp = get_entity_type(node);
1110 } /* get_compound_ent_value_offset_bytes */
1112 /* Return the offset in bits from the last byte address. */
1113 unsigned get_compound_ent_value_offset_bit_remainder(ir_entity *ent, int pos) {
1114 compound_graph_path *path;
1116 ir_entity *last_node;
1118 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
1120 path = get_compound_ent_value_path(ent, pos);
1121 path_len = get_compound_graph_path_length(path);
1122 last_node = get_compound_graph_path_node(path, path_len - 1);
1124 if(last_node == NULL)
1127 return get_entity_offset_bits_remainder(last_node);
1128 } /* get_compound_ent_value_offset_bit_remainder */
1131 (get_entity_offset)(const ir_entity *ent) {
1132 return _get_entity_offset(ent);
1133 } /* get_entity_offset */
1136 (set_entity_offset)(ir_entity *ent, int offset) {
1137 _set_entity_offset(ent, offset);
1138 } /* set_entity_offset */
1141 (get_entity_offset_bits_remainder)(const ir_entity *ent) {
1142 return _get_entity_offset_bits_remainder(ent);
1143 } /* get_entity_offset_bits_remainder */
1146 (set_entity_offset_bits_remainder)(ir_entity *ent, unsigned char offset) {
1147 _set_entity_offset_bits_remainder(ent, offset);
1148 } /* set_entity_offset_bits_remainder */
1151 add_entity_overwrites(ir_entity *ent, ir_entity *overwritten) {
1153 ir_type *owner = get_entity_owner(ent);
1154 ir_type *ovw_ovner = get_entity_owner(overwritten);
1155 assert(is_Class_type(owner));
1156 assert(is_Class_type(ovw_ovner));
1157 assert(! is_class_final(ovw_ovner));
1159 ARR_APP1(ir_entity *, ent->overwrites, overwritten);
1160 ARR_APP1(ir_entity *, overwritten->overwrittenby, ent);
1161 } /* add_entity_overwrites */
1164 get_entity_n_overwrites(ir_entity *ent) {
1165 assert(is_Class_type(get_entity_owner(ent)));
1166 return (ARR_LEN(ent->overwrites));
1167 } /* get_entity_n_overwrites */
1170 get_entity_overwrites_index(ir_entity *ent, ir_entity *overwritten) {
1172 assert(is_Class_type(get_entity_owner(ent)));
1173 n = get_entity_n_overwrites(ent);
1174 for (i = 0; i < n; ++i) {
1175 if (get_entity_overwrites(ent, i) == overwritten)
1179 } /* get_entity_overwrites_index */
1182 get_entity_overwrites(ir_entity *ent, int pos) {
1183 assert(is_Class_type(get_entity_owner(ent)));
1184 assert(pos < get_entity_n_overwrites(ent));
1185 return ent->overwrites[pos];
1186 } /* get_entity_overwrites */
1189 set_entity_overwrites(ir_entity *ent, int pos, ir_entity *overwritten) {
1190 assert(is_Class_type(get_entity_owner(ent)));
1191 assert(pos < get_entity_n_overwrites(ent));
1192 ent->overwrites[pos] = overwritten;
1193 } /* set_entity_overwrites */
1196 remove_entity_overwrites(ir_entity *ent, ir_entity *overwritten) {
1198 assert(is_Class_type(get_entity_owner(ent)));
1199 n = ARR_LEN(ent->overwrites);
1200 for (i = 0; i < n; ++i) {
1201 if (ent->overwrites[i] == overwritten) {
1202 for (; i < n - 1; i++)
1203 ent->overwrites[i] = ent->overwrites[i+1];
1204 ARR_SETLEN(ir_entity*, ent->overwrites, n - 1);
1208 } /* remove_entity_overwrites */
1211 add_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites) {
1212 add_entity_overwrites(overwrites, ent);
1213 } /* add_entity_overwrittenby */
1216 get_entity_n_overwrittenby(ir_entity *ent) {
1217 assert(is_Class_type(get_entity_owner(ent)));
1218 return ARR_LEN(ent->overwrittenby);
1219 } /* get_entity_n_overwrittenby */
1222 get_entity_overwrittenby_index(ir_entity *ent, ir_entity *overwrites) {
1224 assert(is_Class_type(get_entity_owner(ent)));
1225 n = get_entity_n_overwrittenby(ent);
1226 for (i = 0; i < n; ++i) {
1227 if (get_entity_overwrittenby(ent, i) == overwrites)
1231 } /* get_entity_overwrittenby_index */
1234 get_entity_overwrittenby(ir_entity *ent, int pos) {
1235 assert(is_Class_type(get_entity_owner(ent)));
1236 assert(pos < get_entity_n_overwrittenby(ent));
1237 return ent->overwrittenby[pos];
1238 } /* get_entity_overwrittenby */
1241 set_entity_overwrittenby(ir_entity *ent, int pos, ir_entity *overwrites) {
1242 assert(is_Class_type(get_entity_owner(ent)));
1243 assert(pos < get_entity_n_overwrittenby(ent));
1244 ent->overwrittenby[pos] = overwrites;
1245 } /* set_entity_overwrittenby */
1247 void remove_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites) {
1249 assert(is_Class_type(get_entity_owner(ent)));
1251 n = ARR_LEN(ent->overwrittenby);
1252 for (i = 0; i < n; ++i) {
1253 if (ent->overwrittenby[i] == overwrites) {
1254 for(; i < n - 1; ++i)
1255 ent->overwrittenby[i] = ent->overwrittenby[i+1];
1256 ARR_SETLEN(ir_entity*, ent->overwrittenby, n - 1);
1260 } /* remove_entity_overwrittenby */
1262 /* A link to store intermediate information */
1264 (get_entity_link)(const ir_entity *ent) {
1265 return _get_entity_link(ent);
1266 } /* get_entity_link */
1269 (set_entity_link)(ir_entity *ent, void *l) {
1270 _set_entity_link(ent, l);
1271 } /* set_entity_link */
1274 (get_entity_irg)(const ir_entity *ent) {
1275 return _get_entity_irg(ent);
1276 } /* get_entity_irg */
1279 set_entity_irg(ir_entity *ent, ir_graph *irg) {
1280 assert(is_method_entity(ent));
1281 /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
1282 * Methode selbst nicht mehr aufgerufen werden kann, die Entität
1283 * aber erhalten bleiben soll? Wandle die Entitaet in description oder
1286 assert((irg && ent->peculiarity == peculiarity_existent) ||
1287 (!irg && (ent->peculiarity == peculiarity_existent)
1288 && (ent -> visibility == visibility_external_allocated)) ||
1289 (!irg && ent->peculiarity == peculiarity_description) ||
1290 (!irg && ent->peculiarity == peculiarity_inherited));
1291 ent->attr.mtd_attr.irg = irg;
1292 } /* set_entity_irg */
1294 unsigned get_entity_vtable_number(const ir_entity *ent) {
1295 assert(is_method_entity((ir_entity *)ent));
1296 return ent->attr.mtd_attr.vtable_number;
1297 } /* get_entity_vtable_number */
1299 void set_entity_vtable_number(ir_entity *ent, unsigned vtable_number) {
1300 assert(is_method_entity(ent));
1301 ent->attr.mtd_attr.vtable_number = vtable_number;
1302 } /* set_entity_vtable_number */
1305 (is_entity)(const void *thing) {
1306 return _is_entity(thing);
1309 int is_atomic_entity(ir_entity *ent) {
1310 ir_type *t = get_entity_type(ent);
1311 const tp_op *op = get_type_tpop(t);
1312 return (op == type_primitive || op == type_pointer ||
1313 op == type_enumeration || op == type_method);
1314 } /* is_atomic_entity */
1316 int is_compound_entity(ir_entity *ent) {
1317 ir_type *t = get_entity_type(ent);
1318 const tp_op *op = get_type_tpop(t);
1319 return (op == type_class || op == type_struct ||
1320 op == type_array || op == type_union);
1321 } /* is_compound_entity */
1323 int is_method_entity(ir_entity *ent) {
1324 ir_type *t = get_entity_type(ent);
1325 return is_Method_type(t);
1326 } /* is_method_entity */
1329 * @todo not implemented!!! */
1330 int equal_entity(ir_entity *ent1, ir_entity *ent2) {
1333 fprintf(stderr, " calling unimplemented equal entity!!! \n");
1335 } /* equal_entity */
1338 ir_visited_t (get_entity_visited)(ir_entity *ent) {
1339 return _get_entity_visited(ent);
1340 } /* get_entity_visited */
1342 void (set_entity_visited)(ir_entity *ent, ir_visited_t num) {
1343 _set_entity_visited(ent, num);
1344 } /* set_entity_visited */
1346 /* Sets visited field in ir_entity to entity_visited. */
1347 void (mark_entity_visited)(ir_entity *ent) {
1348 _mark_entity_visited(ent);
1349 } /* mark_entity_visited */
1351 int (entity_visited)(ir_entity *ent) {
1352 return _entity_visited(ent);
1353 } /* entity_visited */
1355 int (entity_not_visited)(ir_entity *ent) {
1356 return _entity_not_visited(ent);
1357 } /* entity_not_visited */
1359 /* Returns the mask of the additional entity properties. */
1360 unsigned get_entity_additional_properties(ir_entity *ent) {
1363 assert(is_method_entity(ent));
1365 /* first check, if the graph has additional properties */
1366 irg = get_entity_irg(ent);
1369 return get_irg_additional_properties(irg);
1371 if (ent->attr.mtd_attr.irg_add_properties & mtp_property_inherited)
1372 return get_method_additional_properties(get_entity_type(ent));
1374 return ent->attr.mtd_attr.irg_add_properties;
1375 } /* get_entity_additional_properties */
1377 /* Sets the mask of the additional graph properties. */
1378 void set_entity_additional_properties(ir_entity *ent, unsigned property_mask)
1382 assert(is_method_entity(ent));
1384 /* first check, if the graph exists */
1385 irg = get_entity_irg(ent);
1387 set_irg_additional_properties(irg, property_mask);
1389 /* do not allow to set the mtp_property_inherited flag or
1390 * the automatic inheritance of flags will not work */
1391 ent->attr.mtd_attr.irg_add_properties = property_mask & ~mtp_property_inherited;
1393 } /* set_entity_additional_properties */
1395 /* Sets one additional graph property. */
1396 void set_entity_additional_property(ir_entity *ent, mtp_additional_property flag)
1400 assert(is_method_entity(ent));
1402 /* first check, if the graph exists */
1403 irg = get_entity_irg(ent);
1405 set_irg_additional_property(irg, flag);
1407 unsigned mask = ent->attr.mtd_attr.irg_add_properties;
1409 if (mask & mtp_property_inherited)
1410 mask = get_method_additional_properties(get_entity_type(ent));
1412 /* do not allow to set the mtp_property_inherited flag or
1413 * the automatic inheritance of flags will not work */
1414 ent->attr.mtd_attr.irg_add_properties = mask | (flag & ~mtp_property_inherited);
1416 } /* set_entity_additional_property */
1418 /* Returns the class type that this type info entity represents or NULL
1419 if ent is no type info entity. */
1420 ir_type *(get_entity_repr_class)(const ir_entity *ent) {
1421 return _get_entity_repr_class(ent);
1422 } /* get_entity_repr_class */
1424 dbg_info *(get_entity_dbg_info)(const ir_entity *ent) {
1425 return _get_entity_dbg_info(ent);
1426 } /* get_entity_dbg_info */
1428 void (set_entity_dbg_info)(ir_entity *ent, dbg_info *db) {
1429 _set_entity_dbg_info(ent, db);
1430 } /* set_entity_dbg_info */
1432 /* Initialize entity module. */
1433 void firm_init_entity(void)
1435 symconst_symbol sym;
1437 assert(firm_unknown_type && "Call init_type() before firm_init_entity()!");
1438 assert(!unknown_entity && "Call firm_init_entity() only once!");
1440 unknown_entity = new_rd_entity(NULL, firm_unknown_type, new_id_from_str(UNKNOWN_ENTITY_NAME), firm_unknown_type);
1441 set_entity_visibility(unknown_entity, visibility_external_allocated);
1442 set_entity_ld_ident(unknown_entity, get_entity_ident(unknown_entity));
1444 current_ir_graph = get_const_code_irg();
1445 sym.entity_p = unknown_entity;
1446 /* TODO: we need two unknown_entities here, one for code and one for data */
1447 unknown_entity->value = new_SymConst(mode_P_data, sym, symconst_addr_ent);
1448 } /* firm_init_entity */