bearch.c 8.63 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
/*
Michael Beck's avatar
Michael Beck committed
2
 * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
Christian Würdig's avatar
Christian Würdig committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * 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
/**
Christian Würdig's avatar
Christian Würdig committed
21
22
23
24
 * @file
 * @brief       Processor architecture specification.
 * @author      Sebastian Hack
 * @version     $Id$
25
 */
Michael Beck's avatar
Michael Beck committed
26
#ifdef HAVE_CONFIG_H
27
#include "config.h"
Michael Beck's avatar
Michael Beck committed
28
29
#endif

30
31
#include <string.h>

32
#include "bearch_t.h"
Michael Beck's avatar
Michael Beck committed
33
#include "benode_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

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

Sebastian Hack's avatar
Sebastian Hack committed
41
42
#include "irprintf.h"

43
/* Initialize the architecture environment struct. */
44
arch_env_t *arch_env_init(const arch_isa_if_t *isa_if, FILE *file_handle, be_main_env_t *main_env)
45
{
46
47
48
	arch_env_t *arch_env = isa_if->init(file_handle);
	arch_env->main_env   = main_env;
	return arch_env;
Sebastian Hack's avatar
Sebastian Hack committed
49
50
}

51
52
53
/**
 * Put all registers in a class into a bitset.
 * @param cls The class.
54
 * @param bs The bitset.
55
56
57
 * @return The number of registers in the class.
 */
static int arch_register_class_put(const arch_register_class_t *cls, bitset_t *bs)
58
{
Michael Beck's avatar
Michael Beck committed
59
60
	int i, n = cls->n_regs;
	for (i = n - 1; i >= 0; --i)
61
		bitset_set(bs, i);
Michael Beck's avatar
Michael Beck committed
62
	return n;
63
64
}

Sebastian Hack's avatar
Sebastian Hack committed
65
/**
Sebastian Hack's avatar
Sebastian Hack committed
66
67
68
 * Get the isa responsible for a node.
 * @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
69
 */
70
static INLINE const arch_irn_ops_t *get_irn_ops(const ir_node *irn)
71
{
Matthias Braun's avatar
Matthias Braun committed
72
73
74
75
76
77
78
	const ir_op          *ops;
	const arch_irn_ops_t *be_ops;

	if (is_Proj(irn)) {
		irn = get_Proj_pred(irn);
		assert(!is_Proj(irn));
	}
79

Matthias Braun's avatar
Matthias Braun committed
80
81
	ops    = get_irn_op(irn);
	be_ops = get_op_ops(ops)->be_ops;
Sebastian Hack's avatar
Sebastian Hack committed
82

Matthias Braun's avatar
Matthias Braun committed
83
84
	assert(be_ops);
	return be_ops;
85
86
}

87
const arch_register_req_t *arch_get_register_req(const ir_node *irn, int pos)
Sebastian Hack's avatar
Sebastian Hack committed
88
{
89
	const arch_irn_ops_t *ops = get_irn_ops(irn);
90
	return ops->get_irn_reg_req(irn, pos);
Sebastian Hack's avatar
Sebastian Hack committed
91
92
}

93
void arch_set_frame_offset(ir_node *irn, int offset)
Sebastian Hack's avatar
Sebastian Hack committed
94
{
95
	const arch_irn_ops_t *ops = get_irn_ops(irn);
96
	ops->set_frame_offset(irn, offset);
Sebastian Hack's avatar
Sebastian Hack committed
97
98
}

99
ir_entity *arch_get_frame_entity(const ir_node *irn)
100
{
101
	const arch_irn_ops_t *ops = get_irn_ops(irn);
102
	return ops->get_frame_entity(irn);
103
104
}

105
void arch_set_frame_entity(const arch_env_t *env, ir_node *irn, ir_entity *ent)
106
{
107
108
	const arch_irn_ops_t *ops = get_irn_ops(irn);
	(void)env; // TODO remove parameter
109
	ops->set_frame_entity(irn, ent);
110
111
}

112
113
int arch_get_sp_bias(const arch_env_t *env, ir_node *irn)
{
114
115
	const arch_irn_ops_t *ops = get_irn_ops(irn);
	(void)env; // TODO remove parameter
116
	return ops->get_sp_bias(irn);
117
118
}

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)
{
121
122
	const arch_irn_ops_t *ops = get_irn_ops(irn);
	(void)env; // TODO remove parameter
Matthias Braun's avatar
Matthias Braun committed
123

Matthias Braun's avatar
Matthias Braun committed
124
	if(ops->get_inverse) {
125
		return ops->get_inverse(irn, i, inverse, obstack);
Matthias Braun's avatar
Matthias Braun committed
126
127
128
	} else {
		return NULL;
	}
129
130
}

131
int arch_possible_memory_operand(const arch_env_t *env, const ir_node *irn, unsigned int i) {
132
133
	const arch_irn_ops_t *ops = get_irn_ops(irn);
	(void)env; // TODO remove parameter
Matthias Braun's avatar
Matthias Braun committed
134

Matthias Braun's avatar
Matthias Braun committed
135
	if(ops->possible_memory_operand) {
136
		return ops->possible_memory_operand(irn, i);
137
138
139
140
141
	} else {
		return 0;
	}
}

Christian Würdig's avatar
Christian Würdig committed
142
void arch_perform_memory_operand(const arch_env_t *env, ir_node *irn, ir_node *spill, unsigned int i) {
143
144
	const arch_irn_ops_t *ops = get_irn_ops(irn);
	(void)env; // TODO remove parameter
Matthias Braun's avatar
Matthias Braun committed
145

Matthias Braun's avatar
Matthias Braun committed
146
	if(ops->perform_memory_operand) {
147
		ops->perform_memory_operand(irn, spill, i);
148
149
150
151
152
	} else {
		return;
	}
}

153
154
int arch_get_op_estimated_cost(const arch_env_t *env, const ir_node *irn)
{
155
156
	const arch_irn_ops_t *ops = get_irn_ops(irn);
	(void)env; // TODO remove parameter
Matthias Braun's avatar
Matthias Braun committed
157

Matthias Braun's avatar
Matthias Braun committed
158
	if(ops->get_op_estimated_cost) {
159
		return ops->get_op_estimated_cost(irn);
Matthias Braun's avatar
Matthias Braun committed
160
161
162
	} else {
		return 1;
	}
163
164
165
166
}

int arch_is_possible_memory_operand(const arch_env_t *env, const ir_node *irn, int i)
{
167
168
	const arch_irn_ops_t *ops = get_irn_ops(irn);
	(void)env; // TODO remove parameter
Matthias Braun's avatar
Matthias Braun committed
169

Matthias Braun's avatar
Matthias Braun committed
170
	if(ops->possible_memory_operand) {
171
	   	return ops->possible_memory_operand(irn, i);
Matthias Braun's avatar
Matthias Braun committed
172
173
174
	} else {
		return 0;
	}
175
}
Sebastian Hack's avatar
Sebastian Hack committed
176

Sebastian Hack's avatar
Sebastian Hack committed
177
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
178
{
179
	const arch_irn_ops_t *ops = get_irn_ops(irn);
180
	const arch_register_req_t *req = ops->get_irn_reg_req(irn, pos);
181
	(void)env; // TODO remove parameter
Matthias Braun's avatar
Matthias Braun committed
182
183
184
185
186
187
188
189
190
191
192
193
194

	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
195
196
}

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

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

207
208
209
int arch_is_register_operand(const arch_env_t *env,
    const ir_node *irn, int pos)
{
210
	const arch_irn_ops_t *ops = get_irn_ops(irn);
211
	const arch_register_req_t *req = ops->get_irn_reg_req(irn, pos);
212
	(void)env; // TODO remove parameter
Matthias Braun's avatar
Matthias Braun committed
213

Sebastian Hack's avatar
Sebastian Hack committed
214
	return req != NULL;
215
216
}

Sebastian Hack's avatar
Sebastian Hack committed
217
218
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
219
{
220
221
	const arch_register_req_t *req = arch_get_register_req(irn, pos);
	(void)env; // TODO remove parameter
Sebastian Hack's avatar
Sebastian Hack committed
222

Matthias Braun's avatar
Matthias Braun committed
223
	if(req->type == arch_register_req_type_none)
Sebastian Hack's avatar
Sebastian Hack committed
224
225
		return 0;

Matthias Braun's avatar
Matthias Braun committed
226
227
228
	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));
229
230
	}

Matthias Braun's avatar
Matthias Braun committed
231
	return req->cls == reg->reg_class;
Sebastian Hack's avatar
Sebastian Hack committed
232
233
}

Sebastian Hack's avatar
Sebastian Hack committed
234
235
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
236
{
237
	const arch_irn_ops_t *ops = get_irn_ops(irn);
238
	const arch_register_req_t *req = ops->get_irn_reg_req(irn, pos);
239
	(void)env; // TODO remove parameter
Matthias Braun's avatar
Matthias Braun committed
240
241
242
243

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

	return req->cls;
Sebastian Hack's avatar
Sebastian Hack committed
244
245
}

Sebastian Hack's avatar
Sebastian Hack committed
246
extern const arch_register_t *
Sebastian Hack's avatar
Sebastian Hack committed
247
arch_get_irn_register(const arch_env_t *env, const ir_node *irn)
248
{
249
250
	const arch_irn_ops_t *ops = get_irn_ops(irn);
	(void)env; // TODO remove parameter
251
	return ops->get_irn_reg(irn);
252
}
253

Sebastian Hack's avatar
Sebastian Hack committed
254
extern void arch_set_irn_register(const arch_env_t *env,
Sebastian Hack's avatar
Sebastian Hack committed
255
    ir_node *irn, const arch_register_t *reg)
256
{
257
258
	const arch_irn_ops_t *ops = get_irn_ops(irn);
	(void)env; // TODO remove parameter
259
	ops->set_irn_reg(irn, reg);
260
}
261
262
263

extern arch_irn_class_t arch_irn_classify(const arch_env_t *env, const ir_node *irn)
{
264
265
	const arch_irn_ops_t *ops = get_irn_ops(irn);
	(void)env; // TODO remove parameter
266
	return ops->classify(irn);
267
}
Sebastian Hack's avatar
Sebastian Hack committed
268
269
270

extern arch_irn_flags_t arch_irn_get_flags(const arch_env_t *env, const ir_node *irn)
{
271
272
	const arch_irn_ops_t *ops = get_irn_ops(irn);
	(void)env; // TODO remove parameter
273
	return ops->get_flags(irn);
Sebastian Hack's avatar
Sebastian Hack committed
274
}
Sebastian Hack's avatar
Sebastian Hack committed
275
276
277
278
279
280
281
282

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

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

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

	if(arch_register_req_is(req, should_be_same)) {
313
314
315
316
317
318
319
320
321
		const unsigned other = req->other_same;
		int i;

		ir_snprintf(tmp, sizeof(tmp), " same to:");
		for (i = 0; 1U << i <= other; ++i) {
			if (other & (1U << i)) {
				ir_snprintf(tmp, sizeof(tmp), " %+F", get_irn_n(skip_Proj_const(node), i));
				strncat(buf, tmp, len);
			}
322
		}
Sebastian Hack's avatar
Sebastian Hack committed
323
324
	}

325
	if (arch_register_req_is(req, must_be_different)) {
326
327
328
329
330
331
332
333
334
335
		const unsigned other = req->other_different;
		int i;

		ir_snprintf(tmp, sizeof(tmp), " different from:");
		for (i = 0; 1U << i <= other; ++i) {
			if (other & (1U << i)) {
				ir_snprintf(tmp, sizeof(tmp), " %+F", get_irn_n(skip_Proj_const(node), i));
				strncat(buf, tmp, len);
			}
		}
Sebastian Hack's avatar
Sebastian Hack committed
336
337
338
339
	}

	return buf;
}
Matthias Braun's avatar
Matthias Braun committed
340
341
342
343
344

static const arch_register_req_t no_requirement = {
	arch_register_req_type_none,
	NULL,
	NULL,
345
346
	0,
	0
Matthias Braun's avatar
Matthias Braun committed
347
348
};
const arch_register_req_t *arch_no_register_req = &no_requirement;