+void
+add_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path) {
+ assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
+ assert(is_compound_graph_path(path));
+ ARR_APP1(ir_node *, ent->attr.cmpd_attr.values, val);
+ ARR_APP1(compound_graph_path *, ent->attr.cmpd_attr.val_paths, path);
+} /* add_compound_ent_value_w_path */
+
+void
+set_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path, int pos) {
+ assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
+ assert(is_compound_graph_path(path));
+ assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
+ ent->attr.cmpd_attr.values[pos] = val;
+ ent->attr.cmpd_attr.val_paths[pos] = path;
+} /* set_compound_ent_value_w_path */
+
+int
+get_compound_ent_n_values(ir_entity *ent) {
+ assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
+ return ARR_LEN(ent->attr.cmpd_attr.values);
+} /* get_compound_ent_n_values */
+
+ir_node *
+get_compound_ent_value(ir_entity *ent, int pos) {
+ assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
+ assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
+ return ent->attr.cmpd_attr.values[pos];
+} /* get_compound_ent_value */
+
+compound_graph_path *
+get_compound_ent_value_path(ir_entity *ent, int pos) {
+ assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
+ assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.val_paths));
+ return ent->attr.cmpd_attr.val_paths[pos];
+} /* get_compound_ent_value_path */
+
+/**
+ * Returns non-zero, if two compound_graph_pathes are equal
+ *
+ * @param path1 the first path
+ * @param visited_indices
+ * @param path2 the second path
+ */
+static int equal_paths(compound_graph_path *path1, int *visited_indices, compound_graph_path *path2) {
+ int i;
+ int len1 = get_compound_graph_path_length(path1);
+ int len2 = get_compound_graph_path_length(path2);
+
+ if (len2 != len1) return 0;
+
+ for (i = 0; i < len1; i++) {
+ ir_type *tp;
+ ir_entity *node1 = get_compound_graph_path_node(path1, i);
+ ir_entity *node2 = get_compound_graph_path_node(path2, i);
+
+ if (node1 != node2) return 0;
+
+ /* FIXME: Strange code. What is it good for? */
+ tp = get_entity_owner(node1);
+ if (is_Array_type(tp)) {
+ long low;
+
+ /* Compute the index of this node. */
+ assert(get_array_n_dimensions(tp) == 1 && "multidim not implemented");
+
+ low = get_array_lower_bound_int(tp, 0);
+ if (low + visited_indices[i] < get_compound_graph_path_array_index(path2, i)) {
+ visited_indices[i]++;
+ return 0;
+ } else
+ assert(low + visited_indices[i] == get_compound_graph_path_array_index(path2, i));
+ }
+ }
+ return 1;
+} /* equal_paths */
+
+/**
+ * Returns the position of a value with the given path.
+ * The path must contain array indices for all array element entities.
+ *
+ * @todo This implementation is very slow (O(number of initializers^2) and should
+ * be replaced when the new tree oriented
+ * value representation is finally implemented.
+ */
+static int get_compound_ent_pos_by_path(ir_entity *ent, compound_graph_path *path) {
+ int i, n_paths = get_compound_ent_n_values(ent);
+ int *visited_indices;
+ int path_len = get_compound_graph_path_length(path);
+
+ NEW_ARR_A(int *, visited_indices, path_len);
+ memset(visited_indices, 0, sizeof(*visited_indices) * path_len);
+ for (i = 0; i < n_paths; i ++) {
+ if (equal_paths(get_compound_ent_value_path(ent, i), visited_indices, path))
+ return i;
+ }
+
+#if 0
+ {
+ int j;
+ printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
+ printf("Entity %s : ", get_entity_name(ent));
+ for (j = 0; j < get_compound_graph_path_length(path); ++j) {
+ ir_entity *node = get_compound_graph_path_node(path, j);
+ printf("%s", get_entity_name(node));
+ if (is_Array_type(get_entity_owner(node)))
+ printf("[%d]", get_compound_graph_path_array_index(path, j));
+ }
+ printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
+ }
+#endif
+
+ assert(0 && "path not found");
+ return -1;
+} /* get_compound_ent_pos_by_path */
+
+/* Returns a constant value given the access path.
+ * The path must contain array indices for all array element entities. */
+ir_node *get_compound_ent_value_by_path(ir_entity *ent, compound_graph_path *path) {
+ return get_compound_ent_value(ent, get_compound_ent_pos_by_path(ent, path));
+} /* get_compound_ent_value_by_path */
+
+
+void
+remove_compound_ent_value(ir_entity *ent, ir_entity *value_ent) {
+ int i, n;
+ assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
+
+ n = ARR_LEN(ent->attr.cmpd_attr.val_paths);
+ for (i = 0; i < n; ++i) {
+ compound_graph_path *path = ent->attr.cmpd_attr.val_paths[i];
+ if (path->list[path->len-1].node == value_ent) {
+ for (; i < n - 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(ir_entity*, ent->attr.cmpd_attr.val_paths, n - 1);
+ ARR_SETLEN(ir_node*, ent->attr.cmpd_attr.values, n - 1);
+ break;
+ }
+ }
+} /* remove_compound_ent_value */
+
+void
+add_compound_ent_value(ir_entity *ent, ir_node *val, ir_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, n;
+
+ 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, n = get_compound_ent_n_values(ent); i < n; ++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);
+} /* add_compound_ent_value */
+
+
+ir_entity *
+get_compound_ent_value_member(ir_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);
+} /* get_compound_ent_value_member */
+
+void
+set_compound_ent_value(ir_entity *ent, ir_node *val, ir_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);
+} /* set_compound_ent_value */
+
+void
+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 */
+
+int
+(get_entity_offset)(const ir_entity *ent) {
+ return _get_entity_offset(ent);
+} /* get_entity_offset */
+
+void
+(set_entity_offset)(ir_entity *ent, int offset) {
+ _set_entity_offset(ent, offset);
+} /* set_entity_offset */
+
+unsigned char
+(get_entity_offset_bits_remainder)(const ir_entity *ent) {
+ return _get_entity_offset_bits_remainder(ent);
+} /* get_entity_offset_bits_remainder */
+
+void
+(set_entity_offset_bits_remainder)(ir_entity *ent, unsigned char offset) {
+ _set_entity_offset_bits_remainder(ent, offset);
+} /* set_entity_offset_bits_remainder */
+
+void
+add_entity_overwrites(ir_entity *ent, ir_entity *overwritten) {
+#ifndef NDEBUG
+ ir_type *owner = get_entity_owner(ent);
+ ir_type *ovw_ovner = get_entity_owner(overwritten);
+ assert(is_Class_type(owner));
+ assert(is_Class_type(ovw_ovner));
+ assert(! is_class_final(ovw_ovner));
+#endif /* NDEBUG */
+ ARR_APP1(ir_entity *, ent->overwrites, overwritten);
+ ARR_APP1(ir_entity *, overwritten->overwrittenby, ent);
+} /* add_entity_overwrites */
+
+int
+get_entity_n_overwrites(ir_entity *ent) {
+ assert(is_Class_type(get_entity_owner(ent)));
+ return (ARR_LEN(ent->overwrites));
+} /* get_entity_n_overwrites */
+
+int
+get_entity_overwrites_index(ir_entity *ent, ir_entity *overwritten) {
+ int i, n;
+ assert(is_Class_type(get_entity_owner(ent)));
+ n = get_entity_n_overwrites(ent);
+ for (i = 0; i < n; ++i) {
+ if (get_entity_overwrites(ent, i) == overwritten)
+ return i;
+ }
+ return -1;
+} /* get_entity_overwrites_index */
+
+ir_entity *
+get_entity_overwrites(ir_entity *ent, int pos) {
+ assert(is_Class_type(get_entity_owner(ent)));
+ assert(pos < get_entity_n_overwrites(ent));
+ return ent->overwrites[pos];
+} /* get_entity_overwrites */
+
+void
+set_entity_overwrites(ir_entity *ent, int pos, ir_entity *overwritten) {
+ assert(is_Class_type(get_entity_owner(ent)));
+ assert(pos < get_entity_n_overwrites(ent));
+ ent->overwrites[pos] = overwritten;
+} /* set_entity_overwrites */
+
+void
+remove_entity_overwrites(ir_entity *ent, ir_entity *overwritten) {
+ int i, n;
+ assert(is_Class_type(get_entity_owner(ent)));
+ n = ARR_LEN(ent->overwrites);
+ for (i = 0; i < n; ++i) {
+ if (ent->overwrites[i] == overwritten) {
+ for (; i < n - 1; i++)
+ ent->overwrites[i] = ent->overwrites[i+1];
+ ARR_SETLEN(ir_entity*, ent->overwrites, n - 1);
+ break;
+ }
+ }
+} /* remove_entity_overwrites */
+
+void
+add_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites) {
+ add_entity_overwrites(overwrites, ent);
+} /* add_entity_overwrittenby */
+
+int
+get_entity_n_overwrittenby(ir_entity *ent) {
+ assert(is_Class_type(get_entity_owner(ent)));
+ return ARR_LEN(ent->overwrittenby);
+} /* get_entity_n_overwrittenby */
+
+int
+get_entity_overwrittenby_index(ir_entity *ent, ir_entity *overwrites) {
+ int i, n;
+ assert(is_Class_type(get_entity_owner(ent)));
+ n = get_entity_n_overwrittenby(ent);
+ for (i = 0; i < n; ++i) {
+ if (get_entity_overwrittenby(ent, i) == overwrites)
+ return i;
+ }
+ return -1;
+} /* get_entity_overwrittenby_index */
+
+ir_entity *
+get_entity_overwrittenby(ir_entity *ent, int pos) {
+ assert(is_Class_type(get_entity_owner(ent)));
+ assert(pos < get_entity_n_overwrittenby(ent));
+ return ent->overwrittenby[pos];
+} /* get_entity_overwrittenby */
+
+void
+set_entity_overwrittenby(ir_entity *ent, int pos, ir_entity *overwrites) {
+ assert(is_Class_type(get_entity_owner(ent)));
+ assert(pos < get_entity_n_overwrittenby(ent));
+ ent->overwrittenby[pos] = overwrites;
+} /* set_entity_overwrittenby */
+
+void remove_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites) {
+ int i, n;
+ assert(is_Class_type(get_entity_owner(ent)));
+
+ n = ARR_LEN(ent->overwrittenby);
+ for (i = 0; i < n; ++i) {
+ if (ent->overwrittenby[i] == overwrites) {
+ for(; i < n - 1; ++i)
+ ent->overwrittenby[i] = ent->overwrittenby[i+1];
+ ARR_SETLEN(ir_entity*, ent->overwrittenby, n - 1);
+ break;
+ }
+ }
+} /* remove_entity_overwrittenby */