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