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