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