51e36377fe81d31adeab1e0b67bb87f59cb8cc70
[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
39 /** The initial name of the irp program. */
40 #define INITAL_PROG_NAME "no_name_set"
41
42 /* A variable from where everything in the ir can be accessed. */
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->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         res->max_node_nr    = 0;
67 #ifndef NDEBUG
68         res->reserved_resources = IR_RESOURCE_NONE;
69 #endif
70
71         return res;
72 }
73
74 /**
75  * Completes an incomplete irprog.
76  *
77  * @param irp          the (yet incomplete) irp
78  * @param module_name  the (module) name for this irp
79  */
80 static ir_prog *complete_ir_prog(ir_prog *irp, const char *module_name)
81 {
82 #define IDENT(x)  new_id_from_chars(x, sizeof(x) - 1)
83
84         irp->name = new_id_from_str(module_name);
85         irp->segment_types[IR_SEGMENT_GLOBAL]
86                 = new_type_class(IDENT("GlobalType"));
87         irp->segment_types[IR_SEGMENT_THREAD_LOCAL]
88                 = new_type_struct(IDENT("ThreadLocal"));
89         irp->segment_types[IR_SEGMENT_CONSTRUCTORS]
90                 = new_type_class(IDENT("Constructors"));
91         irp->segment_types[IR_SEGMENT_DESTRUCTORS]
92                 = new_type_class(IDENT("Destructors"));
93
94         /* Set these flags for debugging. */
95         irp->segment_types[IR_SEGMENT_GLOBAL]->flags       |= tf_segment|tf_global_type;
96         irp->segment_types[IR_SEGMENT_THREAD_LOCAL]->flags |= tf_segment|tf_tls_type;
97         irp->segment_types[IR_SEGMENT_CONSTRUCTORS]->flags |= tf_segment|tf_constructors;
98         irp->segment_types[IR_SEGMENT_DESTRUCTORS]->flags  |= tf_segment|tf_destructors;
99
100         /* The global type is a class, but we cannot derive from it, so set
101            the final property to assist optimizations that checks for it. */
102         set_class_final(irp->segment_types[IR_SEGMENT_GLOBAL], 1);
103
104         irp->const_code_irg             = new_const_code_irg();
105         irp->phase_state                = phase_building;
106         irp->class_cast_state           = ir_class_casts_transitive;
107         irp->globals_entity_usage_state = ir_entity_usage_not_computed;
108
109         current_ir_graph = irp->const_code_irg;
110
111         return irp;
112 #undef IDENT
113 }
114
115 /* initializes ir_prog. Constructs only the basic lists. */
116 void init_irprog_1(void)
117 {
118         irp = new_incomplete_ir_prog();
119 }
120
121 /* Completes ir_prog. */
122 void init_irprog_2(void)
123 {
124         (void)complete_ir_prog(irp, INITAL_PROG_NAME);
125 }
126
127 /* Create a new ir prog. Automatically called by init_firm through
128    init_irprog. */
129 ir_prog *new_ir_prog(const char *name)
130 {
131         return complete_ir_prog(new_incomplete_ir_prog(), name);
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 {
138         ir_segment_t s;
139         for (s = IR_SEGMENT_FIRST; s <= IR_SEGMENT_LAST; ++s) {
140                 free_type(irp->segment_types[s]);
141         }
142
143         free_ir_graph(irp->const_code_irg);
144         DEL_ARR_F(irp->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 {
163         assert(irp);
164         return irp->main_irg;
165 }
166
167 void set_irp_main_irg(ir_graph *main_irg)
168 {
169         assert(irp);
170         irp->main_irg = main_irg;
171 }
172
173 ir_type *(get_segment_type)(ir_segment_t segment)
174 {
175         return get_segment_type_(segment);
176 }
177
178 void set_segment_type(ir_segment_t segment, ir_type *new_type)
179 {
180         assert(segment <= IR_SEGMENT_LAST);
181         irp->segment_types[segment] = new_type;
182 }
183
184 ir_type *(get_glob_type)(void)
185 {
186         return get_glob_type_();
187 }
188
189 ir_type *(get_tls_type)(void)
190 {
191         return get_tls_type_();
192 }
193
194 /* Adds irg to the list of ir graphs in irp. */
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 /* Removes irg from the list or irgs, shrinks the list by one. */
203 void remove_irp_irg_from_list(ir_graph *irg)
204 {
205         size_t i, l;
206
207         assert(irg);
208         l = ARR_LEN(irp->graphs);
209         for (i = 0; i < l; ++i) {
210                 if (irp->graphs[i] == irg) {
211                         for (; i < l - 1; ++i) {
212                                 irp->graphs[i] = irp->graphs[i+1];
213                         }
214                         ARR_SETLEN(ir_graph*, irp->graphs, l - 1);
215                         break;
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 {
223         free_ir_graph(irg);
224         remove_irp_irg_from_list(irg);
225 }
226
227 size_t (get_irp_n_irgs)(void)
228 {
229         return get_irp_n_irgs_();
230 }
231
232 ir_graph *(get_irp_irg)(size_t pos)
233 {
234         return get_irp_irg_(pos);
235 }
236
237 size_t get_irp_last_idx(void)
238 {
239         return irp->max_irg_idx;
240 }
241
242 void set_irp_irg(size_t pos, ir_graph *irg)
243 {
244         assert(irp && irg);
245         assert(pos < ARR_LEN(irp->graphs));
246         irp->graphs[pos] = irg;
247 }
248
249 /* Adds type to the list of types in irp. */
250 void add_irp_type(ir_type *typ)
251 {
252         assert(typ != NULL);
253         assert(irp);
254         ARR_APP1(ir_type *, irp->types, typ);
255 }
256
257 /* Remove type from the list of types in irp. */
258 void remove_irp_type(ir_type *typ)
259 {
260         size_t i, l;
261         assert(typ);
262
263         l = ARR_LEN(irp->types);
264         for (i = 0; i < l; ++i) {
265                 if (irp->types[i] == typ) {
266                         for (; i < l - 1; ++i) {
267                                 irp->types[i] = irp->types[i+1];
268                         }
269                         ARR_SETLEN(ir_type *, irp->types, l - 1);
270                         break;
271                 }
272         }
273 }
274
275 size_t (get_irp_n_types) (void)
276 {
277         return get_irp_n_types_();
278 }
279
280 ir_type *(get_irp_type) (size_t pos)
281 {
282         return get_irp_type_(pos);
283 }
284
285 void set_irp_type(size_t pos, ir_type *typ)
286 {
287         assert(irp && typ);
288         assert(pos < ARR_LEN((irp)->types));
289         irp->types[pos] = typ;
290 }
291
292 /* Returns the number of all modes in the irp. */
293 size_t (get_irp_n_modes)(void)
294 {
295         return get_irp_n_modes_();
296 }
297
298 /* Returns the mode at position pos in the irp. */
299 ir_mode *(get_irp_mode)(size_t pos)
300 {
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 {
307         assert(mode != NULL);
308         assert(irp);
309         ARR_APP1(ir_mode *, irp->modes, mode);
310 }
311
312 /* Adds opcode to the list of opcodes in irp. */
313 void add_irp_opcode(ir_op *opcode)
314 {
315         size_t len;
316         size_t code;
317         assert(opcode != NULL);
318         assert(irp);
319         len  = ARR_LEN(irp->opcodes);
320         code = opcode->code;
321         if (code >= len) {
322                 ARR_RESIZE(ir_op*, irp->opcodes, code+1);
323                 memset(&irp->opcodes[len], 0, (code-len+1) * sizeof(irp->opcodes[0]));
324         }
325
326         assert(irp->opcodes[code] == NULL && "opcode registered twice");
327         irp->opcodes[code] = opcode;
328 }
329
330 /* Removes opcode from the list of opcodes and shrinks the list by one. */
331 void remove_irp_opcode(ir_op *opcode)
332 {
333         assert(opcode->code < ARR_LEN(irp->opcodes));
334         irp->opcodes[opcode->code] = NULL;
335 }
336
337 /* Returns the number of all opcodes in the irp. */
338 size_t (get_irp_n_opcodes)(void)
339 {
340         return get_irp_n_opcodes_();
341 }
342
343 /* Returns the opcode at position pos in the irp. */
344 ir_op *(get_irp_opcode)(size_t pos)
345 {
346         return get_irp_opcode_(pos);
347 }
348
349 /* Sets the generic function pointer of all opcodes to NULL */
350 void clear_irp_opcodes_generic_func(void)
351 {
352         size_t i, n;
353
354         for (i = 0, n = get_irp_n_opcodes(); i < n; ++i) {
355                 ir_op *op = get_irp_opcode(i);
356                 op->ops.generic = (op_func)NULL;
357         }
358 }
359
360 /*- File name / executable name or the like -*/
361 void set_irp_prog_name(ident *name)
362 {
363         irp->name = name;
364 }
365 int irp_prog_name_is_set(void)
366 {
367         return irp->name != new_id_from_str(INITAL_PROG_NAME);
368 }
369 ident *get_irp_ident(void)
370 {
371         return irp->name;
372 }
373 const char  *get_irp_name(void)
374 {
375         return get_id_str(irp->name);
376 }
377
378
379 ir_graph *(get_const_code_irg)(void)
380 {
381         return get_const_code_irg_();
382 }
383
384 irg_phase_state get_irp_phase_state(void)
385 {
386         return irp->phase_state;
387 }
388
389 void set_irp_phase_state(irg_phase_state s)
390 {
391         irp->phase_state = s;
392 }
393
394 typedef struct pass_t {
395         ir_prog_pass_t  pass;
396         irg_phase_state state;
397 } pass_t;
398
399 /**
400  * Wrapper for setting the state of a whole ir_prog.
401  */
402 static int set_irp_phase_state_wrapper(ir_prog *irp, void *context)
403 {
404         pass_t         *pass  = (pass_t *)context;
405         irg_phase_state state = pass->state;
406         size_t          i, n;
407
408         (void)irp;
409
410         /* set the phase of all graphs */
411         for (i = 0, n = get_irp_n_irgs(); i < n; ++i)
412                 set_irg_phase_state(get_irp_irg(i), state);
413
414         /* set the irp phase */
415         set_irp_phase_state(state);
416
417         return 0;
418 }
419
420 ir_prog_pass_t *set_irp_phase_state_pass(const char *name, irg_phase_state state)
421 {
422         struct pass_t *pass = XMALLOCZ(struct pass_t);
423
424         def_prog_pass_constructor(
425                 &pass->pass, name ? name : "set_irp_phase", set_irp_phase_state_wrapper);
426         pass->state = state;
427
428         /* no dump/verify */
429         pass->pass.verify_irprog = ir_prog_no_verify;
430         pass->pass.dump_irprog   = ir_prog_no_dump;
431
432         return &pass->pass;
433 }
434
435 void set_irp_ip_outedges(ir_node ** ip_outedges)
436 {
437         irp->ip_outedges = ip_outedges;
438 }
439
440 ir_node** get_irp_ip_outedges(void)
441 {
442         return irp->ip_outedges;
443 }
444
445 irg_callee_info_state get_irp_callee_info_state(void)
446 {
447         return irp->callee_info_state;
448 }
449
450 void set_irp_callee_info_state(irg_callee_info_state s)
451 {
452         irp->callee_info_state = s;
453 }
454
455 /* Returns a new, unique exception region number. */
456 ir_exc_region_t (get_irp_next_region_nr)(void)
457 {
458         return get_irp_next_region_nr_();
459 }
460
461 /* Returns a new, unique label number. */
462 ir_label_t (get_irp_next_label_nr)(void)
463 {
464         return get_irp_next_label_nr_();
465 }
466
467 /* Add a new global asm include */
468 void add_irp_asm(ident *asm_string)
469 {
470         ARR_APP1(ident *, irp->global_asms, asm_string);
471 }
472
473 /* Return the number of global asm includes. */
474 size_t get_irp_n_asms(void)
475 {
476         return ARR_LEN(irp->global_asms);
477 }
478
479 /* Return the global asm include at position pos. */
480 ident *get_irp_asm(size_t pos)
481 {
482         assert(pos < get_irp_n_asms());
483         return irp->global_asms[pos];
484 }
485
486 /** Return whether optimization dump vcg graphs */
487 int (get_irp_optimization_dumps)(void)
488 {
489         return get_irp_optimization_dumps_();
490 }
491
492 /** Enable vcg dumping of optimization */
493 void (enable_irp_optimization_dumps)(void)
494 {
495         enable_irp_optimization_dumps_();
496 }
497
498 #ifndef NDEBUG
499 void irp_reserve_resources(ir_prog *irp, irp_resources_t resources)
500 {
501         assert((irp->reserved_resources & resources) == 0);
502         irp->reserved_resources |= resources;
503 }
504
505 void irp_free_resources(ir_prog *irp, irp_resources_t resources)
506 {
507         assert((irp->reserved_resources & resources) == resources);
508         irp->reserved_resources &= ~resources;
509 }
510
511 irp_resources_t irp_resources_reserved(const ir_prog *irp)
512 {
513         return irp->reserved_resources;
514 }
515 #endif