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);
195 /* Outputs a unique number for this node */
197 get_entity_nr(entity *ent) {
207 get_entity_name (entity *ent) {
209 return get_id_str(get_entity_ident(ent));
213 get_entity_ident (entity *ent) {
219 void set_entitye_ld_name (entity *, char *ld_name);
220 void set_entity_ld_ident (entity *, ident *ld_ident);
224 get_entity_owner (entity *ent) {
225 return ent->owner = skip_tid(ent->owner);
229 set_entity_owner (entity *ent, type *owner) {
230 assert_legal_owner_of_ent(owner);
234 INLINE void /* should this go into type.c? */
235 assert_legal_owner_of_ent(type *owner) {
236 assert (get_type_tpop_code(owner) == tpo_class ||
237 get_type_tpop_code(owner) == tpo_union ||
238 get_type_tpop_code(owner) == tpo_struct ||
239 get_type_tpop_code(owner) == tpo_array); /* Yes, array has an entity
240 -- to select fields! */
244 get_entity_ld_ident (entity *ent)
246 if (ent->ld_name == NULL)
247 ent->ld_name = mangle_entity (ent);
252 set_entity_ld_ident (entity *ent, ident *ld_ident) {
253 ent->ld_name = ld_ident;
257 get_entity_ld_name (entity *ent) {
258 return get_id_str(get_entity_ld_ident(ent));
262 char *get_entity_ld_name (entity *);
263 void set_entity_ld_name (entity *, char *ld_name);
267 get_entity_type (entity *ent) {
268 return ent->type = skip_tid(ent->type);
272 set_entity_type (entity *ent, type *type) {
277 INLINE ent_allocation
278 get_entity_allocation (entity *ent) {
279 return ent->allocation;
283 set_entity_allocation (entity *ent, ent_allocation al) {
284 ent->allocation = al;
287 /* return the name of the visibility */
288 const char *get_allocation_name(ent_allocation all)
290 #define X(a) case a: return #a
292 X(allocation_automatic);
293 X(allocation_parameter);
294 X(allocation_dynamic);
295 X(allocation_static);
296 default: return "BAD VALUE";
302 INLINE ent_visibility
303 get_entity_visibility (entity *ent) {
304 return ent->visibility;
308 set_entity_visibility (entity *ent, ent_visibility vis) {
309 if (vis != visibility_local)
310 assert((ent->allocation == allocation_static) ||
311 (ent->allocation == allocation_automatic));
312 /* @@@ Test that the owner type is not local, but how??
313 && get_class_visibility(get_entity_owner(ent)) != local));*/
314 ent->visibility = vis;
317 /* return the name of the visibility */
318 const char *get_visibility_name(ent_visibility vis)
320 #define X(a) case a: return #a
323 X(visibility_external_visible);
324 X(visibility_external_allocated);
325 default: return "BAD VALUE";
330 INLINE ent_variability
331 get_entity_variability (entity *ent) {
332 return ent->variability;
336 set_entity_variability (entity *ent, ent_variability var)
338 if (var == variability_part_constant)
339 assert(is_class_type(ent->type) || is_struct_type(ent->type));
341 if ((is_compound_type(ent->type)) &&
342 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
343 /* Allocate datastructures for constant values */
344 ent->values = NEW_ARR_F(ir_node *, 1);
345 ent->val_paths = NEW_ARR_F(compound_graph_path *, 1);
348 if ((is_compound_type(ent->type)) &&
349 (var == variability_uninitialized) && (ent->variability != variability_uninitialized)) {
350 /* Free datastructures for constant values */
351 DEL_ARR_F(ent->values);
352 DEL_ARR_F(ent->val_paths);
354 ent->variability = var;
357 /* return the name of the variablity */
358 const char *get_variability_name(ent_variability var)
360 #define X(a) case a: return #a
362 X(variability_uninitialized);
363 X(variability_initialized);
364 X(variability_part_constant);
365 X(variability_constant);
366 default: return "BAD VALUE";
371 INLINE ent_volatility
372 get_entity_volatility (entity *ent) {
374 return ent->volatility;
378 set_entity_volatility (entity *ent, ent_volatility vol) {
380 ent->volatility = vol;
383 /* return the name of the volatility */
384 const char *get_volatility_name(ent_volatility var)
386 #define X(a) case a: return #a
388 X(volatility_non_volatile);
389 X(volatility_is_volatile);
390 default: return "BAD VALUE";
396 get_entity_peculiarity (entity *ent) {
398 return ent->peculiarity;
402 set_entity_peculiarity (entity *ent, peculiarity pec) {
404 /* @@@ why peculiarity only for methods? */
405 assert (is_method_type(ent->type));
406 ent->peculiarity = pec;
409 /* return the name of the peculiarity */
410 const char *get_peculiarity_name(peculiarity var)
412 #define X(a) case a: return #a
414 X(peculiarity_description);
415 X(peculiarity_inherited);
416 X(peculiarity_existent);
417 default: return "BAD VALUE";
422 /* Set has no effect for existent entities of type method. */
424 get_atomic_ent_value(entity *ent)
427 assert(is_atomic_entity(ent));
428 assert(ent->variability != variability_uninitialized);
433 set_atomic_ent_value(entity *ent, ir_node *val) {
434 assert(ent && is_atomic_entity(ent) && (ent->variability != variability_uninitialized));
435 if (is_method_type(ent->type) && (ent->peculiarity == peculiarity_existent))
441 ir_node *copy_const_value(ir_node *n) {
446 switch(get_irn_opcode(n)) {
448 nn = new_Const(m, get_Const_tarval(n)); break;
450 nn = new_SymConst(get_SymConst_type_or_id(n), get_SymConst_kind(n)); break;
452 nn = new_Add(copy_const_value(get_Add_left(n)), copy_const_value(get_Add_right(n)), m); break;
454 nn = new_Unknown(); break;
457 assert(0 && "opdope invalid or not implemented");
464 compound_graph_path *
465 new_compound_graph_path(type *tp, int length) {
466 compound_graph_path *res;
467 assert(is_type(tp) && is_compound_type(tp));
470 res = (compound_graph_path *) malloc (sizeof(compound_graph_path) + (length-1) * sizeof(entity *));
471 res->kind = k_ir_compound_graph_path;
474 memset(res->nodes, 0, sizeof(entity *) * length);
479 free_compound_graph_path (compound_graph_path *gr) {
480 assert(gr && is_compound_graph_path(gr));
485 is_compound_graph_path(void *thing) {
486 return (get_kind(thing) == k_ir_compound_graph_path);
489 /* checks whether nodes 0..pos are correct (all lie on a path.) */
490 /* @@@ not implemented */
491 INLINE int is_proper_compound_graph_path(compound_graph_path *gr, int pos) {
494 type *owner = gr->tp;
495 for (i = 0; i <= pos; i++) {
496 node = get_compound_graph_path_node(gr, i);
497 if (get_entity_owner(node) != owner) return false;
498 owner = get_entity_type(node);
500 if (pos == get_compound_graph_path_length(gr) -1)
501 if (!is_atomic_type(owner)) return false;
506 get_compound_graph_path_length(compound_graph_path *gr) {
507 assert(gr && is_compound_graph_path(gr));
512 get_compound_graph_path_node(compound_graph_path *gr, int pos) {
513 assert(gr && is_compound_graph_path(gr));
514 assert(pos >= 0 && pos < gr->len);
515 return gr->nodes[pos];
519 set_compound_graph_path_node(compound_graph_path *gr, int pos, entity *node) {
520 assert(gr && is_compound_graph_path(gr));
521 assert(pos >= 0 && pos < gr->len);
522 assert(is_entity(node));
523 gr->nodes[pos] = node;
524 assert(is_proper_compound_graph_path(gr, pos));
527 /* A value of a compound entity is a pair of value and the corresponding path to a member of
530 add_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path) {
531 assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
532 ARR_APP1 (ir_node *, ent->values, val);
533 ARR_APP1 (compound_graph_path *, ent->val_paths, path);
537 set_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path, int pos) {
538 assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
539 ent->values[pos+1] = val;
540 ent->val_paths[pos+1] = path;
544 get_compound_ent_n_values(entity *ent) {
545 assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
546 return (ARR_LEN (ent->values))-1;
550 get_compound_ent_value(entity *ent, int pos) {
551 assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
552 return ent->values[pos+1];
555 INLINE compound_graph_path *
556 get_compound_ent_value_path(entity *ent, int pos) {
557 assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
558 return ent->val_paths[pos+1];
562 remove_compound_ent_value(entity *ent, entity *value_ent) {
564 assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
565 for (i = 1; i < (ARR_LEN (ent->val_paths)); i++) {
566 compound_graph_path *path = ent->val_paths[i];
567 if (path->nodes[path->len-1] == value_ent) {
568 for(; i < (ARR_LEN (ent->val_paths))-1; i++) {
569 ent->val_paths[i] = ent->val_paths[i+1];
570 ent->values[i] = ent->values[i+1];
572 ARR_SETLEN(entity*, ent->val_paths, ARR_LEN(ent->val_paths) - 1);
573 ARR_SETLEN(ir_node*, ent->values, ARR_LEN(ent->values) - 1);
580 add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
581 assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
582 compound_graph_path *path = new_compound_graph_path(get_entity_owner(ent), 1);
583 path->nodes[0] = member;
584 add_compound_ent_value_w_path(ent, val, path);
587 /* Copies the firm subgraph referenced by val to const_code_irg and adds
588 the node as constant initialization to ent.
589 The subgraph may not contain control flow operations.
591 copy_and_add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
592 ir_graph *rem = current_ir_graph;
594 assert(get_entity_variability(ent) != variability_uninitialized);
595 current_ir_graph = get_const_code_irg();
597 val = copy_const_value(val);
598 add_compound_ent_value(ent, val, member);
599 current_ir_graph = rem;
602 /* Copies the value i of the entity to current_block in current_ir_graph.
604 copy_compound_ent_value(entity *ent, int pos) {
605 assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
606 return copy_const_value(ent->values[pos+1]);
610 get_compound_ent_value_member(entity *ent, int pos) {
611 compound_graph_path *path;
612 assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
613 path = get_compound_ent_value_path(ent, pos);
614 assert(path->len == 1);
616 return get_compound_graph_path_node(path, get_compound_graph_path_length(path)-1);
620 set_compound_ent_value(entity *ent, ir_node *val, entity *member, int pos) {
621 compound_graph_path *path;
622 assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
623 path = get_compound_ent_value_path(ent, pos);
624 set_compound_graph_path_node(path, 0, member);
625 set_compound_ent_value_w_path(ent, val, path, pos);
629 set_array_entity_values(entity *ent, tarval **values, int num_vals) {
631 ir_graph *rem = current_ir_graph;
632 type *arrtp = get_entity_type(ent);
635 assert(is_array_type(arrtp));
636 assert(get_array_n_dimensions(arrtp) == 1);
637 /* One bound is sufficient, the nunmber of constant fields makes the
639 assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
640 assert(get_entity_variability(ent) != variability_uninitialized);
641 current_ir_graph = get_const_code_irg();
643 for (i = 0; i < num_vals; i++) {
644 val = new_Const(get_tarval_mode (values[i]), values[i]);
645 add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
647 current_ir_graph = rem;
651 get_entity_offset (entity *ent) {
656 set_entity_offset (entity *ent, int offset) {
657 ent->offset = offset;
661 add_entity_overwrites (entity *ent, entity *overwritten) {
663 assert(is_class_type(get_entity_owner(ent)));
664 ARR_APP1 (entity *, ent->overwrites, overwritten);
665 ARR_APP1 (entity *, overwritten->overwrittenby, ent);
669 get_entity_n_overwrites (entity *ent) {
671 assert(is_class_type(get_entity_owner(ent)));
672 return (ARR_LEN (ent->overwrites))-1;
676 get_entity_overwrites_index(entity *ent, entity *overwritten) {
678 assert(ent && is_class_type(get_entity_owner(ent)));
679 for (i = 0; i < get_entity_n_overwrites(ent); i++)
680 if (get_entity_overwrites(ent, i) == overwritten)
686 get_entity_overwrites (entity *ent, int pos) {
688 assert(is_class_type(get_entity_owner(ent)));
689 assert(pos < get_entity_n_overwrites(ent));
690 return ent->overwrites[pos+1];
694 set_entity_overwrites (entity *ent, int pos, entity *overwritten) {
696 assert(is_class_type(get_entity_owner(ent)));
697 assert(pos < get_entity_n_overwrites(ent));
698 ent->overwrites[pos+1] = overwritten;
702 remove_entity_overwrites(entity *ent, entity *overwritten) {
704 assert(ent && is_class_type(get_entity_owner(ent)));
705 for (i = 1; i < (ARR_LEN (ent->overwrites)); i++)
706 if (ent->overwrites[i] == overwritten) {
707 for(; i < (ARR_LEN (ent->overwrites))-1; i++)
708 ent->overwrites[i] = ent->overwrites[i+1];
709 ARR_SETLEN(entity*, ent->overwrites, ARR_LEN(ent->overwrites) - 1);
715 add_entity_overwrittenby (entity *ent, entity *overwrites) {
717 assert(is_class_type(get_entity_owner(ent)));
718 add_entity_overwrites(overwrites, ent);
722 get_entity_n_overwrittenby (entity *ent) {
724 assert(is_class_type(get_entity_owner(ent)));
725 return (ARR_LEN (ent->overwrittenby))-1;
729 get_entity_overwrittenby_index(entity *ent, entity *overwrites) {
731 assert(ent && is_class_type(get_entity_owner(ent)));
732 for (i = 0; i < get_entity_n_overwrittenby(ent); i++)
733 if (get_entity_overwrittenby(ent, i) == overwrites)
739 get_entity_overwrittenby (entity *ent, int pos) {
741 assert(is_class_type(get_entity_owner(ent)));
742 assert(pos < get_entity_n_overwrittenby(ent));
743 return ent->overwrittenby[pos+1];
747 set_entity_overwrittenby (entity *ent, int pos, entity *overwrites) {
749 assert(is_class_type(get_entity_owner(ent)));
750 assert(pos < get_entity_n_overwrittenby(ent));
751 ent->overwrittenby[pos+1] = overwrites;
754 void remove_entity_overwrittenby(entity *ent, entity *overwrites) {
756 assert(ent && is_class_type(get_entity_owner(ent)));
757 for (i = 1; i < (ARR_LEN (ent->overwrittenby)); i++)
758 if (ent->overwrittenby[i] == overwrites) {
759 for(; i < (ARR_LEN (ent->overwrittenby))-1; i++)
760 ent->overwrittenby[i] = ent->overwrittenby[i+1];
761 ARR_SETLEN(entity*, ent->overwrittenby, ARR_LEN(ent->overwrittenby) - 1);
766 /* A link to store intermediate information */
768 get_entity_link(entity *ent) {
774 set_entity_link(entity *ent, void *l) {
780 get_entity_irg(entity *ent) {
782 assert (is_method_type(ent->type));
787 set_entity_irg(entity *ent, ir_graph *irg) {
788 assert (ent && ent->type);
789 /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
790 * Methode selbst nicht mehr aufgerufen werden kann, die Entität
791 * aber erhalten bleiben soll. */
793 assert (is_method_type(ent->type));
794 assert (ent->peculiarity == peculiarity_existent);
798 int is_entity (void *thing) {
800 if (get_kind(thing) == k_entity)
806 int is_atomic_entity(entity *ent) {
807 type* t = get_entity_type(ent);
808 return (is_primitive_type(t) || is_pointer_type(t) ||
809 is_enumeration_type(t) || is_method_type(t));
812 int is_compound_entity(entity *ent) {
813 type* t = get_entity_type(ent);
814 return (is_class_type(t) || is_struct_type(t) ||
815 is_array_type(t) || is_union_type(t));
818 /* @@@ not implemnted!!! */
819 bool equal_entity(entity *ent1, entity *ent2) {
820 printf(" calling unimplemented equal entity!!! \n");
825 unsigned long get_entity_visited(entity *ent) {
829 void set_entity_visited(entity *ent, unsigned long num) {
833 /* Sets visited field in entity to entity_visited. */
834 void mark_entity_visited(entity *ent) {
836 ent->visit = type_visited;
840 INLINE bool entity_visited(entity *ent) {
841 return get_entity_visited(ent) >= type_visited;
844 INLINE bool entity_not_visited(entity *ent) {
845 return get_entity_visited(ent) < type_visited;