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