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.
13 #include "firm_common_t.h"
19 # include "entity_t.h"
21 # include "typegmod.h"
24 /* All this is needed to build the constant node for methods: */
25 # include "irprog_t.h"
30 # include "irdump.h" /* for output if errors occur. */
33 # include "callgraph.h" /* for dumping debug output */
35 /*******************************************************************/
37 /*******************************************************************/
39 entity *unknown_entity = NULL; entity *get_unknown_entity(void) { return unknown_entity; }
40 #define UNKNOWN_ENTITY_NAME "unknown_entity"
42 static INLINE entity *
43 new_rd_entity (dbg_info *db, type *owner, ident *name, type *type);
48 assert(unknown_type && "Call init_type before init_entity!");
49 assert(!unknown_entity && "Call init_entity only once!");
50 unknown_entity = new_rd_entity(NULL, unknown_type, new_id_from_str(UNKNOWN_ENTITY_NAME), unknown_type);
51 set_entity_visibility(unknown_entity, visibility_external_allocated);
52 set_entity_ld_ident(unknown_entity, get_entity_ident(unknown_entity));
56 /*-----------------------------------------------------------------*/
58 /*-----------------------------------------------------------------*/
60 static INLINE void insert_entity_in_owner (entity *ent) {
61 type *owner = ent->owner;
62 switch (get_type_tpop_code(owner)) {
64 add_class_member (owner, ent);
67 add_struct_member (owner, ent);
70 add_union_member (owner, ent);
73 set_array_element_entity(owner, ent);
79 static INLINE entity *
80 new_rd_entity (dbg_info *db, type *owner, ident *name, type *type)
85 assert(!id_contains_char(name, ' ') && "entity name should not contain spaces");
87 res = (entity *) xmalloc (sizeof (entity));
88 memset(res, 0, sizeof(res));
94 if (get_type_tpop(type) == type_method)
95 res->allocation = allocation_static;
97 res->allocation = allocation_automatic;
99 res->visibility = visibility_local;
101 if (is_method_type(type)) {
104 res->variability = variability_constant;
105 rem = current_ir_graph;
106 current_ir_graph = get_const_code_irg();
107 res->value = new_SymConst(sym, symconst_addr_ent);
108 current_ir_graph = rem;
110 res->variability = variability_uninitialized;
113 res->val_paths = NULL;
115 res->peculiarity = peculiarity_existent;
116 res->volatility = volatility_non_volatile;
117 res->stickyness = stickyness_unsticky;
119 if (is_class_type(owner)) {
120 res->overwrites = NEW_ARR_F(entity *, 0);
121 res->overwrittenby = NEW_ARR_F(entity *, 0);
123 res->overwrites = NULL;
124 res->overwrittenby = NULL;
128 //res->accesses = NULL;
131 res->nr = get_irp_new_node_nr();
132 res->c_name = (char *)get_id_str (name);
133 #endif /* DEBUG_libfirm */
136 set_entity_dbg_info(res, db);
142 new_d_entity (type *owner, ident *name, type *type, dbg_info *db) {
143 assert_legal_owner_of_ent(owner);
144 entity *res = new_rd_entity(db, owner, name, type);
145 /* Remember entity in it's owner. */
146 insert_entity_in_owner (res);
152 new_entity (type *owner, ident *name, type *type) {
153 return new_d_entity(owner, name, type, NULL);
159 static void free_entity_attrs(entity *ent) {
161 if (get_type_tpop(get_entity_owner(ent)) == type_class) {
162 DEL_ARR_F(ent->overwrites); ent->overwrites = NULL;
163 DEL_ARR_F(ent->overwrittenby); ent->overwrittenby = NULL;
165 assert(ent->overwrites == NULL);
166 assert(ent->overwrittenby == NULL);
168 /* if (ent->values) DEL_ARR_F(ent->values); *//* @@@ warum nich? */
169 if (ent->val_paths) {
170 if (is_compound_entity(ent))
171 for (i = 0; i < get_compound_ent_n_values(ent); i++)
172 if (ent->val_paths[i]) ;
173 /* free_compound_graph_path(ent->val_paths[i]) ; * @@@ warum nich? */
174 /* Geht nich: wird mehrfach verwendet!!! ==> mehrfach frei gegeben. */
175 /* DEL_ARR_F(ent->val_paths); */
177 ent->val_paths = NULL;
182 copy_entity_own (entity *old, type *new_owner) {
184 assert(old && old->kind == k_entity);
185 assert_legal_owner_of_ent(new_owner);
187 if (old->owner == new_owner) return old;
188 new = (entity *) xmalloc (sizeof (entity));
189 memcpy (new, old, sizeof (entity));
190 new->owner = new_owner;
191 if (is_class_type(new_owner)) {
192 new->overwrites = NEW_ARR_F(entity *, 0);
193 new->overwrittenby = NEW_ARR_F(entity *, 0);
196 new->nr = get_irp_new_node_nr();
199 insert_entity_in_owner (new);
205 copy_entity_name (entity *old, ident *new_name) {
207 assert(old && old->kind == k_entity);
209 if (old->name == new_name) return old;
210 new = (entity *) xmalloc (sizeof (entity));
211 memcpy (new, old, sizeof (entity));
212 new->name = new_name;
214 if (is_class_type(new->owner)) {
215 new->overwrites = DUP_ARR_F(entity *, old->overwrites);
216 new->overwrittenby = DUP_ARR_F(entity *, old->overwrittenby);
219 new->nr = get_irp_new_node_nr();
220 new->c_name = (char *)get_id_str (new->name);
223 insert_entity_in_owner (new);
230 free_entity (entity *ent) {
231 assert(ent && ent->kind == k_entity);
232 free_entity_attrs(ent);
237 /* Outputs a unique number for this node */
239 get_entity_nr(entity *ent) {
240 assert(ent && ent->kind == k_entity);
249 (get_entity_name)(const entity *ent) {
250 return __get_entity_name(ent);
254 (get_entity_ident)(const entity *ent) {
255 return get_entity_ident(ent);
259 void set_entitye_ld_name (entity *, char *ld_name);
260 void set_entity_ld_ident (entity *, ident *ld_ident);
264 (get_entity_owner)(entity *ent) {
265 return __get_entity_owner(ent);
269 set_entity_owner (entity *ent, type *owner) {
270 assert(ent && ent->kind == k_entity);
271 assert_legal_owner_of_ent(owner);
275 void /* should this go into type.c? */
276 assert_legal_owner_of_ent(type *owner) {
277 assert(get_type_tpop_code(owner) == tpo_class ||
278 get_type_tpop_code(owner) == tpo_union ||
279 get_type_tpop_code(owner) == tpo_struct ||
280 get_type_tpop_code(owner) == tpo_array); /* Yes, array has an entity
281 -- to select fields! */
285 (get_entity_ld_ident)(entity *ent) {
286 return __get_entity_ld_ident(ent);
290 (set_entity_ld_ident)(entity *ent, ident *ld_ident) {
291 __set_entity_ld_ident(ent, ld_ident);
295 (get_entity_ld_name)(entity *ent) {
296 return __get_entity_ld_name(ent);
300 (get_entity_type)(entity *ent) {
301 return __get_entity_type(ent);
305 (set_entity_type)(entity *ent, type *type) {
306 __set_entity_type(ent, type);
310 (get_entity_allocation)(const entity *ent) {
311 return __get_entity_allocation(ent);
315 (set_entity_allocation)(entity *ent, ent_allocation al) {
316 __set_entity_allocation(ent, al);
319 /* return the name of the visibility */
320 const char *get_allocation_name(ent_allocation all)
322 #define X(a) case a: return #a
324 X(allocation_automatic);
325 X(allocation_parameter);
326 X(allocation_dynamic);
327 X(allocation_static);
328 default: return "BAD VALUE";
335 (get_entity_visibility)(const entity *ent) {
336 return __get_entity_visibility(ent);
340 set_entity_visibility (entity *ent, ent_visibility vis) {
341 assert(ent && ent->kind == k_entity);
342 if (vis != visibility_local)
343 assert((ent->allocation == allocation_static) ||
344 (ent->allocation == allocation_automatic));
345 /* @@@ Test that the owner type is not local, but how??
346 && get_class_visibility(get_entity_owner(ent)) != local));*/
347 ent->visibility = vis;
350 /* return the name of the visibility */
351 const char *get_visibility_name(ent_visibility vis)
353 #define X(a) case a: return #a
356 X(visibility_external_visible);
357 X(visibility_external_allocated);
358 default: return "BAD VALUE";
364 (get_entity_variability)(const entity *ent) {
365 return __get_entity_variability(ent);
369 set_entity_variability (entity *ent, ent_variability var)
371 assert(ent && ent->kind == k_entity);
372 if (var == variability_part_constant)
373 assert(is_class_type(ent->type) || is_struct_type(ent->type));
375 if ((is_compound_type(ent->type)) &&
376 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
377 /* Allocate datastructures for constant values */
378 ent->values = NEW_ARR_F(ir_node *, 0);
379 ent->val_paths = NEW_ARR_F(compound_graph_path *, 0);
381 if ((is_atomic_type(ent->type)) &&
382 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
383 /* Set default constant value. */
384 ent->value = new_rd_Unknown(get_const_code_irg(), get_type_mode(ent->type));
387 if ((is_compound_type(ent->type)) &&
388 (var == variability_uninitialized) && (ent->variability != variability_uninitialized)) {
389 /* Free datastructures for constant values */
390 DEL_ARR_F(ent->values); ent->values = NULL;
391 DEL_ARR_F(ent->val_paths); ent->val_paths = NULL;
393 ent->variability = var;
396 /* return the name of the variablity */
397 const char *get_variability_name(ent_variability var)
399 #define X(a) case a: return #a
401 X(variability_uninitialized);
402 X(variability_initialized);
403 X(variability_part_constant);
404 X(variability_constant);
405 default: return "BAD VALUE";
411 (get_entity_volatility)(const entity *ent) {
412 return __get_entity_volatility(ent);
416 (set_entity_volatility)(entity *ent, ent_volatility vol) {
417 __set_entity_volatility(ent, vol);
420 /* return the name of the volatility */
421 const char *get_volatility_name(ent_volatility var)
423 #define X(a) case a: return #a
425 X(volatility_non_volatile);
426 X(volatility_is_volatile);
427 default: return "BAD VALUE";
433 (get_entity_peculiarity)(const entity *ent) {
434 return __get_entity_peculiarity(ent);
438 (set_entity_peculiarity)(entity *ent, peculiarity pec) {
439 __set_entity_peculiarity(ent, pec);
442 /* return the name of the peculiarity */
443 const char *get_peculiarity_name(peculiarity var)
445 #define X(a) case a: return #a
447 X(peculiarity_description);
448 X(peculiarity_inherited);
449 X(peculiarity_existent);
450 default: return "BAD VALUE";
455 /* Get the entity's stickyness */
457 (get_entity_stickyness)(const entity *ent) {
458 return __get_entity_stickyness(ent);
461 /* Set the entity's stickyness */
463 (set_entity_stickyness)(entity *ent, ent_stickyness stickyness) {
464 __set_entity_stickyness(ent, stickyness);
467 /* Set has no effect for existent entities of type method. */
469 get_atomic_ent_value(entity *ent)
471 assert(ent && is_atomic_entity(ent));
472 assert(ent->variability != variability_uninitialized);
473 return skip_Id (ent->value);
477 set_atomic_ent_value(entity *ent, ir_node *val) {
478 assert(is_atomic_entity(ent) && (ent->variability != variability_uninitialized));
479 if (is_method_type(ent->type) && (ent->peculiarity == peculiarity_existent))
484 /* Returns true if the the node is representable as code on
486 int is_irn_const_expression(ir_node *n) {
489 /* we are in dange iff an exception will arise. TODO: be more precisely,
490 * for instance Div. will NOT rise if divisor != 0
492 if (is_binop(n) && !is_fragile_op(n))
493 return is_irn_const_expression(get_binop_left(n)) && is_irn_const_expression(get_binop_right(n));
496 switch(get_irn_opcode(n)) {
503 return is_irn_const_expression(get_irn_n(n, 0));
512 ir_node *copy_const_value(ir_node *n) {
516 /* @@@ GL I think we should implement this using the routines from irgopt for
517 dead node elimination/inlineing. */
520 switch(get_irn_opcode(n)) {
522 nn = new_Const(m, get_Const_tarval(n)); set_Const_type(nn, get_Const_type(n));
523 //nn = new_rd_Const_type(get_irn_dbg_info(n), current_ir_graph, get_cur_block(),
524 // m, get_Const_tarval(n), get_Const_type(n));
527 nn = new_d_SymConst_type(NULL, get_SymConst_symbol(n), get_SymConst_kind(n),
528 get_SymConst_value_type(n));
531 nn = new_Add(copy_const_value(get_Add_left(n)),
532 copy_const_value(get_Add_right(n)), m); break;
534 nn = new_Sub(copy_const_value(get_Sub_left(n)),
535 copy_const_value(get_Sub_right(n)), m); break;
537 nn = new_Mul(copy_const_value(get_Mul_left(n)),
538 copy_const_value(get_Mul_right(n)), m); break;
540 nn = new_And(copy_const_value(get_And_left(n)),
541 copy_const_value(get_And_right(n)), m); break;
543 nn = new_Or(copy_const_value(get_Or_left(n)),
544 copy_const_value(get_Or_right(n)), m); break;
546 nn = new_Eor(copy_const_value(get_Eor_left(n)),
547 copy_const_value(get_Eor_right(n)), m); break;
549 nn = new_Cast(copy_const_value(get_Cast_op(n)), get_Cast_type(n)); break;
551 nn = new_Conv(copy_const_value(get_Conv_op(n)), m); break;
553 nn = new_Unknown(m); break;
556 assert(0 && "opcode invalid or not implemented");
563 compound_graph_path *
564 new_compound_graph_path(type *tp, int length) {
565 compound_graph_path *res;
566 assert(is_type(tp) && is_compound_type(tp));
569 res = (compound_graph_path *) calloc (1, sizeof(compound_graph_path) + (length-1) * sizeof(entity *));
570 res->kind = k_ir_compound_graph_path;
573 res ->arr_indicees = (int *) calloc(length, sizeof(int));
578 free_compound_graph_path (compound_graph_path *gr) {
579 assert(gr && is_compound_graph_path(gr));
581 free(gr ->arr_indicees);
586 is_compound_graph_path(void *thing) {
587 return (get_kind(thing) == k_ir_compound_graph_path);
590 /* checks whether nodes 0..pos are correct (all lie on a path.) */
591 /* @@@ not implemented */
592 int is_proper_compound_graph_path(compound_graph_path *gr, int pos) {
595 type *owner = gr->tp;
596 for (i = 0; i <= pos; i++) {
597 node = get_compound_graph_path_node(gr, i);
598 if (get_entity_owner(node) != owner) return false;
599 owner = get_entity_type(node);
601 if (pos == get_compound_graph_path_length(gr))
602 if (!is_atomic_type(owner)) return false;
607 get_compound_graph_path_length(compound_graph_path *gr) {
608 assert(gr && is_compound_graph_path(gr));
613 get_compound_graph_path_node(compound_graph_path *gr, int pos) {
614 assert(gr && is_compound_graph_path(gr));
615 assert(pos >= 0 && pos < gr->len);
616 return gr->nodes[pos];
620 set_compound_graph_path_node(compound_graph_path *gr, int pos, entity *node) {
621 assert(gr && is_compound_graph_path(gr));
622 assert(pos >= 0 && pos < gr->len);
623 assert(is_entity(node));
624 gr->nodes[pos] = node;
625 assert(is_proper_compound_graph_path(gr, pos));
629 get_compound_graph_path_array_index(compound_graph_path *gr, int pos) {
630 assert(gr && is_compound_graph_path(gr));
631 assert(pos >= 0 && pos < gr->len);
632 return gr->arr_indicees[pos];
636 set_compound_graph_path_array_index(compound_graph_path *gr, int pos, int index) {
637 assert(gr && is_compound_graph_path(gr));
638 assert(pos >= 0 && pos < gr->len);
639 gr->arr_indicees[pos] = index;
642 /* A value of a compound entity is a pair of value and the corresponding path to a member of
645 add_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path) {
646 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
647 ARR_APP1 (ir_node *, ent->values, val);
648 ARR_APP1 (compound_graph_path *, ent->val_paths, path);
652 set_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path, int pos) {
653 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
654 ent->values[pos] = val;
655 ent->val_paths[pos] = path;
659 get_compound_ent_n_values(entity *ent) {
660 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
661 return (ARR_LEN (ent->values));
665 get_compound_ent_value(entity *ent, int pos) {
666 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
667 return ent->values[pos];
670 compound_graph_path *
671 get_compound_ent_value_path(entity *ent, int pos) {
672 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
673 return ent->val_paths[pos];
677 remove_compound_ent_value(entity *ent, entity *value_ent) {
679 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
680 for (i = 0; i < (ARR_LEN (ent->val_paths)); i++) {
681 compound_graph_path *path = ent->val_paths[i];
682 if (path->nodes[path->len-1] == value_ent) {
683 for(; i < (ARR_LEN (ent->val_paths))-1; i++) {
684 ent->val_paths[i] = ent->val_paths[i+1];
685 ent->values[i] = ent->values[i+1];
687 ARR_SETLEN(entity*, ent->val_paths, ARR_LEN(ent->val_paths) - 1);
688 ARR_SETLEN(ir_node*, ent->values, ARR_LEN(ent->values) - 1);
695 add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
696 compound_graph_path *path;
697 type *owner_tp = get_entity_owner(ent);
698 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
699 path = new_compound_graph_path(owner_tp, 1);
700 path->nodes[0] = member;
701 if (is_array_type(owner_tp)) {
705 assert(get_array_n_dimensions(owner_tp) == 1 && has_array_lower_bound(owner_tp, 0));
706 max = get_array_lower_bound_int(owner_tp, 0) -1;
707 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
708 int index = get_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0);
713 path->arr_indicees[0] = max + 1;
715 add_compound_ent_value_w_path(ent, val, path);
718 /* Copies the firm subgraph referenced by val to const_code_irg and adds
719 the node as constant initialization to ent.
720 The subgraph may not contain control flow operations.
722 copy_and_add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
723 ir_graph *rem = current_ir_graph;
725 assert(get_entity_variability(ent) != variability_uninitialized);
726 current_ir_graph = get_const_code_irg();
728 val = copy_const_value(val);
729 add_compound_ent_value(ent, val, member);
730 current_ir_graph = rem;
733 /* Copies the value i of the entity to current_block in current_ir_graph.
735 copy_compound_ent_value(entity *ent, int pos) {
736 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
737 return copy_const_value(ent->values[pos+1]);
741 get_compound_ent_value_member(entity *ent, int pos) {
742 compound_graph_path *path;
743 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
744 path = get_compound_ent_value_path(ent, pos);
746 return get_compound_graph_path_node(path, get_compound_graph_path_length(path)-1);
750 set_compound_ent_value(entity *ent, ir_node *val, entity *member, int pos) {
751 compound_graph_path *path;
752 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
753 path = get_compound_ent_value_path(ent, pos);
754 set_compound_graph_path_node(path, 0, member);
755 set_compound_ent_value_w_path(ent, val, path, pos);
759 set_array_entity_values(entity *ent, tarval **values, int num_vals) {
761 ir_graph *rem = current_ir_graph;
762 type *arrtp = get_entity_type(ent);
764 type *elttp = get_array_element_type(arrtp);
766 assert(is_array_type(arrtp));
767 assert(get_array_n_dimensions(arrtp) == 1);
768 /* One bound is sufficient, the nunmber of constant fields makes the
770 assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
771 assert(get_entity_variability(ent) != variability_uninitialized);
772 current_ir_graph = get_const_code_irg();
774 for (i = 0; i < num_vals; i++) {
775 val = new_Const_type(values[i], elttp);
776 add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
777 set_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0, i);
779 current_ir_graph = rem;
782 int get_compound_ent_value_offset_bits(entity *ent, int pos) {
783 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
785 compound_graph_path *path = get_compound_ent_value_path(ent, pos);
786 int i, path_len = get_compound_graph_path_length(path);
789 for (i = 0; i < path_len; ++i) {
790 entity *node = get_compound_graph_path_node(path, i);
791 type *node_tp = get_entity_type(node);
792 type *owner_tp = get_entity_owner(node);
793 if (is_array_type(owner_tp)) {
794 int size = get_type_size_bits(node_tp);
795 int align = get_type_alignment_bits(node_tp);
799 assert(size % align == 0);
800 /* ansonsten aufrunden */
802 offset += size * get_compound_graph_path_array_index(path, i);
804 offset += get_entity_offset_bits(node);
810 int get_compound_ent_value_offset_bytes(entity *ent, int pos) {
811 int offset = get_compound_ent_value_offset_bits(ent, pos);
812 assert(offset % 8 == 0);
817 static void init_index(type *arr) {
821 assert(get_array_n_dimensions(arr) == 1);
823 if (has_array_lower_bound(arr, dim))
824 init = get_array_lower_bound_int(arr, 0) -1;
826 init = get_array_upper_bound_int(arr, 0) +1;
828 set_entity_link(get_array_element_entity(arr), (void *)init);
832 static int get_next_index(entity *elem_ent) {
833 type *arr = get_entity_owner(elem_ent);
837 assert(get_array_n_dimensions(arr) == 1);
839 if (has_array_lower_bound(arr, dim)) {
840 next = (int)get_entity_link(elem_ent) +1;
841 if (has_array_upper_bound(arr, dim)) {
842 int upper = get_array_upper_bound_int(arr, dim);
843 if (next == upper) next = get_array_lower_bound_int(arr, dim);
846 next = (int)get_entity_link(elem_ent) -1;
847 if (has_array_lower_bound(arr, dim)) {
848 int upper = get_array_upper_bound_int(arr, dim);
849 if (next == upper) next = get_array_upper_bound_int(arr, dim);
853 set_entity_link(elem_ent, (void *)next);
857 /* Compute the array indicees in compound graph paths of initialized entities.
859 * All arrays must have fixed lower and upper bounds. One array can
860 * have an open bound. If there are several open bounds, we do
861 * nothing. There must be initializer elements for all array
862 * elements. Uses the link field in the array element entities. The
863 * array bounds must be representable as ints.
865 * (If the bounds are not representable as ints we have to represent
866 * the indicees as firm nodes. But the still we must be able to
867 * evaluate the index against the upper bound.)
869 void compute_compound_ent_array_indicees(entity *ent) {
870 type *tp = get_entity_type(ent);
872 entity *unknown_bound_entity = NULL;
874 if (!is_compound_type(tp) ||
875 (ent->variability == variability_uninitialized)) return ;
877 n_vals = get_compound_ent_n_values(ent);
878 if (n_vals == 0) return;
880 /* We can not compute the indicees if there is more than one array
881 with an unknown bound. For this remember the first entity that
882 represents such an array. It could be ent. */
883 if (is_array_type(tp)) {
884 assert(get_array_n_dimensions(tp) == 1 && "other not implemented");
886 if (!has_array_lower_bound(tp, dim) || !has_array_upper_bound(tp, dim))
887 unknown_bound_entity = ent;
890 /* Initialize the entity links to lower bound -1 and test all path elements
892 for (i = 0; i < n_vals; ++i) {
893 compound_graph_path *path = get_compound_ent_value_path(ent, i);
894 int j, path_len = get_compound_graph_path_length(path);
895 for (j = 0; j < path_len; ++j) {
896 entity *node = get_compound_graph_path_node(path, j);
897 type *elem_tp = get_entity_type(node);
899 if (is_array_type(elem_tp)) {
900 assert(get_array_n_dimensions(elem_tp) == 1 && "other not implemented");
902 if (!has_array_lower_bound(elem_tp, dim) || !has_array_upper_bound(elem_tp, dim)) {
903 if (!unknown_bound_entity) unknown_bound_entity = node;
904 if (node != unknown_bound_entity) return;
912 /* Finally compute the indicees ... */
913 for (i = 0; i < n_vals; ++i) {
914 compound_graph_path *path = get_compound_ent_value_path(ent, i);
915 int j, path_len = get_compound_graph_path_length(path);
916 for (j = 0; j < path_len; ++j) {
917 entity *node = get_compound_graph_path_node(path, j);
918 type *owner_tp = get_entity_owner(node);
919 if (is_array_type(owner_tp))
920 set_compound_graph_path_array_index (path, j, get_next_index(node));
927 static int *resize (int *buf, int new_size) {
928 int *new_buf = (int *)calloc(new_size, 4);
929 memcpy(new_buf, buf, new_size>1);
934 /* We sort the elements by placing them at their bit offset in an
935 array where each entry represents one bit called permutation. In
936 fact, we do not place the values themselves, as we would have to
937 copy two things, the value and the path. We only remember the
938 position in the old order. Each value should have a distinct
939 position in the permutation.
941 A second iteration now permutes the actual elements into two
943 void sort_compound_ent_values(entity *ent) {
944 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
946 type *tp = get_entity_type(ent);
947 int i, n_vals = get_compound_ent_n_values(ent);
948 int tp_size = get_type_size_bits(tp);
952 if (!is_compound_type(tp) ||
953 (ent->variability == variability_uninitialized) ||
954 (get_type_state(tp) != layout_fixed) ||
955 (n_vals == 0) ) return;
957 /* estimated upper bound for size. Better: use flexible array ... */
958 size = ((tp_size > (n_vals * 32)) ? tp_size : (n_vals * 32)) * 4;
959 permutation = (int *)calloc(size, 4);
960 for (i = 0; i < n_vals; ++i) {
961 int pos = get_compound_ent_value_offset_bits(ent, i);
962 while (pos >= size) {
964 permutation = resize(permutation, size);
967 assert(permutation[pos] == 0 && "two values with the same offset");
968 permutation[pos] = i + 1; /* We initialized with 0, so we can not distinguish entry 0.
969 So inc all entries by one. */
970 //fprintf(stderr, "i: %d, pos: %d \n", i, pos);
974 ir_node **my_values = NEW_ARR_F(ir_node *, n_vals);
975 compound_graph_path **my_paths = NEW_ARR_F(compound_graph_path *, n_vals);
976 for (i = 0; i < size; ++i) {
977 int pos = permutation[i];
979 //fprintf(stderr, "pos: %d i: %d next %d \n", i, pos, next);
980 assert(next < n_vals);
981 pos--; /* We increased the pos by one */
982 my_values[next] = get_compound_ent_value (ent, pos);
983 my_paths [next] = get_compound_ent_value_path(ent, pos);
989 DEL_ARR_F(ent->values);
990 ent->values = my_values;
991 DEL_ARR_F(ent->val_paths);
992 ent->val_paths = my_paths;
996 (get_entity_offset_bytes)(const entity *ent) {
997 return __get_entity_offset_bytes(ent);
1001 (get_entity_offset_bits)(const entity *ent) {
1002 return __get_entity_offset_bits(ent);
1006 (set_entity_offset_bytes)(entity *ent, int offset) {
1007 __set_entity_offset_bytes(ent, offset);
1011 (set_entity_offset_bits)(entity *ent, int offset) {
1012 __set_entity_offset_bits(ent, offset);
1016 add_entity_overwrites(entity *ent, entity *overwritten) {
1017 assert(ent && is_class_type(get_entity_owner(ent)));
1018 ARR_APP1(entity *, ent->overwrites, overwritten);
1019 ARR_APP1(entity *, overwritten->overwrittenby, ent);
1023 get_entity_n_overwrites(entity *ent) {
1024 assert(ent && is_class_type(get_entity_owner(ent)));
1025 return (ARR_LEN(ent->overwrites));
1029 get_entity_overwrites_index(entity *ent, entity *overwritten) {
1031 assert(ent && is_class_type(get_entity_owner(ent)));
1032 for (i = 0; i < get_entity_n_overwrites(ent); i++)
1033 if (get_entity_overwrites(ent, i) == overwritten)
1039 get_entity_overwrites (entity *ent, int pos) {
1040 assert(ent && is_class_type(get_entity_owner(ent)));
1041 assert(pos < get_entity_n_overwrites(ent));
1042 return ent->overwrites[pos];
1046 set_entity_overwrites (entity *ent, int pos, entity *overwritten) {
1047 assert(ent && is_class_type(get_entity_owner(ent)));
1048 assert(pos < get_entity_n_overwrites(ent));
1049 ent->overwrites[pos] = overwritten;
1053 remove_entity_overwrites(entity *ent, entity *overwritten) {
1055 assert(ent && is_class_type(get_entity_owner(ent)));
1056 for (i = 0; i < (ARR_LEN (ent->overwrites)); i++)
1057 if (ent->overwrites[i] == overwritten) {
1058 for(; i < (ARR_LEN (ent->overwrites))-1; i++)
1059 ent->overwrites[i] = ent->overwrites[i+1];
1060 ARR_SETLEN(entity*, ent->overwrites, ARR_LEN(ent->overwrites) - 1);
1066 add_entity_overwrittenby (entity *ent, entity *overwrites) {
1067 assert(ent && is_class_type(get_entity_owner(ent)));
1068 add_entity_overwrites(overwrites, ent);
1072 get_entity_n_overwrittenby (entity *ent) {
1073 assert(ent && is_class_type(get_entity_owner(ent)));
1074 return (ARR_LEN (ent->overwrittenby));
1078 get_entity_overwrittenby_index(entity *ent, entity *overwrites) {
1080 assert(ent && is_class_type(get_entity_owner(ent)));
1081 for (i = 0; i < get_entity_n_overwrittenby(ent); i++)
1082 if (get_entity_overwrittenby(ent, i) == overwrites)
1088 get_entity_overwrittenby (entity *ent, int pos) {
1089 assert(ent && is_class_type(get_entity_owner(ent)));
1090 assert(pos < get_entity_n_overwrittenby(ent));
1091 return ent->overwrittenby[pos];
1095 set_entity_overwrittenby (entity *ent, int pos, entity *overwrites) {
1096 assert(ent && is_class_type(get_entity_owner(ent)));
1097 assert(pos < get_entity_n_overwrittenby(ent));
1098 ent->overwrittenby[pos] = overwrites;
1101 void remove_entity_overwrittenby(entity *ent, entity *overwrites) {
1103 assert(ent && is_class_type(get_entity_owner(ent)));
1104 for (i = 0; i < (ARR_LEN (ent->overwrittenby)); i++)
1105 if (ent->overwrittenby[i] == overwrites) {
1106 for(; i < (ARR_LEN (ent->overwrittenby))-1; i++)
1107 ent->overwrittenby[i] = ent->overwrittenby[i+1];
1108 ARR_SETLEN(entity*, ent->overwrittenby, ARR_LEN(ent->overwrittenby) - 1);
1113 /* A link to store intermediate information */
1115 (get_entity_link)(const entity *ent) {
1116 return __get_entity_link(ent);
1120 (set_entity_link)(entity *ent, void *l) {
1121 __set_entity_link(ent, l);
1125 (get_entity_irg)(const entity *ent) {
1126 return __get_entity_irg(ent);
1130 set_entity_irg(entity *ent, ir_graph *irg) {
1131 assert(ent && is_method_type(get_entity_type(ent)));
1132 /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
1133 * Methode selbst nicht mehr aufgerufen werden kann, die Entität
1134 * aber erhalten bleiben soll. */
1136 assert((irg && ent->peculiarity == peculiarity_existent) ||
1137 (!irg && ent->peculiarity == peculiarity_description) ||
1138 (!irg && ent->peculiarity == peculiarity_inherited));
1143 (is_entity)(const void *thing) {
1144 return __is_entity(thing);
1147 int is_atomic_entity(entity *ent) {
1148 type* t = get_entity_type(ent);
1149 assert(ent && ent->kind == k_entity);
1150 return (is_primitive_type(t) || is_pointer_type(t) ||
1151 is_enumeration_type(t) || is_method_type(t));
1154 int is_compound_entity(entity *ent) {
1155 type* t = get_entity_type(ent);
1156 assert(ent && ent->kind == k_entity);
1157 return (is_class_type(t) || is_struct_type(t) ||
1158 is_array_type(t) || is_union_type(t));
1162 * @todo not implemnted!!! */
1163 bool equal_entity(entity *ent1, entity *ent2) {
1164 fprintf(stderr, " calling unimplemented equal entity!!! \n");
1169 unsigned long get_entity_visited(entity *ent) {
1170 assert(ent && ent->kind == k_entity);
1173 void set_entity_visited(entity *ent, unsigned long num) {
1174 assert(ent && ent->kind == k_entity);
1177 /* Sets visited field in entity to entity_visited. */
1178 void mark_entity_visited(entity *ent) {
1179 assert(ent && ent->kind == k_entity);
1180 ent->visit = type_visited;
1184 bool entity_visited(entity *ent) {
1185 assert(ent && ent->kind == k_entity);
1186 return get_entity_visited(ent) >= type_visited;
1189 bool entity_not_visited(entity *ent) {
1190 assert(ent && ent->kind == k_entity);
1191 return get_entity_visited(ent) < type_visited;
1194 /* Need two routines because I want to assert the result. */
1195 static entity *resolve_ent_polymorphy2 (type *dynamic_class, entity* static_ent) {
1196 int i, n_overwrittenby;
1199 if (get_entity_owner(static_ent) == dynamic_class) return static_ent;
1201 n_overwrittenby = get_entity_n_overwrittenby(static_ent);
1202 for (i = 0; i < n_overwrittenby; ++i) {
1203 res = resolve_ent_polymorphy2(dynamic_class, get_entity_overwrittenby(static_ent, i));
1210 /** Resolve polymorphy in the inheritance relation.
1212 * Returns the dynamically referenced entity if the static entity and the
1213 * dynamic type are given.
1214 * Search downwards in overwritten tree. */
1215 entity *resolve_ent_polymorphy(type *dynamic_class, entity* static_ent) {
1217 assert(static_ent && static_ent->kind == k_entity);
1219 res = resolve_ent_polymorphy2(dynamic_class, static_ent);
1222 printf(" Could not find entity "); DDME(static_ent);
1223 printf(" in "); DDMT(dynamic_class);
1225 dump_entity(static_ent);
1226 dump_type(get_entity_owner(static_ent));
1227 dump_type(dynamic_class);