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