-/* Creates a new compound graph path. */
-compound_graph_path *
-new_compound_graph_path(ir_type *tp, int length) {
- compound_graph_path *res;
-
- assert(is_compound_type(tp));
- assert(length > 0);
-
- res = xmalloc(sizeof(*res) + (length-1) * sizeof(res->list[0]));
- memset(res, 0, sizeof(*res) + (length-1) * sizeof(res->list[0]));
- res->kind = k_ir_compound_graph_path;
- res->tp = tp;
- res->len = length;
-
- return res;
-} /* new_compound_graph_path */
-
-/* Frees an graph path object */
-void free_compound_graph_path (compound_graph_path *gr) {
- assert(gr && is_compound_graph_path(gr));
- gr->kind = k_BAD;
- free(gr);
-} /* free_compound_graph_path */
-
-/* Returns non-zero if an object is a compound graph path */
-int is_compound_graph_path(const void *thing) {
- return (get_kind(thing) == k_ir_compound_graph_path);
-} /* is_compound_graph_path */
-
-/* Checks whether the path up to pos is correct. If the path contains a NULL,
- * assumes the path is not complete and returns 'true'. */
-int is_proper_compound_graph_path(compound_graph_path *gr, int pos) {
- int i;
- ir_entity *node;
- ir_type *owner = gr->tp;
-
- for (i = 0; i <= pos; i++) {
- node = get_compound_graph_path_node(gr, i);
- if (node == NULL)
- /* Path not yet complete. */
- return 1;
- if (get_entity_owner(node) != owner)
- return 0;
- owner = get_entity_type(node);
- }
- if (pos == get_compound_graph_path_length(gr))
- if (!is_atomic_type(owner))
- return 0;
- return 1;
-} /* is_proper_compound_graph_path */
-
-/* Returns the length of a graph path */
-int get_compound_graph_path_length(const compound_graph_path *gr) {
- assert(gr && is_compound_graph_path(gr));
- return gr->len;
-} /* get_compound_graph_path_length */
-
-ir_entity *
-get_compound_graph_path_node(const compound_graph_path *gr, int pos) {
- assert(gr && is_compound_graph_path(gr));
- assert(pos >= 0 && pos < gr->len);
- return gr->list[pos].node;
-} /* get_compound_graph_path_node */
-
-void
-set_compound_graph_path_node(compound_graph_path *gr, int pos, ir_entity *node) {
- assert(gr && is_compound_graph_path(gr));
- assert(pos >= 0 && pos < gr->len);
- assert(is_entity(node));
- gr->list[pos].node = node;
- assert(is_proper_compound_graph_path(gr, pos));
-} /* set_compound_graph_path_node */
-
-int
-get_compound_graph_path_array_index(const compound_graph_path *gr, int pos) {
- assert(gr && is_compound_graph_path(gr));
- assert(pos >= 0 && pos < gr->len);
- return gr->list[pos].index;
-} /* get_compound_graph_path_array_index */
-
-void
-set_compound_graph_path_array_index(compound_graph_path *gr, int pos, int index) {
- assert(gr && is_compound_graph_path(gr));
- assert(pos >= 0 && pos < gr->len);
- gr->list[pos].index = index;
-} /* set_compound_graph_path_array_index */
-
-ir_type *
-get_compound_graph_path_type(const compound_graph_path *gr) {
- assert(gr && is_compound_graph_path(gr));
- return gr->tp;
-}
-
-/* A value of a compound entity is a pair of value and the corresponding path to a member of
- the compound. */
-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(!ent->has_initializer);
- 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(!ent->has_initializer);
- assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
- return skip_Id(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(!ent->has_initializer);
- 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 path2 the second path
- */
-static int equal_paths(compound_graph_path *path1, 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;
-
- tp = get_entity_owner(node1);
- if (is_Array_type(tp)) {
- int index1 = get_compound_graph_path_array_index(path1, i);
- int index2 = get_compound_graph_path_array_index(path2, i);
- if (index1 != index2)
- return 0;
- }
- }
- 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 * |path|)
- * 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);
-
- for (i = 0; i < n_paths; i ++) {
- compound_graph_path *gr = get_compound_ent_value_path(ent, i);
- if (equal_paths(gr, path))
- return i;
- }
- 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) {
- int pos = get_compound_ent_pos_by_path(ent, path);
- if (pos >= 0)
- return get_compound_ent_value(ent, pos);
- return NULL;
-} /* 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. */
-unsigned get_compound_ent_value_offset_bytes(ir_entity *ent, int pos) {
- compound_graph_path *path;
- int path_len, i;
- unsigned 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) {
- if (is_Array_type(curr_tp)) {
- ir_type *elem_type = get_array_element_type(curr_tp);
- unsigned size = get_type_size_bytes(elem_type);
- unsigned align = get_type_alignment_bytes(elem_type);
- int idx;
-
- assert(size > 0);
- if(size % align > 0) {
- size += align - (size % align);
- }
- idx = get_compound_graph_path_array_index(path, i);
- assert(idx >= 0);
- offset += size * idx;
- curr_tp = elem_type;
- } else {
- ir_entity *node = get_compound_graph_path_node(path, i);
- offset += get_entity_offset(node);
- curr_tp = get_entity_type(node);
- }
- }
-
- return offset;
-} /* get_compound_ent_value_offset_bytes */
-
-/* Return the offset in bits from the last byte address. */
-unsigned 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);
-
- if(last_node == NULL)
- return 0;
-
- return get_entity_offset_bits_remainder(last_node);
-} /* get_compound_ent_value_offset_bit_remainder */
-
-int
-(get_entity_offset)(const ir_entity *ent) {