0af60763cb243c57af77ca8201ad73929f1ad8a7
[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 "irgopt.h"
17 #include "irgraph.h"
18 #include "irdump.h"
19 #include "phiclass.h"
20
21 #include "be_t.h"
22 #include "bechordal_t.h"
23 #include "benumb_t.h"
24 #include "besched_t.h"
25 #include "belistsched.h"
26 #include "belive_t.h"
27 #include "beutil.h"
28 #include "bechordal.h"
29 #include "bearch.h"
30 #include "becopyoptmain.h"
31 #include "becopystat.h"
32 //#include "bessadestr.h"
33 #include "bearch_firm.h"
34 #include "benode_t.h"
35
36 #include "beasm_dump_globals.h"
37 #include "beasm_asm_gnu.h"
38
39 #undef DUMP_ALLOCATED
40 #undef DUMP_LOCALIZED
41
42 #define N_PHASES 256
43
44
45 void be_init(void)
46 {
47         be_sched_init();
48         be_liveness_init();
49         be_numbering_init();
50         be_ra_chordal_init();
51         be_copy_opt_init();
52 #ifdef DO_STAT
53         stat_init();
54 #endif
55 }
56
57 static void be_init_arch_env(arch_env_t *env)
58 {
59   const arch_isa_if_t *isa = &firm_isa;
60   be_node_factory_t *nf;
61
62   nf = be_new_node_factory(isa);
63
64   arch_env_init(env, isa);
65   env->isa->init();
66
67   arch_env_add_irn_handler(env, &firm_irn_handler);
68   arch_env_add_irn_handler(env, be_node_get_irn_handler(nf));
69 }
70
71 static void be_main_loop(void)
72 {
73         int i, n;
74         arch_env_t arch_env;
75         const arch_isa_if_t *isa;
76
77         be_init_arch_env(&arch_env);
78         isa = arch_env_get_isa(&arch_env);
79
80         for(i = 0, n = get_irp_n_irgs(); i < n; ++i) {
81                 int j, m;
82                 ir_graph *irg = get_irp_irg(i);
83
84     remove_critical_cf_edges(irg);
85
86                 localize_consts(irg);
87 #ifdef DUMP_LOCALIZED
88                 dump_consts_local(0);
89                 dump_ir_block_graph(irg, "-local-const");
90 #endif
91                 be_numbering(irg);
92
93                 /* Schedule the graphs. */
94                 list_sched(irg, trivial_selector);
95
96                 /* Liveness analysis */
97                 be_liveness(irg);
98
99 #ifdef DO_STAT
100                 stat_reset();
101 #endif
102                 /* Perform the following for each register class. */
103                 for(j = 0, m = isa->get_n_reg_class(); j < m; ++j) {
104                         be_chordal_env_t *chordal_env;
105                         const arch_register_class_t *cls = isa->get_reg_class(j);
106
107                         chordal_env = be_ra_chordal(irg, &arch_env, cls);
108
109 #ifdef DUMP_ALLOCATED
110                         dump_allocated_irg(&arch_env, irg, "");
111 #endif
112 #ifdef DO_STAT
113                         stat_collect_irg(irg);
114 #endif
115                         be_copy_opt(chordal_env);
116 //TODO                  be_ssa_destruction(chordal_env);
117                         be_ra_chordal_done(chordal_env);
118                 }
119 #ifdef DO_STAT
120                 stat_dump(irg);
121 #endif
122             be_numbering_done(irg);
123         }
124 }
125
126 void be_main(int argc, const char *argv[])
127 {
128         assembler_t *gnu_assembler;
129         FILE *asm_output_file;
130
131         be_main_loop();
132         gnu_assembler = gnuasm_create_assembler();
133         asm_output_file = fopen("asm_output.asm", "w");
134
135         asm_dump_globals(gnu_assembler);
136         gnuasm_dump(gnu_assembler, asm_output_file);
137         gnuasm_delete_assembler(gnu_assembler);
138         fclose(asm_output_file);
139 }