bearch.c 8.45 KB
Newer Older
1
2
3
4
5
6
7
/**
 * Processor architecture specification.
 * @author Sebastian Hack
 * @date 11.2.2005
 *
 * $Id$
 */
Michael Beck's avatar
Michael Beck committed
8
#ifdef HAVE_CONFIG_H
9
#include "config.h"
Michael Beck's avatar
Michael Beck committed
10
11
12
13
14
#endif

#ifdef HAVE_ALLOCA_H
#include <alloca.h>
#endif
Daniel Grund's avatar
Daniel Grund committed
15

Michael Beck's avatar
Michael Beck committed
16
17
18
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
19

20
21
#include <string.h>

Michael Beck's avatar
Michael Beck committed
22
#include "bearch.h"
Sebastian Hack's avatar
Sebastian Hack committed
23
#include "ircons_t.h"
Daniel Grund's avatar
Daniel Grund committed
24
#include "irnode_t.h"
25

Sebastian Hack's avatar
Sebastian Hack committed
26
#include "bitset.h"
Sebastian Hack's avatar
Sebastian Hack committed
27
#include "pset.h"
28
#include "entity.h"
Matthias Braun's avatar
Matthias Braun committed
29
#include "raw_bitset.h"
30

Sebastian Hack's avatar
Sebastian Hack committed
31
32
#include "irprintf.h"

33
/* Initialize the architecture environment struct. */
Christian Würdig's avatar
Christian Würdig committed
34
arch_env_t *arch_env_init(arch_env_t *env, const arch_isa_if_t *isa_if, FILE *file_handle, be_main_env_t *main_env)
35
{
Matthias Braun's avatar
Matthias Braun committed
36
37
38
39
	memset(env, 0, sizeof(*env));
	env->isa                  = isa_if->init(file_handle);
	env->isa->main_env        = main_env;
	return env;
Sebastian Hack's avatar
Sebastian Hack committed
40
41
}

42
arch_env_t *arch_env_push_irn_handler(arch_env_t *env,
Matthias Braun's avatar
Matthias Braun committed
43
                                      const arch_irn_handler_t *handler)
Sebastian Hack's avatar
Sebastian Hack committed
44
{
Matthias Braun's avatar
Matthias Braun committed
45
46
47
	assert(env->handlers_tos < ARCH_MAX_HANDLERS);
	env->handlers[env->handlers_tos++] = handler;
	return env;
48
49
}

50
51
const arch_irn_handler_t *arch_env_pop_irn_handler(arch_env_t *env)
{
Matthias Braun's avatar
Matthias Braun committed
52
53
   	assert(env->handlers_tos > 0 && env->handlers_tos <= ARCH_MAX_HANDLERS);
	return env->handlers[--env->handlers_tos];
54
55
}

Sebastian Hack's avatar
Sebastian Hack committed
56
static const arch_irn_ops_t *fallback_irn_ops = NULL;
Sebastian Hack's avatar
Sebastian Hack committed
57

Sebastian Hack's avatar
Sebastian Hack committed
58
int arch_register_class_put(const arch_register_class_t *cls, bitset_t *bs)
59
{
Matthias Braun's avatar
Matthias Braun committed
60
61
62
63
64
	if(bs) {
		int i, n;
		for(i = 0, n = cls->n_regs; i < n; ++i)
			bitset_set(bs, i);
	}
65

Matthias Braun's avatar
Matthias Braun committed
66
	return cls->n_regs;
67
68
}

Sebastian Hack's avatar
Sebastian Hack committed
69
/**
Sebastian Hack's avatar
Sebastian Hack committed
70
71
72
73
 * Get the isa responsible for a node.
 * @param env The arch environment with the isa stack.
 * @param irn The node to get the responsible isa for.
 * @return The irn operations given by the responsible isa.
Sebastian Hack's avatar
Sebastian Hack committed
74
 */
Sebastian Hack's avatar
Sebastian Hack committed
75
76
static INLINE const arch_irn_ops_t *
get_irn_ops(const arch_env_t *env, const ir_node *irn)
77
{
Matthias Braun's avatar
Matthias Braun committed
78
	int i;
79

Matthias Braun's avatar
Matthias Braun committed
80
81
82
	for(i = env->handlers_tos - 1; i >= 0; --i) {
		const arch_irn_handler_t *handler = env->handlers[i];
		const arch_irn_ops_t *ops = handler->get_irn_ops(handler, irn);
Sebastian Hack's avatar
Sebastian Hack committed
83

Matthias Braun's avatar
Matthias Braun committed
84
85
86
		if(ops)
			return ops;
	}
Sebastian Hack's avatar
Sebastian Hack committed
87

Matthias Braun's avatar
Matthias Braun committed
88
	return fallback_irn_ops;
89
90
}

91
92
93
94
const arch_irn_ops_t *arch_get_irn_ops(const arch_env_t *env, const ir_node *irn) {
	return get_irn_ops(env, irn);
}

Sebastian Hack's avatar
Sebastian Hack committed
95
const arch_register_req_t *arch_get_register_req(const arch_env_t *env,
Matthias Braun's avatar
Matthias Braun committed
96
                                                 const ir_node *irn, int pos)
Sebastian Hack's avatar
Sebastian Hack committed
97
{
Matthias Braun's avatar
Matthias Braun committed
98
99
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
	return ops->impl->get_irn_reg_req(ops, irn, pos);
Sebastian Hack's avatar
Sebastian Hack committed
100
101
}

102
void arch_set_frame_offset(const arch_env_t *env, ir_node *irn, int offset)
Sebastian Hack's avatar
Sebastian Hack committed
103
{
Matthias Braun's avatar
Matthias Braun committed
104
105
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
	ops->impl->set_frame_offset(ops, irn, offset);
Sebastian Hack's avatar
Sebastian Hack committed
106
107
}

108
ir_entity *arch_get_frame_entity(const arch_env_t *env, ir_node *irn)
109
{
Matthias Braun's avatar
Matthias Braun committed
110
111
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
	return ops->impl->get_frame_entity(ops, irn);
112
113
}

114
void arch_set_frame_entity(const arch_env_t *env, ir_node *irn, ir_entity *ent)
115
116
117
118
119
{
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
	ops->impl->set_frame_entity(ops, irn, ent);
}

120
121
122
123
124
125
int arch_get_sp_bias(const arch_env_t *env, ir_node *irn)
{
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
	return ops->impl->get_sp_bias(ops, irn);
}

126
127
arch_inverse_t *arch_get_inverse(const arch_env_t *env, const ir_node *irn, int i, arch_inverse_t *inverse, struct obstack *obstack)
{
Matthias Braun's avatar
Matthias Braun committed
128
129
130
131
132
133
134
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);

	if(ops->impl->get_inverse) {
		return ops->impl->get_inverse(ops, irn, i, inverse, obstack);
	} else {
		return NULL;
	}
135
136
}

137
138
int arch_possible_memory_operand(const arch_env_t *env, const ir_node *irn, unsigned int i) {
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
Matthias Braun's avatar
Matthias Braun committed
139

140
141
142
143
144
145
146
	if(ops->impl->possible_memory_operand) {
		return ops->impl->possible_memory_operand(ops, irn, i);
	} else {
		return 0;
	}
}

Christian Würdig's avatar
Christian Würdig committed
147
void arch_perform_memory_operand(const arch_env_t *env, ir_node *irn, ir_node *spill, unsigned int i) {
148
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
Matthias Braun's avatar
Matthias Braun committed
149

150
	if(ops->impl->perform_memory_operand) {
151
		ops->impl->perform_memory_operand(ops, irn, spill, i);
152
153
154
155
156
	} else {
		return;
	}
}

157
158
int arch_get_op_estimated_cost(const arch_env_t *env, const ir_node *irn)
{
Matthias Braun's avatar
Matthias Braun committed
159
160
161
162
163
164
165
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);

	if(ops->impl->get_op_estimated_cost) {
		return ops->impl->get_op_estimated_cost(ops, irn);
	} else {
		return 1;
	}
166
167
168
169
}

int arch_is_possible_memory_operand(const arch_env_t *env, const ir_node *irn, int i)
{
Matthias Braun's avatar
Matthias Braun committed
170
171
172
173
174
175
176
   	const arch_irn_ops_t *ops = get_irn_ops(env, irn);

	if(ops->impl->possible_memory_operand) {
	   	return ops->impl->possible_memory_operand(ops, irn, i);
	} else {
		return 0;
	}
177
}
Sebastian Hack's avatar
Sebastian Hack committed
178

Sebastian Hack's avatar
Sebastian Hack committed
179
int arch_get_allocatable_regs(const arch_env_t *env, const ir_node *irn, int pos, bitset_t *bs)
Sebastian Hack's avatar
Sebastian Hack committed
180
{
Matthias Braun's avatar
Matthias Braun committed
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
	const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, irn, pos);

	if(req->type == arch_register_req_type_none) {
		bitset_clear_all(bs);
		return 0;
	}

	if(arch_register_req_is(req, limited)) {
		rbitset_copy_to_bitset(req->limited, bs);
		return bitset_popcnt(bs);
	}

	arch_register_class_put(req->cls, bs);
	return req->cls->n_regs;
Sebastian Hack's avatar
Sebastian Hack committed
196
197
}

Sebastian Hack's avatar
Sebastian Hack committed
198
199
200
201
202
203
204
205
206
207
void arch_put_non_ignore_regs(const arch_env_t *env, const arch_register_class_t *cls, bitset_t *bs)
{
	int i;

	for(i = 0; i < cls->n_regs; ++i) {
		if(!arch_register_type_is(&cls->regs[i], ignore))
			bitset_set(bs, i);
	}
}

208
209
210
211
212
213
214
215
216
217
218
219
220
int arch_count_non_ignore_regs(const arch_env_t *env, const arch_register_class_t *cls)
{
	int i;
	int result = 0;

	for(i = 0; i < cls->n_regs; ++i) {
		if(!arch_register_type_is(&cls->regs[i], ignore))
			result++;
	}

	return result;
}

221
222
223
int arch_is_register_operand(const arch_env_t *env,
    const ir_node *irn, int pos)
{
Sebastian Hack's avatar
Sebastian Hack committed
224
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
Matthias Braun's avatar
Matthias Braun committed
225
226
	const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, irn, pos);

Sebastian Hack's avatar
Sebastian Hack committed
227
	return req != NULL;
228
229
}

Sebastian Hack's avatar
Sebastian Hack committed
230
231
int arch_reg_is_allocatable(const arch_env_t *env, const ir_node *irn,
    int pos, const arch_register_t *reg)
Sebastian Hack's avatar
Sebastian Hack committed
232
{
Matthias Braun's avatar
Matthias Braun committed
233
	const arch_register_req_t *req;
234

Matthias Braun's avatar
Matthias Braun committed
235
	req = arch_get_register_req(env, irn, pos);
Sebastian Hack's avatar
Sebastian Hack committed
236

Matthias Braun's avatar
Matthias Braun committed
237
	if(req->type == arch_register_req_type_none)
Sebastian Hack's avatar
Sebastian Hack committed
238
239
		return 0;

Matthias Braun's avatar
Matthias Braun committed
240
241
242
	if(arch_register_req_is(req, limited)) {
		assert(arch_register_get_class(reg) == req->cls);
		return rbitset_is_set(req->limited, arch_register_get_index(reg));
243
244
	}

Matthias Braun's avatar
Matthias Braun committed
245
	return req->cls == reg->reg_class;
Sebastian Hack's avatar
Sebastian Hack committed
246
247
}

Sebastian Hack's avatar
Sebastian Hack committed
248
249
const arch_register_class_t *
arch_get_irn_reg_class(const arch_env_t *env, const ir_node *irn, int pos)
Sebastian Hack's avatar
Sebastian Hack committed
250
{
Matthias Braun's avatar
Matthias Braun committed
251
252
253
254
255
256
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
	const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, irn, pos);

	assert(req->type != arch_register_req_type_none || req->cls == NULL);

	return req->cls;
Sebastian Hack's avatar
Sebastian Hack committed
257
258
}

Sebastian Hack's avatar
Sebastian Hack committed
259
extern const arch_register_t *
Sebastian Hack's avatar
Sebastian Hack committed
260
arch_get_irn_register(const arch_env_t *env, const ir_node *irn)
261
{
Matthias Braun's avatar
Matthias Braun committed
262
263
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
	return ops->impl->get_irn_reg(ops, irn);
264
}
265

Sebastian Hack's avatar
Sebastian Hack committed
266
extern void arch_set_irn_register(const arch_env_t *env,
Sebastian Hack's avatar
Sebastian Hack committed
267
    ir_node *irn, const arch_register_t *reg)
268
{
Matthias Braun's avatar
Matthias Braun committed
269
270
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
	ops->impl->set_irn_reg(ops, irn, reg);
271
}
272
273
274

extern arch_irn_class_t arch_irn_classify(const arch_env_t *env, const ir_node *irn)
{
Matthias Braun's avatar
Matthias Braun committed
275
276
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
	return ops->impl->classify(ops, irn);
277
}
Sebastian Hack's avatar
Sebastian Hack committed
278
279
280

extern arch_irn_flags_t arch_irn_get_flags(const arch_env_t *env, const ir_node *irn)
{
Matthias Braun's avatar
Matthias Braun committed
281
282
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
	return ops->impl->get_flags(ops, irn);
Sebastian Hack's avatar
Sebastian Hack committed
283
}
Sebastian Hack's avatar
Sebastian Hack committed
284
285
286
287
288
289
290
291

extern const char *arch_irn_flag_str(arch_irn_flags_t fl)
{
	switch(fl) {
#define XXX(x) case arch_irn_flags_ ## x: return #x;
		XXX(dont_spill);
		XXX(ignore);
		XXX(rematerializable);
Sebastian Hack's avatar
Sebastian Hack committed
292
		XXX(modify_sp);
Sebastian Hack's avatar
Sebastian Hack committed
293
294
295
296
297
298
		XXX(none);
#undef XXX
	}
	return "n/a";
}

Matthias Braun's avatar
Matthias Braun committed
299
300
301
extern char *arch_register_req_format(char *buf, size_t len,
                                      const arch_register_req_t *req,
                                      const ir_node *node)
Sebastian Hack's avatar
Sebastian Hack committed
302
303
304
305
306
{
	char tmp[128];
	snprintf(buf, len, "class: %s", req->cls->name);

	if(arch_register_req_is(req, limited)) {
Matthias Braun's avatar
Matthias Braun committed
307
308
309
		unsigned n_regs = req->cls->n_regs;
		unsigned i;

Sebastian Hack's avatar
Sebastian Hack committed
310
		strncat(buf, " limited:", len);
Matthias Braun's avatar
Matthias Braun committed
311
312
313
314
315
316
		for(i = 0; i < n_regs; ++i) {
			if(rbitset_is_set(req->limited, i)) {
				const arch_register_t *reg = &req->cls->regs[i];
				strncat(buf, " ", len);
				strncat(buf, reg->name, len);
			}
Sebastian Hack's avatar
Sebastian Hack committed
317
318
319
320
		}
	}

	if(arch_register_req_is(req, should_be_same)) {
Matthias Braun's avatar
Matthias Braun committed
321
322
		const ir_node *same = get_irn_n(node, req->other_same);
		ir_snprintf(tmp, sizeof(tmp), " same to: %+F", same);
Sebastian Hack's avatar
Sebastian Hack committed
323
324
325
326
		strncat(buf, tmp, len);
	}

	if(arch_register_req_is(req, should_be_different)) {
Matthias Braun's avatar
Matthias Braun committed
327
328
		const ir_node *different = get_irn_n(node, req->other_different);
		ir_snprintf(tmp, sizeof(tmp), " different to: %+F", different);
Sebastian Hack's avatar
Sebastian Hack committed
329
330
331
332
333
		strncat(buf, tmp, len);
	}

	return buf;
}
Matthias Braun's avatar
Matthias Braun committed
334
335
336
337
338
339
340
341
342

static const arch_register_req_t no_requirement = {
	arch_register_req_type_none,
	NULL,
	NULL,
	-1,
	-1
};
const arch_register_req_t *arch_no_register_req = &no_requirement;