merge kaps
[libfirm] / ir / tr / compound_path.c
1 /*
2  * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @author  Martin Trapp, Christian Schaefer, Goetz Lindenmaier, Michael Beck
23  * @version $Id$
24  */
25 #include "config.h"
26
27 #include <stdlib.h>
28 #include <stdbool.h>
29 #include <assert.h>
30
31 #include "firm_types.h"
32 #include "typerep.h"
33 #include "compound_path_t.h"
34 #include "xmalloc.h"
35 #include "type_t.h"
36 #include "entity_t.h"
37 #include "irgraph_t.h"
38 #include "ircons.h"
39
40 compound_graph_path *new_compound_graph_path(ir_type *tp, size_t length)
41 {
42         compound_graph_path *res;
43
44         assert(is_compound_type(tp) || is_Array_type(tp));
45         assert(length > 0);
46
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;
50         res->tp   = tp;
51         res->len  = length;
52
53         return res;
54 }
55
56 void free_compound_graph_path(compound_graph_path *gr)
57 {
58         assert(gr && is_compound_graph_path(gr));
59         gr->kind = k_BAD;
60         free(gr);
61 }
62
63 int is_compound_graph_path(const void *thing)
64 {
65         return get_kind(thing) == k_ir_compound_graph_path;
66 }
67
68 int is_proper_compound_graph_path(compound_graph_path *gr, size_t pos)
69 {
70         size_t i;
71         ir_entity *node;
72         ir_type *owner = gr->tp;
73
74         for (i = 0; i <= pos; i++) {
75                 node = get_compound_graph_path_node(gr, i);
76                 if (node == NULL)
77                         /* Path not yet complete. */
78                         return 1;
79                 if (get_entity_owner(node) != owner)
80                         return 0;
81                 owner = get_entity_type(node);
82         }
83         if (pos == get_compound_graph_path_length(gr))
84                 if (!is_atomic_type(owner))
85                         return 0;
86                 return 1;
87 }
88
89 size_t get_compound_graph_path_length(const compound_graph_path *gr)
90 {
91         assert(gr && is_compound_graph_path(gr));
92         return gr->len;
93 }
94
95 ir_entity *get_compound_graph_path_node(const compound_graph_path *gr,
96                                         size_t pos)
97 {
98         assert(gr && is_compound_graph_path(gr));
99         assert(pos < gr->len);
100         return gr->list[pos].node;
101 }
102
103 void set_compound_graph_path_node(compound_graph_path *gr, size_t pos,
104                                   ir_entity *node)
105 {
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));
111 }
112
113 long get_compound_graph_path_array_index(const compound_graph_path *gr, size_t pos)
114 {
115         assert(gr && is_compound_graph_path(gr));
116         assert(pos < gr->len);
117         return gr->list[pos].index;
118 }
119
120 void set_compound_graph_path_array_index(compound_graph_path *gr, size_t pos,
121                                          long index)
122 {
123         assert(gr && is_compound_graph_path(gr));
124         assert(pos < gr->len);
125         gr->list[pos].index = index;
126 }
127
128 ir_type *get_compound_graph_path_type(const compound_graph_path *gr)
129 {
130         assert(gr && is_compound_graph_path(gr));
131         return gr->tp;
132 }
133
134 static void allocate_values(ir_entity *ent)
135 {
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);
140         }
141 }
142
143 void add_compound_ent_value_w_path(ir_entity *ent, ir_node *val,
144                                    compound_graph_path *path)
145 {
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);
151 }
152
153 void set_compound_ent_value_w_path(ir_entity *ent, ir_node *val,
154                                    compound_graph_path *path, size_t pos)
155 {
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;
161 }
162
163 compound_graph_path *get_compound_ent_value_path(const ir_entity *ent,
164                                                  size_t pos)
165 {
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];
170 }
171
172 /**
173  * Returns non-zero, if two compound_graph_pathes are equal
174  *
175  * @param path1            the first path
176  * @param path2            the second path
177  */
178 static bool equal_paths(compound_graph_path *path1, compound_graph_path *path2)
179 {
180         size_t i;
181         size_t len1 = get_compound_graph_path_length(path1);
182         size_t len2 = get_compound_graph_path_length(path2);
183
184         if (len2 != len1) return false;
185
186         for (i = 0; i < len1; i++) {
187                 ir_type *tp;
188                 ir_entity *node1 = get_compound_graph_path_node(path1, i);
189                 ir_entity *node2 = get_compound_graph_path_node(path2, i);
190
191                 if (node1 != node2) return false;
192
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)
198                                 return false;
199                 }
200         }
201         return true;
202 }
203
204 /**
205  * Returns the position of a value with the given path.
206  * The path must contain array indices for all array element entities.
207  *
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.
211  */
212 static size_t get_compound_ent_pos_by_path(const ir_entity *ent,
213                                            compound_graph_path *path)
214 {
215         size_t i, n_paths = get_compound_ent_n_values(ent);
216
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))
220                         return i;
221         }
222         return (size_t)-1;
223 }
224
225 ir_node *get_compound_ent_value_by_path(const ir_entity *ent,
226                                         compound_graph_path *path)
227 {
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);
231         return NULL;
232 }
233
234 void remove_compound_ent_value(ir_entity *ent, ir_entity *value_ent)
235 {
236         size_t i, n;
237         assert(is_compound_entity(ent));
238
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];
246                         }
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);
249                         break;
250                 }
251         }
252 }
253
254 void add_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member)
255 {
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)) {
263                 long max;
264                 size_t i, n;
265
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);
270                         if (index > max) {
271                                 max = index;
272                         }
273                 }
274                 path->list[0].index = max + 1;
275         }
276         add_compound_ent_value_w_path(ent, val, path);
277 }
278
279 ir_entity *get_compound_ent_value_member(const ir_entity *ent, size_t pos)
280 {
281         compound_graph_path *path;
282         assert(is_compound_entity(ent));
283         path = get_compound_ent_value_path(ent, pos);
284
285         return get_compound_graph_path_node(path, get_compound_graph_path_length(path)-1);
286 }
287
288 void set_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member,
289                             size_t pos)
290 {
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);
296 }
297
298 void set_array_entity_values(ir_entity *ent, ir_tarval **values, size_t num_vals)
299 {
300         size_t i;
301         ir_type  *arrtp = get_entity_type(ent);
302         ir_node  *val;
303         ir_graph *irg = get_const_code_irg();
304
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
308            size. */
309         assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
310
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);
315         }
316 }
317
318 unsigned get_compound_ent_value_offset_bytes(const ir_entity *ent, size_t pos)
319 {
320         compound_graph_path *path;
321         size_t path_len, i;
322         unsigned offset = 0;
323         ir_type *curr_tp;
324
325         assert(get_type_state(get_entity_type(ent)) == layout_fixed);
326
327         path     = get_compound_ent_value_path(ent, pos);
328         path_len = get_compound_graph_path_length(path);
329         curr_tp  = path->tp;
330
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);
336                         size_t   idx;
337
338                         assert(size > 0);
339                         if (size % align > 0) {
340                                 size += align - (size % align);
341                         }
342                         idx = get_compound_graph_path_array_index(path, i);
343                         assert(idx != (size_t)-1);
344                         offset += size * idx;
345                         curr_tp = elem_type;
346                 } else {
347                         ir_entity *node = get_compound_graph_path_node(path, i);
348                         offset += get_entity_offset(node);
349                         curr_tp = get_entity_type(node);
350                 }
351         }
352
353         return offset;
354 }
355
356 unsigned get_compound_ent_value_offset_bit_remainder(const ir_entity *ent,
357                                                      size_t pos)
358 {
359         compound_graph_path *path;
360         size_t path_len;
361         ir_entity *last_node;
362
363         assert(get_type_state(get_entity_type(ent)) == layout_fixed);
364
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);
368
369         if (last_node == NULL)
370                 return 0;
371
372         return get_entity_offset_bits_remainder(last_node);
373 }
374
375 size_t get_compound_ent_n_values(const ir_entity *ent)
376 {
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);
381 }
382
383 ir_node *get_compound_ent_value(const ir_entity *ent, size_t pos)
384 {
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]);
389 }
390
391 int entity_has_compound_ent_values(const ir_entity *entity)
392 {
393         if (!is_compound_entity(entity))
394                 return 0;
395
396         return entity->attr.cmpd_attr.values != NULL;
397 }