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