bearch.c 8.17 KB
Newer Older
1
2
3
4
5
6
7
/**
 * Processor architecture specification.
 * @author Sebastian Hack
 * @date 11.2.2005
 *
 * $Id$
 */
Sebastian Hack's avatar
Sebastian Hack committed
8

Michael Beck's avatar
Michael Beck committed
9
10
11
12
13
14
15
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

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

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

21
22
#include <string.h>

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

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

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
{
Sebastian Hack's avatar
Sebastian Hack committed
36
  memset(env, 0, sizeof(*env));
Christian Würdig's avatar
Christian Würdig committed
37
38
  env->isa                  = isa_if->init(file_handle);
  env->isa->main_env        = main_env;
Sebastian Hack's avatar
Sebastian Hack committed
39
  return env;
Sebastian Hack's avatar
Sebastian Hack committed
40
41
}

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

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

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
{
Sebastian Hack's avatar
Sebastian Hack 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

Sebastian Hack's avatar
Sebastian Hack 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
{
Sebastian Hack's avatar
Sebastian Hack committed
78
  int i;
79

Sebastian Hack's avatar
Sebastian Hack committed
80
  for(i = env->handlers_tos - 1; i >= 0; --i) {
Sebastian Hack's avatar
Sebastian Hack committed
81
82
83
    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
84
85
86
    if(ops)
      return ops;
  }
Sebastian Hack's avatar
Sebastian Hack committed
87

Sebastian Hack's avatar
Sebastian Hack 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
96
97
98
const arch_register_req_t *arch_get_register_req(const arch_env_t *env,
    arch_register_req_t *req, const ir_node *irn, int pos)
{
  const arch_irn_ops_t *ops = get_irn_ops(env, irn);
Sebastian Hack's avatar
Sebastian Hack committed
99
  req->type = arch_register_req_type_none;
100
  return ops->impl->get_irn_reg_req(ops, req, irn, pos);
Sebastian Hack's avatar
Sebastian Hack committed
101
102
}

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

109
110
111
112
113
114
entity *arch_get_frame_entity(const arch_env_t *env, ir_node *irn)
{
  const arch_irn_ops_t *ops = get_irn_ops(env, irn);
  return ops->impl->get_frame_entity(ops, irn);
}

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

121
122
123
124
125
126
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);
}

127
128
129
130
131
132
133
134
135
136
arch_inverse_t *arch_get_inverse(const arch_env_t *env, const ir_node *irn, int i, arch_inverse_t *inverse, struct obstack *obstack)
{
  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;
  }
}

137
138
139
140
141
142
143
144
145
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);
	if(ops->impl->possible_memory_operand) {
		return ops->impl->possible_memory_operand(ops, irn, i);
	} else {
		return 0;
	}
}

146
extern void arch_perform_memory_operand(const arch_env_t *env, ir_node *irn, ir_node *spill, unsigned int i) {
147
148
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
	if(ops->impl->perform_memory_operand) {
149
		ops->impl->perform_memory_operand(ops, irn, spill, i);
150
151
152
153
154
	} else {
		return;
	}
}

155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
int arch_get_op_estimated_cost(const arch_env_t *env, const ir_node *irn)
{
  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;
  }
}

int arch_is_possible_memory_operand(const arch_env_t *env, const ir_node *irn, int i)
{
  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;
  }
}
Sebastian Hack's avatar
Sebastian Hack committed
174

Sebastian Hack's avatar
Sebastian Hack committed
175
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
176
{
Sebastian Hack's avatar
Sebastian Hack committed
177
  arch_register_req_t local_req;
Sebastian Hack's avatar
Sebastian Hack committed
178
  const arch_irn_ops_t *ops = get_irn_ops(env, irn);
179
  const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, &local_req, irn, pos);
Sebastian Hack's avatar
Sebastian Hack committed
180

Sebastian Hack's avatar
Sebastian Hack committed
181
  if(req->type == arch_register_req_type_none) {
Sebastian Hack's avatar
Sebastian Hack committed
182
183
	  bitset_clear_all(bs);
	  return 0;
Sebastian Hack's avatar
Sebastian Hack committed
184
  }
Sebastian Hack's avatar
Sebastian Hack committed
185

Sebastian Hack's avatar
Sebastian Hack committed
186
  if(arch_register_req_is(req, limited)) {
Sebastian Hack's avatar
Sebastian Hack committed
187
	  req->limited(req->limited_env, bs);
Sebastian Hack's avatar
Sebastian Hack committed
188
189
	  return bitset_popcnt(bs);
  }
Sebastian Hack's avatar
Sebastian Hack committed
190
191
192

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

Sebastian Hack's avatar
Sebastian Hack committed
195
196
197
198
199
200
201
202
203
204
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);
	}
}

205
206
207
208
209
210
211
212
213
214
215
216
217
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;
}

218
219
220
int arch_is_register_operand(const arch_env_t *env,
    const ir_node *irn, int pos)
{
Sebastian Hack's avatar
Sebastian Hack committed
221
222
	arch_register_req_t local_req;
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
223
	const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, &local_req, irn, pos);
Sebastian Hack's avatar
Sebastian Hack committed
224
	return req != NULL;
225
226
}

Sebastian Hack's avatar
Sebastian Hack committed
227
228
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
229
{
230
231
232
	arch_register_req_t req;

	arch_get_register_req(env, &req, irn, pos);
Sebastian Hack's avatar
Sebastian Hack committed
233
234
235
236
237
238

	if(req.type == arch_register_req_type_none)
		return 0;

	if(arch_register_req_is(&req, limited)) {
		bitset_t *bs = bitset_alloca(req.cls->n_regs);
Sebastian Hack's avatar
Sebastian Hack committed
239
		req.limited(req.limited_env, bs);
Sebastian Hack's avatar
Sebastian Hack committed
240
		return bitset_is_set(bs, arch_register_get_index(reg));
241
242
	}

Sebastian Hack's avatar
Sebastian Hack committed
243
	return req.cls == reg->reg_class;
Sebastian Hack's avatar
Sebastian Hack committed
244
245
}

Sebastian Hack's avatar
Sebastian Hack committed
246
247
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
248
{
Sebastian Hack's avatar
Sebastian Hack committed
249
  arch_register_req_t local_req;
Sebastian Hack's avatar
Sebastian Hack committed
250
  const arch_irn_ops_t *ops = get_irn_ops(env, irn);
251
  const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, &local_req, irn, pos);
Sebastian Hack's avatar
Sebastian Hack committed
252
  return req ? req->cls : NULL;
Sebastian Hack's avatar
Sebastian Hack committed
253
254
}

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

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

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

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

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
288
		XXX(modify_sp);
Sebastian Hack's avatar
Sebastian Hack committed
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
		XXX(none);
#undef XXX
	}
	return "n/a";
}

extern char *arch_register_req_format(char *buf, size_t len, const arch_register_req_t *req)
{
	char tmp[128];
	snprintf(buf, len, "class: %s", req->cls->name);

	if(arch_register_req_is(req, limited)) {
		bitset_pos_t elm;
		bitset_t *bs = bitset_alloca(req->cls->n_regs);
		req->limited(req->limited_env, bs);
		strncat(buf, " limited:", len);
		bitset_foreach(bs, elm) {
			strncat(buf, " ", len);
			strncat(buf, req->cls->regs[elm].name, len);
		}
	}

	if(arch_register_req_is(req, should_be_same)) {
Sebastian Hack's avatar
Sebastian Hack committed
312
		ir_snprintf(tmp, sizeof(tmp), " same to: %+F", req->other_different);
Sebastian Hack's avatar
Sebastian Hack committed
313
314
315
316
		strncat(buf, tmp, len);
	}

	if(arch_register_req_is(req, should_be_different)) {
Sebastian Hack's avatar
Sebastian Hack committed
317
		ir_snprintf(tmp, sizeof(tmp), " different to: %+F", req->other_different);
Sebastian Hack's avatar
Sebastian Hack committed
318
319
320
321
322
		strncat(buf, tmp, len);
	}

	return buf;
}