added some comments
[libfirm] / ir / be / bemodule.c
1 /*
2  * Author:      Matthias Braun
3  * Date:        29.09.2005
4  * Copyright:   (c) Universitaet Karlsruhe
5  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
6  * CVS-Id:      $Id$
7  */
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif /* HAVE_CONFIG_H */
11
12 #include <stdlib.h>
13
14 #include "bemodule_t.h"
15 #include "xmalloc.h"
16
17 void be_init_sched(void);
18 void be_init_blocksched(void);
19 void be_init_spill(void);
20 void be_init_listsched(void);
21 void be_init_schedrss(void);
22 void be_init_chordal(void);
23 void be_init_copycoal(void);
24 void be_init_copyheur2(void);
25 void be_init_raextern(void);
26 void be_init_copystat(void);
27 void be_init_arch_ia32(void);
28 void be_init_arch_ppc32(void);
29 void be_init_arch_mips(void);
30 void be_init_arch_arm(void);
31 void be_init_arch_sta(void);
32 void be_init_ilpsched(void);
33 void be_init_copyilp(void);
34 void be_init_javacoal(void);
35 void be_init_ra(void);
36 void be_init_spillbelady(void);
37 void be_init_spillmorgan(void);
38 void be_init_spillremat(void);
39 void be_init_ifg(void);
40
41 void be_quit_copystat(void);
42
43 /**
44  * Driver for module intialization.
45  * Call your module initialization function here.
46  */
47 void be_init_modules(void)
48 {
49         static int run_once = 0;
50
51         if (run_once)
52                 return;
53         run_once = 1;
54
55         be_init_sched();
56         be_init_blocksched();
57         be_init_spill();
58         be_init_listsched();
59         be_init_schedrss();
60         be_init_chordal();
61         be_init_copycoal();
62         be_init_copyheur2();
63         be_init_raextern();
64         be_init_copystat();
65         be_init_ra();
66         be_init_spillbelady();
67         be_init_spillmorgan();
68         be_init_ifg();
69
70         be_init_arch_ia32();
71         be_init_arch_ppc32();
72         be_init_arch_mips();
73         be_init_arch_arm();
74
75 #ifdef WITH_ILP
76         be_init_ilpsched();
77         be_init_copyilp();
78         be_init_spillremat();
79 #endif /* WITH_ILP */
80
81 #ifdef WITH_JVM
82         be_init_javacoal();
83 #endif /* WITH_JVM */
84
85 #ifdef PLUGIN_IR_BE_STA
86         be_init_arch_sta();
87 #endif /* PLUGIN_IR_BE_STA */
88 }
89
90 void be_quit_modules(void)
91 {
92         be_quit_copystat();
93 }
94
95 //---------------------------------------------------------------------------
96
97 #ifdef WITH_LIBCORE
98 typedef struct module_opt_data_t {
99         void **var;
100         be_module_list_entry_t * const *list_head;
101 } module_opt_data_t;
102
103 /**
104  * Searches in list for module option. If found, set option to given value and return true.
105  * Beware: return value of 0 means error.
106  */
107 static int set_opt_module(const char *name, lc_opt_type_t type, void *data,
108                           size_t length, ...)
109 {
110         module_opt_data_t            *moddata = data;
111         int                          res      = 0;
112         va_list                      args;
113         const char                   *opt;
114         const be_module_list_entry_t *module;
115
116         va_start(args, length);
117         opt = va_arg(args, const char*);
118
119         for (module = *(moddata->list_head); module != NULL; module = module->next) {
120                 if (strcmp(module->name, opt) == 0) {
121                         *(moddata->var) = module->data;
122                         res = 1;
123                         break;
124                 }
125         }
126         va_end(args);
127
128         return res;
129 }
130
131 /**
132  * Dump the names of all registered module options.
133  */
134 int dump_opt_module(char *buf, size_t buflen, const char *name,
135                     lc_opt_type_t type, void *data, size_t length)
136 {
137         module_opt_data_t            *moddata = data;
138         const be_module_list_entry_t *module;
139
140         for (module = *(moddata->list_head); module != NULL; module = module->next) {
141                 if (module->data == *(moddata->var)) {
142                         snprintf(buf, buflen, "%s", module->name);
143                         return strlen(buf);
144                 }
145         }
146
147         snprintf(buf, buflen, "none");
148         return strlen(buf);
149 }
150
151 /**
152  * Dump the values of all register module options.
153  */
154 int dump_opt_module_vals(char *buf, size_t buflen, const char *name,
155                          lc_opt_type_t type, void *data, size_t len)
156 {
157         module_opt_data_t            *moddata = data;
158         char                         *p       = buf;
159         const be_module_list_entry_t *module;
160
161         for (module = *(moddata->list_head); module != NULL; module = module->next) {
162                 size_t len = strlen(module->name);
163
164                 if (module != *(moddata->list_head)) {
165                         p       = strncat(p, ", ", buflen - 1);
166                         buflen -= 2;
167                 }
168
169                 p = strncat(p, module->name, buflen - 1);
170
171                 if (len >= buflen)
172                         break;
173
174                 buflen -= len;
175         }
176
177         return strlen(buf);
178 }
179
180 /**
181  * Add a new module to list.
182  */
183 void be_add_module_to_list(be_module_list_entry_t **list_head, const char *name,
184                            void *module)
185 {
186         be_module_list_entry_t *entry;
187
188     entry       = xmalloc(sizeof(entry[0]));
189         entry->name = name;
190         entry->data = module;
191         entry->next = *list_head;
192         *list_head  = entry;
193 }
194
195 /**
196  * Add an option for a module.
197  */
198 void be_add_module_list_opt(lc_opt_entry_t *grp, const char *name,
199                             const char *description,
200                             be_module_list_entry_t * const * list_head,
201                             void **var)
202 {
203         module_opt_data_t *moddata;
204
205         moddata            = xmalloc(sizeof(moddata[0]));
206         moddata->var       = var;
207         moddata->list_head = list_head;
208
209         lc_opt_add_opt(grp, name, description, lc_opt_type_enum,
210                        moddata, sizeof(moddata[0]),
211                        set_opt_module, dump_opt_module, dump_opt_module_vals,
212                                    NULL);
213 }
214
215 #endif /* WITH_LIBCORE */