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