bearch.c 9.06 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
 * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
 *
 * This file is part of libFirm.
 *
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation and appearing in the file LICENSE.GPL included in the
 * packaging of this file.
 *
 * Licensees holding valid libFirm Professional Edition licenses may use
 * this file in accordance with the libFirm Commercial License.
 * Agreement provided with the Software.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 */

20
21
22
23
24
25
26
/**
 * Processor architecture specification.
 * @author Sebastian Hack
 * @date 11.2.2005
 *
 * $Id$
 */
Michael Beck's avatar
Michael Beck committed
27
#ifdef HAVE_CONFIG_H
28
#include "config.h"
Michael Beck's avatar
Michael Beck committed
29
30
#endif

31
32
#include <string.h>

33
#include "bearch_t.h"
Sebastian Hack's avatar
Sebastian Hack committed
34
#include "ircons_t.h"
Daniel Grund's avatar
Daniel Grund committed
35
#include "irnode_t.h"
36
#include "xmalloc.h"
37

Sebastian Hack's avatar
Sebastian Hack committed
38
#include "bitset.h"
Sebastian Hack's avatar
Sebastian Hack committed
39
#include "pset.h"
40
#include "entity.h"
Matthias Braun's avatar
Matthias Braun committed
41
#include "raw_bitset.h"
42

Sebastian Hack's avatar
Sebastian Hack committed
43
44
#include "irprintf.h"

45
/* Initialize the architecture environment struct. */
Christian Würdig's avatar
Christian Würdig committed
46
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)
47
{
Matthias Braun's avatar
Matthias Braun committed
48
49
50
51
	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
52
53
}

54
arch_env_t *arch_env_push_irn_handler(arch_env_t *env,
Matthias Braun's avatar
Matthias Braun committed
55
                                      const arch_irn_handler_t *handler)
Sebastian Hack's avatar
Sebastian Hack committed
56
{
Matthias Braun's avatar
Matthias Braun committed
57
58
59
	assert(env->handlers_tos < ARCH_MAX_HANDLERS);
	env->handlers[env->handlers_tos++] = handler;
	return env;
60
61
}

62
63
const arch_irn_handler_t *arch_env_pop_irn_handler(arch_env_t *env)
{
Matthias Braun's avatar
Matthias Braun committed
64
65
   	assert(env->handlers_tos > 0 && env->handlers_tos <= ARCH_MAX_HANDLERS);
	return env->handlers[--env->handlers_tos];
66
67
}

Sebastian Hack's avatar
Sebastian Hack committed
68
static const arch_irn_ops_t *fallback_irn_ops = NULL;
Sebastian Hack's avatar
Sebastian Hack committed
69

Sebastian Hack's avatar
Sebastian Hack committed
70
int arch_register_class_put(const arch_register_class_t *cls, bitset_t *bs)
71
{
Matthias Braun's avatar
Matthias Braun committed
72
73
74
75
76
	if(bs) {
		int i, n;
		for(i = 0, n = cls->n_regs; i < n; ++i)
			bitset_set(bs, i);
	}
77

Matthias Braun's avatar
Matthias Braun committed
78
	return cls->n_regs;
79
80
}

Sebastian Hack's avatar
Sebastian Hack committed
81
/**
Sebastian Hack's avatar
Sebastian Hack committed
82
83
84
85
 * 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
86
 */
Sebastian Hack's avatar
Sebastian Hack committed
87
88
static INLINE const arch_irn_ops_t *
get_irn_ops(const arch_env_t *env, const ir_node *irn)
89
{
Matthias Braun's avatar
Matthias Braun committed
90
	int i;
91

Matthias Braun's avatar
Matthias Braun committed
92
93
94
	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
95

Matthias Braun's avatar
Matthias Braun committed
96
97
98
		if(ops)
			return ops;
	}
Sebastian Hack's avatar
Sebastian Hack committed
99

Matthias Braun's avatar
Matthias Braun committed
100
	return fallback_irn_ops;
101
102
}

103
104
105
106
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
107
const arch_register_req_t *arch_get_register_req(const arch_env_t *env,
Matthias Braun's avatar
Matthias Braun committed
108
                                                 const ir_node *irn, int pos)
Sebastian Hack's avatar
Sebastian Hack committed
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_irn_reg_req(ops, irn, pos);
Sebastian Hack's avatar
Sebastian Hack committed
112
113
}

114
void arch_set_frame_offset(const arch_env_t *env, ir_node *irn, int offset)
Sebastian Hack's avatar
Sebastian Hack committed
115
{
Matthias Braun's avatar
Matthias Braun committed
116
117
	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
118
119
}

120
ir_entity *arch_get_frame_entity(const arch_env_t *env, ir_node *irn)
121
{
Matthias Braun's avatar
Matthias Braun committed
122
123
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
	return ops->impl->get_frame_entity(ops, irn);
124
125
}

126
void arch_set_frame_entity(const arch_env_t *env, ir_node *irn, ir_entity *ent)
127
128
129
130
131
{
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
	ops->impl->set_frame_entity(ops, irn, ent);
}

132
133
134
135
136
137
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);
}

138
139
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
140
141
142
143
144
145
146
	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;
	}
147
148
}

149
150
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
151

152
153
154
155
156
157
158
	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
159
void arch_perform_memory_operand(const arch_env_t *env, ir_node *irn, ir_node *spill, unsigned int i) {
160
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
Matthias Braun's avatar
Matthias Braun committed
161

162
	if(ops->impl->perform_memory_operand) {
163
		ops->impl->perform_memory_operand(ops, irn, spill, i);
164
165
166
167
168
	} else {
		return;
	}
}

169
170
int arch_get_op_estimated_cost(const arch_env_t *env, const ir_node *irn)
{
Matthias Braun's avatar
Matthias Braun committed
171
172
173
174
175
176
177
	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;
	}
178
179
180
181
}

int arch_is_possible_memory_operand(const arch_env_t *env, const ir_node *irn, int i)
{
Matthias Braun's avatar
Matthias Braun committed
182
183
184
185
186
187
188
   	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;
	}
189
}
Sebastian Hack's avatar
Sebastian Hack committed
190

Sebastian Hack's avatar
Sebastian Hack committed
191
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
192
{
Matthias Braun's avatar
Matthias Braun committed
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
	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
208
209
}

Sebastian Hack's avatar
Sebastian Hack committed
210
211
212
213
214
215
216
217
218
219
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);
	}
}

220
221
222
223
224
225
226
227
228
229
230
231
232
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;
}

233
234
235
int arch_is_register_operand(const arch_env_t *env,
    const ir_node *irn, int pos)
{
Sebastian Hack's avatar
Sebastian Hack committed
236
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
Matthias Braun's avatar
Matthias Braun committed
237
238
	const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, irn, pos);

Sebastian Hack's avatar
Sebastian Hack committed
239
	return req != NULL;
240
241
}

Sebastian Hack's avatar
Sebastian Hack committed
242
243
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
244
{
Matthias Braun's avatar
Matthias Braun committed
245
	const arch_register_req_t *req;
246

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

Matthias Braun's avatar
Matthias Braun committed
249
	if(req->type == arch_register_req_type_none)
Sebastian Hack's avatar
Sebastian Hack committed
250
251
		return 0;

Matthias Braun's avatar
Matthias Braun committed
252
253
254
	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));
255
256
	}

Matthias Braun's avatar
Matthias Braun committed
257
	return req->cls == reg->reg_class;
Sebastian Hack's avatar
Sebastian Hack committed
258
259
}

Sebastian Hack's avatar
Sebastian Hack committed
260
261
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
262
{
Matthias Braun's avatar
Matthias Braun committed
263
264
265
266
267
268
	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
269
270
}

Sebastian Hack's avatar
Sebastian Hack committed
271
extern const arch_register_t *
Sebastian Hack's avatar
Sebastian Hack committed
272
arch_get_irn_register(const arch_env_t *env, const ir_node *irn)
273
{
Matthias Braun's avatar
Matthias Braun committed
274
275
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
	return ops->impl->get_irn_reg(ops, irn);
276
}
277

Sebastian Hack's avatar
Sebastian Hack committed
278
extern void arch_set_irn_register(const arch_env_t *env,
Sebastian Hack's avatar
Sebastian Hack committed
279
    ir_node *irn, const arch_register_t *reg)
280
{
Matthias Braun's avatar
Matthias Braun committed
281
282
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
	ops->impl->set_irn_reg(ops, irn, reg);
283
}
284
285
286

extern arch_irn_class_t arch_irn_classify(const arch_env_t *env, const ir_node *irn)
{
Matthias Braun's avatar
Matthias Braun committed
287
288
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
	return ops->impl->classify(ops, irn);
289
}
Sebastian Hack's avatar
Sebastian Hack committed
290
291
292

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
293
294
	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
295
}
Sebastian Hack's avatar
Sebastian Hack committed
296
297
298
299
300
301
302
303

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
304
		XXX(modify_sp);
Sebastian Hack's avatar
Sebastian Hack committed
305
306
307
308
309
310
		XXX(none);
#undef XXX
	}
	return "n/a";
}

Matthias Braun's avatar
Matthias Braun committed
311
312
313
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
314
315
316
317
318
{
	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
319
320
321
		unsigned n_regs = req->cls->n_regs;
		unsigned i;

Sebastian Hack's avatar
Sebastian Hack committed
322
		strncat(buf, " limited:", len);
Matthias Braun's avatar
Matthias Braun committed
323
324
325
326
327
328
		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
329
330
331
332
		}
	}

	if(arch_register_req_is(req, should_be_same)) {
Matthias Braun's avatar
Matthias Braun committed
333
334
		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
335
336
337
338
		strncat(buf, tmp, len);
	}

	if(arch_register_req_is(req, should_be_different)) {
Matthias Braun's avatar
Matthias Braun committed
339
340
		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
341
342
343
344
345
		strncat(buf, tmp, len);
	}

	return buf;
}
Matthias Braun's avatar
Matthias Braun committed
346
347
348
349
350
351
352
353
354

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;