we don't need no stinking selfs
[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_ssaconstr(void);
67 void be_init_stabs(void);
68 void be_init_ifg(void);
69 void be_init_irgmod(void);
70 void be_init_loopana(void);
71 void be_init_spillslots(void);
72 void be_init_live(void);
73 void be_init_state(void);
74 void be_init_pbqp(void);
75
76 void be_quit_copystat(void);
77
78 /**
79  * Driver for module initialization.
80  * Call your module initialization function here.
81  */
82 void be_init_modules(void)
83 {
84         static int run_once = 0;
85
86         if (run_once)
87                 return;
88         run_once = 1;
89
90         be_init_irgmod();
91         be_init_loopana();
92         be_init_live();
93         be_init_spillslots();
94         be_init_sched();
95         be_init_blocksched();
96         be_init_spill();
97         be_init_spilloptions();
98         be_init_dbgout();
99         be_init_listsched();
100         be_init_schedrss();
101         be_init_chordal_main();
102         be_init_chordal();
103         be_init_copycoal();
104         be_init_copyheur2();
105         be_init_copyheur4();
106 //      be_init_copyheur5();
107         be_init_copystat();
108         be_init_peephole();
109         be_init_ra();
110         be_init_spillbelady();
111         be_init_spillbelady2();
112         be_init_spillbelady3();
113         be_init_daemelspill();
114         be_init_ssaconstr();
115         be_init_state();
116         be_init_ifg();
117         be_init_stabs();
118
119         be_init_arch_ia32();
120         be_init_arch_ppc32();
121         be_init_arch_mips();
122         be_init_arch_arm();
123         /* do NOT call be_init_arch_TEMPLATE() here, this is NOT a backend :-) */
124
125 #ifdef WITH_ILP
126         be_init_ilpsched();
127         be_init_copyilp();
128 #endif /* WITH_ILP */
129
130 #ifdef WITH_JVM
131         be_init_copyheur3();
132         be_init_javacoal();
133 #endif /* WITH_JVM */
134
135 #if PLUGIN_IR_BE_STA
136         be_init_arch_sta();
137 #endif /* PLUGIN_IR_BE_STA */
138
139 #ifdef FIRM_GRGEN_BE
140         be_init_pbqp();
141 #endif
142 }
143
144 void be_quit_modules(void)
145 {
146         be_quit_copystat();
147 }
148
149 //---------------------------------------------------------------------------
150
151 typedef struct module_opt_data_t {
152         void **var;
153         be_module_list_entry_t * const *list_head;
154 } module_opt_data_t;
155
156 /**
157  * Searches in list for module option. If found, set option to given value and return true.
158  * Beware: return value of 0 means error.
159  */
160 static int set_opt_module(const char *name, lc_opt_type_t type, void *data,
161                           size_t length, ...)
162 {
163         module_opt_data_t            *moddata = data;
164         int                          res      = 0;
165         va_list                      args;
166         const char                   *opt;
167         const be_module_list_entry_t *module;
168         (void) type;
169         (void) name;
170
171         va_start(args, length);
172         opt = va_arg(args, const char*);
173
174         for (module = *(moddata->list_head); module != NULL; module = module->next) {
175                 if (strcmp(module->name, opt) == 0) {
176                         *(moddata->var) = module->data;
177                         res = 1;
178                         break;
179                 }
180         }
181         va_end(args);
182
183         return res;
184 }
185
186 /**
187  * Dump the names of all registered module options.
188  */
189 int dump_opt_module(char *buf, size_t buflen, const char *name,
190                     lc_opt_type_t type, void *data, size_t length)
191 {
192         module_opt_data_t            *moddata = data;
193         const be_module_list_entry_t *module;
194         (void) name;
195         (void) type;
196         (void) length;
197
198         for (module = *(moddata->list_head); module != NULL; module = module->next) {
199                 if (module->data == *(moddata->var)) {
200                         snprintf(buf, buflen, "%s", module->name);
201                         return strlen(buf);
202                 }
203         }
204
205         snprintf(buf, buflen, "none");
206         return strlen(buf);
207 }
208
209 /**
210  * Dump the values of all register module options.
211  */
212 int dump_opt_module_vals(char *buf, size_t buflen, const char *name,
213                          lc_opt_type_t type, void *data, size_t len)
214 {
215         module_opt_data_t            *moddata = data;
216         char                         *p       = buf;
217         const be_module_list_entry_t *module;
218         (void) name;
219         (void) type;
220         (void) len;
221
222         for (module = *(moddata->list_head); module != NULL; module = module->next) {
223                 size_t len = strlen(module->name);
224
225                 if (module != *(moddata->list_head)) {
226                         p       = strncat(p, ", ", buflen - 1);
227                         buflen -= 2;
228                 }
229
230                 p = strncat(p, module->name, buflen - 1);
231
232                 if (len >= buflen)
233                         break;
234
235                 buflen -= len;
236         }
237
238         return strlen(buf);
239 }
240
241 /**
242  * Add a new module to list.
243  */
244 void be_add_module_to_list(be_module_list_entry_t **list_head, const char *name,
245                            void *module)
246 {
247         be_module_list_entry_t *entry;
248
249     entry       = xmalloc(sizeof(entry[0]));
250         entry->name = name;
251         entry->data = module;
252         entry->next = *list_head;
253         *list_head  = entry;
254 }
255
256 /**
257  * Add an option for a module.
258  */
259 void be_add_module_list_opt(lc_opt_entry_t *grp, const char *name,
260                             const char *description,
261                             be_module_list_entry_t * const * list_head,
262                             void **var)
263 {
264         module_opt_data_t *moddata;
265
266         moddata            = xmalloc(sizeof(moddata[0]));
267         moddata->var       = var;
268         moddata->list_head = list_head;
269
270         lc_opt_add_opt(grp, name, description, lc_opt_type_enum,
271                        moddata, sizeof(moddata[0]),
272                        set_opt_module, dump_opt_module, dump_opt_module_vals,
273                                    NULL);
274 }