-set_array_entity_values(entity *ent, tarval **values, int num_vals) {
- int i;
- ir_graph *rem = current_ir_graph;
- type *arrtp = get_entity_type(ent);
- ir_node *val;
- 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);
- type *node_tp = get_entity_type(node);
- 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(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), (void *)init);
-}
-
-
-static int get_next_index(entity *elem_ent) {
- 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 = (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 = (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, (void *)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) {
- 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);
- 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);
- 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) {
- 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->values);
- ent->values = my_values;
- DEL_ARR_F(ent->val_paths);
- ent->val_paths = my_paths;
-}
-
-int
-(get_entity_offset_bytes)(const entity *ent) {
- return _get_entity_offset_bytes(ent);
-}
+set_array_entity_values(ir_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;
+} /* set_array_entity_values */
+
+/* Return the overall offset of value at position pos in bytes. */
+int get_compound_ent_value_offset_bytes(ir_entity *ent, int pos) {
+ compound_graph_path *path;
+ int path_len, i;
+ int offset = 0;
+ ir_type *curr_tp;
+
+ 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);
+ curr_tp = path->tp;
+
+ for (i = 0; i < path_len; ++i) {
+ ir_entity *node = get_compound_graph_path_node(path, i);
+ ir_type *node_tp = get_entity_type(node);
+
+ if (is_Array_type(curr_tp)) {
+ int size = get_type_size_bits(node_tp);
+ int align = get_type_alignment_bits(node_tp);
+ int idx;
+
+ assert(size > 0);
+ if(size % align > 0) {
+ size += align - (size % align);
+ }
+ assert(size % 8 == 0);
+ size /= 8;
+ idx = get_compound_graph_path_array_index(path, i);
+ assert(idx >= 0);
+ offset += size * idx;
+ } else {
+ offset += get_entity_offset(node);
+ }
+ curr_tp = node_tp;
+ }
+
+ return offset;
+} /* get_compound_ent_value_offset_bytes */
+
+/* Return the offset in bits from the last byte address. */
+int get_compound_ent_value_offset_bit_remainder(ir_entity *ent, int pos) {
+ compound_graph_path *path;
+ int path_len;
+ ir_entity *last_node;
+
+ 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);
+ last_node = get_compound_graph_path_node(path, path_len - 1);
+
+ return get_entity_offset_bits_remainder(last_node);
+} /* get_compound_ent_value_offset_bit_remainder */