The big committ:
[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 void be_init_irgmod(void);
41 void be_init_loopana(void);
42 void be_init_spillslots(void);
43 void be_init_live(void);
44
45 void be_quit_copystat(void);
46
47 /**
48  * Driver for module intialization.
49  * Call your module initialization function here.
50  */
51 void be_init_modules(void)
52 {
53         static int run_once = 0;
54
55         if (run_once)
56                 return;
57         run_once = 1;
58
59         be_init_irgmod();
60         be_init_loopana();
61         be_init_live();
62         be_init_spillslots();
63         be_init_sched();
64         be_init_blocksched();
65         be_init_spill();
66         be_init_listsched();
67         be_init_schedrss();
68         be_init_chordal();
69         be_init_copycoal();
70         be_init_copyheur2();
71         be_init_raextern();
72         be_init_copystat();
73         be_init_ra();
74         be_init_spillbelady();
75         be_init_spillmorgan();
76         be_init_ifg();
77
78         be_init_arch_ia32();
79         be_init_arch_ppc32();
80         be_init_arch_mips();
81         be_init_arch_arm();
82
83 #ifdef WITH_ILP
84         be_init_ilpsched();
85         be_init_copyilp();
86         be_init_spillremat();
87 #endif /* WITH_ILP */
88
89 #ifdef WITH_JVM
90         be_init_javacoal();
91 #endif /* WITH_JVM */
92
93 #if PLUGIN_IR_BE_STA
94         be_init_arch_sta();
95 #endif /* PLUGIN_IR_BE_STA */
96 }
97
98 void be_quit_modules(void)
99 {
100         be_quit_copystat();
101 }
102
103 //---------------------------------------------------------------------------
104
105 typedef struct module_opt_data_t {
106         void **var;
107         be_module_list_entry_t * const *list_head;
108 } module_opt_data_t;
109
110 /**
111  * Searches in list for module option. If found, set option to given value and return true.
112  * Beware: return value of 0 means error.
113  */
114 static int set_opt_module(const char *name, lc_opt_type_t type, void *data,
115                           size_t length, ...)
116 {
117         module_opt_data_t            *moddata = data;
118         int                          res      = 0;
119         va_list                      args;
120         const char                   *opt;
121         const be_module_list_entry_t *module;
122
123         va_start(args, length);
124         opt = va_arg(args, const char*);
125
126         for (module = *(moddata->list_head); module != NULL; module = module->next) {
127                 if (strcmp(module->name, opt) == 0) {
128                         *(moddata->var) = module->data;
129                         res = 1;
130                         break;
131                 }
132         }
133         va_end(args);
134
135         return res;
136 }
137
138 /**
139  * Dump the names of all registered module options.
140  */
141 int dump_opt_module(char *buf, size_t buflen, const char *name,
142                     lc_opt_type_t type, void *data, size_t length)
143 {
144         module_opt_data_t            *moddata = data;
145         const be_module_list_entry_t *module;
146
147         for (module = *(moddata->list_head); module != NULL; module = module->next) {
148                 if (module->data == *(moddata->var)) {
149                         snprintf(buf, buflen, "%s", module->name);
150                         return strlen(buf);
151                 }
152         }
153
154         snprintf(buf, buflen, "none");
155         return strlen(buf);
156 }
157
158 /**
159  * Dump the values of all register module options.
160  */
161 int dump_opt_module_vals(char *buf, size_t buflen, const char *name,
162                          lc_opt_type_t type, void *data, size_t len)
163 {
164         module_opt_data_t            *moddata = data;
165         char                         *p       = buf;
166         const be_module_list_entry_t *module;
167
168         for (module = *(moddata->list_head); module != NULL; module = module->next) {
169                 size_t len = strlen(module->name);
170
171                 if (module != *(moddata->list_head)) {
172                         p       = strncat(p, ", ", buflen - 1);
173                         buflen -= 2;
174                 }
175
176                 p = strncat(p, module->name, buflen - 1);
177
178                 if (len >= buflen)
179                         break;
180
181                 buflen -= len;
182         }
183
184         return strlen(buf);
185 }
186
187 /**
188  * Add a new module to list.
189  */
190 void be_add_module_to_list(be_module_list_entry_t **list_head, const char *name,
191                            void *module)
192 {
193         be_module_list_entry_t *entry;
194
195     entry       = xmalloc(sizeof(entry[0]));
196         entry->name = name;
197         entry->data = module;
198         entry->next = *list_head;
199         *list_head  = entry;
200 }
201
202 /**
203  * Add an option for a module.
204  */
205 void be_add_module_list_opt(lc_opt_entry_t *grp, const char *name,
206                             const char *description,
207                             be_module_list_entry_t * const * list_head,
208                             void **var)
209 {
210         module_opt_data_t *moddata;
211
212         moddata            = xmalloc(sizeof(moddata[0]));
213         moddata->var       = var;
214         moddata->list_head = list_head;
215
216         lc_opt_add_opt(grp, name, description, lc_opt_type_enum,
217                        moddata, sizeof(moddata[0]),
218                        set_opt_module, dump_opt_module, dump_opt_module_vals,
219                                    NULL);
220 }