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