Move current_ir_graph from ir_graph to ir_cons
[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  */
24 #include "config.h"
25
26 #include <stdlib.h>
27 #include <stdbool.h>
28 #include <assert.h>
29
30 #include "firm_types.h"
31 #include "typerep.h"
32 #include "compound_path_t.h"
33 #include "xmalloc.h"
34 #include "type_t.h"
35 #include "entity_t.h"
36 #include "irgraph_t.h"
37 #include "ircons.h"
38
39 compound_graph_path *new_compound_graph_path(ir_type *tp, size_t length)
40 {
41         compound_graph_path *res;
42
43         assert(is_compound_type(tp) || is_Array_type(tp));
44         assert(length > 0);
45
46         res = XMALLOCFZ(compound_graph_path, list, length);
47         res->kind = k_ir_compound_graph_path;
48         res->tp   = tp;
49         res->len  = length;
50
51         return res;
52 }
53
54 void free_compound_graph_path(compound_graph_path *gr)
55 {
56         assert(gr && is_compound_graph_path(gr));
57         gr->kind = k_BAD;
58         free(gr);
59 }
60
61 int is_compound_graph_path(const void *thing)
62 {
63         return get_kind(thing) == k_ir_compound_graph_path;
64 }
65
66 int is_proper_compound_graph_path(compound_graph_path *gr, size_t pos)
67 {
68         size_t i;
69         ir_entity *node;
70         ir_type *owner = gr->tp;
71
72         for (i = 0; i <= pos; i++) {
73                 node = get_compound_graph_path_node(gr, i);
74                 if (node == NULL)
75                         /* Path not yet complete. */
76                         return 1;
77                 if (get_entity_owner(node) != owner)
78                         return 0;
79                 owner = get_entity_type(node);
80         }
81         if (pos == get_compound_graph_path_length(gr))
82                 if (!is_atomic_type(owner))
83                         return 0;
84                 return 1;
85 }
86
87 size_t get_compound_graph_path_length(const compound_graph_path *gr)
88 {
89         assert(gr && is_compound_graph_path(gr));
90         return gr->len;
91 }
92
93 ir_entity *get_compound_graph_path_node(const compound_graph_path *gr,
94                                         size_t pos)
95 {
96         assert(gr && is_compound_graph_path(gr));
97         assert(pos < gr->len);
98         return gr->list[pos].node;
99 }
100
101 void set_compound_graph_path_node(compound_graph_path *gr, size_t pos,
102                                   ir_entity *node)
103 {
104         assert(gr && is_compound_graph_path(gr));
105         assert(pos < gr->len);
106         assert(is_entity(node));
107         gr->list[pos].node = node;
108         assert(is_proper_compound_graph_path(gr, pos));
109 }
110
111 long get_compound_graph_path_array_index(const compound_graph_path *gr, size_t pos)
112 {
113         assert(gr && is_compound_graph_path(gr));
114         assert(pos < gr->len);
115         return gr->list[pos].index;
116 }
117
118 void set_compound_graph_path_array_index(compound_graph_path *gr, size_t pos,
119                                          long index)
120 {
121         assert(gr && is_compound_graph_path(gr));
122         assert(pos < gr->len);
123         gr->list[pos].index = index;
124 }
125
126 ir_type *get_compound_graph_path_type(const compound_graph_path *gr)
127 {
128         assert(gr && is_compound_graph_path(gr));
129         return gr->tp;
130 }
131
132 static void allocate_values(ir_entity *ent)
133 {
134         if (ent->attr.cmpd_attr.values == NULL) {
135                 ent->attr.cmpd_attr.values = NEW_ARR_F(ir_node*, 0);
136                 assert(ent->attr.cmpd_attr.val_paths == NULL);
137                 ent->attr.cmpd_attr.val_paths = NEW_ARR_F(compound_graph_path*, 0);
138         }
139 }
140
141 void add_compound_ent_value_w_path(ir_entity *ent, ir_node *val,
142                                    compound_graph_path *path)
143 {
144         assert(is_compound_entity(ent));
145         assert(is_compound_graph_path(path));
146         allocate_values(ent);
147         ARR_APP1(ir_node *, ent->attr.cmpd_attr.values, val);
148         ARR_APP1(compound_graph_path *, ent->attr.cmpd_attr.val_paths, path);
149 }
150
151 void set_compound_ent_value_w_path(ir_entity *ent, ir_node *val,
152                                    compound_graph_path *path, size_t pos)
153 {
154         assert(is_compound_entity(ent));
155         assert(is_compound_graph_path(path));
156         assert(pos < ARR_LEN(ent->attr.cmpd_attr.values));
157         ent->attr.cmpd_attr.values[pos]    = val;
158         ent->attr.cmpd_attr.val_paths[pos] = path;
159 }
160
161 compound_graph_path *get_compound_ent_value_path(const ir_entity *ent,
162                                                  size_t pos)
163 {
164         assert(is_compound_entity(ent));
165         assert(ent->initializer == NULL);
166         assert(pos < ARR_LEN(ent->attr.cmpd_attr.val_paths));
167         return ent->attr.cmpd_attr.val_paths[pos];
168 }
169
170 /**
171  * Returns non-zero, if two compound_graph_pathes are equal
172  *
173  * @param path1            the first path
174  * @param path2            the second path
175  */
176 static bool equal_paths(compound_graph_path *path1, compound_graph_path *path2)
177 {
178         size_t i;
179         size_t len1 = get_compound_graph_path_length(path1);
180         size_t len2 = get_compound_graph_path_length(path2);
181
182         if (len2 != len1) return false;
183
184         for (i = 0; i < len1; i++) {
185                 ir_type *tp;
186                 ir_entity *node1 = get_compound_graph_path_node(path1, i);
187                 ir_entity *node2 = get_compound_graph_path_node(path2, i);
188
189                 if (node1 != node2) return false;
190
191                 tp = get_entity_owner(node1);
192                 if (is_Array_type(tp)) {
193                         size_t index1 = get_compound_graph_path_array_index(path1, i);
194                         size_t index2 = get_compound_graph_path_array_index(path2, i);
195                         if (index1 != index2)
196                                 return false;
197                 }
198         }
199         return true;
200 }
201
202 /**
203  * Returns the position of a value with the given path.
204  * The path must contain array indices for all array element entities.
205  *
206  * @todo  This implementation is very slow (O(number of initializers * |path|)
207  *        and should be replaced when the new tree oriented
208  *        value representation is finally implemented.
209  */
210 static size_t get_compound_ent_pos_by_path(const ir_entity *ent,
211                                            compound_graph_path *path)
212 {
213         size_t i, n_paths = get_compound_ent_n_values(ent);
214
215         for (i = 0; i < n_paths; i ++) {
216                 compound_graph_path *gr = get_compound_ent_value_path(ent, i);
217                 if (equal_paths(gr, path))
218                         return i;
219         }
220         return (size_t)-1;
221 }
222
223 ir_node *get_compound_ent_value_by_path(const ir_entity *ent,
224                                         compound_graph_path *path)
225 {
226         size_t pos = get_compound_ent_pos_by_path(ent, path);
227         if (pos != (size_t)-1)
228                 return get_compound_ent_value(ent, pos);
229         return NULL;
230 }
231
232 void remove_compound_ent_value(ir_entity *ent, ir_entity *value_ent)
233 {
234         size_t i, n;
235         assert(is_compound_entity(ent));
236
237         n = ARR_LEN(ent->attr.cmpd_attr.val_paths);
238         for (i = 0; i < n; ++i) {
239                 compound_graph_path *path = ent->attr.cmpd_attr.val_paths[i];
240                 if (path->list[path->len-1].node == value_ent) {
241                         for (; i < n - 1; ++i) {
242                                 ent->attr.cmpd_attr.val_paths[i] = ent->attr.cmpd_attr.val_paths[i+1];
243                                 ent->attr.cmpd_attr.values[i]    = ent->attr.cmpd_attr.values[i+1];
244                         }
245                         ARR_SETLEN(compound_graph_path*, ent->attr.cmpd_attr.val_paths, n - 1);
246                         ARR_SETLEN(ir_node*,             ent->attr.cmpd_attr.values,    n - 1);
247                         break;
248                 }
249         }
250 }
251
252 void add_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member)
253 {
254         compound_graph_path *path;
255         ir_type *owner_tp = get_entity_owner(member);
256         assert(is_compound_entity(ent));
257         allocate_values(ent);
258         path = new_compound_graph_path(get_entity_type(ent), 1);
259         path->list[0].node = member;
260         if (is_Array_type(owner_tp)) {
261                 long max;
262                 size_t i, n;
263
264                 assert(get_array_n_dimensions(owner_tp) == 1 && has_array_lower_bound(owner_tp, 0));
265                 max = get_array_lower_bound_int(owner_tp, 0) -1;
266                 for (i = 0, n = get_compound_ent_n_values(ent); i < n; ++i) {
267                         long index = get_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0);
268                         if (index > max) {
269                                 max = index;
270                         }
271                 }
272                 path->list[0].index = max + 1;
273         }
274         add_compound_ent_value_w_path(ent, val, path);
275 }
276
277 ir_entity *get_compound_ent_value_member(const ir_entity *ent, size_t pos)
278 {
279         compound_graph_path *path;
280         assert(is_compound_entity(ent));
281         path = get_compound_ent_value_path(ent, pos);
282
283         return get_compound_graph_path_node(path, get_compound_graph_path_length(path)-1);
284 }
285
286 void set_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member,
287                             size_t pos)
288 {
289         compound_graph_path *path;
290         assert(is_compound_entity(ent));
291         path = get_compound_ent_value_path(ent, pos);
292         set_compound_graph_path_node(path, 0, member);
293         set_compound_ent_value_w_path(ent, val, path, pos);
294 }
295
296 void set_array_entity_values(ir_entity *ent, ir_tarval **values, size_t num_vals)
297 {
298         size_t i;
299         ir_type  *arrtp = get_entity_type(ent);
300         ir_node  *val;
301         ir_graph *irg = get_const_code_irg();
302
303         assert(is_Array_type(arrtp));
304         assert(get_array_n_dimensions(arrtp) == 1);
305         /* One bound is sufficient, the number of constant fields makes the
306            size. */
307         assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
308
309         for (i = 0; i < num_vals; i++) {
310                 val = new_r_Const(irg, values[i]);
311                 add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
312                 set_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0, i);
313         }
314 }
315
316 unsigned get_compound_ent_value_offset_bytes(const ir_entity *ent, size_t pos)
317 {
318         compound_graph_path *path;
319         size_t path_len, i;
320         unsigned offset = 0;
321         ir_type *curr_tp;
322
323         assert(get_type_state(get_entity_type(ent)) == layout_fixed);
324
325         path     = get_compound_ent_value_path(ent, pos);
326         path_len = get_compound_graph_path_length(path);
327         curr_tp  = path->tp;
328
329         for (i = 0; i < path_len; ++i) {
330                 if (is_Array_type(curr_tp)) {
331                         ir_type *elem_type = get_array_element_type(curr_tp);
332                         unsigned size      = get_type_size_bytes(elem_type);
333                         unsigned align     = get_type_alignment_bytes(elem_type);
334                         size_t   idx;
335
336                         assert(size > 0);
337                         if (size % align > 0) {
338                                 size += align - (size % align);
339                         }
340                         idx = get_compound_graph_path_array_index(path, i);
341                         assert(idx != (size_t)-1);
342                         offset += size * idx;
343                         curr_tp = elem_type;
344                 } else {
345                         ir_entity *node = get_compound_graph_path_node(path, i);
346                         offset += get_entity_offset(node);
347                         curr_tp = get_entity_type(node);
348                 }
349         }
350
351         return offset;
352 }
353
354 unsigned get_compound_ent_value_offset_bit_remainder(const ir_entity *ent,
355                                                      size_t pos)
356 {
357         compound_graph_path *path;
358         size_t path_len;
359         ir_entity *last_node;
360
361         assert(get_type_state(get_entity_type(ent)) == layout_fixed);
362
363         path      = get_compound_ent_value_path(ent, pos);
364         path_len  = get_compound_graph_path_length(path);
365         last_node = get_compound_graph_path_node(path, path_len - 1);
366
367         if (last_node == NULL)
368                 return 0;
369
370         return get_entity_offset_bits_remainder(last_node);
371 }
372
373 size_t get_compound_ent_n_values(const ir_entity *ent)
374 {
375         assert(ent->initializer == NULL);
376         assert(is_compound_entity(ent));
377         allocate_values((ir_entity*) ent);
378         return ARR_LEN(ent->attr.cmpd_attr.values);
379 }
380
381 ir_node *get_compound_ent_value(const ir_entity *ent, size_t pos)
382 {
383         assert(is_compound_entity(ent));
384         assert(ent->initializer == NULL);
385         assert(pos < ARR_LEN(ent->attr.cmpd_attr.values));
386         return skip_Id(ent->attr.cmpd_attr.values[pos]);
387 }
388
389 int entity_has_compound_ent_values(const ir_entity *entity)
390 {
391         if (!is_compound_entity(entity))
392                 return 0;
393
394         return entity->attr.cmpd_attr.values != NULL;
395 }