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