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