bearch.c 6.18 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
 */
26
#include "config.h"
Michael Beck's avatar
Michael Beck committed
27

28
29
#include <string.h>

30
#include "bearch_t.h"
Michael Beck's avatar
Michael Beck committed
31
#include "benode_t.h"
Sebastian Hack's avatar
Sebastian Hack committed
32
#include "ircons_t.h"
Daniel Grund's avatar
Daniel Grund committed
33
#include "irnode_t.h"
34

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

Sebastian Hack's avatar
Sebastian Hack committed
39
40
#include "irprintf.h"

41
/* Initialize the architecture environment struct. */
42
arch_env_t *arch_env_init(const arch_isa_if_t *isa_if, FILE *file_handle, be_main_env_t *main_env)
43
{
44
45
46
	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
47
48
49
}

/**
Sebastian Hack's avatar
Sebastian Hack committed
50
51
52
 * 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
53
 */
54
static inline const arch_irn_ops_t *get_irn_ops(const ir_node *irn)
55
{
Matthias Braun's avatar
Matthias Braun committed
56
57
58
59
60
61
62
	const ir_op          *ops;
	const arch_irn_ops_t *be_ops;

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

Matthias Braun's avatar
Matthias Braun committed
64
65
	ops    = get_irn_op(irn);
	be_ops = get_op_ops(ops)->be_ops;
Sebastian Hack's avatar
Sebastian Hack committed
66

Matthias Braun's avatar
Matthias Braun committed
67
68
	assert(be_ops);
	return be_ops;
69
70
}

71
const arch_register_req_t *arch_get_register_req(const ir_node *irn, int pos)
Sebastian Hack's avatar
Sebastian Hack committed
72
{
73
	const arch_irn_ops_t *ops = get_irn_ops(irn);
74
	return ops->get_irn_reg_req(irn, pos);
Sebastian Hack's avatar
Sebastian Hack committed
75
76
}

77
void arch_set_frame_offset(ir_node *irn, int offset)
Sebastian Hack's avatar
Sebastian Hack committed
78
{
79
	const arch_irn_ops_t *ops = get_irn_ops(irn);
80
	ops->set_frame_offset(irn, offset);
Sebastian Hack's avatar
Sebastian Hack committed
81
82
}

83
ir_entity *arch_get_frame_entity(const ir_node *irn)
84
{
85
	const arch_irn_ops_t *ops = get_irn_ops(irn);
86
	return ops->get_frame_entity(irn);
87
88
}

89
void arch_set_frame_entity(ir_node *irn, ir_entity *ent)
90
{
91
	const arch_irn_ops_t *ops = get_irn_ops(irn);
92
	ops->set_frame_entity(irn, ent);
93
94
}

95
int arch_get_sp_bias(ir_node *irn)
96
{
97
	const arch_irn_ops_t *ops = get_irn_ops(irn);
98
	return ops->get_sp_bias(irn);
99
100
}

101
arch_inverse_t *arch_get_inverse(const ir_node *irn, int i, arch_inverse_t *inverse, struct obstack *obstack)
102
{
103
	const arch_irn_ops_t *ops = get_irn_ops(irn);
Matthias Braun's avatar
Matthias Braun committed
104

Matthias Braun's avatar
Matthias Braun committed
105
	if(ops->get_inverse) {
106
		return ops->get_inverse(irn, i, inverse, obstack);
Matthias Braun's avatar
Matthias Braun committed
107
108
109
	} else {
		return NULL;
	}
110
111
}

112
113
int arch_possible_memory_operand(const ir_node *irn, unsigned int i)
{
114
	const arch_irn_ops_t *ops = get_irn_ops(irn);
Matthias Braun's avatar
Matthias Braun committed
115

Matthias Braun's avatar
Matthias Braun committed
116
	if(ops->possible_memory_operand) {
117
		return ops->possible_memory_operand(irn, i);
118
119
120
121
122
	} else {
		return 0;
	}
}

123
124
void arch_perform_memory_operand(ir_node *irn, ir_node *spill, unsigned int i)
{
125
	const arch_irn_ops_t *ops = get_irn_ops(irn);
Matthias Braun's avatar
Matthias Braun committed
126

Matthias Braun's avatar
Matthias Braun committed
127
	if(ops->perform_memory_operand) {
128
		ops->perform_memory_operand(irn, spill, i);
129
130
131
132
133
	} else {
		return;
	}
}

134
int arch_get_op_estimated_cost(const ir_node *irn)
135
{
136
	const arch_irn_ops_t *ops = get_irn_ops(irn);
Matthias Braun's avatar
Matthias Braun committed
137

Matthias Braun's avatar
Matthias Braun committed
138
	if(ops->get_op_estimated_cost) {
139
		return ops->get_op_estimated_cost(irn);
Matthias Braun's avatar
Matthias Braun committed
140
141
142
	} else {
		return 1;
	}
143
144
}

145
void arch_put_non_ignore_regs(const arch_register_class_t *cls, bitset_t *bs)
Sebastian Hack's avatar
Sebastian Hack committed
146
{
147
	unsigned i;
Sebastian Hack's avatar
Sebastian Hack committed
148
149
150
151
152
153
154

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

155
int arch_reg_is_allocatable(const ir_node *irn, int pos, const arch_register_t *reg)
Sebastian Hack's avatar
Sebastian Hack committed
156
{
157
	const arch_register_req_t *req = arch_get_register_req(irn, pos);
Sebastian Hack's avatar
Sebastian Hack committed
158

Matthias Braun's avatar
Matthias Braun committed
159
	if(req->type == arch_register_req_type_none)
Sebastian Hack's avatar
Sebastian Hack committed
160
161
		return 0;

Matthias Braun's avatar
Matthias Braun committed
162
163
164
	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));
165
166
	}

Matthias Braun's avatar
Matthias Braun committed
167
	return req->cls == reg->reg_class;
Sebastian Hack's avatar
Sebastian Hack committed
168
169
}

170
const arch_register_class_t *arch_get_irn_reg_class(const ir_node *irn, int pos)
Sebastian Hack's avatar
Sebastian Hack committed
171
{
172
	const arch_register_req_t *req = arch_get_register_req(irn, pos);
Matthias Braun's avatar
Matthias Braun committed
173
174
175
176

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

	return req->cls;
Sebastian Hack's avatar
Sebastian Hack committed
177
178
}

179
const arch_register_t *arch_get_irn_register(const ir_node *irn)
180
{
181
	const arch_irn_ops_t *ops = get_irn_ops(irn);
182
	return ops->get_irn_reg(irn);
183
}
184

185
void arch_set_irn_register(ir_node *irn, const arch_register_t *reg)
186
{
187
	const arch_irn_ops_t *ops = get_irn_ops(irn);
188
	ops->set_irn_reg(irn, reg);
189
}
190

191
arch_irn_class_t arch_irn_classify(const ir_node *irn)
192
{
193
	const arch_irn_ops_t *ops = get_irn_ops(irn);
194
	return ops->classify(irn);
195
}
Sebastian Hack's avatar
Sebastian Hack committed
196

197
arch_irn_flags_t arch_irn_get_flags(const ir_node *irn)
Sebastian Hack's avatar
Sebastian Hack committed
198
{
199
	const arch_irn_ops_t *ops = get_irn_ops(irn);
200
	return ops->get_flags(irn);
Sebastian Hack's avatar
Sebastian Hack committed
201
}
Sebastian Hack's avatar
Sebastian Hack committed
202

Matthias Braun's avatar
Matthias Braun committed
203
204
205
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
206
207
208
209
210
{
	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
211
212
213
		unsigned n_regs = req->cls->n_regs;
		unsigned i;

Sebastian Hack's avatar
Sebastian Hack committed
214
		strncat(buf, " limited:", len);
Matthias Braun's avatar
Matthias Braun committed
215
216
217
218
219
220
		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
221
222
223
224
		}
	}

	if(arch_register_req_is(req, should_be_same)) {
225
226
227
228
229
230
231
232
233
		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);
			}
234
		}
Sebastian Hack's avatar
Sebastian Hack committed
235
236
	}

237
	if (arch_register_req_is(req, must_be_different)) {
238
239
240
241
242
243
244
245
246
247
		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
248
249
250
251
	}

	return buf;
}
Matthias Braun's avatar
Matthias Braun committed
252
253
254
255
256

static const arch_register_req_t no_requirement = {
	arch_register_req_type_none,
	NULL,
	NULL,
257
258
	0,
	0
Matthias Braun's avatar
Matthias Braun committed
259
260
};
const arch_register_req_t *arch_no_register_req = &no_requirement;