revive belady late spilling
[libfirm] / ir / be / bemodule.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       Backend module interface.
23  * @author      Matthias Braun
24  * @date        29.09.2005
25  * @version     $Id$
26  */
27 #include <config.h>
28
29 #include "firm_config.h"
30
31 #include <stdlib.h>
32
33 #include "bemodule_t.h"
34 #include "xmalloc.h"
35
36 void be_init_sched(void);
37 void be_init_blocksched(void);
38 void be_init_spill(void);
39 void be_init_spilloptions(void);
40 void be_init_listsched(void);
41 void be_init_schedrss(void);
42 void be_init_chordal(void);
43 void be_init_chordal_main(void);
44 void be_init_copycoal(void);
45 void be_init_copyheur2(void);
46 void be_init_copyheur3(void);
47 void be_init_copyheur4(void);
48 void be_init_copyheur5(void);
49 void be_init_copystat(void);
50 void be_init_daemelspill(void);
51 void be_init_dbgout(void);
52 void be_init_arch_ia32(void);
53 void be_init_arch_ppc32(void);
54 void be_init_arch_mips(void);
55 void be_init_arch_arm(void);
56 void be_init_arch_sta(void);
57 void be_init_arch_TEMPLATE(void);
58 void be_init_ilpsched(void);
59 void be_init_copyilp(void);
60 void be_init_javacoal(void);
61 void be_init_peephole(void);
62 void be_init_ra(void);
63 void be_init_spillbelady(void);
64 void be_init_spillbelady2(void);
65 void be_init_spillbelady3(void);
66 void be_init_spilllinearscan(void);
67 void be_init_ssaconstr(void);
68 void be_init_stabs(void);
69 void be_init_ifg(void);
70 void be_init_irgmod(void);
71 void be_init_loopana(void);
72 void be_init_spillslots(void);
73 void be_init_live(void);
74 void be_init_state(void);
75 void be_init_pbqp(void);
76
77 void be_quit_copystat(void);
78
79 /**
80  * Driver for module initialization.
81  * Call your module initialization function here.
82  */
83 void be_init_modules(void)
84 {
85         static int run_once = 0;
86
87         if (run_once)
88                 return;
89         run_once = 1;
90
91         be_init_irgmod();
92         be_init_loopana();
93         be_init_live();
94         be_init_spillslots();
95         be_init_sched();
96         be_init_blocksched();
97         be_init_spill();
98         be_init_spilloptions();
99         be_init_dbgout();
100         be_init_listsched();
101         be_init_schedrss();
102         be_init_chordal_main();
103         be_init_chordal();
104         be_init_copycoal();
105         be_init_copyheur2();
106         be_init_copyheur4();
107 //      be_init_copyheur5();
108         be_init_copystat();
109         be_init_peephole();
110         be_init_ra();
111         be_init_spillbelady();
112         be_init_spillbelady2();
113         be_init_spillbelady3();
114         be_init_spilllinearscan();
115         be_init_daemelspill();
116         be_init_ssaconstr();
117         be_init_state();
118         be_init_ifg();
119         be_init_stabs();
120
121         be_init_arch_ia32();
122         be_init_arch_ppc32();
123         be_init_arch_mips();
124         be_init_arch_arm();
125         be_init_arch_TEMPLATE();
126
127 #ifdef WITH_ILP
128         be_init_ilpsched();
129         be_init_copyilp();
130 #endif /* WITH_ILP */
131
132 #ifdef WITH_JVM
133         be_init_copyheur3();
134         be_init_javacoal();
135 #endif /* WITH_JVM */
136
137 #if PLUGIN_IR_BE_STA
138         be_init_arch_sta();
139 #endif /* PLUGIN_IR_BE_STA */
140
141 #ifdef FIRM_GRGEN_BE
142         be_init_pbqp();
143 #endif
144 }
145
146 void be_quit_modules(void)
147 {
148         be_quit_copystat();
149 }
150
151 //---------------------------------------------------------------------------
152
153 typedef struct module_opt_data_t {
154         void **var;
155         be_module_list_entry_t * const *list_head;
156 } module_opt_data_t;
157
158 /**
159  * Searches in list for module option. If found, set option to given value and return true.
160  * Beware: return value of 0 means error.
161  */
162 static int set_opt_module(const char *name, lc_opt_type_t type, void *data,
163                           size_t length, ...)
164 {
165         module_opt_data_t            *moddata = data;
166         int                          res      = 0;
167         va_list                      args;
168         const char                   *opt;
169         const be_module_list_entry_t *module;
170         (void) type;
171         (void) name;
172
173         va_start(args, length);
174         opt = va_arg(args, const char*);
175
176         for (module = *(moddata->list_head); module != NULL; module = module->next) {
177                 if (strcmp(module->name, opt) == 0) {
178                         *(moddata->var) = module->data;
179                         res = 1;
180                         break;
181                 }
182         }
183         va_end(args);
184
185         return res;
186 }
187
188 /**
189  * Dump the names of all registered module options.
190  */
191 int dump_opt_module(char *buf, size_t buflen, const char *name,
192                     lc_opt_type_t type, void *data, size_t length)
193 {
194         module_opt_data_t            *moddata = data;
195         const be_module_list_entry_t *module;
196         (void) name;
197         (void) type;
198         (void) length;
199
200         for (module = *(moddata->list_head); module != NULL; module = module->next) {
201                 if (module->data == *(moddata->var)) {
202                         snprintf(buf, buflen, "%s", module->name);
203                         return strlen(buf);
204                 }
205         }
206
207         snprintf(buf, buflen, "none");
208         return strlen(buf);
209 }
210
211 /**
212  * Dump the values of all register module options.
213  */
214 int dump_opt_module_vals(char *buf, size_t buflen, const char *name,
215                          lc_opt_type_t type, void *data, size_t len)
216 {
217         module_opt_data_t            *moddata = data;
218         char                         *p       = buf;
219         const be_module_list_entry_t *module;
220         (void) name;
221         (void) type;
222         (void) len;
223
224         for (module = *(moddata->list_head); module != NULL; module = module->next) {
225                 size_t len = strlen(module->name);
226
227                 if (module != *(moddata->list_head)) {
228                         p       = strncat(p, ", ", buflen - 1);
229                         buflen -= 2;
230                 }
231
232                 p = strncat(p, module->name, buflen - 1);
233
234                 if (len >= buflen)
235                         break;
236
237                 buflen -= len;
238         }
239
240         return strlen(buf);
241 }
242
243 /**
244  * Add a new module to list.
245  */
246 void be_add_module_to_list(be_module_list_entry_t **list_head, const char *name,
247                            void *module)
248 {
249         be_module_list_entry_t *entry;
250
251     entry       = xmalloc(sizeof(entry[0]));
252         entry->name = name;
253         entry->data = module;
254         entry->next = *list_head;
255         *list_head  = entry;
256 }
257
258 /**
259  * Add an option for a module.
260  */
261 void be_add_module_list_opt(lc_opt_entry_t *grp, const char *name,
262                             const char *description,
263                             be_module_list_entry_t * const * list_head,
264                             void **var)
265 {
266         module_opt_data_t *moddata;
267
268         moddata            = xmalloc(sizeof(moddata[0]));
269         moddata->var       = var;
270         moddata->list_head = list_head;
271
272         lc_opt_add_opt(grp, name, description, lc_opt_type_enum,
273                        moddata, sizeof(moddata[0]),
274                        set_opt_module, dump_opt_module, dump_opt_module_vals,
275                                    NULL);
276 }