sparc: Add missing #include <stdint.h>.
[libfirm] / ir / be / bemodule.c
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @brief       Backend module interface.
9  * @author      Matthias Braun
10  * @date        29.09.2005
11  */
12 #include "config.h"
13
14 #include <stdlib.h>
15 #include <stdbool.h>
16
17 #include "bemodule_t.h"
18 #include "xmalloc.h"
19
20 void be_init_abi(void);
21 void be_init_sched(void);
22 void be_init_blocksched(void);
23 void be_init_spill(void);
24 void be_init_spilloptions(void);
25 void be_init_listsched(void);
26 void be_init_sched_rand(void);
27 void be_init_sched_normal(void);
28 void be_init_sched_regpress(void);
29 void be_init_sched_trace(void);
30 void be_init_sched_trivial(void);
31 void be_init_chordal(void);
32 void be_init_pbqp_coloring(void);
33 void be_init_chordal_main(void);
34 void be_init_chordal_common(void);
35 void be_init_copyopt(void);
36 void be_init_copyheur(void);
37 void be_init_copyheur2(void);
38 void be_init_copyheur4(void);
39 void be_init_copyilp2(void);
40 void be_init_copynone(void);
41 void be_init_copystat(void);
42 void be_init_daemelspill(void);
43 void be_init_dwarf(void);
44 void be_init_arch_ia32(void);
45 void be_init_arch_arm(void);
46 void be_init_arch_amd64(void);
47 void be_init_arch_sparc(void);
48 void be_init_arch_TEMPLATE(void);
49 void be_init_copyilp(void);
50 void be_init_peephole(void);
51 void be_init_ra(void);
52 void be_init_spillbelady(void);
53 void be_init_ssaconstr(void);
54 void be_init_pref_alloc(void);
55 void be_init_irgmod(void);
56 void be_init_loopana(void);
57 void be_init_spillslots(void);
58 void be_init_live(void);
59 void be_init_state(void);
60 void be_init_pbqp(void);
61
62 void be_quit_copystat(void);
63 void be_quit_pbqp(void);
64
65 /**
66  * Driver for module initialization.
67  * Call your module initialization function here.
68  */
69 void be_init_modules(void)
70 {
71         static bool run_once = false;
72
73         if (run_once)
74                 return;
75         run_once = true;
76
77         be_init_abi();
78         be_init_irgmod();
79         be_init_loopana();
80         be_init_live();
81         be_init_spillslots();
82         be_init_sched();
83         be_init_blocksched();
84         be_init_spill();
85         be_init_spilloptions();
86
87         be_init_listsched();
88         be_init_sched_normal();
89         be_init_sched_trace();
90         be_init_sched_regpress();
91         be_init_sched_rand();
92         be_init_sched_trivial();
93
94         be_init_chordal_main();
95         be_init_chordal_common();
96         be_init_chordal();
97         be_init_copyopt();
98         be_init_copyheur4();
99         be_init_copyheur();
100         be_init_copyheur2();
101         be_init_copyilp2();
102         be_init_pbqp_coloring();
103         be_init_copynone();
104         be_init_copystat();
105         be_init_peephole();
106         be_init_ra();
107         be_init_spillbelady();
108         be_init_daemelspill();
109         be_init_dwarf();
110         be_init_ssaconstr();
111         be_init_pref_alloc();
112         be_init_state();
113
114         be_init_arch_ia32();
115         be_init_arch_arm();
116         be_init_arch_sparc();
117         be_init_arch_amd64();
118         be_init_arch_TEMPLATE();
119
120         be_init_copyilp();
121
122 #ifdef FIRM_GRGEN_BE
123         be_init_pbqp();
124 #endif
125 }
126
127 void be_quit_modules(void)
128 {
129         be_quit_copystat();
130 #ifdef FIRM_GRGEN_BE
131         be_quit_pbqp();
132 #endif
133 }
134
135 //---------------------------------------------------------------------------
136
137 typedef struct module_opt_data_t {
138         void **var;
139         be_module_list_entry_t * const *list_head;
140 } module_opt_data_t;
141
142 /**
143  * Searches in list for module option. If found, set option to given value and return true.
144  * Beware: return value of 0 means error.
145  */
146 static int set_opt_module(const char *name, lc_opt_type_t type, void *data,
147                           size_t length, ...)
148 {
149         module_opt_data_t            *moddata = (module_opt_data_t*)data;
150         int                          res      = 0;
151         va_list                      args;
152         const char                   *opt;
153         const be_module_list_entry_t *module;
154         (void) type;
155         (void) name;
156
157         va_start(args, length);
158         opt = va_arg(args, const char*);
159
160         for (module = *(moddata->list_head); module != NULL; module = module->next) {
161                 if (strcmp(module->name, opt) == 0) {
162                         *(moddata->var) = module->data;
163                         res = 1;
164                         break;
165                 }
166         }
167         va_end(args);
168
169         return res;
170 }
171
172 /**
173  * Dump the names of all registered module options.
174  */
175 static int dump_opt_module(char *buf, size_t buflen, const char *name,
176                            lc_opt_type_t type, void *data, size_t length)
177 {
178         module_opt_data_t            *moddata = (module_opt_data_t*)data;
179         const be_module_list_entry_t *module;
180         (void) name;
181         (void) type;
182         (void) length;
183
184         for (module = *(moddata->list_head); module != NULL; module = module->next) {
185                 if (module->data == *(moddata->var)) {
186                         snprintf(buf, buflen, "%s", module->name);
187                         return strlen(buf);
188                 }
189         }
190
191         snprintf(buf, buflen, "none");
192         return strlen(buf);
193 }
194
195 /**
196  * Dump the values of all register module options.
197  */
198 static int dump_opt_module_vals(char *buf, size_t buflen, const char *name,
199                                 lc_opt_type_t type, void *data, size_t len)
200 {
201         module_opt_data_t            *moddata = (module_opt_data_t*)data;
202         char                         *p       = buf;
203         const be_module_list_entry_t *module;
204         (void) name;
205         (void) type;
206         (void) len;
207
208         for (module = *(moddata->list_head); module != NULL; module = module->next) {
209                 size_t name_len = strlen(module->name);
210
211                 if (module != *(moddata->list_head)) {
212                         p       = strncat(p, ", ", buflen - 1);
213                         buflen -= 2;
214                 }
215
216                 p = strncat(p, module->name, buflen - 1);
217
218                 if (name_len >= buflen)
219                         break;
220
221                 buflen -= name_len;
222         }
223
224         return strlen(buf);
225 }
226
227 /**
228  * Add a new module to list.
229  */
230 void be_add_module_to_list(be_module_list_entry_t **list_head, const char *name,
231                            void *module)
232 {
233         be_module_list_entry_t *entry = XMALLOC(be_module_list_entry_t);
234         entry->name = name;
235         entry->data = module;
236         entry->next = *list_head;
237         *list_head  = entry;
238 }
239
240 /**
241  * Add an option for a module.
242  */
243 void be_add_module_list_opt(lc_opt_entry_t *grp, const char *name,
244                             const char *description,
245                             be_module_list_entry_t * const * list_head,
246                             void **var)
247 {
248         module_opt_data_t *moddata = XMALLOC(module_opt_data_t);
249         moddata->var       = var;
250         moddata->list_head = list_head;
251
252         lc_opt_add_opt(grp, name, description, lc_opt_type_enum,
253                        moddata, sizeof(moddata[0]),
254                        set_opt_module, dump_opt_module, dump_opt_module_vals,
255                                    NULL);
256 }