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"
46 #include "compound_path.h"
48 /*-----------------------------------------------------------------*/
50 /*-----------------------------------------------------------------*/
52 ir_entity *unknown_entity = NULL;
54 ir_entity *get_unknown_entity(void) { return unknown_entity; }
56 /** The name of the unknown entity. */
57 #define UNKNOWN_ENTITY_NAME "unknown_entity"
59 /*-----------------------------------------------------------------*/
61 /*-----------------------------------------------------------------*/
64 * Add an entity to it's already set owner type.
66 static inline void insert_entity_in_owner(ir_entity *ent) {
67 ir_type *owner = ent->owner;
68 switch (get_type_tpop_code(owner)) {
70 add_class_member(owner, ent);
73 add_struct_member(owner, ent);
76 add_union_member(owner, ent);
79 set_array_element_entity(owner, ent);
82 panic("Unsupported type kind");
84 } /* insert_entity_in_owner */
87 * Creates a new entity. This entity is NOT inserted in the owner type.
89 * @param db debug info for this entity
90 * @param owner the owner type of the new entity
91 * @param name the name of the new entity
92 * @param type the type of the new entity
94 * @return the new created entity
96 static inline ir_entity *
97 new_rd_entity(dbg_info *db, ir_type *owner, ident *name, ir_type *type)
102 assert(!id_contains_char(name, ' ') && "entity name should not contain spaces");
104 res = XMALLOCZ(ir_entity);
106 res->kind = k_entity;
112 res->volatility = volatility_non_volatile;
113 res->aligned = align_is_aligned;
114 res->usage = ir_usage_unknown;
115 res->compiler_gen = 0;
116 res->backend_marked = 0;
118 res->offset_bit_remainder = 0;
121 res->repr_class = NULL;
123 if (is_Method_type(type)) {
125 ir_mode *mode = is_Method_type(type) ? mode_P_code : mode_P_data;
127 rem = current_ir_graph;
128 current_ir_graph = get_const_code_irg();
129 set_atomic_ent_value(res, new_SymConst(mode, sym, symconst_addr_ent));
130 current_ir_graph = rem;
131 res->linkage = IR_LINKAGE_CONSTANT;
132 res->attr.mtd_attr.irg_add_properties = mtp_property_inherited;
133 res->attr.mtd_attr.vtable_number = VTABLE_NUM_NOT_SET;
134 res->attr.mtd_attr.param_access = NULL;
135 res->attr.mtd_attr.param_weight = NULL;
136 res->attr.mtd_attr.irg = NULL;
137 } else if (is_compound_type(type)) {
138 res->attr.cmpd_attr.values = NULL;
139 res->attr.cmpd_attr.val_paths = NULL;
140 } else if (is_code_type(type)) {
141 res->attr.code_attr.label = (ir_label_t) -1;
144 if (is_Class_type(owner)) {
145 res->overwrites = NEW_ARR_F(ir_entity *, 0);
146 res->overwrittenby = NEW_ARR_F(ir_entity *, 0);
148 res->overwrites = NULL;
149 res->overwrittenby = NULL;
153 res->nr = get_irp_new_node_nr();
154 #endif /* DEBUG_libfirm */
157 set_entity_dbg_info(res, db);
160 } /* new_rd_entity */
163 new_d_entity(ir_type *owner, ident *name, ir_type *type, dbg_info *db) {
166 assert(is_compound_type(owner));
167 res = new_rd_entity(db, owner, name, type);
168 /* Remember entity in it's owner. */
169 insert_entity_in_owner(res);
171 hook_new_entity(res);
176 new_entity(ir_type *owner, ident *name, ir_type *type) {
177 return new_d_entity(owner, name, type, NULL);
181 * Free entity attributes.
183 * @param ent the entity
185 static void free_entity_attrs(ir_entity *ent)
188 if (get_type_tpop(get_entity_owner(ent)) == type_class) {
189 DEL_ARR_F(ent->overwrites); ent->overwrites = NULL;
190 DEL_ARR_F(ent->overwrittenby); ent->overwrittenby = NULL;
192 assert(ent->overwrites == NULL);
193 assert(ent->overwrittenby == NULL);
195 if (ent->initializer != NULL) {
196 /* TODO: free initializers */
197 } else if (entity_has_compound_ent_values(ent)) {
198 if (ent->attr.cmpd_attr.val_paths) {
199 for (i = get_compound_ent_n_values(ent) - 1; i >= 0; --i)
200 if (ent->attr.cmpd_attr.val_paths[i]) {
201 /* free_compound_graph_path(ent->attr.cmpd_attr.val_paths[i]) ; * @@@ warum nich? */
202 /* Geht nich: wird mehrfach verwendet!!! ==> mehrfach frei gegeben. */
203 /* DEL_ARR_F(ent->attr.cmpd_attr.val_paths); */
205 ent->attr.cmpd_attr.val_paths = NULL;
208 if (is_compound_entity(ent)) {
209 if (ent->attr.cmpd_attr.values) {
210 /*DEL_ARR_F(ent->attr.cmpd_attr.values)*/;
212 ent->attr.cmpd_attr.values = NULL;
213 } else if (is_method_entity(ent)) {
214 if (ent->attr.mtd_attr.param_access) {
215 DEL_ARR_F(ent->attr.mtd_attr.param_access);
216 ent->attr.mtd_attr.param_access = NULL;
218 if (ent->attr.mtd_attr.param_weight) {
219 DEL_ARR_F(ent->attr.mtd_attr.param_weight);
220 ent->attr.mtd_attr.param_weight = NULL;
223 } /* free_entity_attrs */
226 * Creates a deep copy of an entity.
228 static ir_entity *deep_entity_copy(ir_entity *old)
230 ir_entity *newe = XMALLOC(ir_entity);
233 if (old->initializer != NULL) {
234 /* FIXME: the initializers are NOT copied */
235 } else if (entity_has_compound_ent_values(old)) {
236 newe->attr.cmpd_attr.values = NULL;
237 newe->attr.cmpd_attr.val_paths = NULL;
238 if (old->attr.cmpd_attr.values)
239 newe->attr.cmpd_attr.values = DUP_ARR_F(ir_node *, old->attr.cmpd_attr.values);
241 /* FIXME: the compound graph paths are NOT copied */
242 if (old->attr.cmpd_attr.val_paths)
243 newe->attr.cmpd_attr.val_paths = DUP_ARR_F(compound_graph_path *, old->attr.cmpd_attr.val_paths);
244 } else if (is_method_entity(old)) {
245 /* do NOT copy them, reanalyze. This might be the best solution */
246 newe->attr.mtd_attr.param_access = NULL;
247 newe->attr.mtd_attr.param_weight = NULL;
251 newe->nr = get_irp_new_node_nr();
256 * Copies the entity if the new_owner is different from the
257 * owner of the old entity, else returns the old entity.
260 copy_entity_own(ir_entity *old, ir_type *new_owner) {
262 assert(is_entity(old));
263 assert(is_compound_type(new_owner));
264 assert(get_type_state(new_owner) != layout_fixed);
266 if (old->owner == new_owner)
269 /* create a deep copy so we are safe of aliasing and double-freeing. */
270 newe = deep_entity_copy(old);
271 newe->owner = new_owner;
273 if (is_Class_type(new_owner)) {
274 newe->overwrites = NEW_ARR_F(ir_entity *, 0);
275 newe->overwrittenby = NEW_ARR_F(ir_entity *, 0);
278 insert_entity_in_owner(newe);
280 } /* copy_entity_own */
283 copy_entity_name(ir_entity *old, ident *new_name) {
285 assert(old && old->kind == k_entity);
287 if (old->name == new_name) return old;
288 newe = deep_entity_copy(old);
289 newe->name = new_name;
290 newe->ld_name = NULL;
292 if (is_Class_type(newe->owner)) {
293 newe->overwrites = DUP_ARR_F(ir_entity *, old->overwrites);
294 newe->overwrittenby = DUP_ARR_F(ir_entity *, old->overwrittenby);
296 insert_entity_in_owner(newe);
299 } /* copy_entity_name */
302 free_entity(ir_entity *ent) {
303 assert(ent && ent->kind == k_entity);
304 free_entity_attrs(ent);
309 /* Outputs a unique number for this node */
311 get_entity_nr(const ir_entity *ent) {
312 assert(ent && ent->kind == k_entity);
316 return (long)PTR_TO_INT(ent);
318 } /* get_entity_nr */
321 (get_entity_name)(const ir_entity *ent) {
322 return _get_entity_name(ent);
326 (get_entity_ident)(const ir_entity *ent) {
327 return _get_entity_ident(ent);
331 (set_entity_ident)(ir_entity *ent, ident *id) {
332 _set_entity_ident(ent, id);
336 (get_entity_owner)(const ir_entity *ent) {
337 return _get_entity_owner(ent);
341 set_entity_owner(ir_entity *ent, ir_type *owner) {
342 assert(is_entity(ent));
343 assert(is_compound_type(owner));
347 ident *(get_entity_ld_ident)(const ir_entity *ent)
349 return _get_entity_ld_ident(ent);
353 (set_entity_ld_ident)(ir_entity *ent, ident *ld_ident) {
354 _set_entity_ld_ident(ent, ld_ident);
357 const char *(get_entity_ld_name)(const ir_entity *ent)
359 return _get_entity_ld_name(ent);
363 (get_entity_type)(const ir_entity *ent) {
364 return _get_entity_type(ent);
368 (set_entity_type)(ir_entity *ent, ir_type *type) {
369 _set_entity_type(ent, type);
373 (get_entity_volatility)(const ir_entity *ent) {
374 return _get_entity_volatility(ent);
378 (set_entity_volatility)(ir_entity *ent, ir_volatility vol) {
379 _set_entity_volatility(ent, vol);
382 /* Return the name of the volatility. */
383 const char *get_volatility_name(ir_volatility var)
385 #define X(a) case a: return #a
387 X(volatility_non_volatile);
388 X(volatility_is_volatile);
389 default: return "BAD VALUE";
392 } /* get_volatility_name */
395 (get_entity_aligned)(const ir_entity *ent) {
396 return _get_entity_aligned(ent);
400 (set_entity_aligned)(ir_entity *ent, ir_align a) {
401 _set_entity_aligned(ent, a);
405 (get_entity_alignment)(const ir_entity *ent) {
406 return _get_entity_alignment(ent);
410 (set_entity_alignment)(ir_entity *ent, unsigned alignment) {
411 _set_entity_alignment(ent, alignment);
414 /* Return the name of the alignment. */
415 const char *get_align_name(ir_align a)
417 #define X(a) case a: return #a
419 X(align_non_aligned);
421 default: return "BAD VALUE";
424 } /* get_align_name */
427 set_entity_label(ir_entity *ent, ir_label_t label)
429 ent->attr.code_attr.label = label;
432 ir_label_t get_entity_label(const ir_entity *ent)
434 return ent->attr.code_attr.label;
437 static void verify_linkage(ir_entity *entity)
439 ir_linkage linkage = entity->linkage;
440 /* local and extern are mutually exclusive */
442 assert(! ((linkage & IR_LINKAGE_EXTERN) && (linkage & IR_LINKAGE_LOCAL)));
443 if (!is_method_entity(entity) && (linkage & IR_LINKAGE_EXTERN)) {
444 assert(!entity_has_definition(entity));
446 assert(! ((linkage & IR_LINKAGE_CONSTANT) && (linkage & IR_LINKAGE_WEAK)));
449 void set_entity_linkage(ir_entity *entity, ir_linkage linkage)
451 entity->linkage = linkage;
452 verify_linkage(entity);
455 ir_linkage (get_entity_linkage)(const ir_entity *entity)
457 return get_entity_linkage(entity);
460 void add_entity_linkage(ir_entity *entity, ir_linkage linkage)
462 entity->linkage |= linkage;
463 verify_linkage(entity);
466 void remove_entity_linkage(ir_entity *entity, ir_linkage linkage)
468 entity->linkage &= ~linkage;
469 verify_linkage(entity);
472 /* Checks if an entity is compiler generated */
473 int (is_entity_compiler_generated)(const ir_entity *ent) {
474 return _is_entity_compiler_generated(ent);
475 } /* is_entity_compiler_generated */
477 /* Sets/resets the compiler generated flag */
478 void (set_entity_compiler_generated)(ir_entity *ent, int flag) {
479 _set_entity_compiler_generated(ent, flag);
480 } /* set_entity_compiler_generated */
482 /* Checks if an entity is marked by the backend */
483 int (is_entity_backend_marked)(const ir_entity *ent) {
484 return _is_entity_backend_marked(ent);
485 } /* is_entity_backend_marked */
487 /* Sets/resets the compiler generated flag */
488 void (set_entity_backend_marked)(ir_entity *ent, int flag) {
489 _set_entity_backend_marked(ent, flag);
490 } /* set_entity_backend_marked */
492 ir_entity_usage (get_entity_usage)(const ir_entity *ent) {
493 return _get_entity_usage(ent);
496 void (set_entity_usage)(ir_entity *ent, ir_entity_usage flags) {
497 _set_entity_usage(ent, flags);
500 /* Set has no effect for existent entities of type method. */
501 ir_node *get_atomic_ent_value(ir_entity *entity)
503 ir_initializer_t *initializer = get_entity_initializer(entity);
505 assert(entity && is_atomic_entity(entity));
506 if (initializer == NULL) {
507 ir_type *type = get_entity_type(entity);
508 return new_r_Unknown(get_const_code_irg(), get_type_mode(type));
511 switch (get_initializer_kind(initializer)) {
512 case IR_INITIALIZER_NULL: {
513 ir_type *type = get_entity_type(entity);
514 ir_mode *mode = get_type_mode(type);
515 return new_r_Const(get_const_code_irg(), get_mode_null(mode));
517 case IR_INITIALIZER_TARVAL: {
518 tarval *tv = get_initializer_tarval_value(initializer);
519 return new_r_Const(get_const_code_irg(), tv);
521 case IR_INITIALIZER_CONST:
522 return get_initializer_const_value(initializer);
523 case IR_INITIALIZER_COMPOUND:
524 panic("compound initializer in atomic entity not allowed (%+F)", entity);
527 panic("invalid initializer kind in get_atomic_ent_value(%+F)", entity);
530 void set_atomic_ent_value(ir_entity *entity, ir_node *val)
532 ir_initializer_t *initializer;
534 assert(is_atomic_entity(entity));
535 assert(get_entity_peculiarity(entity) != peculiarity_description);
537 assert(is_Dummy(val) || get_irn_mode(val) == get_type_mode(entity->type));
538 initializer = create_initializer_const(val);
539 entity->initializer = initializer;
542 /* Returns true if the the node is representable as code on
544 int is_irn_const_expression(ir_node *n) {
547 /* we are in danger iff an exception will arise. TODO: be more precisely,
548 * for instance Div. will NOT rise if divisor != 0
550 if (is_binop(n) && !is_fragile_op(n))
551 return is_irn_const_expression(get_binop_left(n)) && is_irn_const_expression(get_binop_right(n));
554 switch (get_irn_opcode(n)) {
561 return is_irn_const_expression(get_irn_n(n, 0));
566 } /* is_irn_const_expression */
569 * Copies a firm subgraph that complies to the restrictions for
570 * constant expressions to current_block in current_ir_graph.
572 ir_node *copy_const_value(dbg_info *dbg, ir_node *n) {
576 /* @@@ GL I think we should implement this using the routines from irgopt for
577 dead node elimination/inlineing. */
580 switch (get_irn_opcode(n)) {
582 nn = new_d_Const_type(dbg, get_Const_tarval(n), get_Const_type(n));
585 nn = new_d_SymConst_type(dbg, get_irn_mode(n), get_SymConst_symbol(n), get_SymConst_kind(n),
586 get_SymConst_value_type(n));
589 nn = new_d_Add(dbg, copy_const_value(dbg, get_Add_left(n)),
590 copy_const_value(dbg, get_Add_right(n)), m); break;
592 nn = new_d_Sub(dbg, copy_const_value(dbg, get_Sub_left(n)),
593 copy_const_value(dbg, get_Sub_right(n)), m); break;
595 nn = new_d_Mul(dbg, copy_const_value(dbg, get_Mul_left(n)),
596 copy_const_value(dbg, get_Mul_right(n)), m); break;
598 nn = new_d_And(dbg, copy_const_value(dbg, get_And_left(n)),
599 copy_const_value(dbg, get_And_right(n)), m); break;
601 nn = new_d_Or(dbg, copy_const_value(dbg, get_Or_left(n)),
602 copy_const_value(dbg, get_Or_right(n)), m); break;
604 nn = new_d_Eor(dbg, copy_const_value(dbg, get_Eor_left(n)),
605 copy_const_value(dbg, get_Eor_right(n)), m); break;
607 nn = new_d_Cast(dbg, copy_const_value(dbg, get_Cast_op(n)), get_Cast_type(n)); break;
609 nn = new_d_Conv(dbg, copy_const_value(dbg, get_Conv_op(n)), m); break;
611 nn = new_Unknown(m); break;
613 assert(0 && "opcode invalid or not implemented");
618 } /* copy_const_value */
620 /** Return the name of the initializer kind. */
621 const char *get_initializer_kind_name(ir_initializer_kind_t ini)
623 #define X(a) case a: return #a
625 X(IR_INITIALIZER_CONST);
626 X(IR_INITIALIZER_TARVAL);
627 X(IR_INITIALIZER_NULL);
628 X(IR_INITIALIZER_COMPOUND);
629 default: return "BAD VALUE";
634 static ir_initializer_t null_initializer = { IR_INITIALIZER_NULL };
636 ir_initializer_t *get_initializer_null(void)
638 return &null_initializer;
641 ir_initializer_t *create_initializer_const(ir_node *value)
643 struct obstack *obst = get_irg_obstack(get_const_code_irg());
645 ir_initializer_t *initializer
646 = obstack_alloc(obst, sizeof(ir_initializer_const_t));
647 initializer->kind = IR_INITIALIZER_CONST;
648 initializer->consti.value = value;
653 ir_initializer_t *create_initializer_tarval(tarval *tv)
655 struct obstack *obst = get_irg_obstack(get_const_code_irg());
657 ir_initializer_t *initializer
658 = obstack_alloc(obst, sizeof(ir_initializer_tarval_t));
659 initializer->kind = IR_INITIALIZER_TARVAL;
660 initializer->tarval.value = tv;
665 ir_initializer_t *create_initializer_compound(unsigned n_entries)
667 struct obstack *obst = get_irg_obstack(get_const_code_irg());
670 size_t size = sizeof(ir_initializer_compound_t)
671 + (n_entries-1) * sizeof(ir_initializer_t*);
673 ir_initializer_t *initializer = obstack_alloc(obst, size);
674 initializer->kind = IR_INITIALIZER_COMPOUND;
675 initializer->compound.n_initializers = n_entries;
677 for(i = 0; i < n_entries; ++i) {
678 initializer->compound.initializers[i] = get_initializer_null();
684 ir_node *get_initializer_const_value(const ir_initializer_t *initializer)
686 assert(initializer->kind == IR_INITIALIZER_CONST);
687 return skip_Id(initializer->consti.value);
690 tarval *get_initializer_tarval_value(const ir_initializer_t *initializer)
692 assert(initializer->kind == IR_INITIALIZER_TARVAL);
693 return initializer->tarval.value;
696 unsigned get_initializer_compound_n_entries(const ir_initializer_t *initializer)
698 assert(initializer->kind == IR_INITIALIZER_COMPOUND);
699 return initializer->compound.n_initializers;
702 void set_initializer_compound_value(ir_initializer_t *initializer,
703 unsigned index, ir_initializer_t *value)
705 assert(initializer->kind == IR_INITIALIZER_COMPOUND);
706 assert(index < initializer->compound.n_initializers);
708 initializer->compound.initializers[index] = value;
711 ir_initializer_t *get_initializer_compound_value(
712 const ir_initializer_t *initializer, unsigned index)
714 assert(initializer->kind == IR_INITIALIZER_COMPOUND);
715 assert(index < initializer->compound.n_initializers);
717 return initializer->compound.initializers[index];
720 ir_initializer_kind_t get_initializer_kind(const ir_initializer_t *initializer)
722 return initializer->kind;
725 static void check_entity_initializer(ir_entity *entity)
728 ir_initializer_t *initializer = entity->initializer;
729 switch (initializer->kind) {
730 case IR_INITIALIZER_COMPOUND:
731 assert(is_compound_entity(entity));
733 case IR_INITIALIZER_CONST:
734 case IR_INITIALIZER_TARVAL:
735 assert(is_atomic_entity(entity));
737 case IR_INITIALIZER_NULL:
743 void set_entity_initializer(ir_entity *entity, ir_initializer_t *initializer)
745 entity->initializer = initializer;
746 check_entity_initializer(entity);
749 int has_entity_initializer(const ir_entity *entity)
751 return entity->initializer != NULL;
754 ir_initializer_t *get_entity_initializer(const ir_entity *entity)
756 return entity->initializer;
759 int (get_entity_offset)(const ir_entity *ent)
761 return _get_entity_offset(ent);
764 void (set_entity_offset)(ir_entity *ent, int offset)
766 _set_entity_offset(ent, offset);
769 unsigned char (get_entity_offset_bits_remainder)(const ir_entity *ent)
771 return _get_entity_offset_bits_remainder(ent);
774 void (set_entity_offset_bits_remainder)(ir_entity *ent, unsigned char offset)
776 _set_entity_offset_bits_remainder(ent, offset);
779 void add_entity_overwrites(ir_entity *ent, ir_entity *overwritten)
782 ir_type *owner = get_entity_owner(ent);
783 ir_type *ovw_ovner = get_entity_owner(overwritten);
784 assert(is_Class_type(owner));
785 assert(is_Class_type(ovw_ovner));
786 assert(! is_class_final(ovw_ovner));
788 ARR_APP1(ir_entity *, ent->overwrites, overwritten);
789 ARR_APP1(ir_entity *, overwritten->overwrittenby, ent);
792 int get_entity_n_overwrites(const ir_entity *ent)
794 assert(is_Class_type(get_entity_owner(ent)));
795 return (ARR_LEN(ent->overwrites));
798 int get_entity_overwrites_index(const ir_entity *ent, ir_entity *overwritten)
801 assert(is_Class_type(get_entity_owner(ent)));
802 n = get_entity_n_overwrites(ent);
803 for (i = 0; i < n; ++i) {
804 if (get_entity_overwrites(ent, i) == overwritten)
810 ir_entity *get_entity_overwrites(const ir_entity *ent, int pos)
812 assert(is_Class_type(get_entity_owner(ent)));
813 assert(pos < get_entity_n_overwrites(ent));
814 return ent->overwrites[pos];
817 void set_entity_overwrites(ir_entity *ent, int pos, ir_entity *overwritten)
819 assert(is_Class_type(get_entity_owner(ent)));
820 assert(pos < get_entity_n_overwrites(ent));
821 ent->overwrites[pos] = overwritten;
824 void remove_entity_overwrites(ir_entity *ent, ir_entity *overwritten)
827 assert(is_Class_type(get_entity_owner(ent)));
828 n = ARR_LEN(ent->overwrites);
829 for (i = 0; i < n; ++i) {
830 if (ent->overwrites[i] == overwritten) {
831 for (; i < n - 1; i++)
832 ent->overwrites[i] = ent->overwrites[i+1];
833 ARR_SETLEN(ir_entity*, ent->overwrites, n - 1);
839 void add_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites)
841 add_entity_overwrites(overwrites, ent);
844 int get_entity_n_overwrittenby(const ir_entity *ent)
846 assert(is_Class_type(get_entity_owner(ent)));
847 return ARR_LEN(ent->overwrittenby);
850 int get_entity_overwrittenby_index(const ir_entity *ent, ir_entity *overwrites)
853 assert(is_Class_type(get_entity_owner(ent)));
854 n = get_entity_n_overwrittenby(ent);
855 for (i = 0; i < n; ++i) {
856 if (get_entity_overwrittenby(ent, i) == overwrites)
862 ir_entity *get_entity_overwrittenby(const ir_entity *ent, int pos)
864 assert(is_Class_type(get_entity_owner(ent)));
865 assert(pos < get_entity_n_overwrittenby(ent));
866 return ent->overwrittenby[pos];
869 void set_entity_overwrittenby(ir_entity *ent, int pos, ir_entity *overwrites)
871 assert(is_Class_type(get_entity_owner(ent)));
872 assert(pos < get_entity_n_overwrittenby(ent));
873 ent->overwrittenby[pos] = overwrites;
876 void remove_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites)
879 assert(is_Class_type(get_entity_owner(ent)));
881 n = ARR_LEN(ent->overwrittenby);
882 for (i = 0; i < n; ++i) {
883 if (ent->overwrittenby[i] == overwrites) {
884 for(; i < n - 1; ++i)
885 ent->overwrittenby[i] = ent->overwrittenby[i+1];
886 ARR_SETLEN(ir_entity*, ent->overwrittenby, n - 1);
892 void *(get_entity_link)(const ir_entity *ent)
894 return _get_entity_link(ent);
897 void (set_entity_link)(ir_entity *ent, void *l)
899 _set_entity_link(ent, l);
902 ir_graph *(get_entity_irg)(const ir_entity *ent)
904 return _get_entity_irg(ent);
907 void set_entity_irg(ir_entity *ent, ir_graph *irg)
909 assert(is_method_entity(ent));
910 assert(get_entity_peculiarity(ent) == peculiarity_existent);
911 ent->attr.mtd_attr.irg = irg;
914 unsigned get_entity_vtable_number(const ir_entity *ent)
916 assert(is_method_entity((ir_entity *)ent));
917 return ent->attr.mtd_attr.vtable_number;
920 void set_entity_vtable_number(ir_entity *ent, unsigned vtable_number)
922 assert(is_method_entity(ent));
923 ent->attr.mtd_attr.vtable_number = vtable_number;
926 int (is_entity)(const void *thing)
928 return _is_entity(thing);
931 int is_atomic_entity(const ir_entity *ent)
933 ir_type *t = get_entity_type(ent);
934 const tp_op *op = get_type_tpop(t);
935 return (op == type_primitive || op == type_pointer ||
936 op == type_enumeration || op == type_method);
939 int is_compound_entity(const ir_entity *ent)
941 ir_type *t = get_entity_type(ent);
942 const tp_op *op = get_type_tpop(t);
943 return (op == type_class || op == type_struct ||
944 op == type_array || op == type_union);
947 int is_method_entity(const ir_entity *ent)
949 ir_type *t = get_entity_type(ent);
950 return is_Method_type(t);
953 ir_visited_t (get_entity_visited)(const ir_entity *ent)
955 return _get_entity_visited(ent);
958 void (set_entity_visited)(ir_entity *ent, ir_visited_t num)
960 _set_entity_visited(ent, num);
963 void (mark_entity_visited)(ir_entity *ent)
965 _mark_entity_visited(ent);
968 int (entity_visited)(const ir_entity *ent)
970 return _entity_visited(ent);
973 int (entity_not_visited)(const ir_entity *ent)
975 return _entity_not_visited(ent);
978 unsigned get_entity_additional_properties(const ir_entity *ent)
982 assert(is_method_entity(ent));
984 /* first check, if the graph has additional properties */
985 irg = get_entity_irg(ent);
988 return get_irg_additional_properties(irg);
990 if (ent->attr.mtd_attr.irg_add_properties & mtp_property_inherited)
991 return get_method_additional_properties(get_entity_type(ent));
993 return ent->attr.mtd_attr.irg_add_properties;
996 void set_entity_additional_properties(ir_entity *ent, unsigned property_mask)
1000 assert(is_method_entity(ent));
1002 /* first check, if the graph exists */
1003 irg = get_entity_irg(ent);
1005 set_irg_additional_properties(irg, property_mask);
1007 /* do not allow to set the mtp_property_inherited flag or
1008 * the automatic inheritance of flags will not work */
1009 ent->attr.mtd_attr.irg_add_properties = property_mask & ~mtp_property_inherited;
1013 void set_entity_additional_property(ir_entity *ent, mtp_additional_property flag)
1017 assert(is_method_entity(ent));
1019 /* first check, if the graph exists */
1020 irg = get_entity_irg(ent);
1022 set_irg_additional_property(irg, flag);
1024 unsigned mask = ent->attr.mtd_attr.irg_add_properties;
1026 if (mask & mtp_property_inherited)
1027 mask = get_method_additional_properties(get_entity_type(ent));
1029 /* do not allow to set the mtp_property_inherited flag or
1030 * the automatic inheritance of flags will not work */
1031 ent->attr.mtd_attr.irg_add_properties = mask | (flag & ~mtp_property_inherited);
1035 /* Returns the class type that this type info entity represents or NULL
1036 if ent is no type info entity. */
1037 ir_type *(get_entity_repr_class)(const ir_entity *ent)
1039 return _get_entity_repr_class(ent);
1042 dbg_info *(get_entity_dbg_info)(const ir_entity *ent)
1044 return _get_entity_dbg_info(ent);
1047 void (set_entity_dbg_info)(ir_entity *ent, dbg_info *db)
1049 _set_entity_dbg_info(ent, db);
1052 int entity_is_externally_visible(const ir_entity *entity)
1054 return (get_entity_linkage(entity) & IR_LINKAGE_LOCAL) == 0;
1057 int entity_has_definition(const ir_entity *entity)
1059 return entity->initializer != NULL
1060 || get_entity_irg(entity) != NULL
1061 || entity_has_compound_ent_values(entity);
1064 void firm_init_entity(void)
1066 symconst_symbol sym;
1068 assert(firm_unknown_type && "Call init_type() before firm_init_entity()!");
1069 assert(!unknown_entity && "Call firm_init_entity() only once!");
1071 unknown_entity = new_rd_entity(NULL, firm_unknown_type, new_id_from_str(UNKNOWN_ENTITY_NAME), firm_unknown_type);
1072 set_entity_linkage(unknown_entity, IR_LINKAGE_EXTERN);
1074 set_entity_ld_ident(unknown_entity, get_entity_ident(unknown_entity));
1076 current_ir_graph = get_const_code_irg();
1077 sym.entity_p = unknown_entity;
1080 ir_allocation get_entity_allocation(const ir_entity *entity)
1082 return entity->allocation;
1085 void set_entity_allocation(ir_entity *entity, ir_allocation allocation)
1087 entity->allocation = allocation;
1090 ir_peculiarity get_entity_peculiarity(const ir_entity *entity)
1092 return entity->peculiarity;
1095 void set_entity_peculiarity(ir_entity *entity, ir_peculiarity peculiarity)
1097 entity->peculiarity = peculiarity;
1100 void set_entity_final(ir_entity *entity, int final)
1102 entity->final = final;
1105 int is_entity_final(const ir_entity *entity)
1107 return entity->final;