1 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
4 * Authors: Martin Trapp, Christian Schaefer
18 # include "entity_t.h"
20 # include "typegmod_t.h"
22 /* All this is needed to build the constant node for methods: */
23 # include "irprog_t.h"
27 /*******************************************************************/
29 /*******************************************************************/
36 /*******************************************************************/
38 /*******************************************************************/
40 /* redeclared to declare INLINE. */
41 INLINE entity *get_entity_overwrites (entity *ent, int pos);
42 INLINE entity *get_entity_overwrittenby (entity *ent, int pos);
43 INLINE type *get_entity_owner (entity *ent);
45 INLINE void insert_entity_in_owner (entity *ent) {
46 type *owner = ent->owner;
47 switch (get_type_tpop_code(owner)) {
49 add_class_member (owner, ent);
52 add_struct_member (owner, ent);
55 add_union_member (owner, ent);
58 set_array_element_entity(owner, ent);
65 new_entity (type *owner, ident *name, type *type)
70 assert(!id_contains_char(name, ' ') && "entity name should not contain spaces");
72 res = (entity *) xmalloc (sizeof (entity));
74 assert_legal_owner_of_ent(owner);
78 if (get_type_tpop(type) == type_method)
79 res->allocation = static_allocated;
81 res->allocation = automatic_allocated;
82 res->visibility = local;
84 if (is_method_type(type)) {
85 res->variability = constant;
86 rem = current_ir_graph;
87 current_ir_graph = get_const_code_irg();
88 res->value = new_Const(mode_P_mach, new_tarval_from_entity(res, mode_P_mach));
89 current_ir_graph = rem;
91 res->variability = uninitialized;
95 res->peculiarity = existent;
96 res->volatility = non_volatile;
98 res->overwrites = NEW_ARR_F(entity *, 1);
99 res->overwrittenby = NEW_ARR_F(entity *, 1);
104 res->nr = get_irp_new_node_nr();
109 /* Remember entity in it's owner. */
110 insert_entity_in_owner (res);
114 new_d_entity (type *owner, ident *name, type *type, dbg_info *db) {
115 entity *res = new_entity(owner, name, type);
116 set_entity_dbg_info(res, db);
119 INLINE void free_entity_attrs(entity *ent) {
121 if (get_type_tpop(get_entity_owner(ent)) == type_class) {
122 DEL_ARR_F(ent->overwrites);
123 DEL_ARR_F(ent->overwrittenby);
128 copy_entity_own (entity *old, type *new_owner) {
131 assert_legal_owner_of_ent(new_owner);
132 if (old->owner == new_owner) return old;
133 new = (entity *) xmalloc (sizeof (entity));
134 memcpy (new, old, sizeof (entity));
135 new->owner = new_owner;
137 if ((get_type_tpop(get_entity_owner(old)) == type_class) &&
138 (get_type_tpop(new_owner) == type_class)) {
139 new->overwrites = DUP_ARR_F(entity *, old->overwrites);
140 new->overwrittenby = DUP_ARR_F(entity *, old->overwrittenby);
141 } else if ((get_type_tpop(get_entity_owner(old)) != type_class) &&
142 (get_type_tpop(new_owner) == type_class)) {
143 new->overwrites = NEW_ARR_F(entity *, 1);
144 new->overwrittenby = NEW_ARR_F(entity *, 1);
147 if (is_class_type(new_owner)) {
148 new->overwrites = NEW_ARR_F(entity *, 1);
149 new->overwrittenby = NEW_ARR_F(entity *, 1);
152 new->nr = get_irp_new_node_nr();
155 insert_entity_in_owner (new);
161 copy_entity_name (entity *old, ident *new_name) {
164 if (old->name == new_name) return old;
165 new = (entity *) xmalloc (sizeof (entity));
166 memcpy (new, old, sizeof (entity));
167 new->name = new_name;
169 if (is_class_type(new->owner)) {
170 new->overwrites = DUP_ARR_F(entity *, old->overwrites);
171 new->overwrittenby = DUP_ARR_F(entity *, old->overwrittenby);
174 new->nr = get_irp_new_node_nr();
177 insert_entity_in_owner (new);
184 free_entity (entity *ent) {
185 free_tarval_entity(ent);
186 free_entity_attrs(ent);
190 /* Outputs a unique number for this node */
192 get_entity_nr(entity *ent) {
202 get_entity_name (entity *ent) {
204 return id_to_str(get_entity_ident(ent));
208 get_entity_ident (entity *ent) {
214 void set_entitye_ld_name (entity *, char *ld_name);
215 void set_entity_ld_ident (entity *, ident *ld_ident);
219 get_entity_owner (entity *ent) {
220 return ent->owner = skip_tid(ent->owner);
224 set_entity_owner (entity *ent, type *owner) {
225 assert_legal_owner_of_ent(owner);
229 INLINE void /* should this go into type.c? */
230 assert_legal_owner_of_ent(type *owner) {
231 assert (get_type_tpop_code(owner) == tpo_class ||
232 get_type_tpop_code(owner) == tpo_union ||
233 get_type_tpop_code(owner) == tpo_struct ||
234 get_type_tpop_code(owner) == tpo_array); /* Yes, array has an entity
235 -- to select fields! */
239 get_entity_ld_ident (entity *ent)
241 if (ent->ld_name == NULL)
242 ent->ld_name = mangle_entity (ent);
247 set_entity_ld_ident (entity *ent, ident *ld_ident) {
248 ent->ld_name = ld_ident;
252 get_entity_ld_name (entity *ent) {
253 return id_to_str(get_entity_ld_ident(ent));
257 char *get_entity_ld_name (entity *);
258 void set_entity_ld_name (entity *, char *ld_name);
262 get_entity_type (entity *ent) {
263 return ent->type = skip_tid(ent->type);
267 set_entity_type (entity *ent, type *type) {
272 INLINE ent_allocation
273 get_entity_allocation (entity *ent) {
274 return ent->allocation;
278 set_entity_allocation (entity *ent, ent_allocation al) {
279 ent->allocation = al;
282 /* return the name of the visibility */
283 const char *get_allocation_name(ent_allocation all)
285 #define X(a) case a: return #a
287 X(automatic_allocated);
288 X(parameter_allocated);
289 X(dynamic_allocated);
291 default: return "BAD VALUE";
297 INLINE ent_visibility
298 get_entity_visibility (entity *ent) {
299 return ent->visibility;
303 set_entity_visibility (entity *ent, ent_visibility vis) {
305 assert((ent->allocation == static_allocated) ||
306 (ent->allocation == automatic_allocated));
307 /* @@@ Test that the owner type is not local, but how??
308 && get_class_visibility(get_entity_owner(ent)) != local));*/
309 ent->visibility = vis;
312 /* return the name of the visibility */
313 const char *get_visibility_name(ent_visibility vis)
315 #define X(a) case a: return #a
319 X(external_allocated);
320 default: return "BAD VALUE";
325 INLINE ent_variability
326 get_entity_variability (entity *ent) {
327 return ent->variability;
331 set_entity_variability (entity *ent, ent_variability var){
332 if (var == part_constant)
333 assert(is_class_type(ent->type) || is_struct_type(ent->type));
334 if ((is_compound_type(ent->type)) &&
335 (ent->variability == uninitialized) && (var != uninitialized)) {
336 /* Allocate datastructures for constant values */
337 ent->values = NEW_ARR_F(ir_node *, 1);
338 ent->val_paths = NEW_ARR_F(compound_graph_path *, 1);
340 if ((is_compound_type(ent->type)) &&
341 (var == uninitialized) && (ent->variability != uninitialized)) {
342 /* Free datastructures for constant values */
343 DEL_ARR_F(ent->values);
344 DEL_ARR_F(ent->val_paths);
346 ent->variability = var;
349 /* return the name of the variablity */
350 const char *get_variability_name(ent_variability var)
352 #define X(a) case a: return #a
358 default: return "BAD VALUE";
363 INLINE ent_volatility
364 get_entity_volatility (entity *ent) {
366 return ent->volatility;
370 set_entity_volatility (entity *ent, ent_volatility vol) {
372 ent->volatility = vol;
375 /* return the name of the volatility */
376 const char *get_volatility_name(ent_volatility var)
378 #define X(a) case a: return #a
382 default: return "BAD VALUE";
388 get_entity_peculiarity (entity *ent) {
390 return ent->peculiarity;
394 set_entity_peculiarity (entity *ent, peculiarity pec) {
396 /* @@@ why peculiarity only for methods? */
397 assert (is_method_type(ent->type));
398 ent->peculiarity = pec;
401 /* return the name of the peculiarity */
402 const char *get_peculiarity_name(peculiarity var)
404 #define X(a) case a: return #a
409 default: return "BAD VALUE";
414 /* Set has no effect for existent entities of type method. */
416 get_atomic_ent_value(entity *ent) {
417 assert(ent); assert(is_atomic_entity(ent));
418 assert((ent->variability != uninitialized));
423 set_atomic_ent_value(entity *ent, ir_node *val) {
424 assert(ent && is_atomic_entity(ent) && (ent->variability != uninitialized));
425 if ((is_method_type(ent->type)) && (ent->peculiarity==existent)) return;
430 ir_node *copy_const_value(ir_node *n) {
435 switch(get_irn_opcode(n)) {
437 nn = new_Const(m, get_Const_tarval(n)); break;
439 nn = new_SymConst(get_SymConst_type_or_id(n), get_SymConst_kind(n)); break;
441 nn = new_Add(copy_const_value(get_Add_left(n)), copy_const_value(get_Add_right(n)), m); break;
443 assert(0 && "opdope invalid or not implemented");
450 compound_graph_path *
451 new_compound_graph_path(type *tp, int length) {
452 compound_graph_path *res;
453 assert(is_type(tp) && is_compound_type(tp));
456 res = (compound_graph_path *) malloc (sizeof(compound_graph_path) + (length-1) * sizeof(entity *));
457 res->kind = k_ir_compound_graph_path;
460 memset(res->nodes, 0, sizeof(entity *) * length);
465 free_compound_graph_path (compound_graph_path *gr) {
466 assert(gr && is_compound_graph_path(gr));
471 is_compound_graph_path(void *thing) {
472 return (get_kind(thing) == k_ir_compound_graph_path);
475 /* checks whether nodes 0..pos are correct (all lie on a path.) */
476 /* @@@ not implemented */
477 INLINE int is_proper_compound_graph_path(compound_graph_path *gr, int pos) {
479 type *owner = gr->tp;
480 for (i = 0; i <= pos; i++) {
481 node = get_compound_graph_path_node(gr, i);
482 if (get_entity_owner(node) != owner) return false;
483 type = get_entity_type(node);
485 if (pos == get_compound_graph_path_length(gr) -1)
486 if (!is_atomic_type(type)) return false;
491 get_compound_graph_path_length(compound_graph_path *gr) {
492 assert(gr && is_compound_graph_path(gr));
497 get_compound_graph_path_node(compound_graph_path *gr, int pos) {
498 assert(gr && is_compound_graph_path(gr));
499 assert(pos >= 0 && pos < gr->len);
500 return gr->nodes[pos];
504 set_compound_graph_path_node(compound_graph_path *gr, int pos, entity *node) {
505 assert(gr && is_compound_graph_path(gr));
506 assert(pos >= 0 && pos < gr->len);
507 assert(is_entity(node));
508 gr->nodes[pos] = node;
509 assert(is_proper_compound_graph_path(gr, pos));
512 /* A value of a compound entity is a pair of value and the corresponding path to a member of
515 add_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path) {
516 assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
517 ARR_APP1 (ir_node *, ent->values, val);
518 ARR_APP1 (compound_graph_path *, ent->val_paths, path);
522 set_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path, int pos) {
523 assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
524 ent->values[pos+1] = val;
525 ent->val_paths[pos+1] = path;
529 get_compound_ent_n_values(entity *ent) {
530 assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
531 return (ARR_LEN (ent->values))-1;
535 get_compound_ent_value(entity *ent, int pos) {
536 assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
537 return ent->values[pos+1];
540 INLINE compound_graph_path *
541 get_compound_ent_value_path(entity *ent, int pos) {
542 assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
543 return ent->val_paths[pos+1];
547 remove_compound_ent_value(entity *ent, entity *value_ent) {
549 assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
550 for (i = 1; i < (ARR_LEN (ent->val_paths)); i++) {
551 compound_graph_path *path = ent->val_paths[i];
552 if (path->nodes[path->len] == value_ent) {
553 for(; i < (ARR_LEN (ent->val_paths))-1; i++) {
554 ent->val_paths[i] = ent->val_paths[i+1];
555 ent->values[i] = ent->values[i+1];
557 ARR_SETLEN(entity*, ent->val_paths, ARR_LEN(ent->val_paths) - 1);
558 ARR_SETLEN(ir_node*, ent->values, ARR_LEN(ent->values) - 1);
565 add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
566 assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
567 compound_graph_path *path = new_compound_graph_path(get_entity_owner(ent), 1);
568 path->nodes[0] = member;
569 add_compound_ent_value_w_path(ent, val, path);
572 /* Copies the firm subgraph referenced by val to const_code_irg and adds
573 the node as constant initialization to ent.
574 The subgraph may not contain control flow operations.
576 copy_and_add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
577 ir_graph *rem = current_ir_graph;
579 assert(get_entity_variability(ent) != uninitialized);
580 current_ir_graph = get_const_code_irg();
582 val = copy_const_value(val);
583 add_compound_ent_value(ent, val, member);
584 current_ir_graph = rem;
587 /* Copies the value i of the entity to current_block in current_ir_graph.
589 copy_compound_ent_value(entity *ent, int pos) {
590 assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
591 return copy_const_value(ent->values[pos+1]);
595 get_compound_ent_value_member(entity *ent, int pos) {
596 compound_graph_path *path;
597 assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
598 path = get_compound_ent_value_path(ent, pos);
599 assert(path->len == 1);
601 return get_compound_graph_path_node(path, 0);
605 set_compound_ent_value(entity *ent, ir_node *val, entity *member, int pos) {
606 compound_graph_path *path;
607 assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
608 path = new_compound_graph_path(get_entity_owner(ent), 1);
609 set_compound_graph_path_node(path, 0, member);
610 set_compound_ent_value_w_path(ent, val, path, pos);
614 set_array_entity_values(entity *ent, tarval **values, int num_vals) {
616 ir_graph *rem = current_ir_graph;
617 type *arrtp = get_entity_type(ent);
620 assert(is_array_type(arrtp));
621 assert(get_array_n_dimensions(arrtp) == 1);
622 /* One bound is sufficient, the nunmber of constant fields makes the
624 assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
625 assert(get_entity_variability(ent) != uninitialized);
626 current_ir_graph = get_const_code_irg();
628 for (i = 0; i < num_vals; i++) {
629 val = new_Const(get_tarval_mode (values[i]), values[i]);
630 add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
632 current_ir_graph = rem;
636 get_entity_offset (entity *ent) {
641 set_entity_offset (entity *ent, int offset) {
642 ent->offset = offset;
646 add_entity_overwrites (entity *ent, entity *overwritten) {
648 assert(is_class_type(get_entity_owner(ent)));
649 ARR_APP1 (entity *, ent->overwrites, overwritten);
650 ARR_APP1 (entity *, overwritten->overwrittenby, ent);
654 get_entity_n_overwrites (entity *ent) {
656 assert(is_class_type(get_entity_owner(ent)));
657 return (ARR_LEN (ent->overwrites))-1;
661 get_entity_overwrites_index(entity *ent, entity *overwritten) {
663 assert(ent && is_class_type(get_entity_owner(ent)));
664 for (i = 0; i < get_entity_n_overwrites(ent); i++)
665 if (get_entity_overwrites(ent, i) == overwritten)
671 get_entity_overwrites (entity *ent, int pos) {
673 assert(is_class_type(get_entity_owner(ent)));
674 assert(pos < get_entity_n_overwrites(ent));
675 return ent->overwrites[pos+1];
679 set_entity_overwrites (entity *ent, int pos, entity *overwritten) {
681 assert(is_class_type(get_entity_owner(ent)));
682 assert(pos < get_entity_n_overwrites(ent));
683 ent->overwrites[pos+1] = overwritten;
687 remove_entity_overwrites(entity *ent, entity *overwritten) {
689 assert(ent && is_class_type(get_entity_owner(ent)));
690 for (i = 1; i < (ARR_LEN (ent->overwrites)); i++)
691 if (ent->overwrites[i] == overwritten) {
692 for(; i < (ARR_LEN (ent->overwrites))-1; i++)
693 ent->overwrites[i] = ent->overwrites[i+1];
694 ARR_SETLEN(entity*, ent->overwrites, ARR_LEN(ent->overwrites) - 1);
700 add_entity_overwrittenby (entity *ent, entity *overwrites) {
702 assert(is_class_type(get_entity_owner(ent)));
703 add_entity_overwrites(overwrites, ent);
707 get_entity_n_overwrittenby (entity *ent) {
709 assert(is_class_type(get_entity_owner(ent)));
710 return (ARR_LEN (ent->overwrittenby))-1;
714 get_entity_overwrittenby_index(entity *ent, entity *overwrites) {
716 assert(ent && is_class_type(get_entity_owner(ent)));
717 for (i = 0; i < get_entity_n_overwrittenby(ent); i++)
718 if (get_entity_overwrittenby(ent, i) == overwrites)
724 get_entity_overwrittenby (entity *ent, int pos) {
726 assert(is_class_type(get_entity_owner(ent)));
727 assert(pos < get_entity_n_overwrittenby(ent));
728 return ent->overwrittenby[pos+1];
732 set_entity_overwrittenby (entity *ent, int pos, entity *overwrites) {
734 assert(is_class_type(get_entity_owner(ent)));
735 assert(pos < get_entity_n_overwrittenby(ent));
736 ent->overwrittenby[pos+1] = overwrites;
739 void remove_entity_overwrittenby(entity *ent, entity *overwrites) {
741 assert(ent && is_class_type(get_entity_owner(ent)));
742 for (i = 1; i < (ARR_LEN (ent->overwrittenby)); i++)
743 if (ent->overwrittenby[i] == overwrites) {
744 for(; i < (ARR_LEN (ent->overwrittenby))-1; i++)
745 ent->overwrittenby[i] = ent->overwrittenby[i+1];
746 ARR_SETLEN(entity*, ent->overwrittenby, ARR_LEN(ent->overwrittenby) - 1);
751 /* A link to store intermediate information */
753 get_entity_link(entity *ent) {
759 set_entity_link(entity *ent, void *l) {
765 get_entity_irg(entity *ent) {
767 assert (is_method_type(ent->type));
772 set_entity_irg(entity *ent, ir_graph *irg) {
773 assert (ent && ent->type);
774 /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
775 * Methode selbst nicht mehr aufgerufen werden kann, die Entität
776 * aber erhalten bleiben soll. */
778 assert (is_method_type(ent->type));
779 assert (ent->peculiarity == existent);
783 int is_entity (void *thing) {
785 if (get_kind(thing) == k_entity)
791 int is_atomic_entity(entity *ent) {
792 type* t = get_entity_type(ent);
793 return (is_primitive_type(t) || is_pointer_type(t) ||
794 is_enumeration_type(t) || is_method_type(t));
797 int is_compound_entity(entity *ent) {
798 type* t = get_entity_type(ent);
799 return (is_class_type(t) || is_struct_type(t) ||
800 is_array_type(t) || is_union_type(t));
803 /* @@@ not implemnted!!! */
804 bool equal_entity(entity *ent1, entity *ent2) {
805 printf(" calling unimplemented equal entity!!! \n");
810 unsigned long get_entity_visited(entity *ent) {
814 void set_entity_visited(entity *ent, unsigned long num) {
818 /* Sets visited field in entity to entity_visited. */
819 void mark_entity_visited(entity *ent) {
821 ent->visit = type_visited;
825 INLINE bool entity_visited(entity *ent) {
826 return get_entity_visited(ent) >= type_visited;
828 INLINE bool entity_not_visited(entity *ent) {
829 return get_entity_visited(ent) < type_visited;