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