be: let begnuas call be_dbg_method_begin/end
[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_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_copyilp(void);
66 void be_init_peephole(void);
67 void be_init_ra(void);
68 void be_init_spillbelady(void);
69 void be_init_ssaconstr(void);
70 void be_init_stabs(void);
71 void be_init_dwarf(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         be_init_copyilp2();
121         be_init_pbqp_coloring();
122         be_init_copynone();
123         be_init_copystat();
124         be_init_peephole();
125         be_init_ra();
126         be_init_spillbelady();
127         be_init_daemelspill();
128         be_init_ssaconstr();
129         be_init_pref_alloc();
130         be_init_state();
131         be_init_stabs();
132         be_init_dwarf();
133
134         be_init_arch_ia32();
135         be_init_arch_arm();
136         be_init_arch_sparc();
137         be_init_arch_amd64();
138         be_init_arch_TEMPLATE();
139
140         be_init_copyilp();
141
142 #if PLUGIN_IR_BE_STA
143         be_init_arch_sta();
144 #endif /* PLUGIN_IR_BE_STA */
145
146 #ifdef FIRM_GRGEN_BE
147         be_init_pbqp();
148 #endif
149 }
150
151 void be_quit_modules(void)
152 {
153         be_quit_copystat();
154 #ifdef FIRM_GRGEN_BE
155         be_quit_pbqp();
156 #endif
157 }
158
159 //---------------------------------------------------------------------------
160
161 typedef struct module_opt_data_t {
162         void **var;
163         be_module_list_entry_t * const *list_head;
164 } module_opt_data_t;
165
166 /**
167  * Searches in list for module option. If found, set option to given value and return true.
168  * Beware: return value of 0 means error.
169  */
170 static int set_opt_module(const char *name, lc_opt_type_t type, void *data,
171                           size_t length, ...)
172 {
173         module_opt_data_t            *moddata = (module_opt_data_t*)data;
174         int                          res      = 0;
175         va_list                      args;
176         const char                   *opt;
177         const be_module_list_entry_t *module;
178         (void) type;
179         (void) name;
180
181         va_start(args, length);
182         opt = va_arg(args, const char*);
183
184         for (module = *(moddata->list_head); module != NULL; module = module->next) {
185                 if (strcmp(module->name, opt) == 0) {
186                         *(moddata->var) = module->data;
187                         res = 1;
188                         break;
189                 }
190         }
191         va_end(args);
192
193         return res;
194 }
195
196 /**
197  * Dump the names of all registered module options.
198  */
199 static int dump_opt_module(char *buf, size_t buflen, const char *name,
200                            lc_opt_type_t type, void *data, size_t length)
201 {
202         module_opt_data_t            *moddata = (module_opt_data_t*)data;
203         const be_module_list_entry_t *module;
204         (void) name;
205         (void) type;
206         (void) length;
207
208         for (module = *(moddata->list_head); module != NULL; module = module->next) {
209                 if (module->data == *(moddata->var)) {
210                         snprintf(buf, buflen, "%s", module->name);
211                         return strlen(buf);
212                 }
213         }
214
215         snprintf(buf, buflen, "none");
216         return strlen(buf);
217 }
218
219 /**
220  * Dump the values of all register module options.
221  */
222 static int dump_opt_module_vals(char *buf, size_t buflen, const char *name,
223                                 lc_opt_type_t type, void *data, size_t len)
224 {
225         module_opt_data_t            *moddata = (module_opt_data_t*)data;
226         char                         *p       = buf;
227         const be_module_list_entry_t *module;
228         (void) name;
229         (void) type;
230         (void) len;
231
232         for (module = *(moddata->list_head); module != NULL; module = module->next) {
233                 size_t name_len = strlen(module->name);
234
235                 if (module != *(moddata->list_head)) {
236                         p       = strncat(p, ", ", buflen - 1);
237                         buflen -= 2;
238                 }
239
240                 p = strncat(p, module->name, buflen - 1);
241
242                 if (name_len >= buflen)
243                         break;
244
245                 buflen -= name_len;
246         }
247
248         return strlen(buf);
249 }
250
251 /**
252  * Add a new module to list.
253  */
254 void be_add_module_to_list(be_module_list_entry_t **list_head, const char *name,
255                            void *module)
256 {
257         be_module_list_entry_t *entry = XMALLOC(be_module_list_entry_t);
258         entry->name = name;
259         entry->data = module;
260         entry->next = *list_head;
261         *list_head  = entry;
262 }
263
264 /**
265  * Add an option for a module.
266  */
267 void be_add_module_list_opt(lc_opt_entry_t *grp, const char *name,
268                             const char *description,
269                             be_module_list_entry_t * const * list_head,
270                             void **var)
271 {
272         module_opt_data_t *moddata = XMALLOC(module_opt_data_t);
273         moddata->var       = var;
274         moddata->list_head = list_head;
275
276         lc_opt_add_opt(grp, name, description, lc_opt_type_enum,
277                        moddata, sizeof(moddata[0]),
278                        set_opt_module, dump_opt_module, dump_opt_module_vals,
279                                    NULL);
280 }