verify: Clarify assertion message.
[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]       = new_type_segment(IDENT("GlobalType"),   tf_global_type);
83         irp->segment_types[IR_SEGMENT_THREAD_LOCAL] = new_type_segment(IDENT("ThreadLocal"),  tf_tls_type);
84         irp->segment_types[IR_SEGMENT_CONSTRUCTORS] = new_type_segment(IDENT("Constructors"), tf_constructors);
85         irp->segment_types[IR_SEGMENT_DESTRUCTORS]  = new_type_segment(IDENT("Destructors"),  tf_destructors);
86
87         /* The global type is a class, but we cannot derive from it, so set
88            the final property to assist optimizations that checks for it. */
89         set_class_final(irp->segment_types[IR_SEGMENT_GLOBAL], 1);
90
91         irp->const_code_irg             = new_const_code_irg();
92         irp->globals_entity_usage_state = ir_entity_usage_not_computed;
93 #undef IDENT
94 }
95
96 void init_irprog_1(void)
97 {
98         irp = new_incomplete_ir_prog();
99 }
100
101 void init_irprog_2(void)
102 {
103         complete_ir_prog(irp, INITAL_PROG_NAME);
104         ir_init_type(irp);
105         ir_init_entity(irp);
106 }
107
108 ir_prog *new_ir_prog(const char *name)
109 {
110         ir_prog *irp = new_incomplete_ir_prog();
111         complete_ir_prog(irp, name);
112         return irp;
113 }
114
115 void free_ir_prog(void)
116 {
117         if (irp == NULL)
118                 return;
119
120         size_t i;
121         /* must iterate backwards here */
122         for (i = get_irp_n_irgs(); i > 0;)
123                 free_ir_graph(get_irp_irg(--i));
124
125         /* free entities first to avoid entity types being destroyed before
126          * the entities using them */
127         for (i = get_irp_n_types(); i > 0;)
128                 free_type_entities(get_irp_type(--i));
129
130         ir_finish_entity(irp);
131
132         for (i = get_irp_n_types(); i > 0;)
133                 free_type(get_irp_type(--i));
134
135         free_ir_graph(irp->const_code_irg);
136
137         ir_finish_type(irp);
138
139         DEL_ARR_F(irp->graphs);
140         DEL_ARR_F(irp->types);
141
142         DEL_ARR_F(irp->global_asms);
143
144         irp->name           = NULL;
145         irp->const_code_irg = NULL;
146         irp->kind           = k_BAD;
147         free(irp);
148         irp = NULL;
149 }
150
151 ir_graph *get_irp_main_irg(void)
152 {
153         assert(irp);
154         return irp->main_irg;
155 }
156
157 void set_irp_main_irg(ir_graph *main_irg)
158 {
159         assert(irp);
160         irp->main_irg = main_irg;
161 }
162
163 ir_type *(get_segment_type)(ir_segment_t segment)
164 {
165         return get_segment_type_(segment);
166 }
167
168 void set_segment_type(ir_segment_t segment, ir_type *new_type)
169 {
170         assert(segment <= IR_SEGMENT_LAST);
171         irp->segment_types[segment] = new_type;
172 }
173
174 ir_type *(get_glob_type)(void)
175 {
176         return get_glob_type_();
177 }
178
179 ir_type *(get_tls_type)(void)
180 {
181         return get_tls_type_();
182 }
183
184 void add_irp_irg(ir_graph *irg)
185 {
186         assert(irg != NULL);
187         assert(irp && irp->graphs);
188         ARR_APP1(ir_graph *, irp->graphs, irg);
189 }
190
191 void remove_irp_irg(ir_graph *irg)
192 {
193         size_t i, l;
194
195         assert(irg);
196         l = ARR_LEN(irp->graphs);
197         for (i = 0; i < l; ++i) {
198                 if (irp->graphs[i] == irg) {
199                         for (; i < l - 1; ++i) {
200                                 irp->graphs[i] = irp->graphs[i+1];
201                         }
202                         ARR_SETLEN(ir_graph*, irp->graphs, l - 1);
203                         break;
204                 }
205         }
206 }
207
208 size_t (get_irp_n_irgs)(void)
209 {
210         return get_irp_n_irgs_();
211 }
212
213 ir_graph *(get_irp_irg)(size_t pos)
214 {
215         return get_irp_irg_(pos);
216 }
217
218 size_t get_irp_last_idx(void)
219 {
220         return irp->max_irg_idx;
221 }
222
223 void set_irp_irg(size_t pos, ir_graph *irg)
224 {
225         assert(irp && irg);
226         assert(pos < ARR_LEN(irp->graphs));
227         irp->graphs[pos] = irg;
228 }
229
230 void add_irp_type(ir_type *typ)
231 {
232         assert(typ != NULL);
233         assert(irp);
234         ARR_APP1(ir_type *, irp->types, typ);
235 }
236
237 void remove_irp_type(ir_type *typ)
238 {
239         size_t i, l;
240         assert(typ);
241
242         l = ARR_LEN(irp->types);
243         for (i = 0; i < l; ++i) {
244                 if (irp->types[i] == typ) {
245                         for (; i < l - 1; ++i) {
246                                 irp->types[i] = irp->types[i+1];
247                         }
248                         ARR_SETLEN(ir_type *, irp->types, l - 1);
249                         break;
250                 }
251         }
252 }
253
254 size_t (get_irp_n_types) (void)
255 {
256         return get_irp_n_types_();
257 }
258
259 ir_type *(get_irp_type) (size_t pos)
260 {
261         return get_irp_type_(pos);
262 }
263
264 void set_irp_type(size_t pos, ir_type *typ)
265 {
266         assert(irp && typ);
267         assert(pos < ARR_LEN((irp)->types));
268         irp->types[pos] = typ;
269 }
270
271 void set_irp_prog_name(ident *name)
272 {
273         irp->name = name;
274 }
275 int irp_prog_name_is_set(void)
276 {
277         return irp->name != new_id_from_str(INITAL_PROG_NAME);
278 }
279 ident *get_irp_ident(void)
280 {
281         return irp->name;
282 }
283 const char  *get_irp_name(void)
284 {
285         return get_id_str(irp->name);
286 }
287
288
289 ir_graph *(get_const_code_irg)(void)
290 {
291         return get_const_code_irg_();
292 }
293
294 void set_irp_ip_outedges(ir_node ** ip_outedges)
295 {
296         irp->ip_outedges = ip_outedges;
297 }
298
299 ir_node** get_irp_ip_outedges(void)
300 {
301         return irp->ip_outedges;
302 }
303
304 irg_callee_info_state get_irp_callee_info_state(void)
305 {
306         return irp->callee_info_state;
307 }
308
309 void set_irp_callee_info_state(irg_callee_info_state s)
310 {
311         irp->callee_info_state = s;
312 }
313
314 ir_label_t (get_irp_next_label_nr)(void)
315 {
316         return get_irp_next_label_nr_();
317 }
318
319 void add_irp_asm(ident *asm_string)
320 {
321         ARR_APP1(ident *, irp->global_asms, asm_string);
322 }
323
324 size_t get_irp_n_asms(void)
325 {
326         return ARR_LEN(irp->global_asms);
327 }
328
329 ident *get_irp_asm(size_t pos)
330 {
331         assert(pos < get_irp_n_asms());
332         return irp->global_asms[pos];
333 }
334
335 #ifndef NDEBUG
336 void irp_reserve_resources(ir_prog *irp, irp_resources_t resources)
337 {
338         assert((irp->reserved_resources & resources) == 0);
339         irp->reserved_resources |= resources;
340 }
341
342 void irp_free_resources(ir_prog *irp, irp_resources_t resources)
343 {
344         assert((irp->reserved_resources & resources) == resources);
345         irp->reserved_resources &= ~resources;
346 }
347
348 irp_resources_t irp_resources_reserved(const ir_prog *irp)
349 {
350         return irp->reserved_resources;
351 }
352 #endif