8d475de44c62d5de96cb18cef08f847797c40fb7
[libfirm] / ir / ir / irprog.c
1 /*
2  * Copyright (C) 1995-2008 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  * @version  $Id$
26  */
27 #include "config.h"
28
29 #ifdef HAVE_STRING_H
30 # include <string.h>
31 #endif
32
33 #include "irprog_t.h"
34 #include "irgraph_t.h"
35 #include "pseudo_irg.h"
36 #include "array.h"
37 #include "error.h"
38 #include "obst.h"
39 #include "irop_t.h"
40 #include "irmemory.h"
41
42 /** The initial name of the irp program. */
43 #define INITAL_PROG_NAME "no_name_set"
44
45 /* A variable from where everything in the ir can be accessed. */
46 ir_prog *irp;
47 ir_prog *get_irp(void) { return irp; }
48
49 /**
50  *  Create a new incomplete ir_prog.
51  */
52 static ir_prog *new_incomplete_ir_prog(void)
53 {
54         ir_prog *res = XMALLOCZ(ir_prog);
55
56         res->kind           = k_ir_prog;
57         res->graphs         = NEW_ARR_F(ir_graph *, 0);
58         res->pseudo_graphs  = NEW_ARR_F(ir_graph *, 0);
59         res->types          = NEW_ARR_F(ir_type *, 0);
60         res->modes          = NEW_ARR_F(ir_mode *, 0);
61         res->opcodes        = NEW_ARR_F(ir_op *, 0);
62         res->global_asms    = NEW_ARR_F(ident *, 0);
63         res->last_region_nr = 0;
64         res->last_label_nr  = 1;  /* 0 is reserved as non-label */
65         res->max_irg_idx    = 0;
66
67 #ifdef DEBUG_libfirm
68         res->max_node_nr    = 0;
69 #endif
70 #ifndef NDEBUG
71         res->reserved_resources = 0;
72 #endif
73
74         return res;
75 }
76
77 /** Completes an incomplete irprog. */
78 static ir_prog *complete_ir_prog(ir_prog *irp) {
79         int i;
80 #define IDENT(s) new_id_from_chars(s, sizeof(s)-1)
81
82         irp->name              = IDENT(INITAL_PROG_NAME);
83
84         irp->segment_types[IR_SEGMENT_GLOBAL] = new_type_class(IDENT("GlobalType"));
85         irp->segment_types[IR_SEGMENT_THREAD_LOCAL]
86                 = new_type_struct(IDENT("ThreadLocal"));
87         irp->segment_types[IR_SEGMENT_CONSTRUCTORS]
88                 = new_type_struct(IDENT("Constructors"));
89         irp->segment_types[IR_SEGMENT_DESTRUCTORS]
90                 = new_type_struct(IDENT("Destructors"));
91         /* Remove these types from type list.  Must be treated differently than
92            other types. */
93         for (i = 0; i < IR_SEGMENT_COUNT; ++i) {
94                 remove_irp_type(irp->segment_types[i]);
95         }
96
97         /* Set these flags for debugging. */
98         irp->segment_types[IR_SEGMENT_GLOBAL]->flags       |= tf_global_type;
99         irp->segment_types[IR_SEGMENT_THREAD_LOCAL]->flags |= tf_tls_type;
100
101         /* The global type is a class, but we cannot derive from it, so set
102            the final property to assist optimizations that checks for it. */
103         set_class_final(irp->segment_types[IR_SEGMENT_GLOBAL], 1);
104
105         irp->const_code_irg   = new_const_code_irg();
106
107         irp->phase_state                = phase_building;
108         irp->outs_state                 = outs_none;
109         irp->ip_outedges                = NULL;
110         irp->trouts_state               = outs_none;
111         irp->class_cast_state           = ir_class_casts_transitive;
112         irp->globals_entity_usage_state = ir_entity_usage_not_computed;
113
114         return irp;
115 #undef IDENT
116 }
117
118 /* initializes ir_prog. Constructs only the basic lists. */
119 void init_irprog_1(void) {
120         irp = new_incomplete_ir_prog();
121 }
122
123 /* Completes ir_prog. */
124 void init_irprog_2(void) {
125         complete_ir_prog(irp);
126 }
127
128 /* Create a new ir prog. Automatically called by init_firm through
129    init_irprog. */
130 ir_prog *new_ir_prog(void) {
131         return complete_ir_prog(new_incomplete_ir_prog());
132 }
133
134 /* frees all memory used by irp.  Types in type list, irgs in irg
135    list and entities in global type must be freed by hand before. */
136 void free_ir_prog(void) {
137         int i;
138         for (i = 0; i < IR_SEGMENT_COUNT; ++i) {
139                 free_type(irp->segment_types[i]);
140         }
141
142         free_ir_graph(irp->const_code_irg);
143         DEL_ARR_F(irp->graphs);
144         DEL_ARR_F(irp->pseudo_graphs);
145         DEL_ARR_F(irp->types);
146         DEL_ARR_F(irp->modes);
147
148         finish_op();
149         DEL_ARR_F(irp->opcodes);
150         DEL_ARR_F(irp->global_asms);
151
152         irp->name           = NULL;
153         irp->const_code_irg = NULL;
154         irp->kind           = k_BAD;
155 }
156
157 /*- Functions to access the fields of ir_prog -*/
158
159
160 /* Access the main routine of the compiled program. */
161 ir_graph *get_irp_main_irg(void) {
162         assert(irp);
163         return irp->main_irg;
164 }
165
166 void set_irp_main_irg(ir_graph *main_irg) {
167         assert(irp);
168         irp->main_irg = main_irg;
169 }
170
171 ir_type *(get_segment_type)(ir_segment_t segment) {
172         return _get_segment_type(segment);
173 }
174
175 ir_type *(get_glob_type)(void) {
176         return _get_glob_type();
177 }
178
179 ir_type *(get_tls_type)(void) {
180         return _get_tls_type();
181 }
182
183 /* Adds irg to the list of ir graphs in irp. */
184 void add_irp_irg(ir_graph *irg) {
185         assert(irg != NULL);
186         assert(irp && irp->graphs);
187         ARR_APP1(ir_graph *, irp->graphs, irg);
188 }
189
190 /* Removes irg from the list or irgs, shrinks the list by one. */
191 void remove_irp_irg_from_list(ir_graph *irg){
192         int i, l, found = 0;
193
194         assert(irg);
195         l = ARR_LEN(irp->graphs);
196         for (i = 0; i < l; i++) {
197                 if (irp->graphs[i] == irg) {
198                         found = 1;
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         if (!found) {
207                 l = ARR_LEN(irp->pseudo_graphs);
208                 for (i = 0; i < l; i++) {
209                         if (irp->pseudo_graphs[i] == irg) {
210                                 for(; i < l - 1; i++) {
211                                         irp->pseudo_graphs[i] = irp->pseudo_graphs[i+1];
212                                 }
213                                 ARR_SETLEN(ir_graph*, irp->pseudo_graphs, l - 1);
214                                 break;
215                         }
216                 }
217         }
218 }
219
220 /* Removes irg from the list or irgs, shrinks the list by one. */
221 void remove_irp_irg(ir_graph *irg){
222         free_ir_graph(irg);
223         remove_irp_irg_from_list(irg);
224 }
225
226 int (get_irp_n_irgs)(void) {
227         return _get_irp_n_irgs();
228 }
229
230 ir_graph *(get_irp_irg)(int pos){
231         return _get_irp_irg(pos);
232 }
233
234 int get_irp_last_idx(void) {
235         return irp->max_irg_idx;
236 }
237
238 void set_irp_irg(int pos, ir_graph *irg) {
239         assert(irp && irg);
240         assert(pos < (ARR_LEN(irp->graphs)));
241         irp->graphs[pos] = irg;
242 }
243
244 /* Gets the number of graphs _and_ pseudo graphs. */
245 int get_irp_n_allirgs(void) {
246         /* We can not call get_irp_n_irgs, as we end up in a recursion ... */
247         return ARR_LEN(irp->graphs) + get_irp_n_pseudo_irgs();
248 }
249
250 /* Returns the ir graph at position pos of all graphs (including
251  pseudo graphs).  Visits first graphs, then pseudo graphs. */
252 ir_graph *get_irp_allirg(int pos) {
253         int n_irgs = ARR_LEN(irp->graphs);
254         assert(0 <= pos);
255         if (pos < n_irgs) {
256                 return irp->graphs[pos];
257         } else {
258                 return get_irp_pseudo_irg(pos-n_irgs);
259         }
260 }
261
262 /* Adds type to the list of types in irp. */
263 void add_irp_type(ir_type *typ) {
264         assert(typ != NULL);
265         assert(irp);
266         ARR_APP1 (ir_type *, irp->types, typ);
267 }
268
269 /* Remove type form the list of types in irp. */
270 void remove_irp_type(ir_type *typ) {
271         int i;
272         assert(typ);
273
274         for (i = ARR_LEN(irp->types) -1; i >= 0; i--) {
275                 if (irp->types[i] == typ) {
276                         for(; i < (ARR_LEN(irp->types)) - 1; i++) {
277                                 irp->types[i] = irp->types[i+1];
278                         }
279                         ARR_SETLEN(ir_type *, irp->types, (ARR_LEN(irp->types)) - 1);
280                         break;
281                 }
282         }
283 }
284
285 int (get_irp_n_types) (void) {
286         return _get_irp_n_types();
287 }
288
289 ir_type *(get_irp_type) (int pos) {
290         return _get_irp_type(pos);
291 }
292
293 void set_irp_type(int pos, ir_type *typ) {
294         assert(irp && typ);
295         assert(pos < (ARR_LEN((irp)->types)));
296         irp->types[pos] = typ;
297 }
298
299 /* Returns the number of all modes in the irp. */
300 int (get_irp_n_modes)(void) {
301         return _get_irp_n_modes();
302 }
303
304 /* Returns the mode at position pos in the irp. */
305 ir_mode *(get_irp_mode)(int pos) {
306         return _get_irp_mode(pos);
307 }
308
309 /* Adds mode to the list of modes in irp. */
310 void add_irp_mode(ir_mode *mode) {
311         assert(mode != NULL);
312         assert(irp);
313         ARR_APP1(ir_mode *, irp->modes, mode);
314 }
315
316 /* Adds opcode to the list of opcodes in irp. */
317 void add_irp_opcode(ir_op *opcode) {
318         assert(opcode != NULL);
319         assert(irp);
320         assert(opcode->code == (unsigned) ARR_LEN(irp->opcodes) && "new_ir_op() called in wrong order");
321         ARR_APP1(ir_op *, irp->opcodes, opcode);
322 }
323
324 /* Removes opcode from the list of opcodes and shrinks the list by one. */
325 void remove_irp_opcode(ir_op *opcode) {
326         int i;
327
328         assert(opcode);
329         for (i = ARR_LEN(irp->opcodes) -1; i >= 0; i--) {
330                 if (irp->opcodes[i] != opcode)
331                         continue;
332                 for (; i < (ARR_LEN(irp->opcodes)) - 1; i++) {
333                         irp->opcodes[i] = irp->opcodes[i+1];
334                 }
335                 ARR_SETLEN(ir_op *, irp->opcodes, (ARR_LEN(irp->opcodes)) - 1);
336                 return;
337         }
338         panic("Deleting unknown opcode");
339 }
340
341 /* Returns the number of all opcodes in the irp. */
342 int (get_irp_n_opcodes)(void) {
343         return _get_irp_n_opcodes();
344 }
345
346 /* Returns the opcode at position pos in the irp. */
347 ir_op *(get_irp_opcode)(int pos) {
348         return _get_irp_opcode(pos);
349 }
350
351 /* Sets the generic function pointer of all opcodes to NULL */
352 void  clear_irp_opcodes_generic_func(void) {
353         int i;
354
355         for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
356                 ir_op *op = get_irp_opcode(i);
357                 op->ops.generic = (op_func)NULL;
358         }
359 }
360
361 /*- File name / executable name or the like -*/
362 void   set_irp_prog_name(ident *name) {
363         irp->name = name;
364 }
365 int irp_prog_name_is_set(void) {
366         return irp->name != new_id_from_str(INITAL_PROG_NAME);
367 }
368 ident *get_irp_prog_ident(void) {
369         return irp->name;
370 }
371 const char  *get_irp_prog_name(void) {
372         return get_id_str(irp->name);
373 }
374
375
376 ir_graph *(get_const_code_irg)(void) {
377         return _get_const_code_irg();
378 }
379
380 irg_phase_state get_irp_phase_state(void) {
381         return irp->phase_state;
382 }
383
384 void set_irp_phase_state(irg_phase_state s) {
385         irp->phase_state = s;
386 }
387
388 irg_outs_state get_irp_ip_outs_state(void) {
389         return irp->outs_state;
390 }
391
392 void set_irp_ip_outs_inconsistent(void) {
393         irp->outs_state = outs_inconsistent;
394 }
395
396 void set_irp_ip_outedges(ir_node ** ip_outedges) {
397         irp->ip_outedges = ip_outedges;
398 }
399
400 ir_node** get_irp_ip_outedges(void) {
401         return irp->ip_outedges;
402 }
403
404 irg_callee_info_state get_irp_callee_info_state(void) {
405         return irp->callee_info_state;
406 }
407
408 void set_irp_callee_info_state(irg_callee_info_state s) {
409         irp->callee_info_state = s;
410 }
411
412 /* Returns a new, unique exception region number. */
413 ir_exc_region_t (get_irp_next_region_nr)(void) {
414         return _get_irp_next_region_nr();
415 }
416
417 /* Returns a new, unique label number. */
418 ir_label_t (get_irp_next_label_nr)(void) {
419         return _get_irp_next_label_nr();
420 }
421
422 /* Add a new global asm include */
423 void add_irp_asm(ident *asm_string) {
424         ARR_APP1(ident *, irp->global_asms, asm_string);
425 }
426
427 /* Return the number of global asm includes. */
428 int get_irp_n_asms(void) {
429         return ARR_LEN(irp->global_asms);
430 }
431
432 /* Return the global asm include at position pos. */
433 ident *get_irp_asm(int pos) {
434         assert(pos <= 0 && pos < get_irp_n_asms());
435         return irp->global_asms[pos];
436 }
437
438 #ifndef NDEBUG
439 void irp_reserve_resources(ir_prog *irp, ir_resources_t resources) {
440         assert((resources & ~IR_RESOURCE_GLOBAL_MASK) == 0);
441         assert((irp->reserved_resources & resources) == 0);
442         irp->reserved_resources |= resources;
443 }
444
445 void irp_free_resources(ir_prog *irp, ir_resources_t resources) {
446         assert((irp->reserved_resources & resources) == resources);
447         irp->reserved_resources &= ~resources;
448 }
449
450 ir_resources_t irp_resources_reserved(const ir_prog *irp) {
451         return irp->reserved_resources;
452 }
453 #endif