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