bearch.c 8.37 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
#endif

12
13
#include <string.h>

Michael Beck's avatar
Michael Beck committed
14
#include "bearch.h"
Sebastian Hack's avatar
Sebastian Hack committed
15
#include "ircons_t.h"
Daniel Grund's avatar
Daniel Grund committed
16
#include "irnode_t.h"
17
#include "xmalloc.h"
18

Sebastian Hack's avatar
Sebastian Hack committed
19
#include "bitset.h"
Sebastian Hack's avatar
Sebastian Hack committed
20
#include "pset.h"
21
#include "entity.h"
Matthias Braun's avatar
Matthias Braun committed
22
#include "raw_bitset.h"
23

Sebastian Hack's avatar
Sebastian Hack committed
24
25
#include "irprintf.h"

26
/* Initialize the architecture environment struct. */
Christian Würdig's avatar
Christian Würdig committed
27
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)
28
{
Matthias Braun's avatar
Matthias Braun committed
29
30
31
32
	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
33
34
}

35
arch_env_t *arch_env_push_irn_handler(arch_env_t *env,
Matthias Braun's avatar
Matthias Braun committed
36
                                      const arch_irn_handler_t *handler)
Sebastian Hack's avatar
Sebastian Hack committed
37
{
Matthias Braun's avatar
Matthias Braun committed
38
39
40
	assert(env->handlers_tos < ARCH_MAX_HANDLERS);
	env->handlers[env->handlers_tos++] = handler;
	return env;
41
42
}

43
44
const arch_irn_handler_t *arch_env_pop_irn_handler(arch_env_t *env)
{
Matthias Braun's avatar
Matthias Braun committed
45
46
   	assert(env->handlers_tos > 0 && env->handlers_tos <= ARCH_MAX_HANDLERS);
	return env->handlers[--env->handlers_tos];
47
48
}

Sebastian Hack's avatar
Sebastian Hack committed
49
static const arch_irn_ops_t *fallback_irn_ops = NULL;
Sebastian Hack's avatar
Sebastian Hack committed
50

Sebastian Hack's avatar
Sebastian Hack committed
51
int arch_register_class_put(const arch_register_class_t *cls, bitset_t *bs)
52
{
Matthias Braun's avatar
Matthias Braun committed
53
54
55
56
57
	if(bs) {
		int i, n;
		for(i = 0, n = cls->n_regs; i < n; ++i)
			bitset_set(bs, i);
	}
58

Matthias Braun's avatar
Matthias Braun committed
59
	return cls->n_regs;
60
61
}

Sebastian Hack's avatar
Sebastian Hack committed
62
/**
Sebastian Hack's avatar
Sebastian Hack committed
63
64
65
66
 * 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
67
 */
Sebastian Hack's avatar
Sebastian Hack committed
68
69
static INLINE const arch_irn_ops_t *
get_irn_ops(const arch_env_t *env, const ir_node *irn)
70
{
Matthias Braun's avatar
Matthias Braun committed
71
	int i;
72

Matthias Braun's avatar
Matthias Braun committed
73
74
75
	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
76

Matthias Braun's avatar
Matthias Braun committed
77
78
79
		if(ops)
			return ops;
	}
Sebastian Hack's avatar
Sebastian Hack committed
80

Matthias Braun's avatar
Matthias Braun committed
81
	return fallback_irn_ops;
82
83
}

84
85
86
87
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
88
const arch_register_req_t *arch_get_register_req(const arch_env_t *env,
Matthias Braun's avatar
Matthias Braun committed
89
                                                 const ir_node *irn, int pos)
Sebastian Hack's avatar
Sebastian Hack committed
90
{
Matthias Braun's avatar
Matthias Braun committed
91
92
	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
93
94
}

95
void arch_set_frame_offset(const arch_env_t *env, ir_node *irn, int offset)
Sebastian Hack's avatar
Sebastian Hack committed
96
{
Matthias Braun's avatar
Matthias Braun committed
97
98
	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
99
100
}

101
ir_entity *arch_get_frame_entity(const arch_env_t *env, ir_node *irn)
102
{
Matthias Braun's avatar
Matthias Braun committed
103
104
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
	return ops->impl->get_frame_entity(ops, irn);
105
106
}

107
void arch_set_frame_entity(const arch_env_t *env, ir_node *irn, ir_entity *ent)
108
109
110
111
112
{
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
	ops->impl->set_frame_entity(ops, irn, ent);
}

113
114
115
116
117
118
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);
}

119
120
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
121
122
123
124
125
126
127
	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;
	}
128
129
}

130
131
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
132

133
134
135
136
137
138
139
	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
140
void arch_perform_memory_operand(const arch_env_t *env, ir_node *irn, ir_node *spill, unsigned int i) {
141
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
Matthias Braun's avatar
Matthias Braun committed
142

143
	if(ops->impl->perform_memory_operand) {
144
		ops->impl->perform_memory_operand(ops, irn, spill, i);
145
146
147
148
149
	} else {
		return;
	}
}

150
151
int arch_get_op_estimated_cost(const arch_env_t *env, const ir_node *irn)
{
Matthias Braun's avatar
Matthias Braun committed
152
153
154
155
156
157
158
	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;
	}
159
160
161
162
}

int arch_is_possible_memory_operand(const arch_env_t *env, const ir_node *irn, int i)
{
Matthias Braun's avatar
Matthias Braun committed
163
164
165
166
167
168
169
   	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;
	}
170
}
Sebastian Hack's avatar
Sebastian Hack committed
171

Sebastian Hack's avatar
Sebastian Hack committed
172
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
173
{
Matthias Braun's avatar
Matthias Braun committed
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
	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
189
190
}

Sebastian Hack's avatar
Sebastian Hack committed
191
192
193
194
195
196
197
198
199
200
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);
	}
}

201
202
203
204
205
206
207
208
209
210
211
212
213
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;
}

214
215
216
int arch_is_register_operand(const arch_env_t *env,
    const ir_node *irn, int pos)
{
Sebastian Hack's avatar
Sebastian Hack committed
217
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
Matthias Braun's avatar
Matthias Braun committed
218
219
	const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, irn, pos);

Sebastian Hack's avatar
Sebastian Hack committed
220
	return req != NULL;
221
222
}

Sebastian Hack's avatar
Sebastian Hack committed
223
224
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
225
{
Matthias Braun's avatar
Matthias Braun committed
226
	const arch_register_req_t *req;
227

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

Matthias Braun's avatar
Matthias Braun committed
230
	if(req->type == arch_register_req_type_none)
Sebastian Hack's avatar
Sebastian Hack committed
231
232
		return 0;

Matthias Braun's avatar
Matthias Braun committed
233
234
235
	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));
236
237
	}

Matthias Braun's avatar
Matthias Braun committed
238
	return req->cls == reg->reg_class;
Sebastian Hack's avatar
Sebastian Hack committed
239
240
}

Sebastian Hack's avatar
Sebastian Hack committed
241
242
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
243
{
Matthias Braun's avatar
Matthias Braun committed
244
245
246
247
248
249
	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
250
251
}

Sebastian Hack's avatar
Sebastian Hack committed
252
extern const arch_register_t *
Sebastian Hack's avatar
Sebastian Hack committed
253
arch_get_irn_register(const arch_env_t *env, const ir_node *irn)
254
{
Matthias Braun's avatar
Matthias Braun committed
255
256
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
	return ops->impl->get_irn_reg(ops, irn);
257
}
258

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

extern arch_irn_class_t arch_irn_classify(const arch_env_t *env, const ir_node *irn)
{
Matthias Braun's avatar
Matthias Braun committed
268
269
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
	return ops->impl->classify(ops, irn);
270
}
Sebastian Hack's avatar
Sebastian Hack committed
271
272
273

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
274
275
	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
276
}
Sebastian Hack's avatar
Sebastian Hack committed
277
278
279
280
281
282
283
284

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
285
		XXX(modify_sp);
Sebastian Hack's avatar
Sebastian Hack committed
286
287
288
289
290
291
		XXX(none);
#undef XXX
	}
	return "n/a";
}

Matthias Braun's avatar
Matthias Braun committed
292
293
294
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
295
296
297
298
299
{
	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
300
301
302
		unsigned n_regs = req->cls->n_regs;
		unsigned i;

Sebastian Hack's avatar
Sebastian Hack committed
303
		strncat(buf, " limited:", len);
Matthias Braun's avatar
Matthias Braun committed
304
305
306
307
308
309
		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
310
311
312
313
		}
	}

	if(arch_register_req_is(req, should_be_same)) {
Matthias Braun's avatar
Matthias Braun committed
314
315
		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
316
317
318
319
		strncat(buf, tmp, len);
	}

	if(arch_register_req_is(req, should_be_different)) {
Matthias Braun's avatar
Matthias Braun committed
320
321
		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
322
323
324
325
326
		strncat(buf, tmp, len);
	}

	return buf;
}
Matthias Braun's avatar
Matthias Braun committed
327
328
329
330
331
332
333
334
335

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;