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