don't put dbg into the environment
[libfirm] / ir / be / bemodule.c
1 /*
2  * Copyright (C) 1995-2007 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 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif /* HAVE_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_listsched(void);
40 void be_init_schedrss(void);
41 void be_init_chordal(void);
42 void be_init_chordal_main(void);
43 void be_init_copycoal(void);
44 void be_init_copyheur2(void);
45 void be_init_copyheur3(void);
46 void be_init_copyheur4(void);
47 void be_init_copystat(void);
48 void be_init_arch_ia32(void);
49 void be_init_arch_ppc32(void);
50 void be_init_arch_mips(void);
51 void be_init_arch_arm(void);
52 void be_init_arch_sta(void);
53 void be_init_ilpsched(void);
54 void be_init_copyilp(void);
55 void be_init_javacoal(void);
56 void be_init_ra(void);
57 void be_init_spillbelady(void);
58 void be_init_spillmorgan(void);
59 void be_init_spillremat(void);
60 void be_init_ssaconstr(void);
61 void be_init_ifg(void);
62 void be_init_irgmod(void);
63 void be_init_loopana(void);
64 void be_init_spillslots(void);
65 void be_init_live(void);
66 void be_init_state(void);
67
68 void be_quit_copystat(void);
69
70 /**
71  * Driver for module intialization.
72  * Call your module initialization function here.
73  */
74 void be_init_modules(void)
75 {
76         static int run_once = 0;
77
78         if (run_once)
79                 return;
80         run_once = 1;
81
82         be_init_irgmod();
83         be_init_loopana();
84         be_init_live();
85         be_init_spillslots();
86         be_init_sched();
87         be_init_blocksched();
88         be_init_spill();
89         be_init_listsched();
90         be_init_schedrss();
91         be_init_chordal_main();
92         be_init_chordal();
93         be_init_copycoal();
94         be_init_copyheur2();
95         be_init_copyheur4();
96         be_init_copystat();
97         be_init_ra();
98         be_init_spillbelady();
99         be_init_spillmorgan();
100         be_init_ssaconstr();
101         be_init_state();
102         be_init_ifg();
103
104         be_init_arch_ia32();
105         be_init_arch_ppc32();
106         be_init_arch_mips();
107         be_init_arch_arm();
108
109 #ifdef WITH_ILP
110         be_init_ilpsched();
111         be_init_copyilp();
112         be_init_spillremat();
113 #endif /* WITH_ILP */
114
115 #ifdef WITH_JVM
116         be_init_copyheur3();
117         be_init_javacoal();
118 #endif /* WITH_JVM */
119
120 #if PLUGIN_IR_BE_STA
121         be_init_arch_sta();
122 #endif /* PLUGIN_IR_BE_STA */
123 }
124
125 void be_quit_modules(void)
126 {
127         be_quit_copystat();
128 }
129
130 //---------------------------------------------------------------------------
131
132 typedef struct module_opt_data_t {
133         void **var;
134         be_module_list_entry_t * const *list_head;
135 } module_opt_data_t;
136
137 /**
138  * Searches in list for module option. If found, set option to given value and return true.
139  * Beware: return value of 0 means error.
140  */
141 static int set_opt_module(const char *name, lc_opt_type_t type, void *data,
142                           size_t length, ...)
143 {
144         module_opt_data_t            *moddata = data;
145         int                          res      = 0;
146         va_list                      args;
147         const char                   *opt;
148         const be_module_list_entry_t *module;
149
150         va_start(args, length);
151         opt = va_arg(args, const char*);
152
153         for (module = *(moddata->list_head); module != NULL; module = module->next) {
154                 if (strcmp(module->name, opt) == 0) {
155                         *(moddata->var) = module->data;
156                         res = 1;
157                         break;
158                 }
159         }
160         va_end(args);
161
162         return res;
163 }
164
165 /**
166  * Dump the names of all registered module options.
167  */
168 int dump_opt_module(char *buf, size_t buflen, const char *name,
169                     lc_opt_type_t type, void *data, size_t length)
170 {
171         module_opt_data_t            *moddata = data;
172         const be_module_list_entry_t *module;
173
174         for (module = *(moddata->list_head); module != NULL; module = module->next) {
175                 if (module->data == *(moddata->var)) {
176                         snprintf(buf, buflen, "%s", module->name);
177                         return strlen(buf);
178                 }
179         }
180
181         snprintf(buf, buflen, "none");
182         return strlen(buf);
183 }
184
185 /**
186  * Dump the values of all register module options.
187  */
188 int dump_opt_module_vals(char *buf, size_t buflen, const char *name,
189                          lc_opt_type_t type, void *data, size_t len)
190 {
191         module_opt_data_t            *moddata = data;
192         char                         *p       = buf;
193         const be_module_list_entry_t *module;
194
195         for (module = *(moddata->list_head); module != NULL; module = module->next) {
196                 size_t len = strlen(module->name);
197
198                 if (module != *(moddata->list_head)) {
199                         p       = strncat(p, ", ", buflen - 1);
200                         buflen -= 2;
201                 }
202
203                 p = strncat(p, module->name, buflen - 1);
204
205                 if (len >= buflen)
206                         break;
207
208                 buflen -= len;
209         }
210
211         return strlen(buf);
212 }
213
214 /**
215  * Add a new module to list.
216  */
217 void be_add_module_to_list(be_module_list_entry_t **list_head, const char *name,
218                            void *module)
219 {
220         be_module_list_entry_t *entry;
221
222     entry       = xmalloc(sizeof(entry[0]));
223         entry->name = name;
224         entry->data = module;
225         entry->next = *list_head;
226         *list_head  = entry;
227 }
228
229 /**
230  * Add an option for a module.
231  */
232 void be_add_module_list_opt(lc_opt_entry_t *grp, const char *name,
233                             const char *description,
234                             be_module_list_entry_t * const * list_head,
235                             void **var)
236 {
237         module_opt_data_t *moddata;
238
239         moddata            = xmalloc(sizeof(moddata[0]));
240         moddata->var       = var;
241         moddata->list_head = list_head;
242
243         lc_opt_add_opt(grp, name, description, lc_opt_type_enum,
244                        moddata, sizeof(moddata[0]),
245                        set_opt_module, dump_opt_module, dump_opt_module_vals,
246                                    NULL);
247 }