Added new arch interface
[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 "bera_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_init();
48         be_ra_chordal_init();
49         be_copy_opt_init();
50 #ifdef DO_STAT
51         stat_init();
52 #endif
53 }
54
55 static void be_init_arch_env(arch_env_t *env)
56 {
57   arch_env_init(env, &firm_isa);
58   arch_env_add_irn_handler(env, &firm_irn_handler);
59
60   env->isa->init();
61 }
62
63 static void be_main_loop(void)
64 {
65         int i, n;
66   arch_env_t env;
67   const arch_isa_if_t *isa;
68
69   be_init_arch_env(&env);
70
71   isa = arch_env_get_isa(&env);
72
73         for(i = 0, n = get_irp_n_irgs(); i < n; ++i) {
74                 int j, m;
75                 ir_graph *irg = get_irp_irg(i);
76
77                 localize_consts(irg);
78 #ifdef DUMP_LOCALIZED
79                 dump_consts_local(0);
80                 dump_ir_block_graph(irg, "-local-const");
81 #endif
82                 be_numbering(irg);
83
84                 /* Schedule the graphs. */
85                 list_sched(irg, trivial_selector);
86
87                 /* Liveness analysis */
88                 be_liveness(irg);
89
90 #ifdef DO_STAT
91                 stat_reset();
92 #endif
93                 /* Perform the following for each register class. */
94                 for(j = 0, m = isa->get_n_reg_class(); j < m; ++j) {
95                         const arch_register_class_t *cls = isa->get_reg_class(j);
96
97                         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(irg, isa, cls);
106                         be_ra_chordal_done(irg);
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 }