08a4576ca6bb7f73bf489e961f358bd871e66272
[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 "bearch.h"
28 #include "becopyoptmain.h"
29
30 #include "beasm_dump_globals.h"
31 #include "beasm_asm_gnu.h"
32
33 #undef DUMP_ALLOCATED
34 #undef DUMP_LOCALIZED
35
36 #define N_PHASES 256
37
38 typedef struct _be_graph_info_t {
39         bitset_t *applied_phases;
40 } be_graph_info_t;
41
42 static size_t be_info_offset = 0;
43
44 #define get_irg_be_info(irg) get_irg_data(irg, be_graph_info_t, be_info_offset)
45
46 static int phase_ids = 1;
47 static struct obstack obst;
48
49 int phase_register(phase_t *phase)
50 {
51         phase->id = phase_ids;
52         return phase_ids++;
53 }
54
55 void phase_applied(const ir_graph *irg, const phase_t *phase)
56 {
57         be_graph_info_t *info = get_irg_be_info(irg);
58
59         if(!info->applied_phases)
60                 info->applied_phases = bitset_obstack_alloc(&obst, N_PHASES);
61
62         bitset_set(info->applied_phases, phase->id);
63 }
64
65 int phase_depends_on(const ir_graph *irg, const phase_t *phase, int n, ...)
66 {
67         int errors = 0;
68         int i;
69         va_list args;
70
71         if(n > 0) {
72                 const be_graph_info_t *info = get_irg_be_info(irg);
73                 const bitset_t *applied_phases = info->applied_phases;
74
75                 va_start(args, n);
76
77                 for(i = 0; i < n; ++i) {
78                         const phase_t *dep_phase = va_arg(args, const phase_t *);
79
80                         if(!applied_phases || !bitset_is_set(applied_phases, dep_phase->id)) {
81                                 errors++;
82                                 fprintf(stderr, "phase dependency unfulfilled: \"%s\" depends on \"%s\"\n",
83                                                 phase->name, dep_phase->name);
84                         }
85                 }
86
87                 va_end(args);
88
89                 assert(errors > 0 && "There were phase dependency errors");
90         }
91
92         return errors;
93 }
94
95 void be_init(void)
96 {
97         obstack_init(&obst);
98         be_info_offset = register_additional_graph_data(sizeof(be_graph_info_t));
99
100         be_sched_init();
101         be_liveness_init();
102         be_numbering_init();
103         be_ra_init();
104         be_ra_chordal_init();
105         be_copy_opt_init();
106 }
107
108 /* The preliminary Firm backend isa. */
109 extern arch_isa_if_t arch_isa_if_firm;
110
111 static void be_main_loop(void)
112 {
113         int i, n;
114   const arch_isa_if_t *isa = &arch_isa_if_firm;
115
116         for(i = 0, n = get_irp_n_irgs(); i < n; ++i) {
117     int j, m;
118
119                 ir_graph *irg = get_irp_irg(i);
120
121                 localize_consts(irg);
122 #ifdef DUMP_LOCALIZED
123                 dump_consts_local(0);
124                 dump_ir_block_graph(irg, "-local-const");
125 #endif
126                 be_numbering(irg);
127
128     /* Schedule the graphs. */
129                 list_sched(irg, trivial_selector);
130
131     /* Liveness analysis */
132     be_liveness(irg);
133
134     /*
135      * Perform the following for each register
136      * class.
137      */
138     for(j = 0, m = isa->get_n_reg_class(); j < m; ++j) {
139       const arch_register_class_t *cls = isa->get_reg_class(j);
140
141       be_ra_chordal(irg, isa, cls);
142
143 #ifdef DUMP_ALLOCATED
144       dump_allocated_irg(irg, "");
145 #endif
146       // be_copy_opt(irg);
147       be_ra_chordal_done(irg);
148     }
149
150     be_numbering_done(irg);
151         }
152 }
153
154 void be_main(int argc, const char *argv[])
155 {
156   assembler_t *gnu_assembler;
157   FILE *asm_output_file;
158
159         be_main_loop();
160         gnu_assembler = gnuasm_create_assembler();
161         asm_output_file = fopen("asm_output.asm", "w");
162
163         asm_dump_globals(gnu_assembler);
164         gnuasm_dump(gnu_assembler, asm_output_file);
165         gnuasm_delete_assembler(gnu_assembler);
166         fclose(asm_output_file);
167 }