- assert(0 && "path not found");
- return -1;
-}
-
-/* Returns a constant value given the access path.
- * The path must contain array indicees for all array element entities. */
-ir_node *get_compound_ent_value_by_path(entity *ent, compound_graph_path *path) {
- return get_compound_ent_value(ent, get_compound_ent_pos_by_path(ent, path));
-}
-
-
-void
-remove_compound_ent_value(entity *ent, entity *value_ent) {
- int i;
- assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
- for (i = 0; i < (ARR_LEN (ent->attr.cmpd_attr.val_paths)); i++) {
- compound_graph_path *path = ent->attr.cmpd_attr.val_paths[i];
- if (path->list[path->len-1].node == value_ent) {
- for(; i < (ARR_LEN (ent->attr.cmpd_attr.val_paths))-1; i++) {
- ent->attr.cmpd_attr.val_paths[i] = ent->attr.cmpd_attr.val_paths[i+1];
- ent->attr.cmpd_attr.values[i] = ent->attr.cmpd_attr.values[i+1];
- }
- ARR_SETLEN(entity*, ent->attr.cmpd_attr.val_paths, ARR_LEN(ent->attr.cmpd_attr.val_paths) - 1);
- ARR_SETLEN(ir_node*, ent->attr.cmpd_attr.values, ARR_LEN(ent->attr.cmpd_attr.values) - 1);
- break;
- }
- }
-}
-
-void
-add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
- compound_graph_path *path;
- ir_type *owner_tp = get_entity_owner(member);
- assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
- path = new_compound_graph_path(get_entity_type(ent), 1);
- path->list[0].node = member;
- if (is_Array_type(owner_tp)) {
- int max;
- int i;
-
- assert(get_array_n_dimensions(owner_tp) == 1 && has_array_lower_bound(owner_tp, 0));
- max = get_array_lower_bound_int(owner_tp, 0) -1;
- for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
- int index = get_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0);
- if (index > max) {
- max = index;
- }
- }
- path->list[0].index = max + 1;
- }
- add_compound_ent_value_w_path(ent, val, path);
-}
-
-/* Copies the firm subgraph referenced by val to const_code_irg and adds
- the node as constant initialization to ent.
- The subgraph may not contain control flow operations.
-void
-copy_and_add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
- ir_graph *rem = current_ir_graph;
-
- assert(get_entity_variability(ent) != variability_uninitialized);
- current_ir_graph = get_const_code_irg();
-
- val = copy_const_value(val);
- add_compound_ent_value(ent, val, member);
- current_ir_graph = rem;
- }*/
-
-/* Copies the value i of the entity to current_block in current_ir_graph.
-ir_node *
-copy_compound_ent_value(entity *ent, int pos) {
- assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
- return copy_const_value(ent->values[pos+1]);
- }*/
-
-entity *
-get_compound_ent_value_member(entity *ent, int pos) {
- compound_graph_path *path;
- assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
- path = get_compound_ent_value_path(ent, pos);
-
- return get_compound_graph_path_node(path, get_compound_graph_path_length(path)-1);
-}
-
-void
-set_compound_ent_value(entity *ent, ir_node *val, entity *member, int pos) {
- compound_graph_path *path;
- assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
- path = get_compound_ent_value_path(ent, pos);
- set_compound_graph_path_node(path, 0, member);
- set_compound_ent_value_w_path(ent, val, path, pos);
-}
-
-void
-set_array_entity_values(entity *ent, tarval **values, int num_vals) {
- int i;
- ir_graph *rem = current_ir_graph;
- ir_type *arrtp = get_entity_type(ent);
- ir_node *val;
- ir_type *elttp = get_array_element_type(arrtp);
-
- assert(is_Array_type(arrtp));
- assert(get_array_n_dimensions(arrtp) == 1);
- /* One bound is sufficient, the number of constant fields makes the
- size. */
- assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
- assert(get_entity_variability(ent) != variability_uninitialized);
- current_ir_graph = get_const_code_irg();
-
- for (i = 0; i < num_vals; i++) {
- val = new_Const_type(values[i], elttp);
- add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
- set_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0, i);
- }
- current_ir_graph = rem;
-}
-
-int get_compound_ent_value_offset_bits(entity *ent, int pos) {
- compound_graph_path *path;
- int i, path_len;
- int offset = 0;
-
- assert(get_type_state(get_entity_type(ent)) == layout_fixed);
-
- path = get_compound_ent_value_path(ent, pos);
- path_len = get_compound_graph_path_length(path);
-
- for (i = 0; i < path_len; ++i) {
- entity *node = get_compound_graph_path_node(path, i);
- ir_type *node_tp = get_entity_type(node);
- ir_type *owner_tp = get_entity_owner(node);
- if (is_Array_type(owner_tp)) {
- int size = get_type_size_bits(node_tp);
- int align = get_type_alignment_bits(node_tp);
- if (size < align)
- size = align;
- else {
- assert(size % align == 0);
- /* ansonsten aufrunden */
- }
- offset += size * get_compound_graph_path_array_index(path, i);
- } else {
- offset += get_entity_offset_bits(node);
- }
- }
- return offset;
-}
-
-int get_compound_ent_value_offset_bytes(entity *ent, int pos) {
- int offset = get_compound_ent_value_offset_bits(ent, pos);
- assert(offset % 8 == 0);
- return offset >> 3;
-}
-
-
-static void init_index(ir_type *arr) {
- int init;
- int dim = 0;
-
- assert(get_array_n_dimensions(arr) == 1);
-
- if (has_array_lower_bound(arr, dim))
- init = get_array_lower_bound_int(arr, 0) -1;
- else
- init = get_array_upper_bound_int(arr, 0) +1;
-
- set_entity_link(get_array_element_entity(arr), INT_TO_PTR(init));
-}
-
-
-static int get_next_index(entity *elem_ent) {
- ir_type *arr = get_entity_owner(elem_ent);
- int next;
- int dim = 0;
-
- assert(get_array_n_dimensions(arr) == 1);
-
- if (has_array_lower_bound(arr, dim)) {
- next = PTR_TO_INT(get_entity_link(elem_ent)) + 1;
- if (has_array_upper_bound(arr, dim)) {
- int upper = get_array_upper_bound_int(arr, dim);
- if (next == upper) next = get_array_lower_bound_int(arr, dim);
- }
- } else {
- next = PTR_TO_INT(get_entity_link(elem_ent)) - 1;
- if (has_array_lower_bound(arr, dim)) {
- int upper = get_array_upper_bound_int(arr, dim);
- if (next == upper) next = get_array_upper_bound_int(arr, dim);
- }
- }
-
- set_entity_link(elem_ent, INT_TO_PTR(next));
- return next;
-}
-
-/* Compute the array indices in compound graph paths of initialized entities.
- *
- * All arrays must have fixed lower and upper bounds. One array can
- * have an open bound. If there are several open bounds, we do
- * nothing. There must be initializer elements for all array
- * elements. Uses the link field in the array element entities. The
- * array bounds must be representable as ints.
- *
- * (If the bounds are not representable as ints we have to represent
- * the indices as firm nodes. But still we must be able to
- * evaluate the index against the upper bound.)
- */
-void compute_compound_ent_array_indicees(entity *ent) {
- ir_type *tp = get_entity_type(ent);
- int i, n_vals;
- entity *unknown_bound_entity = NULL;
-
- if (!is_compound_type(tp) ||
- (ent->variability == variability_uninitialized)) return ;
-
- n_vals = get_compound_ent_n_values(ent);
- if (n_vals == 0) return;
-
- /* We can not compute the indexes if there is more than one array
- with an unknown bound. For this remember the first entity that
- represents such an array. It could be ent. */
- if (is_Array_type(tp)) {
- int dim = 0;
-
- assert(get_array_n_dimensions(tp) == 1 && "other not implemented");
- if (!has_array_lower_bound(tp, dim) || !has_array_upper_bound(tp, dim))
- unknown_bound_entity = ent;
- }
-
- /* Initialize the entity links to lower bound -1 and test all path elements
- for known bounds. */
- for (i = 0; i < n_vals; ++i) {
- compound_graph_path *path = get_compound_ent_value_path(ent, i);
- int j, path_len = get_compound_graph_path_length(path);
- for (j = 0; j < path_len; ++j) {
- entity *node = get_compound_graph_path_node(path, j);
- ir_type *elem_tp = get_entity_type(node);
-
- if (is_Array_type(elem_tp)) {
- int dim = 0;
- assert(get_array_n_dimensions(elem_tp) == 1 && "other not implemented");
- if (!has_array_lower_bound(elem_tp, dim) || !has_array_upper_bound(elem_tp, dim)) {
- if (!unknown_bound_entity) unknown_bound_entity = node;
- if (node != unknown_bound_entity) return;
- }
-
- init_index(elem_tp);
- }
- }
- }
-
- /* Finally compute the indexes ... */
- for (i = 0; i < n_vals; ++i) {
- compound_graph_path *path = get_compound_ent_value_path(ent, i);
- int j, path_len = get_compound_graph_path_length(path);
- for (j = 0; j < path_len; ++j) {
- entity *node = get_compound_graph_path_node(path, j);
- ir_type *owner_tp = get_entity_owner(node);
- if (is_Array_type(owner_tp))
- set_compound_graph_path_array_index (path, j, get_next_index(node));
- }
- }
-}
-
-/** resize: double the allocated buffer */
-static int *resize (int *buf, int *size) {
- int new_size = *size * 2;
- int *new_buf = xcalloc(new_size, sizeof(new_buf[0]));
- memcpy(new_buf, buf, *size);
- free(buf);
- *size = new_size;
- return new_buf;
-}
-
-/* We sort the elements by placing them at their bit offset in an
- array where each entry represents one bit called permutation. In
- fact, we do not place the values themselves, as we would have to
- copy two things, the value and the path. We only remember the
- position in the old order. Each value should have a distinct
- position in the permutation.
-
- A second iteration now permutes the actual elements into two
- new arrays. */
-void sort_compound_ent_values(entity *ent) {
- ir_type *tp;
- int i, n_vals;
- int tp_size;
- int size;
- int *permutation;
-
- int next;
- ir_node **my_values;
- compound_graph_path **my_paths;
-
- assert(get_type_state(get_entity_type(ent)) == layout_fixed);
-
- tp = get_entity_type(ent);
- n_vals = get_compound_ent_n_values(ent);
- tp_size = get_type_size_bits(tp);
-
- if (!is_compound_type(tp) ||
- (ent->variability == variability_uninitialized) ||
- (get_type_state(tp) != layout_fixed) ||
- (n_vals == 0) ) return;
-
- /* estimated upper bound for size. Better: use flexible array ... */
- size = ((tp_size > (n_vals * 32)) ? tp_size : (n_vals * 32)) * 4;
- permutation = xcalloc(size, sizeof(permutation[0]));
-
- for (i = 0; i < n_vals; ++i) {
- int pos = get_compound_ent_value_offset_bits(ent, i);
- while (pos >= size) {
- permutation = resize(permutation, &size);
- }
- assert(pos < size);
- assert(permutation[pos] == 0 && "two values with the same offset");
- permutation[pos] = i + 1; /* We initialized with 0, so we can not distinguish entry 0.
- So inc all entries by one. */
- //fprintf(stderr, "i: %d, pos: %d \n", i, pos);
- }
-
- next = 0;
- my_values = NEW_ARR_F(ir_node *, n_vals);
- my_paths = NEW_ARR_F(compound_graph_path *, n_vals);
- for (i = 0; i < size; ++i) {
- int pos = permutation[i];
- if (pos) {
- //fprintf(stderr, "pos: %d i: %d next %d \n", i, pos, next);
- assert(next < n_vals);
- pos--; /* We increased the pos by one */
- my_values[next] = get_compound_ent_value (ent, pos);
- my_paths [next] = get_compound_ent_value_path(ent, pos);
- next++;
- }
- }
- free(permutation);
-
- DEL_ARR_F(ent->attr.cmpd_attr.values);
- ent->attr.cmpd_attr.values = my_values;
- DEL_ARR_F(ent->attr.cmpd_attr.val_paths);
- ent->attr.cmpd_attr.val_paths = my_paths;
-}
-
-int
-(get_entity_offset_bytes)(const entity *ent) {
- return _get_entity_offset_bytes(ent);
-}
-
-int
-(get_entity_offset_bits)(const entity *ent) {
- return _get_entity_offset_bits(ent);
-}
-
-void
-(set_entity_offset_bytes)(entity *ent, int offset) {
- _set_entity_offset_bytes(ent, offset);
-}
-
-void
-(set_entity_offset_bits)(entity *ent, int offset) {
- _set_entity_offset_bits(ent, offset);
-}
-
-void
-add_entity_overwrites(entity *ent, entity *overwritten) {
- assert(ent && is_Class_type(get_entity_owner(ent)));
- ARR_APP1(entity *, ent->overwrites, overwritten);
- ARR_APP1(entity *, overwritten->overwrittenby, ent);
-}
-
-int
-get_entity_n_overwrites(entity *ent) {
- assert(ent && is_Class_type(get_entity_owner(ent)));
- return (ARR_LEN(ent->overwrites));
-}
-
-int
-get_entity_overwrites_index(entity *ent, entity *overwritten) {
- int i;
- assert(ent && is_Class_type(get_entity_owner(ent)));
- for (i = 0; i < get_entity_n_overwrites(ent); i++)
- if (get_entity_overwrites(ent, i) == overwritten)
- return i;
- return -1;
-}
-
-entity *
-get_entity_overwrites (entity *ent, int pos) {
- assert(ent && is_Class_type(get_entity_owner(ent)));
- assert(pos < get_entity_n_overwrites(ent));
- return ent->overwrites[pos];
-}
-
-void
-set_entity_overwrites (entity *ent, int pos, entity *overwritten) {
- assert(ent && is_Class_type(get_entity_owner(ent)));
- assert(pos < get_entity_n_overwrites(ent));
- ent->overwrites[pos] = overwritten;
-}
-
-void
-remove_entity_overwrites(entity *ent, entity *overwritten) {
- int i;
- assert(ent && is_Class_type(get_entity_owner(ent)));
- for (i = 0; i < (ARR_LEN (ent->overwrites)); i++)
- if (ent->overwrites[i] == overwritten) {
- for(; i < (ARR_LEN (ent->overwrites))-1; i++)
- ent->overwrites[i] = ent->overwrites[i+1];
- ARR_SETLEN(entity*, ent->overwrites, ARR_LEN(ent->overwrites) - 1);
- break;
- }