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