3 * File name: ir/tr/entity.c
4 * Purpose: Representation of all program known entities.
5 * Author: Martin Trapp, Christian Schaefer
6 * Modified by: Goetz Lindenmaier
9 * Copyright: (c) 1998-2003 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
21 # include "entity_t.h"
23 # include "typegmod.h"
25 /* All this is needed to build the constant node for methods: */
26 # include "irprog_t.h"
30 /*******************************************************************/
32 /*******************************************************************/
39 /*******************************************************************/
41 /*******************************************************************/
43 /* redeclared to declare INLINE. */
44 INLINE entity *get_entity_overwrites (entity *ent, int pos);
45 INLINE entity *get_entity_overwrittenby (entity *ent, int pos);
46 INLINE type *get_entity_owner (entity *ent);
48 INLINE void insert_entity_in_owner (entity *ent) {
49 type *owner = ent->owner;
50 switch (get_type_tpop_code(owner)) {
52 add_class_member (owner, ent);
55 add_struct_member (owner, ent);
58 add_union_member (owner, ent);
61 set_array_element_entity(owner, ent);
68 new_entity (type *owner, ident *name, type *type)
73 assert(!id_contains_char(name, ' ') && "entity name should not contain spaces");
75 res = (entity *) xmalloc (sizeof (entity));
77 assert_legal_owner_of_ent(owner);
82 if (get_type_tpop(type) == type_method)
83 res->allocation = allocation_static;
85 res->allocation = allocation_automatic;
87 res->visibility = visibility_local;
89 if (is_method_type(type)) {
90 res->variability = variability_constant;
91 rem = current_ir_graph;
92 current_ir_graph = get_const_code_irg();
93 res->value = new_Const(mode_P_mach, new_tarval_from_entity(res, mode_P_mach));
94 current_ir_graph = rem;
96 res->variability = variability_uninitialized;
99 res->val_paths = NULL;
101 res->peculiarity = peculiarity_existent;
102 res->volatility = volatility_non_volatile;
104 res->overwrites = NEW_ARR_F(entity *, 0);
105 res->overwrittenby = NEW_ARR_F(entity *, 0);
110 res->nr = get_irp_new_node_nr();
115 /* Remember entity in it's owner. */
116 insert_entity_in_owner (res);
120 new_d_entity (type *owner, ident *name, type *type, dbg_info *db) {
121 entity *res = new_entity(owner, name, type);
122 set_entity_dbg_info(res, db);
126 INLINE void free_compound_graph_path (compound_graph_path *gr);
127 INLINE int is_compound_graph_path(void *thing);
128 INLINE int get_compound_graph_path_length(compound_graph_path *gr);
129 INLINE entity *get_compound_graph_path_node(compound_graph_path *gr, int pos);
130 INLINE int get_compound_ent_n_values(entity *ent);
132 INLINE void free_entity_attrs(entity *ent) {
135 if (get_type_tpop(get_entity_owner(ent)) == type_class) {
136 DEL_ARR_F(ent->overwrites); ent->overwrites = NULL;
137 DEL_ARR_F(ent->overwrittenby); ent->overwrittenby = NULL;
139 //if (ent->values) DEL_ARR_F(ent->values); /* @@@ warum nich? */
140 if (ent->val_paths) {
141 if (is_compound_entity(ent))
142 for (i = 0; i < get_compound_ent_n_values(ent); i++)
143 if (ent->val_paths[i])
144 free_compound_graph_path(ent->val_paths[i]) ; /* @@@ warum nich? */
145 //DEL_ARR_F(ent->val_paths);
147 ent->val_paths = NULL;
152 copy_entity_own (entity *old, type *new_owner) {
155 assert_legal_owner_of_ent(new_owner);
156 if (old->owner == new_owner) return old;
157 new = (entity *) xmalloc (sizeof (entity));
158 memcpy (new, old, sizeof (entity));
159 new->owner = new_owner;
161 if ((get_type_tpop(get_entity_owner(old)) == type_class) &&
162 (get_type_tpop(new_owner) == type_class)) {
163 new->overwrites = DUP_ARR_F(entity *, old->overwrites);
164 new->overwrittenby = DUP_ARR_F(entity *, old->overwrittenby);
165 } else if ((get_type_tpop(get_entity_owner(old)) != type_class) &&
166 (get_type_tpop(new_owner) == type_class)) {
167 new->overwrites = NEW_ARR_F(entity *, 0);
168 new->overwrittenby = NEW_ARR_F(entity *, 0);
171 if (is_class_type(new_owner)) {
172 new->overwrites = NEW_ARR_F(entity *, 0);
173 new->overwrittenby = NEW_ARR_F(entity *, 0);
176 new->nr = get_irp_new_node_nr();
179 insert_entity_in_owner (new);
185 copy_entity_name (entity *old, ident *new_name) {
188 if (old->name == new_name) return old;
189 new = (entity *) xmalloc (sizeof (entity));
190 memcpy (new, old, sizeof (entity));
191 new->name = new_name;
193 if (is_class_type(new->owner)) {
194 new->overwrites = DUP_ARR_F(entity *, old->overwrites);
195 new->overwrittenby = DUP_ARR_F(entity *, old->overwrittenby);
198 new->nr = get_irp_new_node_nr();
201 insert_entity_in_owner (new);
208 free_entity (entity *ent) {
209 free_tarval_entity(ent);
210 free_entity_attrs(ent);
215 /* Outputs a unique number for this node */
217 get_entity_nr(entity *ent) {
227 get_entity_name (entity *ent) {
229 return get_id_str(get_entity_ident(ent));
233 get_entity_ident (entity *ent) {
239 void set_entitye_ld_name (entity *, char *ld_name);
240 void set_entity_ld_ident (entity *, ident *ld_ident);
244 get_entity_owner (entity *ent) {
245 return ent->owner = skip_tid(ent->owner);
249 set_entity_owner (entity *ent, type *owner) {
250 assert_legal_owner_of_ent(owner);
254 INLINE void /* should this go into type.c? */
255 assert_legal_owner_of_ent(type *owner) {
256 assert (get_type_tpop_code(owner) == tpo_class ||
257 get_type_tpop_code(owner) == tpo_union ||
258 get_type_tpop_code(owner) == tpo_struct ||
259 get_type_tpop_code(owner) == tpo_array); /* Yes, array has an entity
260 -- to select fields! */
264 get_entity_ld_ident (entity *ent)
266 if (ent->ld_name == NULL)
267 ent->ld_name = mangle_entity (ent);
272 set_entity_ld_ident (entity *ent, ident *ld_ident) {
273 ent->ld_name = ld_ident;
277 get_entity_ld_name (entity *ent) {
278 return get_id_str(get_entity_ld_ident(ent));
282 char *get_entity_ld_name (entity *);
283 void set_entity_ld_name (entity *, char *ld_name);
287 get_entity_type (entity *ent) {
288 return ent->type = skip_tid(ent->type);
292 set_entity_type (entity *ent, type *type) {
297 INLINE ent_allocation
298 get_entity_allocation (entity *ent) {
299 return ent->allocation;
303 set_entity_allocation (entity *ent, ent_allocation al) {
304 ent->allocation = al;
307 /* return the name of the visibility */
308 const char *get_allocation_name(ent_allocation all)
310 #define X(a) case a: return #a
312 X(allocation_automatic);
313 X(allocation_parameter);
314 X(allocation_dynamic);
315 X(allocation_static);
316 default: return "BAD VALUE";
322 INLINE ent_visibility
323 get_entity_visibility (entity *ent) {
324 return ent->visibility;
328 set_entity_visibility (entity *ent, ent_visibility vis) {
329 if (vis != visibility_local)
330 assert((ent->allocation == allocation_static) ||
331 (ent->allocation == allocation_automatic));
332 /* @@@ Test that the owner type is not local, but how??
333 && get_class_visibility(get_entity_owner(ent)) != local));*/
334 ent->visibility = vis;
337 /* return the name of the visibility */
338 const char *get_visibility_name(ent_visibility vis)
340 #define X(a) case a: return #a
343 X(visibility_external_visible);
344 X(visibility_external_allocated);
345 default: return "BAD VALUE";
350 INLINE ent_variability
351 get_entity_variability (entity *ent) {
352 return ent->variability;
356 set_entity_variability (entity *ent, ent_variability var)
358 if (var == variability_part_constant)
359 assert(is_class_type(ent->type) || is_struct_type(ent->type));
361 if ((is_compound_type(ent->type)) &&
362 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
363 /* Allocate datastructures for constant values */
364 ent->values = NEW_ARR_F(ir_node *, 0);
365 ent->val_paths = NEW_ARR_F(compound_graph_path *, 0);
368 if ((is_compound_type(ent->type)) &&
369 (var == variability_uninitialized) && (ent->variability != variability_uninitialized)) {
370 /* Free datastructures for constant values */
371 DEL_ARR_F(ent->values); ent->values = NULL;
372 DEL_ARR_F(ent->val_paths); ent->val_paths = NULL;
374 ent->variability = var;
377 /* return the name of the variablity */
378 const char *get_variability_name(ent_variability var)
380 #define X(a) case a: return #a
382 X(variability_uninitialized);
383 X(variability_initialized);
384 X(variability_part_constant);
385 X(variability_constant);
386 default: return "BAD VALUE";
391 INLINE ent_volatility
392 get_entity_volatility (entity *ent) {
394 return ent->volatility;
398 set_entity_volatility (entity *ent, ent_volatility vol) {
400 ent->volatility = vol;
403 /* return the name of the volatility */
404 const char *get_volatility_name(ent_volatility var)
406 #define X(a) case a: return #a
408 X(volatility_non_volatile);
409 X(volatility_is_volatile);
410 default: return "BAD VALUE";
416 get_entity_peculiarity (entity *ent) {
418 return ent->peculiarity;
422 set_entity_peculiarity (entity *ent, peculiarity pec) {
424 /* @@@ why peculiarity only for methods? */
425 assert (is_method_type(ent->type));
426 ent->peculiarity = pec;
429 /* return the name of the peculiarity */
430 const char *get_peculiarity_name(peculiarity var)
432 #define X(a) case a: return #a
434 X(peculiarity_description);
435 X(peculiarity_inherited);
436 X(peculiarity_existent);
437 default: return "BAD VALUE";
442 /* Set has no effect for existent entities of type method. */
444 get_atomic_ent_value(entity *ent)
447 assert(is_atomic_entity(ent));
448 assert(ent->variability != variability_uninitialized);
453 set_atomic_ent_value(entity *ent, ir_node *val) {
454 assert(ent && is_atomic_entity(ent) && (ent->variability != variability_uninitialized));
455 if (is_method_type(ent->type) && (ent->peculiarity == peculiarity_existent))
460 /* Returns true if the the node is representable as code on
462 int is_irn_const_expression(ir_node *n) {
466 switch(get_irn_opcode(n)) {
472 if (is_irn_const_expression(get_Add_left(n)))
473 return is_irn_const_expression(get_Add_right(n));
476 return is_irn_const_expression(get_irn_n(n, 0));
485 ir_node *copy_const_value(ir_node *n) {
490 switch(get_irn_opcode(n)) {
492 nn = new_Const(m, get_Const_tarval(n)); break;
494 nn = new_SymConst(get_SymConst_type_or_id(n), get_SymConst_kind(n)); break;
496 nn = new_Add(copy_const_value(get_Add_left(n)),
497 copy_const_value(get_Add_right(n)), m); break;
499 nn = new_Cast(copy_const_value(get_Cast_op(n)), get_Cast_type(n)); break;
501 nn = new_Conv(copy_const_value(get_Conv_op(n)), m); break;
503 nn = new_Unknown(m); break;
506 assert(0 && "opdope invalid or not implemented");
513 compound_graph_path *
514 new_compound_graph_path(type *tp, int length) {
515 compound_graph_path *res;
516 assert(is_type(tp) && is_compound_type(tp));
519 res = (compound_graph_path *) malloc (sizeof(compound_graph_path) + (length-1) * sizeof(entity *));
520 res->kind = k_ir_compound_graph_path;
523 memset(res->nodes, 0, sizeof(entity *) * length);
528 free_compound_graph_path (compound_graph_path *gr) {
529 assert(gr && is_compound_graph_path(gr));
535 is_compound_graph_path(void *thing) {
536 return (get_kind(thing) == k_ir_compound_graph_path);
539 /* checks whether nodes 0..pos are correct (all lie on a path.) */
540 /* @@@ not implemented */
541 INLINE int is_proper_compound_graph_path(compound_graph_path *gr, int pos) {
544 type *owner = gr->tp;
545 for (i = 0; i <= pos; i++) {
546 node = get_compound_graph_path_node(gr, i);
547 if (get_entity_owner(node) != owner) return false;
548 owner = get_entity_type(node);
550 if (pos == get_compound_graph_path_length(gr))
551 if (!is_atomic_type(owner)) return false;
556 get_compound_graph_path_length(compound_graph_path *gr) {
557 assert(gr && is_compound_graph_path(gr));
562 get_compound_graph_path_node(compound_graph_path *gr, int pos) {
563 assert(gr && is_compound_graph_path(gr));
564 assert(pos >= 0 && pos < gr->len);
565 return gr->nodes[pos];
569 set_compound_graph_path_node(compound_graph_path *gr, int pos, entity *node) {
570 assert(gr && is_compound_graph_path(gr));
571 assert(pos >= 0 && pos < gr->len);
572 assert(is_entity(node));
573 gr->nodes[pos] = node;
574 assert(is_proper_compound_graph_path(gr, pos));
577 /* A value of a compound entity is a pair of value and the corresponding path to a member of
580 add_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path) {
581 assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
582 ARR_APP1 (ir_node *, ent->values, val);
583 ARR_APP1 (compound_graph_path *, ent->val_paths, path);
587 set_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path, int pos) {
588 assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
589 ent->values[pos] = val;
590 ent->val_paths[pos] = path;
594 get_compound_ent_n_values(entity *ent) {
595 assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
596 return (ARR_LEN (ent->values));
600 get_compound_ent_value(entity *ent, int pos) {
601 assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
602 return ent->values[pos];
605 INLINE compound_graph_path *
606 get_compound_ent_value_path(entity *ent, int pos) {
607 assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
608 return ent->val_paths[pos];
612 remove_compound_ent_value(entity *ent, entity *value_ent) {
614 assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
615 for (i = 0; i < (ARR_LEN (ent->val_paths)); i++) {
616 compound_graph_path *path = ent->val_paths[i];
617 if (path->nodes[path->len-1] == value_ent) {
618 for(; i < (ARR_LEN (ent->val_paths))-1; i++) {
619 ent->val_paths[i] = ent->val_paths[i+1];
620 ent->values[i] = ent->values[i+1];
622 ARR_SETLEN(entity*, ent->val_paths, ARR_LEN(ent->val_paths) - 1);
623 ARR_SETLEN(ir_node*, ent->values, ARR_LEN(ent->values) - 1);
630 add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
631 compound_graph_path *path;
632 assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
633 path = new_compound_graph_path(get_entity_owner(ent), 1);
634 path->nodes[0] = member;
635 add_compound_ent_value_w_path(ent, val, path);
638 /* Copies the firm subgraph referenced by val to const_code_irg and adds
639 the node as constant initialization to ent.
640 The subgraph may not contain control flow operations.
642 copy_and_add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
643 ir_graph *rem = current_ir_graph;
645 assert(get_entity_variability(ent) != variability_uninitialized);
646 current_ir_graph = get_const_code_irg();
648 val = copy_const_value(val);
649 add_compound_ent_value(ent, val, member);
650 current_ir_graph = rem;
653 /* Copies the value i of the entity to current_block in current_ir_graph.
655 copy_compound_ent_value(entity *ent, int pos) {
656 assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
657 return copy_const_value(ent->values[pos+1]);
661 get_compound_ent_value_member(entity *ent, int pos) {
662 compound_graph_path *path;
663 assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
664 path = get_compound_ent_value_path(ent, pos);
666 return get_compound_graph_path_node(path, get_compound_graph_path_length(path)-1);
670 set_compound_ent_value(entity *ent, ir_node *val, entity *member, int pos) {
671 compound_graph_path *path;
672 assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
673 path = get_compound_ent_value_path(ent, pos);
674 set_compound_graph_path_node(path, 0, member);
675 set_compound_ent_value_w_path(ent, val, path, pos);
679 set_array_entity_values(entity *ent, tarval **values, int num_vals) {
681 ir_graph *rem = current_ir_graph;
682 type *arrtp = get_entity_type(ent);
685 assert(is_array_type(arrtp));
686 assert(get_array_n_dimensions(arrtp) == 1);
687 /* One bound is sufficient, the nunmber of constant fields makes the
689 assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
690 assert(get_entity_variability(ent) != variability_uninitialized);
691 current_ir_graph = get_const_code_irg();
693 for (i = 0; i < num_vals; i++) {
694 val = new_Const(get_tarval_mode (values[i]), values[i]);
695 add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
697 current_ir_graph = rem;
701 get_entity_offset (entity *ent) {
706 set_entity_offset (entity *ent, int offset) {
707 ent->offset = offset;
711 add_entity_overwrites (entity *ent, entity *overwritten) {
713 assert(is_class_type(get_entity_owner(ent)));
714 ARR_APP1 (entity *, ent->overwrites, overwritten);
715 ARR_APP1 (entity *, overwritten->overwrittenby, ent);
719 get_entity_n_overwrites (entity *ent) {
721 assert(is_class_type(get_entity_owner(ent)));
722 return (ARR_LEN (ent->overwrites));
726 get_entity_overwrites_index(entity *ent, entity *overwritten) {
728 assert(ent && is_class_type(get_entity_owner(ent)));
729 for (i = 0; i < get_entity_n_overwrites(ent); i++)
730 if (get_entity_overwrites(ent, i) == overwritten)
736 get_entity_overwrites (entity *ent, int pos) {
738 assert(is_class_type(get_entity_owner(ent)));
739 assert(pos < get_entity_n_overwrites(ent));
740 return ent->overwrites[pos];
744 set_entity_overwrites (entity *ent, int pos, entity *overwritten) {
746 assert(is_class_type(get_entity_owner(ent)));
747 assert(pos < get_entity_n_overwrites(ent));
748 ent->overwrites[pos] = overwritten;
752 remove_entity_overwrites(entity *ent, entity *overwritten) {
754 assert(ent && is_class_type(get_entity_owner(ent)));
755 for (i = 0; i < (ARR_LEN (ent->overwrites)); i++)
756 if (ent->overwrites[i] == overwritten) {
757 for(; i < (ARR_LEN (ent->overwrites))-1; i++)
758 ent->overwrites[i] = ent->overwrites[i+1];
759 ARR_SETLEN(entity*, ent->overwrites, ARR_LEN(ent->overwrites) - 1);
765 add_entity_overwrittenby (entity *ent, entity *overwrites) {
767 assert(is_class_type(get_entity_owner(ent)));
768 add_entity_overwrites(overwrites, ent);
772 get_entity_n_overwrittenby (entity *ent) {
774 assert(is_class_type(get_entity_owner(ent)));
775 return (ARR_LEN (ent->overwrittenby));
779 get_entity_overwrittenby_index(entity *ent, entity *overwrites) {
781 assert(ent && is_class_type(get_entity_owner(ent)));
782 for (i = 0; i < get_entity_n_overwrittenby(ent); i++)
783 if (get_entity_overwrittenby(ent, i) == overwrites)
789 get_entity_overwrittenby (entity *ent, int pos) {
791 assert(is_class_type(get_entity_owner(ent)));
792 assert(pos < get_entity_n_overwrittenby(ent));
793 return ent->overwrittenby[pos];
797 set_entity_overwrittenby (entity *ent, int pos, entity *overwrites) {
799 assert(is_class_type(get_entity_owner(ent)));
800 assert(pos < get_entity_n_overwrittenby(ent));
801 ent->overwrittenby[pos] = overwrites;
804 void remove_entity_overwrittenby(entity *ent, entity *overwrites) {
806 assert(ent && is_class_type(get_entity_owner(ent)));
807 for (i = 0; i < (ARR_LEN (ent->overwrittenby)); i++)
808 if (ent->overwrittenby[i] == overwrites) {
809 for(; i < (ARR_LEN (ent->overwrittenby))-1; i++)
810 ent->overwrittenby[i] = ent->overwrittenby[i+1];
811 ARR_SETLEN(entity*, ent->overwrittenby, ARR_LEN(ent->overwrittenby) - 1);
816 /* A link to store intermediate information */
818 get_entity_link(entity *ent) {
824 set_entity_link(entity *ent, void *l) {
830 get_entity_irg(entity *ent) {
832 assert (is_method_type(ent->type));
837 set_entity_irg(entity *ent, ir_graph *irg) {
838 assert (ent && ent->type);
839 /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
840 * Methode selbst nicht mehr aufgerufen werden kann, die Entität
841 * aber erhalten bleiben soll. */
843 assert (is_method_type(ent->type));
844 assert (ent->peculiarity == peculiarity_existent);
848 int is_entity (void *thing) {
850 if (get_kind(thing) == k_entity)
856 int is_atomic_entity(entity *ent) {
857 type* t = get_entity_type(ent);
858 return (is_primitive_type(t) || is_pointer_type(t) ||
859 is_enumeration_type(t) || is_method_type(t));
862 int is_compound_entity(entity *ent) {
863 type* t = get_entity_type(ent);
864 return (is_class_type(t) || is_struct_type(t) ||
865 is_array_type(t) || is_union_type(t));
868 /* @@@ not implemnted!!! */
869 bool equal_entity(entity *ent1, entity *ent2) {
870 printf(" calling unimplemented equal entity!!! \n");
875 unsigned long get_entity_visited(entity *ent) {
879 void set_entity_visited(entity *ent, unsigned long num) {
883 /* Sets visited field in entity to entity_visited. */
884 void mark_entity_visited(entity *ent) {
886 ent->visit = type_visited;
890 INLINE bool entity_visited(entity *ent) {
891 return get_entity_visited(ent) >= type_visited;
894 INLINE bool entity_not_visited(entity *ent) {
895 return get_entity_visited(ent) < type_visited;