BugFix: callback must return true if everything was ok
[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  */
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10
11 #include <stdlib.h>
12
13 #include "bemodule_t.h"
14 #include "xmalloc.h"
15
16 void be_init_sched(void);
17 void be_init_blocksched(void);
18 void be_init_spill(void);
19 void be_init_listsched(void);
20 void be_init_schedrss(void);
21 void be_init_chordal(void);
22 void be_init_copycoal(void);
23 void be_init_copyheur2(void);
24 void be_init_raextern(void);
25 void be_init_copystat(void);
26 void be_init_arch_ia32(void);
27 void be_init_arch_ppc32(void);
28 void be_init_arch_mips(void);
29 void be_init_arch_arm(void);
30 void be_init_arch_sta(void);
31 void be_init_ilpsched(void);
32 void be_init_copyilp(void);
33 void be_init_javacoal(void);
34 void be_init_ra(void);
35 void be_init_spillbelady(void);
36 void be_init_spillmorgan(void);
37 void be_init_spillremat(void);
38
39 void be_quit_copystat(void);
40
41 void be_init_modules(void)
42 {
43         static int run_once = 0;
44
45         if(run_once)
46                 return;
47         run_once = 1;
48
49         be_init_sched();
50         be_init_blocksched();
51         be_init_spill();
52         be_init_listsched();
53         be_init_schedrss();
54         be_init_chordal();
55         be_init_copycoal();
56         be_init_copyheur2();
57         be_init_raextern();
58         be_init_copystat();
59         be_init_ra();
60         be_init_spillbelady();
61         be_init_spillmorgan();
62
63         be_init_arch_ia32();
64         be_init_arch_ppc32();
65         be_init_arch_mips();
66         be_init_arch_arm();
67         be_init_arch_sta();
68
69 #ifdef WITH_ILP
70         be_init_ilpsched();
71         be_init_copyilp();
72         be_init_spillremat();
73 #endif
74
75 #ifdef WITH_JVM
76         be_init_javacoal();
77 #endif
78 }
79
80 void be_quit_modules(void)
81 {
82         be_quit_copystat();
83 }
84
85 //---------------------------------------------------------------------------
86
87 #ifdef WITH_LIBCORE
88 typedef struct module_opt_data_t {
89         void **var;
90         be_module_list_entry_t * const *list_head;
91 } module_opt_data_t;
92
93 /**
94  * Beware: return value of 0 means error.
95  */
96 static int set_opt_module(const char *name, lc_opt_type_t type, void *data,
97                           size_t length, ...)
98 {
99         module_opt_data_t *moddata = data;
100         va_list args;
101         const char* opt;
102         const be_module_list_entry_t *module;
103         int res = 0;
104
105         va_start(args, length);
106         opt = va_arg(args, const char*);
107
108         for(module = *(moddata->list_head); module != NULL; module = module->next) {
109                 if(strcmp(module->name, opt) == 0) {
110                         *(moddata->var) = module->data;
111                         res = 1;
112                         break;
113                 }
114         }
115         va_end(args);
116
117         return res;
118 }
119
120 int dump_opt_module(char *buf, size_t buflen, const char *name,
121                     lc_opt_type_t type, void *data, size_t length)
122 {
123         module_opt_data_t *moddata = data;
124         const be_module_list_entry_t *module;
125
126         for(module = *(moddata->list_head); module != NULL; module = module->next) {
127                 if(module->data == *(moddata->var)) {
128                         snprintf(buf, buflen, "%s", module->name);
129                         return strlen(buf);
130                 }
131         }
132
133         snprintf(buf, buflen, "none");
134         return strlen(buf);
135 }
136
137 int dump_opt_module_vals(char *buf, size_t buflen, const char *name,
138                          lc_opt_type_t type, void *data, size_t len)
139 {
140         module_opt_data_t *moddata = data;
141         const be_module_list_entry_t *module;
142         char *p = buf;
143
144         for(module = *(moddata->list_head); module != NULL; module = module->next) {
145                 size_t len = strlen(module->name);
146
147                 if(module != *(moddata->list_head)) {
148                         p = strncat(p, ", ", buflen - 1);
149                         buflen -= 2;
150                 }
151
152                 p = strncat(p, module->name, buflen - 1);
153                 if(len >= buflen) {
154                         break;
155                 }
156                 buflen -= len;
157         }
158
159         return strlen(buf);
160 }
161
162
163 void be_add_module_to_list(be_module_list_entry_t **list_head, const char *name,
164                            void *module)
165 {
166         be_module_list_entry_t *entry = xmalloc(sizeof(entry[0]));
167         entry->name = name;
168         entry->data = module;
169         entry->next = *list_head;
170         *list_head = entry;
171 }
172
173 void be_add_module_list_opt(lc_opt_entry_t *grp, const char *name,
174                             const char *description,
175                             be_module_list_entry_t * const * list_head,
176                             void **var)
177 {
178         module_opt_data_t *moddata = xmalloc(sizeof(moddata[0]));
179         moddata->var = var;
180         moddata->list_head = list_head;
181
182         lc_opt_add_opt(grp, name, description, lc_opt_type_enum,
183                        moddata, sizeof(moddata[0]),
184                        set_opt_module, dump_opt_module, dump_opt_module_vals,
185                                    NULL);
186 }
187
188 #endif