remove general ifg interface and always use ifg_std, this considerably simplifies...
[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 <stdlib.h>
30 #include <stdbool.h>
31
32 #include "bemodule_t.h"
33 #include "xmalloc.h"
34
35 void be_init_abi(void);
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_pbqp_coloring(void);
44 void be_init_chordal_main(void);
45 void be_init_chordal_common(void);
46 void be_init_copyopt(void);
47 void be_init_copyheur(void);
48 void be_init_copyheur2(void);
49 void be_init_copyheur4(void);
50 void be_init_copyilp2(void);
51 void be_init_copypbqp(void);
52 void be_init_copynone(void);
53 void be_init_copystat(void);
54 void be_init_daemelspill(void);
55 void be_init_dbgout(void);
56 void be_init_arch_ia32(void);
57 void be_init_arch_ppc32(void);
58 void be_init_arch_mips(void);
59 void be_init_arch_arm(void);
60 void be_init_arch_sparc(void);
61 void be_init_arch_amd64(void);
62 void be_init_arch_sta(void);
63 void be_init_arch_sparc(void);
64 void be_init_arch_TEMPLATE(void);
65 void be_init_ilpsched(void);
66 void be_init_copyilp(void);
67 void be_init_peephole(void);
68 void be_init_ra(void);
69 void be_init_spillbelady(void);
70 void be_init_spillbelady2(void);
71 void be_init_spillbelady3(void);
72 void be_init_ssaconstr(void);
73 void be_init_stabs(void);
74 void be_init_pref_alloc(void);
75 void be_init_irgmod(void);
76 void be_init_loopana(void);
77 void be_init_spillslots(void);
78 void be_init_live(void);
79 void be_init_state(void);
80 void be_init_pbqp(void);
81
82 void be_quit_copystat(void);
83 void be_quit_pbqp(void);
84
85 /**
86  * Driver for module initialization.
87  * Call your module initialization function here.
88  */
89 void be_init_modules(void)
90 {
91         static bool run_once = false;
92
93         if (run_once)
94                 return;
95         run_once = true;
96
97         be_init_abi();
98         be_init_irgmod();
99         be_init_loopana();
100         be_init_live();
101         be_init_spillslots();
102         be_init_sched();
103         be_init_blocksched();
104         be_init_spill();
105         be_init_spilloptions();
106         be_init_dbgout();
107         be_init_listsched();
108         be_init_schedrss();
109         be_init_chordal_main();
110         be_init_chordal_common();
111         be_init_chordal();
112         be_init_copyopt();
113         be_init_copyheur4();
114         be_init_copyheur();
115         be_init_copyheur2();
116 #ifdef WITH_ILP
117         be_init_copyilp2();
118 #endif
119 #ifdef FIRM_KAPS
120         be_init_pbqp_coloring();
121         be_init_copypbqp();
122 #endif
123         be_init_copynone();
124         be_init_copystat();
125         be_init_peephole();
126         be_init_ra();
127         be_init_spillbelady();
128         be_init_spillbelady2();
129         be_init_spillbelady3();
130         be_init_daemelspill();
131         be_init_ssaconstr();
132         be_init_pref_alloc();
133         be_init_state();
134         be_init_stabs();
135
136         be_init_arch_ia32();
137         be_init_arch_ppc32();
138         be_init_arch_mips();
139         be_init_arch_arm();
140         be_init_arch_sparc();
141         be_init_arch_amd64();
142         be_init_arch_TEMPLATE();
143
144 #ifdef WITH_ILP
145         be_init_ilpsched();
146         be_init_copyilp();
147 #endif /* WITH_ILP */
148
149 #if PLUGIN_IR_BE_STA
150         be_init_arch_sta();
151 #endif /* PLUGIN_IR_BE_STA */
152
153 #ifdef FIRM_GRGEN_BE
154         be_init_pbqp();
155 #endif
156 }
157
158 void be_quit_modules(void)
159 {
160         be_quit_copystat();
161 #ifdef FIRM_GRGEN_BE
162         be_quit_pbqp();
163 #endif
164 }
165
166 //---------------------------------------------------------------------------
167
168 typedef struct module_opt_data_t {
169         void **var;
170         be_module_list_entry_t * const *list_head;
171 } module_opt_data_t;
172
173 /**
174  * Searches in list for module option. If found, set option to given value and return true.
175  * Beware: return value of 0 means error.
176  */
177 static int set_opt_module(const char *name, lc_opt_type_t type, void *data,
178                           size_t length, ...)
179 {
180         module_opt_data_t            *moddata = data;
181         int                          res      = 0;
182         va_list                      args;
183         const char                   *opt;
184         const be_module_list_entry_t *module;
185         (void) type;
186         (void) name;
187
188         va_start(args, length);
189         opt = va_arg(args, const char*);
190
191         for (module = *(moddata->list_head); module != NULL; module = module->next) {
192                 if (strcmp(module->name, opt) == 0) {
193                         *(moddata->var) = module->data;
194                         res = 1;
195                         break;
196                 }
197         }
198         va_end(args);
199
200         return res;
201 }
202
203 /**
204  * Dump the names of all registered module options.
205  */
206 static int dump_opt_module(char *buf, size_t buflen, const char *name,
207                            lc_opt_type_t type, void *data, size_t length)
208 {
209         module_opt_data_t            *moddata = data;
210         const be_module_list_entry_t *module;
211         (void) name;
212         (void) type;
213         (void) length;
214
215         for (module = *(moddata->list_head); module != NULL; module = module->next) {
216                 if (module->data == *(moddata->var)) {
217                         snprintf(buf, buflen, "%s", module->name);
218                         return strlen(buf);
219                 }
220         }
221
222         snprintf(buf, buflen, "none");
223         return strlen(buf);
224 }
225
226 /**
227  * Dump the values of all register module options.
228  */
229 static int dump_opt_module_vals(char *buf, size_t buflen, const char *name,
230                                 lc_opt_type_t type, void *data, size_t len)
231 {
232         module_opt_data_t            *moddata = data;
233         char                         *p       = buf;
234         const be_module_list_entry_t *module;
235         (void) name;
236         (void) type;
237         (void) len;
238
239         for (module = *(moddata->list_head); module != NULL; module = module->next) {
240                 size_t len = strlen(module->name);
241
242                 if (module != *(moddata->list_head)) {
243                         p       = strncat(p, ", ", buflen - 1);
244                         buflen -= 2;
245                 }
246
247                 p = strncat(p, module->name, buflen - 1);
248
249                 if (len >= buflen)
250                         break;
251
252                 buflen -= len;
253         }
254
255         return strlen(buf);
256 }
257
258 /**
259  * Add a new module to list.
260  */
261 void be_add_module_to_list(be_module_list_entry_t **list_head, const char *name,
262                            void *module)
263 {
264         be_module_list_entry_t *entry = XMALLOC(be_module_list_entry_t);
265         entry->name = name;
266         entry->data = module;
267         entry->next = *list_head;
268         *list_head  = entry;
269 }
270
271 /**
272  * Add an option for a module.
273  */
274 void be_add_module_list_opt(lc_opt_entry_t *grp, const char *name,
275                             const char *description,
276                             be_module_list_entry_t * const * list_head,
277                             void **var)
278 {
279         module_opt_data_t *moddata = XMALLOC(module_opt_data_t);
280         moddata->var       = var;
281         moddata->list_head = list_head;
282
283         lc_opt_add_opt(grp, name, description, lc_opt_type_enum,
284                        moddata, sizeof(moddata[0]),
285                        set_opt_module, dump_opt_module, dump_opt_module_vals,
286                                    NULL);
287 }