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