removed C99 features
[libfirm] / ir / be / bemain.c
1 /**
2  * Backend driver.
3  * @author Sebastian Hack
4  * @date 25.11.2004
5  */
6 #ifdef HAVE_CONFIG_H
7 #include "config.h"
8 #endif
9
10 #include <stdarg.h>
11
12 #include "obst.h"
13 #include "bitset.h"
14
15 #include "irprog.h"
16 #include "irgraph.h"
17 #include "irdump.h"
18
19 #include "be_t.h"
20 #include "bera_t.h"
21 #include "benumb_t.h"
22 #include "besched_t.h"
23 #include "belistsched.h"
24 #include "belive_t.h"
25 #include "beutil.h"
26 #include "bechordal.h"
27 #include "bechordal.h"
28 #include "bephiopt.h"
29 #include "phistat.h"
30
31 #include "beasm_dump_globals.h"
32 #include "beasm_asm_gnu.h"
33
34 #define DUMP_ALLOCATED
35 #undef DUMP_LOCALIZED
36
37 #define N_PHASES 256
38
39 typedef struct _be_graph_info_t {
40         bitset_t *applied_phases;
41 } be_graph_info_t;
42
43 static size_t be_info_offset = 0;
44
45 #define get_irg_be_info(irg) get_irg_data(irg, be_graph_info_t, be_info_offset)
46
47 static int phase_ids = 1;
48 static struct obstack obst;
49
50 int phase_register(phase_t *phase)
51 {
52         phase->id = phase_ids;
53         return phase_ids++;
54 }
55
56 void phase_applied(const ir_graph *irg, const phase_t *phase)
57 {
58         be_graph_info_t *info = get_irg_be_info(irg);
59
60         if(!info->applied_phases)
61                 info->applied_phases = bitset_obstack_alloc(&obst, N_PHASES);
62
63         bitset_set(info->applied_phases, phase->id);
64 }
65
66 int phase_depends_on(const ir_graph *irg, const phase_t *phase, int n, ...)
67 {
68         int errors = 0;
69         int i;
70         va_list args;
71
72         if(n > 0) {
73                 const be_graph_info_t *info = get_irg_be_info(irg);
74                 const bitset_t *applied_phases = info->applied_phases;
75
76                 va_start(args, n);
77
78                 for(i = 0; i < n; ++i) {
79                         const phase_t *dep_phase = va_arg(args, const phase_t *);
80
81                         if(!applied_phases || !bitset_is_set(applied_phases, dep_phase->id)) {
82                                 errors++;
83                                 fprintf(stderr, "phase dependency unfulfilled: \"%s\" depends on \"%s\"\n",
84                                                 phase->name, dep_phase->name);
85                         }
86                 }
87
88                 va_end(args);
89
90                 assert(errors > 0 && "There were phase dependency errors");
91         }
92
93         return errors;
94 }
95
96 void be_init(void)
97 {
98         obstack_init(&obst);
99         be_info_offset = register_additional_graph_data(sizeof(be_graph_info_t));
100
101         be_sched_init();
102         be_liveness_init();
103         be_numbering_init();
104         be_ra_init();
105         be_ra_chordal_init();
106         be_phi_opt_init();
107 }
108
109 extern void be_ra_chordal(ir_graph *irg);
110
111 static void be_main_loop(void)
112 {
113         int i, n;
114
115         for(i = 0, n = get_irp_n_irgs(); i < n; ++i) {
116                 ir_graph *irg = get_irp_irg(i);
117
118                 localize_consts(irg);
119 #ifdef DUMP_LOCALIZED
120                 dump_consts_local(0);
121                 dump_ir_block_graph(irg, "-local-const");
122 #endif
123                 be_numbering(irg);
124                 list_sched(irg, trivial_selector, NULL);
125                 be_liveness(irg);
126                 be_ra_chordal(irg);
127
128 #ifdef DUMP_ALLOCATED
129                 dump_allocated_irg(irg, "");
130 #endif
131                 //be_phi_opt(irg);
132
133                 be_ra_chordal_done(irg);
134                 be_numbering_done(irg);
135         }
136 }
137
138 void be_main(int argc, const char *argv[])
139 {
140   assembler_t *gnu_assembler;
141   FILE *asm_output_file;
142
143         be_main_loop();
144         gnu_assembler = gnuasm_create_assembler();
145         asm_output_file = fopen("asm_output.asm", "w");
146
147         asm_dump_globals ( gnu_assembler );
148         gnuasm_dump ( gnu_assembler, asm_output_file );
149         gnuasm_delete_assembler ( gnu_assembler );
150         fclose(asm_output_file);
151 }