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;
100 res->peculiarity = peculiarity_existent;
101 res->volatility = volatility_non_volatile;
103 res->overwrites = NEW_ARR_F(entity *, 1);
104 res->overwrittenby = NEW_ARR_F(entity *, 1);
109 res->nr = get_irp_new_node_nr();
114 /* Remember entity in it's owner. */
115 insert_entity_in_owner (res);
119 new_d_entity (type *owner, ident *name, type *type, dbg_info *db) {
120 entity *res = new_entity(owner, name, type);
121 set_entity_dbg_info(res, db);
124 INLINE void free_entity_attrs(entity *ent) {
126 if (get_type_tpop(get_entity_owner(ent)) == type_class) {
127 DEL_ARR_F(ent->overwrites);
128 DEL_ARR_F(ent->overwrittenby);
133 copy_entity_own (entity *old, type *new_owner) {
136 assert_legal_owner_of_ent(new_owner);
137 if (old->owner == new_owner) return old;
138 new = (entity *) xmalloc (sizeof (entity));
139 memcpy (new, old, sizeof (entity));
140 new->owner = new_owner;
142 if ((get_type_tpop(get_entity_owner(old)) == type_class) &&
143 (get_type_tpop(new_owner) == type_class)) {
144 new->overwrites = DUP_ARR_F(entity *, old->overwrites);
145 new->overwrittenby = DUP_ARR_F(entity *, old->overwrittenby);
146 } else if ((get_type_tpop(get_entity_owner(old)) != type_class) &&
147 (get_type_tpop(new_owner) == type_class)) {
148 new->overwrites = NEW_ARR_F(entity *, 1);
149 new->overwrittenby = NEW_ARR_F(entity *, 1);
152 if (is_class_type(new_owner)) {
153 new->overwrites = NEW_ARR_F(entity *, 1);
154 new->overwrittenby = NEW_ARR_F(entity *, 1);
157 new->nr = get_irp_new_node_nr();
160 insert_entity_in_owner (new);
166 copy_entity_name (entity *old, ident *new_name) {
169 if (old->name == new_name) return old;
170 new = (entity *) xmalloc (sizeof (entity));
171 memcpy (new, old, sizeof (entity));
172 new->name = new_name;
174 if (is_class_type(new->owner)) {
175 new->overwrites = DUP_ARR_F(entity *, old->overwrites);
176 new->overwrittenby = DUP_ARR_F(entity *, old->overwrittenby);
179 new->nr = get_irp_new_node_nr();
182 insert_entity_in_owner (new);
189 free_entity (entity *ent) {
190 free_tarval_entity(ent);
191 free_entity_attrs(ent);
196 /* Outputs a unique number for this node */
198 get_entity_nr(entity *ent) {
208 get_entity_name (entity *ent) {
210 return get_id_str(get_entity_ident(ent));
214 get_entity_ident (entity *ent) {
220 void set_entitye_ld_name (entity *, char *ld_name);
221 void set_entity_ld_ident (entity *, ident *ld_ident);
225 get_entity_owner (entity *ent) {
226 return ent->owner = skip_tid(ent->owner);
230 set_entity_owner (entity *ent, type *owner) {
231 assert_legal_owner_of_ent(owner);
235 INLINE void /* should this go into type.c? */
236 assert_legal_owner_of_ent(type *owner) {
237 assert (get_type_tpop_code(owner) == tpo_class ||
238 get_type_tpop_code(owner) == tpo_union ||
239 get_type_tpop_code(owner) == tpo_struct ||
240 get_type_tpop_code(owner) == tpo_array); /* Yes, array has an entity
241 -- to select fields! */
245 get_entity_ld_ident (entity *ent)
247 if (ent->ld_name == NULL)
248 ent->ld_name = mangle_entity (ent);
253 set_entity_ld_ident (entity *ent, ident *ld_ident) {
254 ent->ld_name = ld_ident;
258 get_entity_ld_name (entity *ent) {
259 return get_id_str(get_entity_ld_ident(ent));
263 char *get_entity_ld_name (entity *);
264 void set_entity_ld_name (entity *, char *ld_name);
268 get_entity_type (entity *ent) {
269 return ent->type = skip_tid(ent->type);
273 set_entity_type (entity *ent, type *type) {
278 INLINE ent_allocation
279 get_entity_allocation (entity *ent) {
280 return ent->allocation;
284 set_entity_allocation (entity *ent, ent_allocation al) {
285 ent->allocation = al;
288 /* return the name of the visibility */
289 const char *get_allocation_name(ent_allocation all)
291 #define X(a) case a: return #a
293 X(allocation_automatic);
294 X(allocation_parameter);
295 X(allocation_dynamic);
296 X(allocation_static);
297 default: return "BAD VALUE";
303 INLINE ent_visibility
304 get_entity_visibility (entity *ent) {
305 return ent->visibility;
309 set_entity_visibility (entity *ent, ent_visibility vis) {
310 if (vis != visibility_local)
311 assert((ent->allocation == allocation_static) ||
312 (ent->allocation == allocation_automatic));
313 /* @@@ Test that the owner type is not local, but how??
314 && get_class_visibility(get_entity_owner(ent)) != local));*/
315 ent->visibility = vis;
318 /* return the name of the visibility */
319 const char *get_visibility_name(ent_visibility vis)
321 #define X(a) case a: return #a
324 X(visibility_external_visible);
325 X(visibility_external_allocated);
326 default: return "BAD VALUE";
331 INLINE ent_variability
332 get_entity_variability (entity *ent) {
333 return ent->variability;
337 set_entity_variability (entity *ent, ent_variability var)
339 if (var == variability_part_constant)
340 assert(is_class_type(ent->type) || is_struct_type(ent->type));
342 if ((is_compound_type(ent->type)) &&
343 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
344 /* Allocate datastructures for constant values */
345 ent->values = NEW_ARR_F(ir_node *, 1);
346 ent->val_paths = NEW_ARR_F(compound_graph_path *, 1);
349 if ((is_compound_type(ent->type)) &&
350 (var == variability_uninitialized) && (ent->variability != variability_uninitialized)) {
351 /* Free datastructures for constant values */
352 DEL_ARR_F(ent->values);
353 DEL_ARR_F(ent->val_paths);
355 ent->variability = var;
358 /* return the name of the variablity */
359 const char *get_variability_name(ent_variability var)
361 #define X(a) case a: return #a
363 X(variability_uninitialized);
364 X(variability_initialized);
365 X(variability_part_constant);
366 X(variability_constant);
367 default: return "BAD VALUE";
372 INLINE ent_volatility
373 get_entity_volatility (entity *ent) {
375 return ent->volatility;
379 set_entity_volatility (entity *ent, ent_volatility vol) {
381 ent->volatility = vol;
384 /* return the name of the volatility */
385 const char *get_volatility_name(ent_volatility var)
387 #define X(a) case a: return #a
389 X(volatility_non_volatile);
390 X(volatility_is_volatile);
391 default: return "BAD VALUE";
397 get_entity_peculiarity (entity *ent) {
399 return ent->peculiarity;
403 set_entity_peculiarity (entity *ent, peculiarity pec) {
405 /* @@@ why peculiarity only for methods? */
406 assert (is_method_type(ent->type));
407 ent->peculiarity = pec;
410 /* return the name of the peculiarity */
411 const char *get_peculiarity_name(peculiarity var)
413 #define X(a) case a: return #a
415 X(peculiarity_description);
416 X(peculiarity_inherited);
417 X(peculiarity_existent);
418 default: return "BAD VALUE";
423 /* Set has no effect for existent entities of type method. */
425 get_atomic_ent_value(entity *ent)
428 assert(is_atomic_entity(ent));
429 assert(ent->variability != variability_uninitialized);
434 set_atomic_ent_value(entity *ent, ir_node *val) {
435 assert(ent && is_atomic_entity(ent) && (ent->variability != variability_uninitialized));
436 if (is_method_type(ent->type) && (ent->peculiarity == peculiarity_existent))
441 /* Returns true if the the node is representable as code on
443 int is_irn_const_expression(ir_node *n) {
447 switch(get_irn_opcode(n)) {
453 if (is_irn_const_expression(get_Add_left(n)))
454 return is_irn_const_expression(get_Add_right(n));
457 return is_irn_const_expression(get_irn_n(n, 0));
466 ir_node *copy_const_value(ir_node *n) {
471 switch(get_irn_opcode(n)) {
473 nn = new_Const(m, get_Const_tarval(n)); break;
475 nn = new_SymConst(get_SymConst_type_or_id(n), get_SymConst_kind(n)); break;
477 nn = new_Add(copy_const_value(get_Add_left(n)),
478 copy_const_value(get_Add_right(n)), m); break;
480 nn = new_Cast(copy_const_value(get_Cast_op(n)), get_Cast_type(n)); break;
482 nn = new_Conv(copy_const_value(get_Conv_op(n)), m); break;
484 nn = new_Unknown(m); break;
487 assert(0 && "opdope invalid or not implemented");
494 compound_graph_path *
495 new_compound_graph_path(type *tp, int length) {
496 compound_graph_path *res;
497 assert(is_type(tp) && is_compound_type(tp));
500 res = (compound_graph_path *) malloc (sizeof(compound_graph_path) + (length-1) * sizeof(entity *));
501 res->kind = k_ir_compound_graph_path;
504 memset(res->nodes, 0, sizeof(entity *) * length);
509 free_compound_graph_path (compound_graph_path *gr) {
510 assert(gr && is_compound_graph_path(gr));
516 is_compound_graph_path(void *thing) {
517 return (get_kind(thing) == k_ir_compound_graph_path);
520 /* checks whether nodes 0..pos are correct (all lie on a path.) */
521 /* @@@ not implemented */
522 INLINE int is_proper_compound_graph_path(compound_graph_path *gr, int pos) {
525 type *owner = gr->tp;
526 for (i = 0; i <= pos; i++) {
527 node = get_compound_graph_path_node(gr, i);
528 if (get_entity_owner(node) != owner) return false;
529 owner = get_entity_type(node);
531 if (pos == get_compound_graph_path_length(gr) -1)
532 if (!is_atomic_type(owner)) return false;
537 get_compound_graph_path_length(compound_graph_path *gr) {
538 assert(gr && is_compound_graph_path(gr));
543 get_compound_graph_path_node(compound_graph_path *gr, int pos) {
544 assert(gr && is_compound_graph_path(gr));
545 assert(pos >= 0 && pos < gr->len);
546 return gr->nodes[pos];
550 set_compound_graph_path_node(compound_graph_path *gr, int pos, entity *node) {
551 assert(gr && is_compound_graph_path(gr));
552 assert(pos >= 0 && pos < gr->len);
553 assert(is_entity(node));
554 gr->nodes[pos] = node;
555 assert(is_proper_compound_graph_path(gr, pos));
558 /* A value of a compound entity is a pair of value and the corresponding path to a member of
561 add_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path) {
562 assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
563 ARR_APP1 (ir_node *, ent->values, val);
564 ARR_APP1 (compound_graph_path *, ent->val_paths, path);
568 set_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path, int pos) {
569 assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
570 ent->values[pos+1] = val;
571 ent->val_paths[pos+1] = path;
575 get_compound_ent_n_values(entity *ent) {
576 assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
577 return (ARR_LEN (ent->values))-1;
581 get_compound_ent_value(entity *ent, int pos) {
582 assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
583 return ent->values[pos+1];
586 INLINE compound_graph_path *
587 get_compound_ent_value_path(entity *ent, int pos) {
588 assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
589 return ent->val_paths[pos+1];
593 remove_compound_ent_value(entity *ent, entity *value_ent) {
595 assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
596 for (i = 1; i < (ARR_LEN (ent->val_paths)); i++) {
597 compound_graph_path *path = ent->val_paths[i];
598 if (path->nodes[path->len-1] == value_ent) {
599 for(; i < (ARR_LEN (ent->val_paths))-1; i++) {
600 ent->val_paths[i] = ent->val_paths[i+1];
601 ent->values[i] = ent->values[i+1];
603 ARR_SETLEN(entity*, ent->val_paths, ARR_LEN(ent->val_paths) - 1);
604 ARR_SETLEN(ir_node*, ent->values, ARR_LEN(ent->values) - 1);
611 add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
612 compound_graph_path *path;
613 assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
614 path = new_compound_graph_path(get_entity_owner(ent), 1);
615 path->nodes[0] = member;
616 add_compound_ent_value_w_path(ent, val, path);
619 /* Copies the firm subgraph referenced by val to const_code_irg and adds
620 the node as constant initialization to ent.
621 The subgraph may not contain control flow operations.
623 copy_and_add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
624 ir_graph *rem = current_ir_graph;
626 assert(get_entity_variability(ent) != variability_uninitialized);
627 current_ir_graph = get_const_code_irg();
629 val = copy_const_value(val);
630 add_compound_ent_value(ent, val, member);
631 current_ir_graph = rem;
634 /* Copies the value i of the entity to current_block in current_ir_graph.
636 copy_compound_ent_value(entity *ent, int pos) {
637 assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
638 return copy_const_value(ent->values[pos+1]);
642 get_compound_ent_value_member(entity *ent, int pos) {
643 compound_graph_path *path;
644 assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
645 path = get_compound_ent_value_path(ent, pos);
647 return get_compound_graph_path_node(path, get_compound_graph_path_length(path)-1);
651 set_compound_ent_value(entity *ent, ir_node *val, entity *member, int pos) {
652 compound_graph_path *path;
653 assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
654 path = get_compound_ent_value_path(ent, pos);
655 set_compound_graph_path_node(path, 0, member);
656 set_compound_ent_value_w_path(ent, val, path, pos);
660 set_array_entity_values(entity *ent, tarval **values, int num_vals) {
662 ir_graph *rem = current_ir_graph;
663 type *arrtp = get_entity_type(ent);
666 assert(is_array_type(arrtp));
667 assert(get_array_n_dimensions(arrtp) == 1);
668 /* One bound is sufficient, the nunmber of constant fields makes the
670 assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
671 assert(get_entity_variability(ent) != variability_uninitialized);
672 current_ir_graph = get_const_code_irg();
674 for (i = 0; i < num_vals; i++) {
675 val = new_Const(get_tarval_mode (values[i]), values[i]);
676 add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
678 current_ir_graph = rem;
682 get_entity_offset (entity *ent) {
687 set_entity_offset (entity *ent, int offset) {
688 ent->offset = offset;
692 add_entity_overwrites (entity *ent, entity *overwritten) {
694 assert(is_class_type(get_entity_owner(ent)));
695 ARR_APP1 (entity *, ent->overwrites, overwritten);
696 ARR_APP1 (entity *, overwritten->overwrittenby, ent);
700 get_entity_n_overwrites (entity *ent) {
702 assert(is_class_type(get_entity_owner(ent)));
703 return (ARR_LEN (ent->overwrites))-1;
707 get_entity_overwrites_index(entity *ent, entity *overwritten) {
709 assert(ent && is_class_type(get_entity_owner(ent)));
710 for (i = 0; i < get_entity_n_overwrites(ent); i++)
711 if (get_entity_overwrites(ent, i) == overwritten)
717 get_entity_overwrites (entity *ent, int pos) {
719 assert(is_class_type(get_entity_owner(ent)));
720 assert(pos < get_entity_n_overwrites(ent));
721 return ent->overwrites[pos+1];
725 set_entity_overwrites (entity *ent, int pos, entity *overwritten) {
727 assert(is_class_type(get_entity_owner(ent)));
728 assert(pos < get_entity_n_overwrites(ent));
729 ent->overwrites[pos+1] = overwritten;
733 remove_entity_overwrites(entity *ent, entity *overwritten) {
735 assert(ent && is_class_type(get_entity_owner(ent)));
736 for (i = 1; i < (ARR_LEN (ent->overwrites)); i++)
737 if (ent->overwrites[i] == overwritten) {
738 for(; i < (ARR_LEN (ent->overwrites))-1; i++)
739 ent->overwrites[i] = ent->overwrites[i+1];
740 ARR_SETLEN(entity*, ent->overwrites, ARR_LEN(ent->overwrites) - 1);
746 add_entity_overwrittenby (entity *ent, entity *overwrites) {
748 assert(is_class_type(get_entity_owner(ent)));
749 add_entity_overwrites(overwrites, ent);
753 get_entity_n_overwrittenby (entity *ent) {
755 assert(is_class_type(get_entity_owner(ent)));
756 return (ARR_LEN (ent->overwrittenby))-1;
760 get_entity_overwrittenby_index(entity *ent, entity *overwrites) {
762 assert(ent && is_class_type(get_entity_owner(ent)));
763 for (i = 0; i < get_entity_n_overwrittenby(ent); i++)
764 if (get_entity_overwrittenby(ent, i) == overwrites)
770 get_entity_overwrittenby (entity *ent, int pos) {
772 assert(is_class_type(get_entity_owner(ent)));
773 assert(pos < get_entity_n_overwrittenby(ent));
774 return ent->overwrittenby[pos+1];
778 set_entity_overwrittenby (entity *ent, int pos, entity *overwrites) {
780 assert(is_class_type(get_entity_owner(ent)));
781 assert(pos < get_entity_n_overwrittenby(ent));
782 ent->overwrittenby[pos+1] = overwrites;
785 void remove_entity_overwrittenby(entity *ent, entity *overwrites) {
787 assert(ent && is_class_type(get_entity_owner(ent)));
788 for (i = 1; i < (ARR_LEN (ent->overwrittenby)); i++)
789 if (ent->overwrittenby[i] == overwrites) {
790 for(; i < (ARR_LEN (ent->overwrittenby))-1; i++)
791 ent->overwrittenby[i] = ent->overwrittenby[i+1];
792 ARR_SETLEN(entity*, ent->overwrittenby, ARR_LEN(ent->overwrittenby) - 1);
797 /* A link to store intermediate information */
799 get_entity_link(entity *ent) {
805 set_entity_link(entity *ent, void *l) {
811 get_entity_irg(entity *ent) {
813 assert (is_method_type(ent->type));
818 set_entity_irg(entity *ent, ir_graph *irg) {
819 assert (ent && ent->type);
820 /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
821 * Methode selbst nicht mehr aufgerufen werden kann, die Entität
822 * aber erhalten bleiben soll. */
824 assert (is_method_type(ent->type));
825 assert (ent->peculiarity == peculiarity_existent);
829 int is_entity (void *thing) {
831 if (get_kind(thing) == k_entity)
837 int is_atomic_entity(entity *ent) {
838 type* t = get_entity_type(ent);
839 return (is_primitive_type(t) || is_pointer_type(t) ||
840 is_enumeration_type(t) || is_method_type(t));
843 int is_compound_entity(entity *ent) {
844 type* t = get_entity_type(ent);
845 return (is_class_type(t) || is_struct_type(t) ||
846 is_array_type(t) || is_union_type(t));
849 /* @@@ not implemnted!!! */
850 bool equal_entity(entity *ent1, entity *ent2) {
851 printf(" calling unimplemented equal entity!!! \n");
856 unsigned long get_entity_visited(entity *ent) {
860 void set_entity_visited(entity *ent, unsigned long num) {
864 /* Sets visited field in entity to entity_visited. */
865 void mark_entity_visited(entity *ent) {
867 ent->visit = type_visited;
871 INLINE bool entity_visited(entity *ent) {
872 return get_entity_visited(ent) >= type_visited;
875 INLINE bool entity_not_visited(entity *ent) {
876 return get_entity_visited(ent) < type_visited;