irverify: check that switch_table != NULL
[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         ir_segment_t s;
83
84 #define IDENT(x)  new_id_from_chars(x, sizeof(x) - 1)
85
86         irp->name = new_id_from_str(module_name);
87         irp->segment_types[IR_SEGMENT_GLOBAL]
88                 = new_type_class(IDENT("GlobalType"));
89         irp->segment_types[IR_SEGMENT_THREAD_LOCAL]
90                 = new_type_struct(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->class_cast_state           = ir_class_casts_transitive;
114         irp->globals_entity_usage_state = ir_entity_usage_not_computed;
115
116         current_ir_graph = irp->const_code_irg;
117
118         return irp;
119 #undef IDENT
120 }
121
122 /* initializes ir_prog. Constructs only the basic lists. */
123 void init_irprog_1(void)
124 {
125         irp = new_incomplete_ir_prog();
126 }
127
128 /* Completes ir_prog. */
129 void init_irprog_2(void)
130 {
131         (void)complete_ir_prog(irp, INITAL_PROG_NAME);
132 }
133
134 /* Create a new ir prog. Automatically called by init_firm through
135    init_irprog. */
136 ir_prog *new_ir_prog(const char *name)
137 {
138         return complete_ir_prog(new_incomplete_ir_prog(), name);
139 }
140
141 /* frees all memory used by irp.  Types in type list, irgs in irg
142    list and entities in global type must be freed by hand before. */
143 void free_ir_prog(void)
144 {
145         ir_segment_t s;
146         for (s = IR_SEGMENT_FIRST; s <= IR_SEGMENT_LAST; ++s) {
147                 free_type(irp->segment_types[s]);
148         }
149
150         free_ir_graph(irp->const_code_irg);
151         DEL_ARR_F(irp->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         size_t i, l;
215
216         assert(irg);
217         l = ARR_LEN(irp->graphs);
218         for (i = 0; i < l; ++i) {
219                 if (irp->graphs[i] == irg) {
220                         for (; i < l - 1; ++i) {
221                                 irp->graphs[i] = irp->graphs[i+1];
222                         }
223                         ARR_SETLEN(ir_graph*, irp->graphs, l - 1);
224                         break;
225                 }
226         }
227 }
228
229 /* Removes irg from the list or irgs, shrinks the list by one. */
230 void remove_irp_irg(ir_graph *irg)
231 {
232         free_ir_graph(irg);
233         remove_irp_irg_from_list(irg);
234 }
235
236 size_t (get_irp_n_irgs)(void)
237 {
238         return get_irp_n_irgs_();
239 }
240
241 ir_graph *(get_irp_irg)(size_t pos)
242 {
243         return get_irp_irg_(pos);
244 }
245
246 size_t get_irp_last_idx(void)
247 {
248         return irp->max_irg_idx;
249 }
250
251 void set_irp_irg(size_t pos, ir_graph *irg)
252 {
253         assert(irp && irg);
254         assert(pos < ARR_LEN(irp->graphs));
255         irp->graphs[pos] = irg;
256 }
257
258 /* Adds type to the list of types in irp. */
259 void add_irp_type(ir_type *typ)
260 {
261         assert(typ != NULL);
262         assert(irp);
263         ARR_APP1(ir_type *, irp->types, typ);
264 }
265
266 /* Remove type from the list of types in irp. */
267 void remove_irp_type(ir_type *typ)
268 {
269         size_t i, l;
270         assert(typ);
271
272         l = ARR_LEN(irp->types);
273         for (i = 0; i < l; ++i) {
274                 if (irp->types[i] == typ) {
275                         for (; i < l - 1; ++i) {
276                                 irp->types[i] = irp->types[i+1];
277                         }
278                         ARR_SETLEN(ir_type *, irp->types, l - 1);
279                         break;
280                 }
281         }
282 }
283
284 size_t (get_irp_n_types) (void)
285 {
286         return get_irp_n_types_();
287 }
288
289 ir_type *(get_irp_type) (size_t pos)
290 {
291         return get_irp_type_(pos);
292 }
293
294 void set_irp_type(size_t pos, ir_type *typ)
295 {
296         assert(irp && typ);
297         assert(pos < ARR_LEN((irp)->types));
298         irp->types[pos] = typ;
299 }
300
301 /* Returns the number of all modes in the irp. */
302 size_t (get_irp_n_modes)(void)
303 {
304         return get_irp_n_modes_();
305 }
306
307 /* Returns the mode at position pos in the irp. */
308 ir_mode *(get_irp_mode)(size_t pos)
309 {
310         return get_irp_mode_(pos);
311 }
312
313 /* Adds mode to the list of modes in irp. */
314 void add_irp_mode(ir_mode *mode)
315 {
316         assert(mode != NULL);
317         assert(irp);
318         ARR_APP1(ir_mode *, irp->modes, mode);
319 }
320
321 /* Adds opcode to the list of opcodes in irp. */
322 void add_irp_opcode(ir_op *opcode)
323 {
324         size_t len;
325         size_t code;
326         assert(opcode != NULL);
327         assert(irp);
328         len  = ARR_LEN(irp->opcodes);
329         code = opcode->code;
330         if (code >= len) {
331                 ARR_RESIZE(ir_op*, irp->opcodes, code+1);
332                 memset(&irp->opcodes[len], 0, (code-len+1) * sizeof(irp->opcodes[0]));
333         }
334
335         assert(irp->opcodes[code] == NULL && "opcode registered twice");
336         irp->opcodes[code] = opcode;
337 }
338
339 /* Removes opcode from the list of opcodes and shrinks the list by one. */
340 void remove_irp_opcode(ir_op *opcode)
341 {
342         assert(opcode->code < ARR_LEN(irp->opcodes));
343         irp->opcodes[opcode->code] = NULL;
344 }
345
346 /* Returns the number of all opcodes in the irp. */
347 size_t (get_irp_n_opcodes)(void)
348 {
349         return get_irp_n_opcodes_();
350 }
351
352 /* Returns the opcode at position pos in the irp. */
353 ir_op *(get_irp_opcode)(size_t pos)
354 {
355         return get_irp_opcode_(pos);
356 }
357
358 /* Sets the generic function pointer of all opcodes to NULL */
359 void clear_irp_opcodes_generic_func(void)
360 {
361         size_t i, n;
362
363         for (i = 0, n = get_irp_n_opcodes(); i < n; ++i) {
364                 ir_op *op = get_irp_opcode(i);
365                 op->ops.generic = (op_func)NULL;
366         }
367 }
368
369 /*- File name / executable name or the like -*/
370 void set_irp_prog_name(ident *name)
371 {
372         irp->name = name;
373 }
374 int irp_prog_name_is_set(void)
375 {
376         return irp->name != new_id_from_str(INITAL_PROG_NAME);
377 }
378 ident *get_irp_ident(void)
379 {
380         return irp->name;
381 }
382 const char  *get_irp_name(void)
383 {
384         return get_id_str(irp->name);
385 }
386
387
388 ir_graph *(get_const_code_irg)(void)
389 {
390         return get_const_code_irg_();
391 }
392
393 irg_phase_state get_irp_phase_state(void)
394 {
395         return irp->phase_state;
396 }
397
398 void set_irp_phase_state(irg_phase_state s)
399 {
400         irp->phase_state = s;
401 }
402
403 typedef struct pass_t {
404         ir_prog_pass_t  pass;
405         irg_phase_state state;
406 } pass_t;
407
408 /**
409  * Wrapper for setting the state of a whole ir_prog.
410  */
411 static int set_irp_phase_state_wrapper(ir_prog *irp, void *context)
412 {
413         pass_t         *pass  = (pass_t *)context;
414         irg_phase_state state = pass->state;
415         size_t          i, n;
416
417         (void)irp;
418
419         /* set the phase of all graphs */
420         for (i = 0, n = get_irp_n_irgs(); i < n; ++i)
421                 set_irg_phase_state(get_irp_irg(i), state);
422
423         /* set the irp phase */
424         set_irp_phase_state(state);
425
426         return 0;
427 }
428
429 ir_prog_pass_t *set_irp_phase_state_pass(const char *name, irg_phase_state state)
430 {
431         struct pass_t *pass = XMALLOCZ(struct pass_t);
432
433         def_prog_pass_constructor(
434                 &pass->pass, name ? name : "set_irp_phase", set_irp_phase_state_wrapper);
435         pass->state = state;
436
437         /* no dump/verify */
438         pass->pass.verify_irprog = ir_prog_no_verify;
439         pass->pass.dump_irprog   = ir_prog_no_dump;
440
441         return &pass->pass;
442 }
443
444 void set_irp_ip_outedges(ir_node ** ip_outedges)
445 {
446         irp->ip_outedges = ip_outedges;
447 }
448
449 ir_node** get_irp_ip_outedges(void)
450 {
451         return irp->ip_outedges;
452 }
453
454 irg_callee_info_state get_irp_callee_info_state(void)
455 {
456         return irp->callee_info_state;
457 }
458
459 void set_irp_callee_info_state(irg_callee_info_state s)
460 {
461         irp->callee_info_state = s;
462 }
463
464 /* Returns a new, unique exception region number. */
465 ir_exc_region_t (get_irp_next_region_nr)(void)
466 {
467         return get_irp_next_region_nr_();
468 }
469
470 /* Returns a new, unique label number. */
471 ir_label_t (get_irp_next_label_nr)(void)
472 {
473         return get_irp_next_label_nr_();
474 }
475
476 /* Add a new global asm include */
477 void add_irp_asm(ident *asm_string)
478 {
479         ARR_APP1(ident *, irp->global_asms, asm_string);
480 }
481
482 /* Return the number of global asm includes. */
483 size_t get_irp_n_asms(void)
484 {
485         return ARR_LEN(irp->global_asms);
486 }
487
488 /* Return the global asm include at position pos. */
489 ident *get_irp_asm(size_t pos)
490 {
491         assert(pos < get_irp_n_asms());
492         return irp->global_asms[pos];
493 }
494
495 /** Return whether optimization dump vcg graphs */
496 int (get_irp_optimization_dumps)(void)
497 {
498         return get_irp_optimization_dumps_();
499 }
500
501 /** Enable vcg dumping of optimization */
502 void (enable_irp_optimization_dumps)(void)
503 {
504         enable_irp_optimization_dumps_();
505 }
506
507 #ifndef NDEBUG
508 void irp_reserve_resources(ir_prog *irp, irp_resources_t resources)
509 {
510         assert((irp->reserved_resources & resources) == 0);
511         irp->reserved_resources |= resources;
512 }
513
514 void irp_free_resources(ir_prog *irp, irp_resources_t resources)
515 {
516         assert((irp->reserved_resources & resources) == resources);
517         irp->reserved_resources &= ~resources;
518 }
519
520 irp_resources_t irp_resources_reserved(const ir_prog *irp)
521 {
522         return irp->reserved_resources;
523 }
524 #endif