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