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