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 nn = new_Unknown(); break;
446 assert(0 && "opdope invalid or not implemented");
453 compound_graph_path *
454 new_compound_graph_path(type *tp, int length) {
455 compound_graph_path *res;
456 assert(is_type(tp) && is_compound_type(tp));
459 res = (compound_graph_path *) malloc (sizeof(compound_graph_path) + (length-1) * sizeof(entity *));
460 res->kind = k_ir_compound_graph_path;
463 memset(res->nodes, 0, sizeof(entity *) * length);
468 free_compound_graph_path (compound_graph_path *gr) {
469 assert(gr && is_compound_graph_path(gr));
474 is_compound_graph_path(void *thing) {
475 return (get_kind(thing) == k_ir_compound_graph_path);
478 /* checks whether nodes 0..pos are correct (all lie on a path.) */
479 /* @@@ not implemented */
480 INLINE int is_proper_compound_graph_path(compound_graph_path *gr, int pos) {
483 type *owner = gr->tp;
484 for (i = 0; i <= pos; i++) {
485 node = get_compound_graph_path_node(gr, i);
486 if (get_entity_owner(node) != owner) return false;
487 owner = get_entity_type(node);
489 if (pos == get_compound_graph_path_length(gr) -1)
490 if (!is_atomic_type(owner)) return false;
495 get_compound_graph_path_length(compound_graph_path *gr) {
496 assert(gr && is_compound_graph_path(gr));
501 get_compound_graph_path_node(compound_graph_path *gr, int pos) {
502 assert(gr && is_compound_graph_path(gr));
503 assert(pos >= 0 && pos < gr->len);
504 return gr->nodes[pos];
508 set_compound_graph_path_node(compound_graph_path *gr, int pos, entity *node) {
509 assert(gr && is_compound_graph_path(gr));
510 assert(pos >= 0 && pos < gr->len);
511 assert(is_entity(node));
512 gr->nodes[pos] = node;
513 assert(is_proper_compound_graph_path(gr, pos));
516 /* A value of a compound entity is a pair of value and the corresponding path to a member of
519 add_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path) {
520 assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
521 ARR_APP1 (ir_node *, ent->values, val);
522 ARR_APP1 (compound_graph_path *, ent->val_paths, path);
526 set_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path, int pos) {
527 assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
528 ent->values[pos+1] = val;
529 ent->val_paths[pos+1] = path;
533 get_compound_ent_n_values(entity *ent) {
534 assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
535 return (ARR_LEN (ent->values))-1;
539 get_compound_ent_value(entity *ent, int pos) {
540 assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
541 return ent->values[pos+1];
544 INLINE compound_graph_path *
545 get_compound_ent_value_path(entity *ent, int pos) {
546 assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
547 return ent->val_paths[pos+1];
551 remove_compound_ent_value(entity *ent, entity *value_ent) {
553 assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
554 for (i = 1; i < (ARR_LEN (ent->val_paths)); i++) {
555 compound_graph_path *path = ent->val_paths[i];
556 if (path->nodes[path->len] == value_ent) {
557 for(; i < (ARR_LEN (ent->val_paths))-1; i++) {
558 ent->val_paths[i] = ent->val_paths[i+1];
559 ent->values[i] = ent->values[i+1];
561 ARR_SETLEN(entity*, ent->val_paths, ARR_LEN(ent->val_paths) - 1);
562 ARR_SETLEN(ir_node*, ent->values, ARR_LEN(ent->values) - 1);
569 add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
570 assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
571 compound_graph_path *path = new_compound_graph_path(get_entity_owner(ent), 1);
572 path->nodes[0] = member;
573 add_compound_ent_value_w_path(ent, val, path);
576 /* Copies the firm subgraph referenced by val to const_code_irg and adds
577 the node as constant initialization to ent.
578 The subgraph may not contain control flow operations.
580 copy_and_add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
581 ir_graph *rem = current_ir_graph;
583 assert(get_entity_variability(ent) != uninitialized);
584 current_ir_graph = get_const_code_irg();
586 val = copy_const_value(val);
587 add_compound_ent_value(ent, val, member);
588 current_ir_graph = rem;
591 /* Copies the value i of the entity to current_block in current_ir_graph.
593 copy_compound_ent_value(entity *ent, int pos) {
594 assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
595 return copy_const_value(ent->values[pos+1]);
599 get_compound_ent_value_member(entity *ent, int pos) {
600 compound_graph_path *path;
601 assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
602 path = get_compound_ent_value_path(ent, pos);
603 assert(path->len == 1);
605 return get_compound_graph_path_node(path, 0);
609 set_compound_ent_value(entity *ent, ir_node *val, entity *member, int pos) {
610 compound_graph_path *path;
611 assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
612 path = new_compound_graph_path(get_entity_owner(ent), 1);
613 set_compound_graph_path_node(path, 0, member);
614 set_compound_ent_value_w_path(ent, val, path, pos);
618 set_array_entity_values(entity *ent, tarval **values, int num_vals) {
620 ir_graph *rem = current_ir_graph;
621 type *arrtp = get_entity_type(ent);
624 assert(is_array_type(arrtp));
625 assert(get_array_n_dimensions(arrtp) == 1);
626 /* One bound is sufficient, the nunmber of constant fields makes the
628 assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
629 assert(get_entity_variability(ent) != uninitialized);
630 current_ir_graph = get_const_code_irg();
632 for (i = 0; i < num_vals; i++) {
633 val = new_Const(get_tarval_mode (values[i]), values[i]);
634 add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
636 current_ir_graph = rem;
640 get_entity_offset (entity *ent) {
645 set_entity_offset (entity *ent, int offset) {
646 ent->offset = offset;
650 add_entity_overwrites (entity *ent, entity *overwritten) {
652 assert(is_class_type(get_entity_owner(ent)));
653 ARR_APP1 (entity *, ent->overwrites, overwritten);
654 ARR_APP1 (entity *, overwritten->overwrittenby, ent);
658 get_entity_n_overwrites (entity *ent) {
660 assert(is_class_type(get_entity_owner(ent)));
661 return (ARR_LEN (ent->overwrites))-1;
665 get_entity_overwrites_index(entity *ent, entity *overwritten) {
667 assert(ent && is_class_type(get_entity_owner(ent)));
668 for (i = 0; i < get_entity_n_overwrites(ent); i++)
669 if (get_entity_overwrites(ent, i) == overwritten)
675 get_entity_overwrites (entity *ent, int pos) {
677 assert(is_class_type(get_entity_owner(ent)));
678 assert(pos < get_entity_n_overwrites(ent));
679 return ent->overwrites[pos+1];
683 set_entity_overwrites (entity *ent, int pos, entity *overwritten) {
685 assert(is_class_type(get_entity_owner(ent)));
686 assert(pos < get_entity_n_overwrites(ent));
687 ent->overwrites[pos+1] = overwritten;
691 remove_entity_overwrites(entity *ent, entity *overwritten) {
693 assert(ent && is_class_type(get_entity_owner(ent)));
694 for (i = 1; i < (ARR_LEN (ent->overwrites)); i++)
695 if (ent->overwrites[i] == overwritten) {
696 for(; i < (ARR_LEN (ent->overwrites))-1; i++)
697 ent->overwrites[i] = ent->overwrites[i+1];
698 ARR_SETLEN(entity*, ent->overwrites, ARR_LEN(ent->overwrites) - 1);
704 add_entity_overwrittenby (entity *ent, entity *overwrites) {
706 assert(is_class_type(get_entity_owner(ent)));
707 add_entity_overwrites(overwrites, ent);
711 get_entity_n_overwrittenby (entity *ent) {
713 assert(is_class_type(get_entity_owner(ent)));
714 return (ARR_LEN (ent->overwrittenby))-1;
718 get_entity_overwrittenby_index(entity *ent, entity *overwrites) {
720 assert(ent && is_class_type(get_entity_owner(ent)));
721 for (i = 0; i < get_entity_n_overwrittenby(ent); i++)
722 if (get_entity_overwrittenby(ent, i) == overwrites)
728 get_entity_overwrittenby (entity *ent, int pos) {
730 assert(is_class_type(get_entity_owner(ent)));
731 assert(pos < get_entity_n_overwrittenby(ent));
732 return ent->overwrittenby[pos+1];
736 set_entity_overwrittenby (entity *ent, int pos, entity *overwrites) {
738 assert(is_class_type(get_entity_owner(ent)));
739 assert(pos < get_entity_n_overwrittenby(ent));
740 ent->overwrittenby[pos+1] = overwrites;
743 void remove_entity_overwrittenby(entity *ent, entity *overwrites) {
745 assert(ent && is_class_type(get_entity_owner(ent)));
746 for (i = 1; i < (ARR_LEN (ent->overwrittenby)); i++)
747 if (ent->overwrittenby[i] == overwrites) {
748 for(; i < (ARR_LEN (ent->overwrittenby))-1; i++)
749 ent->overwrittenby[i] = ent->overwrittenby[i+1];
750 ARR_SETLEN(entity*, ent->overwrittenby, ARR_LEN(ent->overwrittenby) - 1);
755 /* A link to store intermediate information */
757 get_entity_link(entity *ent) {
763 set_entity_link(entity *ent, void *l) {
769 get_entity_irg(entity *ent) {
771 assert (is_method_type(ent->type));
776 set_entity_irg(entity *ent, ir_graph *irg) {
777 assert (ent && ent->type);
778 /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
779 * Methode selbst nicht mehr aufgerufen werden kann, die Entität
780 * aber erhalten bleiben soll. */
782 assert (is_method_type(ent->type));
783 assert (ent->peculiarity == existent);
787 int is_entity (void *thing) {
789 if (get_kind(thing) == k_entity)
795 int is_atomic_entity(entity *ent) {
796 type* t = get_entity_type(ent);
797 return (is_primitive_type(t) || is_pointer_type(t) ||
798 is_enumeration_type(t) || is_method_type(t));
801 int is_compound_entity(entity *ent) {
802 type* t = get_entity_type(ent);
803 return (is_class_type(t) || is_struct_type(t) ||
804 is_array_type(t) || is_union_type(t));
807 /* @@@ not implemnted!!! */
808 bool equal_entity(entity *ent1, entity *ent2) {
809 printf(" calling unimplemented equal entity!!! \n");
814 unsigned long get_entity_visited(entity *ent) {
818 void set_entity_visited(entity *ent, unsigned long num) {
822 /* Sets visited field in entity to entity_visited. */
823 void mark_entity_visited(entity *ent) {
825 ent->visit = type_visited;
829 INLINE bool entity_visited(entity *ent) {
830 return get_entity_visited(ent) >= type_visited;
832 INLINE bool entity_not_visited(entity *ent) {
833 return get_entity_visited(ent) < type_visited;