avoid unnecessary macros in public headers
[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 "irpass_t.h"
34 #include "pseudo_irg.h"
35 #include "array.h"
36 #include "error.h"
37 #include "obst.h"
38 #include "irop_t.h"
39 #include "irmemory.h"
40
41 /** The initial name of the irp program. */
42 #define INITAL_PROG_NAME "no_name_set"
43
44 /* A variable from where everything in the ir can be accessed. */
45 ir_prog *irp;
46 ir_prog *get_irp(void) { return irp; }
47
48 /**
49  *  Create a new incomplete ir_prog.
50  */
51 static ir_prog *new_incomplete_ir_prog(void)
52 {
53         ir_prog *res = XMALLOCZ(ir_prog);
54
55         res->kind           = k_ir_prog;
56         res->graphs         = NEW_ARR_F(ir_graph *, 0);
57         res->pseudo_graphs  = NEW_ARR_F(ir_graph *, 0);
58         res->types          = NEW_ARR_F(ir_type *, 0);
59         res->modes          = NEW_ARR_F(ir_mode *, 0);
60         res->opcodes        = NEW_ARR_F(ir_op *, 0);
61         res->global_asms    = NEW_ARR_F(ident *, 0);
62         res->last_region_nr = 0;
63         res->last_label_nr  = 1;  /* 0 is reserved as non-label */
64         res->max_irg_idx    = 0;
65         res->max_node_nr    = 0;
66 #ifndef NDEBUG
67         res->reserved_resources = 0;
68 #endif
69
70         return res;
71 }
72
73 /**
74  * Completes an incomplete irprog.
75  *
76  * @param irp          the (yet incomplete) irp
77  * @param module_name  the (module) name for this irp
78  */
79 static ir_prog *complete_ir_prog(ir_prog *irp, const char *module_name)
80 {
81         ir_segment_t s;
82
83 #define IDENT(x)  new_id_from_chars(x, sizeof(x) - 1)
84
85         irp->name = new_id_from_str(module_name);
86         irp->segment_types[IR_SEGMENT_GLOBAL] = new_type_class(IDENT("GlobalType"));
87         irp->segment_types[IR_SEGMENT_THREAD_LOCAL]
88                 = new_type_class(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         /* Remove these types from type list.  Must be treated differently than
94            other types. */
95         for (s = IR_SEGMENT_FIRST; s <= IR_SEGMENT_LAST; ++s) {
96                 remove_irp_type(irp->segment_types[s]);
97         }
98
99         /* Set these flags for debugging. */
100         irp->segment_types[IR_SEGMENT_GLOBAL]->flags       |= tf_global_type;
101         irp->segment_types[IR_SEGMENT_THREAD_LOCAL]->flags |= tf_tls_type;
102         irp->segment_types[IR_SEGMENT_CONSTRUCTORS]->flags |= tf_constructors;
103         irp->segment_types[IR_SEGMENT_DESTRUCTORS]->flags  |= tf_destructors;
104
105         /* The global type is a class, but we cannot derive from it, so set
106            the final property to assist optimizations that checks for it. */
107         set_class_final(irp->segment_types[IR_SEGMENT_GLOBAL], 1);
108
109         irp->const_code_irg             = new_const_code_irg();
110         irp->phase_state                = phase_building;
111         irp->outs_state                 = outs_none;
112         irp->ip_outedges                = NULL;
113         irp->trouts_state               = outs_none;
114         irp->class_cast_state           = ir_class_casts_transitive;
115         irp->globals_entity_usage_state = ir_entity_usage_not_computed;
116
117         return irp;
118 #undef IDENT
119 }
120
121 /* initializes ir_prog. Constructs only the basic lists. */
122 void init_irprog_1(void)
123 {
124         irp = new_incomplete_ir_prog();
125 }
126
127 /* Completes ir_prog. */
128 void init_irprog_2(void)
129 {
130         (void)complete_ir_prog(irp, INITAL_PROG_NAME);
131 }
132
133 /* Create a new ir prog. Automatically called by init_firm through
134    init_irprog. */
135 ir_prog *new_ir_prog(const char *name)
136 {
137         return complete_ir_prog(new_incomplete_ir_prog(), name);
138 }
139
140 /* frees all memory used by irp.  Types in type list, irgs in irg
141    list and entities in global type must be freed by hand before. */
142 void free_ir_prog(void)
143 {
144         ir_segment_t s;
145         for (s = IR_SEGMENT_FIRST; s <= IR_SEGMENT_LAST; ++s) {
146                 free_type(irp->segment_types[s]);
147         }
148
149         free_ir_graph(irp->const_code_irg);
150         DEL_ARR_F(irp->graphs);
151         DEL_ARR_F(irp->pseudo_graphs);
152         DEL_ARR_F(irp->types);
153         DEL_ARR_F(irp->modes);
154
155         finish_op();
156         DEL_ARR_F(irp->opcodes);
157         DEL_ARR_F(irp->global_asms);
158
159         irp->name           = NULL;
160         irp->const_code_irg = NULL;
161         irp->kind           = k_BAD;
162 }
163
164 /*- Functions to access the fields of ir_prog -*/
165
166
167 /* Access the main routine of the compiled program. */
168 ir_graph *get_irp_main_irg(void)
169 {
170         assert(irp);
171         return irp->main_irg;
172 }
173
174 void set_irp_main_irg(ir_graph *main_irg)
175 {
176         assert(irp);
177         irp->main_irg = main_irg;
178 }
179
180 ir_type *(get_segment_type)(ir_segment_t segment)
181 {
182         return _get_segment_type(segment);
183 }
184
185 void set_segment_type(ir_segment_t segment, ir_type *new_type)
186 {
187         assert(segment <= IR_SEGMENT_LAST);
188         irp->segment_types[segment] = new_type;
189         /* segment types are not in the type list... */
190         remove_irp_type(new_type);
191 }
192
193 ir_type *(get_glob_type)(void)
194 {
195         return _get_glob_type();
196 }
197
198 ir_type *(get_tls_type)(void)
199 {
200         return _get_tls_type();
201 }
202
203 /* Adds irg to the list of ir graphs in irp. */
204 void add_irp_irg(ir_graph *irg)
205 {
206         assert(irg != NULL);
207         assert(irp && irp->graphs);
208         ARR_APP1(ir_graph *, irp->graphs, irg);
209 }
210
211 /* Removes irg from the list or irgs, shrinks the list by one. */
212 void remove_irp_irg_from_list(ir_graph *irg)
213 {
214         int i, l, found = 0;
215
216         assert(irg);
217         l = ARR_LEN(irp->graphs);
218         for (i = 0; i < l; i++) {
219                 if (irp->graphs[i] == irg) {
220                         found = 1;
221                         for (; i < l - 1; i++) {
222                                 irp->graphs[i] = irp->graphs[i+1];
223                         }
224                         ARR_SETLEN(ir_graph*, irp->graphs, l - 1);
225                         break;
226                 }
227         }
228         if (!found) {
229                 l = ARR_LEN(irp->pseudo_graphs);
230                 for (i = 0; i < l; i++) {
231                         if (irp->pseudo_graphs[i] == irg) {
232                                 for (; i < l - 1; i++) {
233                                         irp->pseudo_graphs[i] = irp->pseudo_graphs[i+1];
234                                 }
235                                 ARR_SETLEN(ir_graph*, irp->pseudo_graphs, l - 1);
236                                 break;
237                         }
238                 }
239         }
240 }
241
242 /* Removes irg from the list or irgs, shrinks the list by one. */
243 void remove_irp_irg(ir_graph *irg)
244 {
245         free_ir_graph(irg);
246         remove_irp_irg_from_list(irg);
247 }
248
249 int (get_irp_n_irgs)(void)
250 {
251         return _get_irp_n_irgs();
252 }
253
254 ir_graph *(get_irp_irg)(int pos)
255 {
256         return _get_irp_irg(pos);
257 }
258
259 int get_irp_last_idx(void)
260 {
261         return irp->max_irg_idx;
262 }
263
264 void set_irp_irg(int pos, ir_graph *irg)
265 {
266         assert(irp && irg);
267         assert(pos < (ARR_LEN(irp->graphs)));
268         irp->graphs[pos] = irg;
269 }
270
271 /* Gets the number of graphs _and_ pseudo graphs. */
272 int get_irp_n_allirgs(void)
273 {
274         /* We can not call get_irp_n_irgs, as we end up in a recursion ... */
275         return ARR_LEN(irp->graphs) + get_irp_n_pseudo_irgs();
276 }
277
278 /* Returns the ir graph at position pos of all graphs (including
279  pseudo graphs).  Visits first graphs, then pseudo graphs. */
280 ir_graph *get_irp_allirg(int pos)
281 {
282         int n_irgs = ARR_LEN(irp->graphs);
283         assert(0 <= pos);
284         if (pos < n_irgs) {
285                 return irp->graphs[pos];
286         } else {
287                 return get_irp_pseudo_irg(pos-n_irgs);
288         }
289 }
290
291 /* Adds type to the list of types in irp. */
292 void add_irp_type(ir_type *typ)
293 {
294         assert(typ != NULL);
295         assert(irp);
296         ARR_APP1(ir_type *, irp->types, typ);
297 }
298
299 /* Remove type from the list of types in irp. */
300 void remove_irp_type(ir_type *typ)
301 {
302         int i;
303         assert(typ);
304
305         for (i = ARR_LEN(irp->types) - 1; i >= 0; i--) {
306                 if (irp->types[i] == typ) {
307                         for (; i < (ARR_LEN(irp->types)) - 1; i++) {
308                                 irp->types[i] = irp->types[i+1];
309                         }
310                         ARR_SETLEN(ir_type *, irp->types, (ARR_LEN(irp->types)) - 1);
311                         break;
312                 }
313         }
314 }
315
316 int (get_irp_n_types) (void)
317 {
318         return _get_irp_n_types();
319 }
320
321 ir_type *(get_irp_type) (int pos)
322 {
323         return _get_irp_type(pos);
324 }
325
326 void set_irp_type(int pos, ir_type *typ)
327 {
328         assert(irp && typ);
329         assert(pos < (ARR_LEN((irp)->types)));
330         irp->types[pos] = typ;
331 }
332
333 /* Returns the number of all modes in the irp. */
334 int (get_irp_n_modes)(void)
335 {
336         return _get_irp_n_modes();
337 }
338
339 /* Returns the mode at position pos in the irp. */
340 ir_mode *(get_irp_mode)(int pos)
341 {
342         return _get_irp_mode(pos);
343 }
344
345 /* Adds mode to the list of modes in irp. */
346 void add_irp_mode(ir_mode *mode)
347 {
348         assert(mode != NULL);
349         assert(irp);
350         ARR_APP1(ir_mode *, irp->modes, mode);
351 }
352
353 /* Adds opcode to the list of opcodes in irp. */
354 void add_irp_opcode(ir_op *opcode)
355 {
356         int    len;
357         size_t code;
358         assert(opcode != NULL);
359         assert(irp);
360         len  = ARR_LEN(irp->opcodes);
361         code = opcode->code;
362         if ((int) code >= len) {
363                 ARR_RESIZE(ir_op*, irp->opcodes, code+1);
364                 memset(&irp->opcodes[len], 0, (code-len+1) * sizeof(irp->opcodes[0]));
365         }
366
367         assert(irp->opcodes[code] == NULL && "opcode registered twice");
368         irp->opcodes[code] = opcode;
369 }
370
371 /* Removes opcode from the list of opcodes and shrinks the list by one. */
372 void remove_irp_opcode(ir_op *opcode)
373 {
374         assert((int) opcode->code < ARR_LEN(irp->opcodes));
375         irp->opcodes[opcode->code] = NULL;
376 }
377
378 /* Returns the number of all opcodes in the irp. */
379 int (get_irp_n_opcodes)(void)
380 {
381         return _get_irp_n_opcodes();
382 }
383
384 /* Returns the opcode at position pos in the irp. */
385 ir_op *(get_irp_opcode)(int pos)
386 {
387         return _get_irp_opcode(pos);
388 }
389
390 /* Sets the generic function pointer of all opcodes to NULL */
391 void  clear_irp_opcodes_generic_func(void)
392 {
393         int i;
394
395         for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
396                 ir_op *op = get_irp_opcode(i);
397                 op->ops.generic = (op_func)NULL;
398         }
399 }
400
401 /*- File name / executable name or the like -*/
402 void   set_irp_prog_name(ident *name)
403 {
404         irp->name = name;
405 }
406 int irp_prog_name_is_set(void)
407 {
408         return irp->name != new_id_from_str(INITAL_PROG_NAME);
409 }
410 ident *get_irp_ident(void)
411 {
412         return irp->name;
413 }
414 const char  *get_irp_name(void)
415 {
416         return get_id_str(irp->name);
417 }
418
419
420 ir_graph *(get_const_code_irg)(void)
421 {
422         return _get_const_code_irg();
423 }
424
425 irg_phase_state get_irp_phase_state(void)
426 {
427         return irp->phase_state;
428 }
429
430 void set_irp_phase_state(irg_phase_state s)
431 {
432         irp->phase_state = s;
433 }
434
435 struct pass_t {
436         ir_prog_pass_t  pass;
437         irg_phase_state state;
438 };
439
440 /**
441  * Wrapper for setting the state of a whole ir_prog.
442  */
443 static int set_irp_phase_state_wrapper(ir_prog *irp, void *context)
444 {
445         struct pass_t  *pass  = context;
446         irg_phase_state state = pass->state;
447         int             i;
448
449         (void)irp;
450
451         /* set the phase of all graphs */
452         for (i = get_irp_n_irgs() - 1; i >= 0; --i)
453                 set_irg_phase_state(get_irp_irg(i), state);
454
455         /* set the irp phase */
456         set_irp_phase_state(state);
457
458         return 0;
459 }
460
461 ir_prog_pass_t *set_irp_phase_state_pass(const char *name, irg_phase_state state)
462 {
463         struct pass_t *pass = XMALLOCZ(struct pass_t);
464
465         def_prog_pass_constructor(
466                 &pass->pass, name ? name : "set_irp_phase", set_irp_phase_state_wrapper);
467         pass->state = state;
468
469         /* no dump/verify */
470         pass->pass.verify_irprog = ir_prog_no_verify;
471         pass->pass.dump_irprog   = ir_prog_no_dump;
472
473         return &pass->pass;
474 }
475
476 irg_outs_state get_irp_ip_outs_state(void)
477 {
478         return irp->outs_state;
479 }
480
481 void set_irp_ip_outs_inconsistent(void)
482 {
483         irp->outs_state = outs_inconsistent;
484 }
485
486 void set_irp_ip_outedges(ir_node ** ip_outedges)
487 {
488         irp->ip_outedges = ip_outedges;
489 }
490
491 ir_node** get_irp_ip_outedges(void)
492 {
493         return irp->ip_outedges;
494 }
495
496 irg_callee_info_state get_irp_callee_info_state(void)
497 {
498         return irp->callee_info_state;
499 }
500
501 void set_irp_callee_info_state(irg_callee_info_state s)
502 {
503         irp->callee_info_state = s;
504 }
505
506 /* Returns a new, unique exception region number. */
507 ir_exc_region_t (get_irp_next_region_nr)(void)
508 {
509         return _get_irp_next_region_nr();
510 }
511
512 /* Returns a new, unique label number. */
513 ir_label_t (get_irp_next_label_nr)(void)
514 {
515         return _get_irp_next_label_nr();
516 }
517
518 /* Add a new global asm include */
519 void add_irp_asm(ident *asm_string)
520 {
521         ARR_APP1(ident *, irp->global_asms, asm_string);
522 }
523
524 /* Return the number of global asm includes. */
525 int get_irp_n_asms(void)
526 {
527         return ARR_LEN(irp->global_asms);
528 }
529
530 /* Return the global asm include at position pos. */
531 ident *get_irp_asm(int pos)
532 {
533         assert(0 <= pos && pos < get_irp_n_asms());
534         return irp->global_asms[pos];
535 }
536
537 #ifndef NDEBUG
538 void irp_reserve_resources(ir_prog *irp, ir_resources_t resources)
539 {
540         assert((resources & ~IR_RESOURCE_GLOBAL_MASK) == 0);
541         assert((irp->reserved_resources & resources) == 0);
542         irp->reserved_resources |= resources;
543 }
544
545 void irp_free_resources(ir_prog *irp, ir_resources_t resources)
546 {
547         assert((irp->reserved_resources & resources) == resources);
548         irp->reserved_resources &= ~resources;
549 }
550
551 ir_resources_t irp_resources_reserved(const ir_prog *irp)
552 {
553         return irp->reserved_resources;
554 }
555 #endif