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