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
43 #include "irgraph_t.h"
44 #include "callgraph.h"
47 /*-----------------------------------------------------------------*/
49 /*-----------------------------------------------------------------*/
51 ir_entity *unknown_entity = NULL;
53 ir_entity *get_unknown_entity(void) { return unknown_entity; }
55 /** The name of the unknown entity. */
56 #define UNKNOWN_ENTITY_NAME "unknown_entity"
58 /*-----------------------------------------------------------------*/
60 /*-----------------------------------------------------------------*/
63 * Add an entity to it's already set owner type.
65 static inline void insert_entity_in_owner(ir_entity *ent) {
66 ir_type *owner = ent->owner;
67 switch (get_type_tpop_code(owner)) {
69 add_class_member(owner, ent);
72 add_struct_member(owner, ent);
75 add_union_member(owner, ent);
78 set_array_element_entity(owner, ent);
81 panic("Unsupported type kind");
83 } /* insert_entity_in_owner */
86 * Creates a new entity. This entity is NOT inserted in the owner type.
88 * @param db debug info for this entity
89 * @param owner the owner type of the new entity
90 * @param name the name of the new entity
91 * @param type the type of the new entity
93 * @return the new created entity
95 static inline ir_entity *
96 new_rd_entity(dbg_info *db, ir_type *owner, ident *name, ir_type *type)
101 assert(!id_contains_char(name, ' ') && "entity name should not contain spaces");
103 res = XMALLOCZ(ir_entity);
105 res->kind = k_entity;
111 res->allocation = allocation_automatic;
112 res->visibility = visibility_local;
113 res->volatility = volatility_non_volatile;
114 res->aligned = align_is_aligned;
115 res->stickyness = stickyness_unsticky;
116 res->peculiarity = peculiarity_existent;
117 res->usage = ir_usage_unknown;
119 res->compiler_gen = 0;
120 res->backend_marked = 0;
122 res->offset_bit_remainder = 0;
125 res->repr_class = NULL;
127 if (is_Method_type(type)) {
129 ir_mode *mode = is_Method_type(type) ? mode_P_code : mode_P_data;
131 rem = current_ir_graph;
132 current_ir_graph = get_const_code_irg();
133 res->value = new_SymConst(mode, sym, symconst_addr_ent);
134 current_ir_graph = rem;
135 res->allocation = allocation_static;
136 res->variability = variability_constant;
137 res->attr.mtd_attr.irg_add_properties = mtp_property_inherited;
138 res->attr.mtd_attr.vtable_number = VTABLE_NUM_NOT_SET;
139 res->attr.mtd_attr.param_access = NULL;
140 res->attr.mtd_attr.param_weight = NULL;
141 res->attr.mtd_attr.irg = NULL;
142 } else if (is_compound_type(type)) {
143 res->variability = variability_uninitialized;
145 res->attr.cmpd_attr.values = NULL;
146 res->attr.cmpd_attr.val_paths = NULL;
147 } else if (is_code_type(type)) {
148 res->attr.code_attr.label = (ir_label_t) -1;
150 res->variability = variability_uninitialized;
154 if (is_Class_type(owner)) {
155 res->overwrites = NEW_ARR_F(ir_entity *, 0);
156 res->overwrittenby = NEW_ARR_F(ir_entity *, 0);
158 res->overwrites = NULL;
159 res->overwrittenby = NULL;
163 res->nr = get_irp_new_node_nr();
164 #endif /* DEBUG_libfirm */
167 set_entity_dbg_info(res, db);
170 } /* new_rd_entity */
173 new_d_entity(ir_type *owner, ident *name, ir_type *type, dbg_info *db) {
176 assert(is_compound_type(owner));
177 res = new_rd_entity(db, owner, name, type);
178 /* Remember entity in it's owner. */
179 insert_entity_in_owner(res);
181 hook_new_entity(res);
186 new_entity(ir_type *owner, ident *name, ir_type *type) {
187 return new_d_entity(owner, name, type, NULL);
191 * Free entity attributes.
193 * @param ent the entity
195 static void free_entity_attrs(ir_entity *ent) {
197 if (get_type_tpop(get_entity_owner(ent)) == type_class) {
198 DEL_ARR_F(ent->overwrites); ent->overwrites = NULL;
199 DEL_ARR_F(ent->overwrittenby); ent->overwrittenby = NULL;
201 assert(ent->overwrites == NULL);
202 assert(ent->overwrittenby == NULL);
204 if (is_compound_entity(ent)) {
205 if (ent->has_initializer) {
206 /* TODO: free initializers */
208 if (ent->attr.cmpd_attr.val_paths) {
209 for (i = get_compound_ent_n_values(ent) - 1; i >= 0; --i)
210 if (ent->attr.cmpd_attr.val_paths[i]) {
211 /* free_compound_graph_path(ent->attr.cmpd_attr.val_paths[i]) ; * @@@ warum nich? */
212 /* Geht nich: wird mehrfach verwendet!!! ==> mehrfach frei gegeben. */
213 /* DEL_ARR_F(ent->attr.cmpd_attr.val_paths); */
215 ent->attr.cmpd_attr.val_paths = NULL;
217 if (ent->attr.cmpd_attr.values) {
218 /*DEL_ARR_F(ent->attr.cmpd_attr.values)*/;
220 ent->attr.cmpd_attr.values = NULL;
222 } else if (is_method_entity(ent)) {
223 if (ent->attr.mtd_attr.param_access) {
224 DEL_ARR_F(ent->attr.mtd_attr.param_access);
225 ent->attr.mtd_attr.param_access = NULL;
227 if (ent->attr.mtd_attr.param_weight) {
228 DEL_ARR_F(ent->attr.mtd_attr.param_weight);
229 ent->attr.mtd_attr.param_weight = NULL;
232 } /* free_entity_attrs */
235 * Creates a deep copy of an entity.
237 static ir_entity *deep_entity_copy(ir_entity *old)
239 ir_entity *newe = XMALLOC(ir_entity);
242 if (is_compound_entity(old)) {
243 if (old->has_initializer) {
244 /* FIXME: the initializers are NOT copied */
246 newe->attr.cmpd_attr.values = NULL;
247 newe->attr.cmpd_attr.val_paths = NULL;
248 if (old->attr.cmpd_attr.values)
249 newe->attr.cmpd_attr.values = DUP_ARR_F(ir_node *, old->attr.cmpd_attr.values);
251 /* FIXME: the compound graph paths are NOT copied */
252 if (old->attr.cmpd_attr.val_paths)
253 newe->attr.cmpd_attr.val_paths = DUP_ARR_F(compound_graph_path *, old->attr.cmpd_attr.val_paths);
255 } else if (is_method_entity(old)) {
256 /* do NOT copy them, reanalyze. This might be the best solution */
257 newe->attr.mtd_attr.param_access = NULL;
258 newe->attr.mtd_attr.param_weight = NULL;
262 newe->nr = get_irp_new_node_nr();
267 * Copies the entity if the new_owner is different from the
268 * owner of the old entity, else returns the old entity.
271 copy_entity_own(ir_entity *old, ir_type *new_owner) {
273 assert(is_entity(old));
274 assert(is_compound_type(new_owner));
275 assert(get_type_state(new_owner) != layout_fixed);
277 if (old->owner == new_owner)
280 /* create a deep copy so we are safe of aliasing and double-freeing. */
281 newe = deep_entity_copy(old);
282 newe->owner = new_owner;
284 if (is_Class_type(new_owner)) {
285 newe->overwrites = NEW_ARR_F(ir_entity *, 0);
286 newe->overwrittenby = NEW_ARR_F(ir_entity *, 0);
289 insert_entity_in_owner(newe);
291 } /* copy_entity_own */
294 copy_entity_name(ir_entity *old, ident *new_name) {
296 assert(old && old->kind == k_entity);
298 if (old->name == new_name) return old;
299 newe = deep_entity_copy(old);
300 newe->name = new_name;
301 newe->ld_name = NULL;
303 if (is_Class_type(newe->owner)) {
304 newe->overwrites = DUP_ARR_F(ir_entity *, old->overwrites);
305 newe->overwrittenby = DUP_ARR_F(ir_entity *, old->overwrittenby);
307 insert_entity_in_owner(newe);
310 } /* copy_entity_name */
313 free_entity(ir_entity *ent) {
314 assert(ent && ent->kind == k_entity);
315 free_entity_attrs(ent);
320 /* Outputs a unique number for this node */
322 get_entity_nr(const ir_entity *ent) {
323 assert(ent && ent->kind == k_entity);
327 return (long)PTR_TO_INT(ent);
329 } /* get_entity_nr */
332 (get_entity_name)(const ir_entity *ent) {
333 return _get_entity_name(ent);
334 } /* get_entity_name */
337 (get_entity_ident)(const ir_entity *ent) {
338 return _get_entity_ident(ent);
339 } /* get_entity_ident */
342 (set_entity_ident)(ir_entity *ent, ident *id) {
343 _set_entity_ident(ent, id);
344 } /* set_entity_ident */
347 (get_entity_owner)(ir_entity *ent) {
348 return _get_entity_owner(ent);
349 } /* get_entity_owner */
352 set_entity_owner(ir_entity *ent, ir_type *owner) {
353 assert(is_entity(ent));
354 assert(is_compound_type(owner));
356 } /* set_entity_owner */
359 (get_entity_ld_ident)(ir_entity *ent) {
360 return _get_entity_ld_ident(ent);
361 } /* get_entity_ld_ident */
364 (set_entity_ld_ident)(ir_entity *ent, ident *ld_ident) {
365 _set_entity_ld_ident(ent, ld_ident);
366 } /* set_entity_ld_ident */
369 (get_entity_ld_name)(ir_entity *ent) {
370 return _get_entity_ld_name(ent);
371 } /* get_entity_ld_name */
374 (get_entity_type)(ir_entity *ent) {
375 return _get_entity_type(ent);
376 } /* get_entity_type */
379 (set_entity_type)(ir_entity *ent, ir_type *type) {
380 _set_entity_type(ent, type);
381 } /* set_entity_type */
384 (get_entity_allocation)(const ir_entity *ent) {
385 return _get_entity_allocation(ent);
386 } /* get_entity_allocation */
389 (set_entity_allocation)(ir_entity *ent, ir_allocation al) {
390 _set_entity_allocation(ent, al);
391 } /* set_entity_allocation */
393 /* return the name of the visibility */
394 const char *get_allocation_name(ir_allocation al)
396 #define X(a) case a: return #a
398 X(allocation_automatic);
399 X(allocation_parameter);
400 X(allocation_dynamic);
401 X(allocation_static);
402 default: return "BAD VALUE";
405 } /* get_allocation_name */
408 (get_entity_visibility)(const ir_entity *ent) {
409 return _get_entity_visibility(ent);
410 } /* get_entity_visibility */
413 set_entity_visibility(ir_entity *ent, ir_visibility vis) {
414 assert(ent && ent->kind == k_entity);
415 if (vis != visibility_local)
416 assert((ent->allocation == allocation_static) ||
417 (ent->allocation == allocation_automatic));
418 /* @@@ Test that the owner type is not local, but how??
419 && get_class_visibility(get_entity_owner(ent)) != local));*/
420 ent->visibility = vis;
421 } /* set_entity_visibility */
423 /* return the name of the visibility */
424 const char *get_visibility_name(ir_visibility vis)
426 #define X(a) case a: return #a
429 X(visibility_external_visible);
430 X(visibility_external_allocated);
431 default: return "BAD VALUE";
434 } /* get_visibility_name */
437 (get_entity_variability)(const ir_entity *ent) {
438 return _get_entity_variability(ent);
439 } /* get_entity_variability */
442 set_entity_variability(ir_entity *ent, ir_variability var)
444 assert(ent && ent->kind == k_entity);
445 if (var == variability_part_constant)
446 assert(is_Class_type(ent->type) || is_Struct_type(ent->type));
448 if ((is_compound_type(ent->type)) &&
449 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
450 /* Allocate data structures for constant values */
451 ent->attr.cmpd_attr.values = NEW_ARR_F(ir_node *, 0);
452 ent->attr.cmpd_attr.val_paths = NEW_ARR_F(compound_graph_path *, 0);
454 if ((is_atomic_type(ent->type)) &&
455 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
456 /* Set default constant value. */
457 ent->value = new_r_Unknown(get_const_code_irg(), get_type_mode(ent->type));
460 if ((is_compound_type(ent->type)) &&
461 (var == variability_uninitialized) && (ent->variability != variability_uninitialized)) {
462 /* Free data structures for constant values */
463 DEL_ARR_F(ent->attr.cmpd_attr.values); ent->attr.cmpd_attr.values = NULL;
464 DEL_ARR_F(ent->attr.cmpd_attr.val_paths); ent->attr.cmpd_attr.val_paths = NULL;
466 ent->variability = var;
467 } /* set_entity_variability */
469 /* return the name of the variability */
470 const char *get_variability_name(ir_variability var)
472 #define X(a) case a: return #a
474 X(variability_uninitialized);
475 X(variability_initialized);
476 X(variability_part_constant);
477 X(variability_constant);
478 default: return "BAD VALUE";
481 } /* get_variability_name */
484 (get_entity_volatility)(const ir_entity *ent) {
485 return _get_entity_volatility(ent);
486 } /* get_entity_volatility */
489 (set_entity_volatility)(ir_entity *ent, ir_volatility vol) {
490 _set_entity_volatility(ent, vol);
491 } /* set_entity_volatility */
493 /* Return the name of the volatility. */
494 const char *get_volatility_name(ir_volatility var)
496 #define X(a) case a: return #a
498 X(volatility_non_volatile);
499 X(volatility_is_volatile);
500 default: return "BAD VALUE";
503 } /* get_volatility_name */
506 (get_entity_aligned)(const ir_entity *ent) {
507 return _get_entity_aligned(ent);
511 (set_entity_aligned)(ir_entity *ent, ir_align a) {
512 _set_entity_aligned(ent, a);
516 (get_entity_alignment)(const ir_entity *ent) {
517 return _get_entity_alignment(ent);
521 (set_entity_alignment)(ir_entity *ent, unsigned alignment) {
522 _set_entity_alignment(ent, alignment);
525 /* Return the name of the alignment. */
526 const char *get_align_name(ir_align a)
528 #define X(a) case a: return #a
530 X(align_non_aligned);
532 default: return "BAD VALUE";
535 } /* get_align_name */
538 set_entity_label(ir_entity *ent, ir_label_t label)
540 ent->attr.code_attr.label = label;
543 ir_label_t get_entity_label(const ir_entity *ent)
545 return ent->attr.code_attr.label;
549 (get_entity_peculiarity)(const ir_entity *ent) {
550 return _get_entity_peculiarity(ent);
551 } /* get_entity_peculiarity */
554 (set_entity_peculiarity)(ir_entity *ent, ir_peculiarity pec) {
555 _set_entity_peculiarity(ent, pec);
556 } /* set_entity_peculiarity */
558 /* Checks if an entity cannot be overridden anymore. */
559 int (is_entity_final)(const ir_entity *ent) {
560 return _is_entity_final(ent);
561 } /* is_entity_final */
563 /* Sets/resets the final flag of an entity. */
564 void (set_entity_final)(ir_entity *ent, int final) {
565 _set_entity_final(ent, final);
566 } /* set_entity_final */
568 /* Checks if an entity is compiler generated */
569 int (is_entity_compiler_generated)(const ir_entity *ent) {
570 return _is_entity_compiler_generated(ent);
571 } /* is_entity_compiler_generated */
573 /* Sets/resets the compiler generated flag */
574 void (set_entity_compiler_generated)(ir_entity *ent, int flag) {
575 _set_entity_compiler_generated(ent, flag);
576 } /* set_entity_compiler_generated */
578 /* Checks if an entity is marked by the backend */
579 int (is_entity_backend_marked)(const ir_entity *ent) {
580 return _is_entity_backend_marked(ent);
581 } /* is_entity_backend_marked */
583 /* Sets/resets the compiler generated flag */
584 void (set_entity_backend_marked)(ir_entity *ent, int flag) {
585 _set_entity_backend_marked(ent, flag);
586 } /* set_entity_backend_marked */
588 ir_entity_usage (get_entity_usage)(const ir_entity *ent) {
589 return _get_entity_usage(ent);
592 void (set_entity_usage)(ir_entity *ent, ir_entity_usage flags) {
593 _set_entity_usage(ent, flags);
596 /* Get the entity's stickyness */
598 (get_entity_stickyness)(const ir_entity *ent) {
599 return _get_entity_stickyness(ent);
600 } /* get_entity_stickyness */
602 /* Set the entity's stickyness */
604 (set_entity_stickyness)(ir_entity *ent, ir_stickyness stickyness) {
605 _set_entity_stickyness(ent, stickyness);
606 } /* set_entity_stickyness */
608 /* Set has no effect for existent entities of type method. */
610 get_atomic_ent_value(ir_entity *ent)
612 assert(ent && is_atomic_entity(ent));
613 assert(ent->variability != variability_uninitialized);
614 return skip_Id(ent->value);
615 } /* get_atomic_ent_value */
618 set_atomic_ent_value(ir_entity *ent, ir_node *val) {
619 assert(is_atomic_entity(ent) && (ent->variability != variability_uninitialized));
620 if (is_Method_type(ent->type) && (ent->peculiarity == peculiarity_existent))
622 assert(is_Dummy(val) || get_irn_mode(val) == get_type_mode(ent->type));
624 } /* set_atomic_ent_value */
626 /* Returns true if the the node is representable as code on
628 int is_irn_const_expression(ir_node *n) {
631 /* we are in danger iff an exception will arise. TODO: be more precisely,
632 * for instance Div. will NOT rise if divisor != 0
634 if (is_binop(n) && !is_fragile_op(n))
635 return is_irn_const_expression(get_binop_left(n)) && is_irn_const_expression(get_binop_right(n));
638 switch (get_irn_opcode(n)) {
645 return is_irn_const_expression(get_irn_n(n, 0));
650 } /* is_irn_const_expression */
653 * Copies a firm subgraph that complies to the restrictions for
654 * constant expressions to current_block in current_ir_graph.
656 ir_node *copy_const_value(dbg_info *dbg, ir_node *n) {
660 /* @@@ GL I think we should implement this using the routines from irgopt for
661 dead node elimination/inlineing. */
664 switch (get_irn_opcode(n)) {
666 nn = new_d_Const_type(dbg, get_Const_tarval(n), get_Const_type(n));
669 nn = new_d_SymConst_type(dbg, get_irn_mode(n), get_SymConst_symbol(n), get_SymConst_kind(n),
670 get_SymConst_value_type(n));
673 nn = new_d_Add(dbg, copy_const_value(dbg, get_Add_left(n)),
674 copy_const_value(dbg, get_Add_right(n)), m); break;
676 nn = new_d_Sub(dbg, copy_const_value(dbg, get_Sub_left(n)),
677 copy_const_value(dbg, get_Sub_right(n)), m); break;
679 nn = new_d_Mul(dbg, copy_const_value(dbg, get_Mul_left(n)),
680 copy_const_value(dbg, get_Mul_right(n)), m); break;
682 nn = new_d_And(dbg, copy_const_value(dbg, get_And_left(n)),
683 copy_const_value(dbg, get_And_right(n)), m); break;
685 nn = new_d_Or(dbg, copy_const_value(dbg, get_Or_left(n)),
686 copy_const_value(dbg, get_Or_right(n)), m); break;
688 nn = new_d_Eor(dbg, copy_const_value(dbg, get_Eor_left(n)),
689 copy_const_value(dbg, get_Eor_right(n)), m); break;
691 nn = new_d_Cast(dbg, copy_const_value(dbg, get_Cast_op(n)), get_Cast_type(n)); break;
693 nn = new_d_Conv(dbg, copy_const_value(dbg, get_Conv_op(n)), m); break;
695 nn = new_Unknown(m); break;
697 assert(0 && "opcode invalid or not implemented");
702 } /* copy_const_value */
704 /** Return the name of the initializer kind. */
705 const char *get_initializer_kind_name(ir_initializer_kind_t ini)
707 #define X(a) case a: return #a
709 X(IR_INITIALIZER_CONST);
710 X(IR_INITIALIZER_TARVAL);
711 X(IR_INITIALIZER_NULL);
712 X(IR_INITIALIZER_COMPOUND);
713 default: return "BAD VALUE";
718 static ir_initializer_t null_initializer = { IR_INITIALIZER_NULL };
720 ir_initializer_t *get_initializer_null(void)
722 return &null_initializer;
725 ir_initializer_t *create_initializer_const(ir_node *value)
727 struct obstack *obst = get_irg_obstack(get_const_code_irg());
729 ir_initializer_t *initializer
730 = obstack_alloc(obst, sizeof(ir_initializer_const_t));
731 initializer->kind = IR_INITIALIZER_CONST;
732 initializer->consti.value = value;
737 ir_initializer_t *create_initializer_tarval(tarval *tv)
739 struct obstack *obst = get_irg_obstack(get_const_code_irg());
741 ir_initializer_t *initializer
742 = obstack_alloc(obst, sizeof(ir_initializer_tarval_t));
743 initializer->kind = IR_INITIALIZER_TARVAL;
744 initializer->tarval.value = tv;
749 ir_initializer_t *create_initializer_compound(unsigned n_entries)
751 struct obstack *obst = get_irg_obstack(get_const_code_irg());
754 size_t size = sizeof(ir_initializer_compound_t)
755 + (n_entries-1) * sizeof(ir_initializer_t*);
757 ir_initializer_t *initializer = obstack_alloc(obst, size);
758 initializer->kind = IR_INITIALIZER_COMPOUND;
759 initializer->compound.n_initializers = n_entries;
761 for(i = 0; i < n_entries; ++i) {
762 initializer->compound.initializers[i] = get_initializer_null();
768 ir_node *get_initializer_const_value(const ir_initializer_t *initializer)
770 assert(initializer->kind == IR_INITIALIZER_CONST);
771 return skip_Id(initializer->consti.value);
774 tarval *get_initializer_tarval_value(const ir_initializer_t *initializer)
776 assert(initializer->kind == IR_INITIALIZER_TARVAL);
777 return initializer->tarval.value;
780 unsigned get_initializer_compound_n_entries(const ir_initializer_t *initializer)
782 assert(initializer->kind == IR_INITIALIZER_COMPOUND);
783 return initializer->compound.n_initializers;
786 void set_initializer_compound_value(ir_initializer_t *initializer,
787 unsigned index, ir_initializer_t *value)
789 assert(initializer->kind == IR_INITIALIZER_COMPOUND);
790 assert(index < initializer->compound.n_initializers);
792 initializer->compound.initializers[index] = value;
795 ir_initializer_t *get_initializer_compound_value(
796 const ir_initializer_t *initializer, unsigned index)
798 assert(initializer->kind == IR_INITIALIZER_COMPOUND);
799 assert(index < initializer->compound.n_initializers);
801 return initializer->compound.initializers[index];
804 ir_initializer_kind_t get_initializer_kind(const ir_initializer_t *initializer)
806 return initializer->kind;
809 static void check_entity_initializer(ir_entity *entity)
815 void set_entity_initializer(ir_entity *entity, ir_initializer_t *initializer)
817 entity->attr.initializer = initializer;
818 entity->has_initializer = 1;
819 check_entity_initializer(entity);
822 int has_entity_initializer(const ir_entity *entity)
824 return entity->has_initializer;
827 ir_initializer_t *get_entity_initializer(const ir_entity *entity)
829 assert(entity->has_initializer);
830 return entity->attr.initializer;
833 int (get_entity_offset)(const ir_entity *ent)
835 return _get_entity_offset(ent);
838 void (set_entity_offset)(ir_entity *ent, int offset)
840 _set_entity_offset(ent, offset);
843 unsigned char (get_entity_offset_bits_remainder)(const ir_entity *ent)
845 return _get_entity_offset_bits_remainder(ent);
848 void (set_entity_offset_bits_remainder)(ir_entity *ent, unsigned char offset)
850 _set_entity_offset_bits_remainder(ent, offset);
853 void add_entity_overwrites(ir_entity *ent, ir_entity *overwritten)
856 ir_type *owner = get_entity_owner(ent);
857 ir_type *ovw_ovner = get_entity_owner(overwritten);
858 assert(is_Class_type(owner));
859 assert(is_Class_type(ovw_ovner));
860 assert(! is_class_final(ovw_ovner));
862 ARR_APP1(ir_entity *, ent->overwrites, overwritten);
863 ARR_APP1(ir_entity *, overwritten->overwrittenby, ent);
866 int get_entity_n_overwrites(ir_entity *ent)
868 assert(is_Class_type(get_entity_owner(ent)));
869 return (ARR_LEN(ent->overwrites));
872 int get_entity_overwrites_index(ir_entity *ent, ir_entity *overwritten)
875 assert(is_Class_type(get_entity_owner(ent)));
876 n = get_entity_n_overwrites(ent);
877 for (i = 0; i < n; ++i) {
878 if (get_entity_overwrites(ent, i) == overwritten)
884 ir_entity *get_entity_overwrites(ir_entity *ent, int pos)
886 assert(is_Class_type(get_entity_owner(ent)));
887 assert(pos < get_entity_n_overwrites(ent));
888 return ent->overwrites[pos];
891 void set_entity_overwrites(ir_entity *ent, int pos, ir_entity *overwritten)
893 assert(is_Class_type(get_entity_owner(ent)));
894 assert(pos < get_entity_n_overwrites(ent));
895 ent->overwrites[pos] = overwritten;
898 void remove_entity_overwrites(ir_entity *ent, ir_entity *overwritten)
901 assert(is_Class_type(get_entity_owner(ent)));
902 n = ARR_LEN(ent->overwrites);
903 for (i = 0; i < n; ++i) {
904 if (ent->overwrites[i] == overwritten) {
905 for (; i < n - 1; i++)
906 ent->overwrites[i] = ent->overwrites[i+1];
907 ARR_SETLEN(ir_entity*, ent->overwrites, n - 1);
913 void add_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites)
915 add_entity_overwrites(overwrites, ent);
918 int get_entity_n_overwrittenby(ir_entity *ent)
920 assert(is_Class_type(get_entity_owner(ent)));
921 return ARR_LEN(ent->overwrittenby);
924 int get_entity_overwrittenby_index(ir_entity *ent, ir_entity *overwrites)
927 assert(is_Class_type(get_entity_owner(ent)));
928 n = get_entity_n_overwrittenby(ent);
929 for (i = 0; i < n; ++i) {
930 if (get_entity_overwrittenby(ent, i) == overwrites)
936 ir_entity *get_entity_overwrittenby(ir_entity *ent, int pos)
938 assert(is_Class_type(get_entity_owner(ent)));
939 assert(pos < get_entity_n_overwrittenby(ent));
940 return ent->overwrittenby[pos];
943 void set_entity_overwrittenby(ir_entity *ent, int pos, ir_entity *overwrites)
945 assert(is_Class_type(get_entity_owner(ent)));
946 assert(pos < get_entity_n_overwrittenby(ent));
947 ent->overwrittenby[pos] = overwrites;
950 void remove_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites)
953 assert(is_Class_type(get_entity_owner(ent)));
955 n = ARR_LEN(ent->overwrittenby);
956 for (i = 0; i < n; ++i) {
957 if (ent->overwrittenby[i] == overwrites) {
958 for(; i < n - 1; ++i)
959 ent->overwrittenby[i] = ent->overwrittenby[i+1];
960 ARR_SETLEN(ir_entity*, ent->overwrittenby, n - 1);
966 void *(get_entity_link)(const ir_entity *ent)
968 return _get_entity_link(ent);
971 void (set_entity_link)(ir_entity *ent, void *l)
973 _set_entity_link(ent, l);
976 ir_graph *(get_entity_irg)(const ir_entity *ent)
978 return _get_entity_irg(ent);
981 void set_entity_irg(ir_entity *ent, ir_graph *irg)
983 assert(is_method_entity(ent));
984 /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
985 * Methode selbst nicht mehr aufgerufen werden kann, die Entität
986 * aber erhalten bleiben soll? Wandle die Entitaet in description oder
989 assert((irg && ent->peculiarity == peculiarity_existent) ||
990 (!irg && (ent->peculiarity == peculiarity_existent)
991 && (ent -> visibility == visibility_external_allocated)) ||
992 (!irg && ent->peculiarity == peculiarity_description) ||
993 (!irg && ent->peculiarity == peculiarity_inherited));
994 ent->attr.mtd_attr.irg = irg;
997 unsigned get_entity_vtable_number(const ir_entity *ent)
999 assert(is_method_entity((ir_entity *)ent));
1000 return ent->attr.mtd_attr.vtable_number;
1003 void set_entity_vtable_number(ir_entity *ent, unsigned vtable_number)
1005 assert(is_method_entity(ent));
1006 ent->attr.mtd_attr.vtable_number = vtable_number;
1009 int (is_entity)(const void *thing)
1011 return _is_entity(thing);
1014 int is_atomic_entity(ir_entity *ent)
1016 ir_type *t = get_entity_type(ent);
1017 const tp_op *op = get_type_tpop(t);
1018 return (op == type_primitive || op == type_pointer ||
1019 op == type_enumeration || op == type_method);
1022 int is_compound_entity(ir_entity *ent)
1024 ir_type *t = get_entity_type(ent);
1025 const tp_op *op = get_type_tpop(t);
1026 return (op == type_class || op == type_struct ||
1027 op == type_array || op == type_union);
1030 int is_method_entity(ir_entity *ent)
1032 ir_type *t = get_entity_type(ent);
1033 return is_Method_type(t);
1036 ir_visited_t (get_entity_visited)(ir_entity *ent)
1038 return _get_entity_visited(ent);
1041 void (set_entity_visited)(ir_entity *ent, ir_visited_t num)
1043 _set_entity_visited(ent, num);
1046 void (mark_entity_visited)(ir_entity *ent)
1048 _mark_entity_visited(ent);
1051 int (entity_visited)(ir_entity *ent)
1053 return _entity_visited(ent);
1056 int (entity_not_visited)(ir_entity *ent)
1058 return _entity_not_visited(ent);
1061 unsigned get_entity_additional_properties(ir_entity *ent)
1065 assert(is_method_entity(ent));
1067 /* first check, if the graph has additional properties */
1068 irg = get_entity_irg(ent);
1071 return get_irg_additional_properties(irg);
1073 if (ent->attr.mtd_attr.irg_add_properties & mtp_property_inherited)
1074 return get_method_additional_properties(get_entity_type(ent));
1076 return ent->attr.mtd_attr.irg_add_properties;
1079 void set_entity_additional_properties(ir_entity *ent, unsigned property_mask)
1083 assert(is_method_entity(ent));
1085 /* first check, if the graph exists */
1086 irg = get_entity_irg(ent);
1088 set_irg_additional_properties(irg, property_mask);
1090 /* do not allow to set the mtp_property_inherited flag or
1091 * the automatic inheritance of flags will not work */
1092 ent->attr.mtd_attr.irg_add_properties = property_mask & ~mtp_property_inherited;
1096 void set_entity_additional_property(ir_entity *ent, mtp_additional_property flag)
1100 assert(is_method_entity(ent));
1102 /* first check, if the graph exists */
1103 irg = get_entity_irg(ent);
1105 set_irg_additional_property(irg, flag);
1107 unsigned mask = ent->attr.mtd_attr.irg_add_properties;
1109 if (mask & mtp_property_inherited)
1110 mask = get_method_additional_properties(get_entity_type(ent));
1112 /* do not allow to set the mtp_property_inherited flag or
1113 * the automatic inheritance of flags will not work */
1114 ent->attr.mtd_attr.irg_add_properties = mask | (flag & ~mtp_property_inherited);
1118 /* Returns the class type that this type info entity represents or NULL
1119 if ent is no type info entity. */
1120 ir_type *(get_entity_repr_class)(const ir_entity *ent)
1122 return _get_entity_repr_class(ent);
1125 dbg_info *(get_entity_dbg_info)(const ir_entity *ent)
1127 return _get_entity_dbg_info(ent);
1130 void (set_entity_dbg_info)(ir_entity *ent, dbg_info *db)
1132 _set_entity_dbg_info(ent, db);
1135 void firm_init_entity(void)
1137 symconst_symbol sym;
1139 assert(firm_unknown_type && "Call init_type() before firm_init_entity()!");
1140 assert(!unknown_entity && "Call firm_init_entity() only once!");
1142 unknown_entity = new_rd_entity(NULL, firm_unknown_type, new_id_from_str(UNKNOWN_ENTITY_NAME), firm_unknown_type);
1143 set_entity_visibility(unknown_entity, visibility_external_allocated);
1144 set_entity_ld_ident(unknown_entity, get_entity_ident(unknown_entity));
1146 current_ir_graph = get_const_code_irg();
1147 sym.entity_p = unknown_entity;
1148 /* TODO: we need two unknown_entities here, one for code and one for data */
1149 unknown_entity->value = new_SymConst(mode_P_data, sym, symconst_addr_ent);