cleanup: Use get_Block_n_cfgpreds()/get_Block_cfgpred() instead of get_irn_arity...
[libfirm] / ir / ir / irprog.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  * @brief    Entry point to the representation of a whole program.
23  * @author   Goetz Lindenmaier, Michael Beck
24  * @date     2000
25  */
26 #include "config.h"
27
28 #include <string.h>
29
30 #include "irprog_t.h"
31 #include "irgraph_t.h"
32 #include "irpass_t.h"
33 #include "array.h"
34 #include "error.h"
35 #include "obst.h"
36 #include "irop_t.h"
37 #include "irmemory.h"
38 #include "ircons.h"
39
40 /** The initial name of the irp program. */
41 #define INITAL_PROG_NAME "no_name_set"
42
43 ir_prog *irp;
44 ir_prog *get_irp(void) { return irp; }
45 void set_irp(ir_prog *new_irp)
46 {
47         irp = new_irp;
48 }
49
50 /**
51  *  Create a new incomplete ir_prog.
52  */
53 static ir_prog *new_incomplete_ir_prog(void)
54 {
55         ir_prog *res = XMALLOCZ(ir_prog);
56
57         res->kind           = k_ir_prog;
58         res->graphs         = NEW_ARR_F(ir_graph *, 0);
59         res->types          = NEW_ARR_F(ir_type *, 0);
60         res->global_asms    = NEW_ARR_F(ident *, 0);
61         res->last_label_nr  = 1;  /* 0 is reserved as non-label */
62         res->max_irg_idx    = 0;
63         res->max_node_nr    = 0;
64 #ifndef NDEBUG
65         res->reserved_resources = IRP_RESOURCE_NONE;
66 #endif
67
68         return res;
69 }
70
71 /**
72  * Completes an incomplete irprog.
73  *
74  * @param irp          the (yet incomplete) irp
75  * @param module_name  the (module) name for this irp
76  */
77 static void complete_ir_prog(ir_prog *irp, const char *module_name)
78 {
79 #define IDENT(x)  new_id_from_chars(x, sizeof(x) - 1)
80
81         irp->name = new_id_from_str(module_name);
82         irp->segment_types[IR_SEGMENT_GLOBAL]
83                 = new_type_class(IDENT("GlobalType"));
84         irp->segment_types[IR_SEGMENT_THREAD_LOCAL]
85                 = new_type_struct(IDENT("ThreadLocal"));
86         irp->segment_types[IR_SEGMENT_CONSTRUCTORS]
87                 = new_type_class(IDENT("Constructors"));
88         irp->segment_types[IR_SEGMENT_DESTRUCTORS]
89                 = new_type_class(IDENT("Destructors"));
90
91         /* Set these flags for debugging. */
92         irp->segment_types[IR_SEGMENT_GLOBAL]->flags       |= tf_segment|tf_global_type;
93         irp->segment_types[IR_SEGMENT_THREAD_LOCAL]->flags |= tf_segment|tf_tls_type;
94         irp->segment_types[IR_SEGMENT_CONSTRUCTORS]->flags |= tf_segment|tf_constructors;
95         irp->segment_types[IR_SEGMENT_DESTRUCTORS]->flags  |= tf_segment|tf_destructors;
96
97         /* The global type is a class, but we cannot derive from it, so set
98            the final property to assist optimizations that checks for it. */
99         set_class_final(irp->segment_types[IR_SEGMENT_GLOBAL], 1);
100
101         irp->const_code_irg             = new_const_code_irg();
102         irp->class_cast_state           = ir_class_casts_transitive;
103         irp->globals_entity_usage_state = ir_entity_usage_not_computed;
104 #undef IDENT
105 }
106
107 void init_irprog_1(void)
108 {
109         irp = new_incomplete_ir_prog();
110 }
111
112 void init_irprog_2(void)
113 {
114         complete_ir_prog(irp, INITAL_PROG_NAME);
115         ir_init_type(irp);
116         ir_init_entity(irp);
117 }
118
119 ir_prog *new_ir_prog(const char *name)
120 {
121         ir_prog *irp = new_incomplete_ir_prog();
122         complete_ir_prog(irp, name);
123         return irp;
124 }
125
126 void free_ir_prog(void)
127 {
128         if (irp == NULL)
129                 return;
130
131         size_t i;
132         /* must iterate backwards here */
133         for (i = get_irp_n_irgs(); i > 0;)
134                 free_ir_graph(get_irp_irg(--i));
135
136         /* free entities first to avoid entity types being destroyed before
137          * the entities using them */
138         for (i = get_irp_n_types(); i > 0;)
139                 free_type_entities(get_irp_type(--i));
140
141         ir_finish_entity(irp);
142
143         for (i = get_irp_n_types(); i > 0;)
144                 free_type(get_irp_type(--i));
145
146         free_ir_graph(irp->const_code_irg);
147
148         ir_finish_type(irp);
149
150         DEL_ARR_F(irp->graphs);
151         DEL_ARR_F(irp->types);
152
153         DEL_ARR_F(irp->global_asms);
154
155         irp->name           = NULL;
156         irp->const_code_irg = NULL;
157         irp->kind           = k_BAD;
158         free(irp);
159         irp = NULL;
160 }
161
162 ir_graph *get_irp_main_irg(void)
163 {
164         assert(irp);
165         return irp->main_irg;
166 }
167
168 void set_irp_main_irg(ir_graph *main_irg)
169 {
170         assert(irp);
171         irp->main_irg = main_irg;
172 }
173
174 ir_type *(get_segment_type)(ir_segment_t segment)
175 {
176         return get_segment_type_(segment);
177 }
178
179 void set_segment_type(ir_segment_t segment, ir_type *new_type)
180 {
181         assert(segment <= IR_SEGMENT_LAST);
182         irp->segment_types[segment] = new_type;
183 }
184
185 ir_type *(get_glob_type)(void)
186 {
187         return get_glob_type_();
188 }
189
190 ir_type *(get_tls_type)(void)
191 {
192         return get_tls_type_();
193 }
194
195 void add_irp_irg(ir_graph *irg)
196 {
197         assert(irg != NULL);
198         assert(irp && irp->graphs);
199         ARR_APP1(ir_graph *, irp->graphs, irg);
200 }
201
202 void remove_irp_irg(ir_graph *irg)
203 {
204         size_t i, l;
205
206         assert(irg);
207         l = ARR_LEN(irp->graphs);
208         for (i = 0; i < l; ++i) {
209                 if (irp->graphs[i] == irg) {
210                         for (; i < l - 1; ++i) {
211                                 irp->graphs[i] = irp->graphs[i+1];
212                         }
213                         ARR_SETLEN(ir_graph*, irp->graphs, l - 1);
214                         break;
215                 }
216         }
217 }
218
219 size_t (get_irp_n_irgs)(void)
220 {
221         return get_irp_n_irgs_();
222 }
223
224 ir_graph *(get_irp_irg)(size_t pos)
225 {
226         return get_irp_irg_(pos);
227 }
228
229 size_t get_irp_last_idx(void)
230 {
231         return irp->max_irg_idx;
232 }
233
234 void set_irp_irg(size_t pos, ir_graph *irg)
235 {
236         assert(irp && irg);
237         assert(pos < ARR_LEN(irp->graphs));
238         irp->graphs[pos] = irg;
239 }
240
241 void add_irp_type(ir_type *typ)
242 {
243         assert(typ != NULL);
244         assert(irp);
245         ARR_APP1(ir_type *, irp->types, typ);
246 }
247
248 void remove_irp_type(ir_type *typ)
249 {
250         size_t i, l;
251         assert(typ);
252
253         l = ARR_LEN(irp->types);
254         for (i = 0; i < l; ++i) {
255                 if (irp->types[i] == typ) {
256                         for (; i < l - 1; ++i) {
257                                 irp->types[i] = irp->types[i+1];
258                         }
259                         ARR_SETLEN(ir_type *, irp->types, l - 1);
260                         break;
261                 }
262         }
263 }
264
265 size_t (get_irp_n_types) (void)
266 {
267         return get_irp_n_types_();
268 }
269
270 ir_type *(get_irp_type) (size_t pos)
271 {
272         return get_irp_type_(pos);
273 }
274
275 void set_irp_type(size_t pos, ir_type *typ)
276 {
277         assert(irp && typ);
278         assert(pos < ARR_LEN((irp)->types));
279         irp->types[pos] = typ;
280 }
281
282 void set_irp_prog_name(ident *name)
283 {
284         irp->name = name;
285 }
286 int irp_prog_name_is_set(void)
287 {
288         return irp->name != new_id_from_str(INITAL_PROG_NAME);
289 }
290 ident *get_irp_ident(void)
291 {
292         return irp->name;
293 }
294 const char  *get_irp_name(void)
295 {
296         return get_id_str(irp->name);
297 }
298
299
300 ir_graph *(get_const_code_irg)(void)
301 {
302         return get_const_code_irg_();
303 }
304
305 void set_irp_ip_outedges(ir_node ** ip_outedges)
306 {
307         irp->ip_outedges = ip_outedges;
308 }
309
310 ir_node** get_irp_ip_outedges(void)
311 {
312         return irp->ip_outedges;
313 }
314
315 irg_callee_info_state get_irp_callee_info_state(void)
316 {
317         return irp->callee_info_state;
318 }
319
320 void set_irp_callee_info_state(irg_callee_info_state s)
321 {
322         irp->callee_info_state = s;
323 }
324
325 ir_label_t (get_irp_next_label_nr)(void)
326 {
327         return get_irp_next_label_nr_();
328 }
329
330 void add_irp_asm(ident *asm_string)
331 {
332         ARR_APP1(ident *, irp->global_asms, asm_string);
333 }
334
335 size_t get_irp_n_asms(void)
336 {
337         return ARR_LEN(irp->global_asms);
338 }
339
340 ident *get_irp_asm(size_t pos)
341 {
342         assert(pos < get_irp_n_asms());
343         return irp->global_asms[pos];
344 }
345
346 #ifndef NDEBUG
347 void irp_reserve_resources(ir_prog *irp, irp_resources_t resources)
348 {
349         assert((irp->reserved_resources & resources) == 0);
350         irp->reserved_resources |= resources;
351 }
352
353 void irp_free_resources(ir_prog *irp, irp_resources_t resources)
354 {
355         assert((irp->reserved_resources & resources) == resources);
356         irp->reserved_resources &= ~resources;
357 }
358
359 irp_resources_t irp_resources_reserved(const ir_prog *irp)
360 {
361         return irp->reserved_resources;
362 }
363 #endif