bemodule.c 5.53 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
2
/*
 * This file is part of libFirm.
3
 * Copyright (C) 2012 University of Karlsruhe.
Christian Würdig's avatar
Christian Würdig committed
4
5
 */

Christian Würdig's avatar
Christian Würdig committed
6
7
8
9
10
/**
 * @file
 * @brief       Backend module interface.
 * @author      Matthias Braun
 * @date        29.09.2005
Matthias Braun's avatar
Matthias Braun committed
11
12
 */
#include <stdlib.h>
13
#include <stdbool.h>
Matthias Braun's avatar
Matthias Braun committed
14

15
#include "bemodule.h"
Christoph Mallon's avatar
Christoph Mallon committed
16
#include "util.h"
Matthias Braun's avatar
Matthias Braun committed
17
18
#include "xmalloc.h"

19
20
21
22
23
24
25
26
27
/**
 * A module list entry.
 */
struct be_module_list_entry_t {
	const char *name;                    /**< The name of the entry. */
	void *data;                          /**< Some data associated with this entry. */
	struct be_module_list_entry_t *next; /**< Points to the next entry. */
};

28
void be_init_2addr(void);
29
void be_init_arch(void);
Matthias Braun's avatar
Matthias Braun committed
30
31
32
33
34
void be_init_arch_TEMPLATE(void);
void be_init_arch_amd64(void);
void be_init_arch_arm(void);
void be_init_arch_ia32(void);
void be_init_arch_sparc(void);
Matthias Braun's avatar
Matthias Braun committed
35
36
void be_init_blocksched(void);
void be_init_chordal(void);
37
void be_init_chordal_common(void);
Matthias Braun's avatar
Matthias Braun committed
38
void be_init_chordal_main(void);
39
void be_init_copyheur4(void);
Matthias Braun's avatar
Matthias Braun committed
40
void be_init_copyilp(void);
41
void be_init_copyilp2(void);
yb9976's avatar
yb9976 committed
42
void be_init_copynone(void);
Matthias Braun's avatar
Matthias Braun committed
43
void be_init_copyopt(void);
44
void be_init_daemelspill(void);
45
void be_init_dwarf(void);
46
void be_init_gas(void);
Matthias Braun's avatar
Matthias Braun committed
47
48
49
50
51
void be_init_listsched(void);
void be_init_live(void);
void be_init_loopana(void);
void be_init_pbqp(void);
void be_init_pbqp_coloring(void);
52
void be_init_peephole(void);
Matthias Braun's avatar
Matthias Braun committed
53
void be_init_pref_alloc(void);
Matthias Braun's avatar
Matthias Braun committed
54
void be_init_ra(void);
Matthias Braun's avatar
Matthias Braun committed
55
56
57
58
59
void be_init_sched(void);
void be_init_sched_normal(void);
void be_init_sched_rand(void);
void be_init_sched_trivial(void);
void be_init_spill(void);
60
void be_init_spillbelady(void);
Matthias Braun's avatar
Matthias Braun committed
61
void be_init_spilloptions(void);
Matthias Braun's avatar
Matthias Braun committed
62
void be_init_spillslots(void);
Matthias Braun's avatar
Matthias Braun committed
63
void be_init_ssaconstr(void);
64
void be_init_state(void);
Matthias Braun's avatar
Matthias Braun committed
65

66
void be_quit_pbqp(void);
Matthias Braun's avatar
Matthias Braun committed
67

Christian Würdig's avatar
Christian Würdig committed
68
/**
Michael Beck's avatar
Michael Beck committed
69
 * Driver for module initialization.
Christian Würdig's avatar
Christian Würdig committed
70
71
 * Call your module initialization function here.
 */
Matthias Braun's avatar
Matthias Braun committed
72
73
void be_init_modules(void)
{
74
	static bool run_once = false;
Christian Würdig's avatar
Christian Würdig committed
75
	if (run_once)
Matthias Braun's avatar
Matthias Braun committed
76
		return;
77
	run_once = true;
Matthias Braun's avatar
Matthias Braun committed
78

79
	be_init_2addr();
80
	be_init_arch();
Matthias Braun's avatar
Matthias Braun committed
81
82
83
84
	be_init_blocksched();
	be_init_chordal_common();
	be_init_copyopt();
	be_init_dwarf();
85
	be_init_gas();
Matthias Braun's avatar
Matthias Braun committed
86
	be_init_live();
Matthias Braun's avatar
Matthias Braun committed
87
88
89
	be_init_loopana();
	be_init_peephole();
	be_init_ra();
Matthias Braun's avatar
Matthias Braun committed
90
91
	be_init_sched();
	be_init_spill();
92
	be_init_spilloptions();
Matthias Braun's avatar
Matthias Braun committed
93
94
95
96
97
98
99
100
101
102
	be_init_spillslots();
	be_init_ssaconstr();
	be_init_state();

	/* in the following groups the first one is the default */
	be_init_arch_ia32();
	be_init_arch_arm();
	be_init_arch_sparc();
	be_init_arch_amd64();
	be_init_arch_TEMPLATE();
103

Matthias Braun's avatar
Matthias Braun committed
104
	be_init_listsched();
105
106
107
108
	be_init_sched_normal();
	be_init_sched_rand();
	be_init_sched_trivial();

109
	be_init_chordal_main();
Matthias Braun's avatar
Matthias Braun committed
110
111
	be_init_pref_alloc();

Matthias Braun's avatar
Matthias Braun committed
112
	be_init_chordal();
113
	be_init_pbqp_coloring();
Matthias Braun's avatar
Matthias Braun committed
114

115
	be_init_spillbelady();
116
	be_init_daemelspill();
Matthias Braun's avatar
Matthias Braun committed
117

Matthias Braun's avatar
Matthias Braun committed
118
119
120
	be_init_copyheur4();
	be_init_copyilp2();
	be_init_copynone();
Matthias Braun's avatar
Matthias Braun committed
121
122
	be_init_copyilp();

123
124
125
#ifdef FIRM_GRGEN_BE
	be_init_pbqp();
#endif
Matthias Braun's avatar
Matthias Braun committed
126
127
128
129
}

void be_quit_modules(void)
{
130
131
132
#ifdef FIRM_GRGEN_BE
	be_quit_pbqp();
#endif
Matthias Braun's avatar
Matthias Braun committed
133
134
135
136
137
}

//---------------------------------------------------------------------------

typedef struct module_opt_data_t {
138
	void **var;
139
	be_module_list_entry_t * const *list_head;
Matthias Braun's avatar
Matthias Braun committed
140
141
} module_opt_data_t;

142
/**
Matthias Braun's avatar
Matthias Braun committed
143
144
 * Searches in list for module option. If found, set option to given value and
 * return true.
145
146
 * Beware: return value of 0 means error.
 */
147
static bool set_opt_module(void *const data, size_t const length, char const *const opt)
Matthias Braun's avatar
Matthias Braun committed
148
{
Matthias Braun's avatar
Matthias Braun committed
149
	(void)length;
Matthias Braun's avatar
Matthias Braun committed
150

Matthias Braun's avatar
Matthias Braun committed
151
152
153
154
	const module_opt_data_t *moddata = (module_opt_data_t*)data;
	bool                     res     = false;
	for (const be_module_list_entry_t *module = *(moddata->list_head);
	     module != NULL; module = module->next) {
Christoph Mallon's avatar
Christoph Mallon committed
155
		if (streq(module->name, opt)) {
Matthias Braun's avatar
Matthias Braun committed
156
			*(moddata->var) = module->data;
157
			res = true;
Matthias Braun's avatar
Matthias Braun committed
158
159
160
161
			break;
		}
	}

162
	return res;
Matthias Braun's avatar
Matthias Braun committed
163
164
}

Christian Würdig's avatar
Christian Würdig committed
165
166
167
/**
 * Dump the names of all registered module options.
 */
168
static int dump_opt_module(char *buf, size_t buflen, void *data)
Matthias Braun's avatar
Matthias Braun committed
169
{
Matthias Braun's avatar
Matthias Braun committed
170
171
172
	const module_opt_data_t *moddata = (module_opt_data_t*)data;
	for (const be_module_list_entry_t *module = *(moddata->list_head);
	     module != NULL; module = module->next) {
Christian Würdig's avatar
Christian Würdig committed
173
		if (module->data == *(moddata->var)) {
Matthias Braun's avatar
Matthias Braun committed
174
175
176
177
178
179
180
181
182
			snprintf(buf, buflen, "%s", module->name);
			return strlen(buf);
		}
	}

	snprintf(buf, buflen, "none");
	return strlen(buf);
}

Christian Würdig's avatar
Christian Würdig committed
183
184
185
/**
 * Dump the values of all register module options.
 */
186
static int dump_opt_module_vals(char *buf, size_t buflen, void *data)
Matthias Braun's avatar
Matthias Braun committed
187
{
Matthias Braun's avatar
Matthias Braun committed
188
189
190
191
	const module_opt_data_t *moddata = (module_opt_data_t*)data;
	char                    *p       = buf;
	for (const be_module_list_entry_t *module = *(moddata->list_head);
	     module != NULL; module = module->next) {
192
		size_t name_len = strlen(module->name);
Matthias Braun's avatar
Matthias Braun committed
193

Christian Würdig's avatar
Christian Würdig committed
194
195
		if (module != *(moddata->list_head)) {
			p       = strncat(p, ", ", buflen - 1);
Matthias Braun's avatar
Matthias Braun committed
196
197
198
199
			buflen -= 2;
		}

		p = strncat(p, module->name, buflen - 1);
200
		if (name_len >= buflen)
Matthias Braun's avatar
Matthias Braun committed
201
			break;
Christian Würdig's avatar
Christian Würdig committed
202

203
		buflen -= name_len;
Matthias Braun's avatar
Matthias Braun committed
204
205
206
207
208
	}

	return strlen(buf);
}

Christian Würdig's avatar
Christian Würdig committed
209
210
211
/**
 * Add a new module to list.
 */
Matthias Braun's avatar
Matthias Braun committed
212
void be_add_module_to_list(be_module_list_entry_t **list_head, const char *name,
213
                           void *module)
Matthias Braun's avatar
Matthias Braun committed
214
{
215
	be_module_list_entry_t *entry = XMALLOC(be_module_list_entry_t);
Matthias Braun's avatar
Matthias Braun committed
216
217
218
	entry->name = name;
	entry->data = module;
	entry->next = *list_head;
Christian Würdig's avatar
Christian Würdig committed
219
	*list_head  = entry;
Matthias Braun's avatar
Matthias Braun committed
220
221
}

Christian Würdig's avatar
Christian Würdig committed
222
223
224
/**
 * Add an option for a module.
 */
Matthias Braun's avatar
Matthias Braun committed
225
226
void be_add_module_list_opt(lc_opt_entry_t *grp, const char *name,
                            const char *description,
227
                            be_module_list_entry_t * const * list_head,
Matthias Braun's avatar
Matthias Braun committed
228
229
                            void **var)
{
230
	module_opt_data_t *moddata = XMALLOC(module_opt_data_t);
Christian Würdig's avatar
Christian Würdig committed
231
	moddata->var       = var;
Matthias Braun's avatar
Matthias Braun committed
232
233
234
	moddata->list_head = list_head;

	lc_opt_add_opt(grp, name, description, lc_opt_type_enum,
Matthias Braun's avatar
Matthias Braun committed
235
236
	               moddata, sizeof(moddata[0]), set_opt_module,
	               dump_opt_module, dump_opt_module_vals);
Matthias Braun's avatar
Matthias Braun committed
237
}