2 * Copyright (C) 1995-2011 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @author Martin Trapp, Christian Schaefer, Goetz Lindenmaier, Michael Beck
31 #include "firm_types.h"
33 #include "compound_path_t.h"
37 #include "irgraph_t.h"
40 compound_graph_path *new_compound_graph_path(ir_type *tp, size_t length)
42 compound_graph_path *res;
44 assert(is_compound_type(tp) || is_Array_type(tp));
47 res = (compound_graph_path*)xmalloc(sizeof(*res) + (length-1) * sizeof(res->list[0]));
48 memset(res, 0, sizeof(*res) + (length-1) * sizeof(res->list[0]));
49 res->kind = k_ir_compound_graph_path;
56 void free_compound_graph_path(compound_graph_path *gr)
58 assert(gr && is_compound_graph_path(gr));
63 int is_compound_graph_path(const void *thing)
65 return get_kind(thing) == k_ir_compound_graph_path;
68 int is_proper_compound_graph_path(compound_graph_path *gr, size_t pos)
72 ir_type *owner = gr->tp;
74 for (i = 0; i <= pos; i++) {
75 node = get_compound_graph_path_node(gr, i);
77 /* Path not yet complete. */
79 if (get_entity_owner(node) != owner)
81 owner = get_entity_type(node);
83 if (pos == get_compound_graph_path_length(gr))
84 if (!is_atomic_type(owner))
89 size_t get_compound_graph_path_length(const compound_graph_path *gr)
91 assert(gr && is_compound_graph_path(gr));
95 ir_entity *get_compound_graph_path_node(const compound_graph_path *gr,
98 assert(gr && is_compound_graph_path(gr));
99 assert(pos < gr->len);
100 return gr->list[pos].node;
103 void set_compound_graph_path_node(compound_graph_path *gr, size_t pos,
106 assert(gr && is_compound_graph_path(gr));
107 assert(pos < gr->len);
108 assert(is_entity(node));
109 gr->list[pos].node = node;
110 assert(is_proper_compound_graph_path(gr, pos));
113 long get_compound_graph_path_array_index(const compound_graph_path *gr, size_t pos)
115 assert(gr && is_compound_graph_path(gr));
116 assert(pos < gr->len);
117 return gr->list[pos].index;
120 void set_compound_graph_path_array_index(compound_graph_path *gr, size_t pos,
123 assert(gr && is_compound_graph_path(gr));
124 assert(pos < gr->len);
125 gr->list[pos].index = index;
128 ir_type *get_compound_graph_path_type(const compound_graph_path *gr)
130 assert(gr && is_compound_graph_path(gr));
134 static void allocate_values(ir_entity *ent)
136 if (ent->attr.cmpd_attr.values == NULL) {
137 ent->attr.cmpd_attr.values = NEW_ARR_F(ir_node*, 0);
138 assert(ent->attr.cmpd_attr.val_paths == NULL);
139 ent->attr.cmpd_attr.val_paths = NEW_ARR_F(compound_graph_path*, 0);
143 void add_compound_ent_value_w_path(ir_entity *ent, ir_node *val,
144 compound_graph_path *path)
146 assert(is_compound_entity(ent));
147 assert(is_compound_graph_path(path));
148 allocate_values(ent);
149 ARR_APP1(ir_node *, ent->attr.cmpd_attr.values, val);
150 ARR_APP1(compound_graph_path *, ent->attr.cmpd_attr.val_paths, path);
153 void set_compound_ent_value_w_path(ir_entity *ent, ir_node *val,
154 compound_graph_path *path, size_t pos)
156 assert(is_compound_entity(ent));
157 assert(is_compound_graph_path(path));
158 assert(pos < ARR_LEN(ent->attr.cmpd_attr.values));
159 ent->attr.cmpd_attr.values[pos] = val;
160 ent->attr.cmpd_attr.val_paths[pos] = path;
163 compound_graph_path *get_compound_ent_value_path(const ir_entity *ent,
166 assert(is_compound_entity(ent));
167 assert(ent->initializer == NULL);
168 assert(pos < ARR_LEN(ent->attr.cmpd_attr.val_paths));
169 return ent->attr.cmpd_attr.val_paths[pos];
173 * Returns non-zero, if two compound_graph_pathes are equal
175 * @param path1 the first path
176 * @param path2 the second path
178 static bool equal_paths(compound_graph_path *path1, compound_graph_path *path2)
181 size_t len1 = get_compound_graph_path_length(path1);
182 size_t len2 = get_compound_graph_path_length(path2);
184 if (len2 != len1) return false;
186 for (i = 0; i < len1; i++) {
188 ir_entity *node1 = get_compound_graph_path_node(path1, i);
189 ir_entity *node2 = get_compound_graph_path_node(path2, i);
191 if (node1 != node2) return false;
193 tp = get_entity_owner(node1);
194 if (is_Array_type(tp)) {
195 size_t index1 = get_compound_graph_path_array_index(path1, i);
196 size_t index2 = get_compound_graph_path_array_index(path2, i);
197 if (index1 != index2)
205 * Returns the position of a value with the given path.
206 * The path must contain array indices for all array element entities.
208 * @todo This implementation is very slow (O(number of initializers * |path|)
209 * and should be replaced when the new tree oriented
210 * value representation is finally implemented.
212 static size_t get_compound_ent_pos_by_path(const ir_entity *ent,
213 compound_graph_path *path)
215 size_t i, n_paths = get_compound_ent_n_values(ent);
217 for (i = 0; i < n_paths; i ++) {
218 compound_graph_path *gr = get_compound_ent_value_path(ent, i);
219 if (equal_paths(gr, path))
225 ir_node *get_compound_ent_value_by_path(const ir_entity *ent,
226 compound_graph_path *path)
228 size_t pos = get_compound_ent_pos_by_path(ent, path);
229 if (pos != (size_t)-1)
230 return get_compound_ent_value(ent, pos);
234 void remove_compound_ent_value(ir_entity *ent, ir_entity *value_ent)
237 assert(is_compound_entity(ent));
239 n = ARR_LEN(ent->attr.cmpd_attr.val_paths);
240 for (i = 0; i < n; ++i) {
241 compound_graph_path *path = ent->attr.cmpd_attr.val_paths[i];
242 if (path->list[path->len-1].node == value_ent) {
243 for (; i < n - 1; ++i) {
244 ent->attr.cmpd_attr.val_paths[i] = ent->attr.cmpd_attr.val_paths[i+1];
245 ent->attr.cmpd_attr.values[i] = ent->attr.cmpd_attr.values[i+1];
247 ARR_SETLEN(compound_graph_path*, ent->attr.cmpd_attr.val_paths, n - 1);
248 ARR_SETLEN(ir_node*, ent->attr.cmpd_attr.values, n - 1);
254 void add_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member)
256 compound_graph_path *path;
257 ir_type *owner_tp = get_entity_owner(member);
258 assert(is_compound_entity(ent));
259 allocate_values(ent);
260 path = new_compound_graph_path(get_entity_type(ent), 1);
261 path->list[0].node = member;
262 if (is_Array_type(owner_tp)) {
266 assert(get_array_n_dimensions(owner_tp) == 1 && has_array_lower_bound(owner_tp, 0));
267 max = get_array_lower_bound_int(owner_tp, 0) -1;
268 for (i = 0, n = get_compound_ent_n_values(ent); i < n; ++i) {
269 long index = get_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0);
274 path->list[0].index = max + 1;
276 add_compound_ent_value_w_path(ent, val, path);
279 ir_entity *get_compound_ent_value_member(const ir_entity *ent, size_t pos)
281 compound_graph_path *path;
282 assert(is_compound_entity(ent));
283 path = get_compound_ent_value_path(ent, pos);
285 return get_compound_graph_path_node(path, get_compound_graph_path_length(path)-1);
288 void set_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member,
291 compound_graph_path *path;
292 assert(is_compound_entity(ent));
293 path = get_compound_ent_value_path(ent, pos);
294 set_compound_graph_path_node(path, 0, member);
295 set_compound_ent_value_w_path(ent, val, path, pos);
298 void set_array_entity_values(ir_entity *ent, ir_tarval **values, size_t num_vals)
301 ir_type *arrtp = get_entity_type(ent);
303 ir_graph *irg = get_const_code_irg();
305 assert(is_Array_type(arrtp));
306 assert(get_array_n_dimensions(arrtp) == 1);
307 /* One bound is sufficient, the number of constant fields makes the
309 assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
311 for (i = 0; i < num_vals; i++) {
312 val = new_r_Const(irg, values[i]);
313 add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
314 set_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0, i);
318 unsigned get_compound_ent_value_offset_bytes(const ir_entity *ent, size_t pos)
320 compound_graph_path *path;
325 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
327 path = get_compound_ent_value_path(ent, pos);
328 path_len = get_compound_graph_path_length(path);
331 for (i = 0; i < path_len; ++i) {
332 if (is_Array_type(curr_tp)) {
333 ir_type *elem_type = get_array_element_type(curr_tp);
334 unsigned size = get_type_size_bytes(elem_type);
335 unsigned align = get_type_alignment_bytes(elem_type);
339 if (size % align > 0) {
340 size += align - (size % align);
342 idx = get_compound_graph_path_array_index(path, i);
343 assert(idx != (size_t)-1);
344 offset += size * idx;
347 ir_entity *node = get_compound_graph_path_node(path, i);
348 offset += get_entity_offset(node);
349 curr_tp = get_entity_type(node);
356 unsigned get_compound_ent_value_offset_bit_remainder(const ir_entity *ent,
359 compound_graph_path *path;
361 ir_entity *last_node;
363 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
365 path = get_compound_ent_value_path(ent, pos);
366 path_len = get_compound_graph_path_length(path);
367 last_node = get_compound_graph_path_node(path, path_len - 1);
369 if (last_node == NULL)
372 return get_entity_offset_bits_remainder(last_node);
375 size_t get_compound_ent_n_values(const ir_entity *ent)
377 assert(ent->initializer == NULL);
378 assert(is_compound_entity(ent));
379 allocate_values((ir_entity*) ent);
380 return ARR_LEN(ent->attr.cmpd_attr.values);
383 ir_node *get_compound_ent_value(const ir_entity *ent, size_t pos)
385 assert(is_compound_entity(ent));
386 assert(ent->initializer == NULL);
387 assert(pos < ARR_LEN(ent->attr.cmpd_attr.values));
388 return skip_Id(ent->attr.cmpd_attr.values[pos]);
391 int entity_has_compound_ent_values(const ir_entity *entity)
393 if (!is_compound_entity(entity))
396 return entity->attr.cmpd_attr.values != NULL;