bearch.c 4.36 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"
25

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

Sebastian Hack's avatar
Sebastian Hack committed
30
arch_env_t *arch_env_init(arch_env_t *env, const arch_isa_if_t *isa_if)
31
{
Sebastian Hack's avatar
Sebastian Hack committed
32
  memset(env, 0, sizeof(*env));
Sebastian Hack's avatar
Sebastian Hack committed
33
  env->isa = isa_if->init();
Sebastian Hack's avatar
Sebastian Hack committed
34
  return env;
Sebastian Hack's avatar
Sebastian Hack committed
35
36
}

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

Sebastian Hack's avatar
Sebastian Hack committed
45
static const arch_irn_ops_t *fallback_irn_ops = NULL;
Sebastian Hack's avatar
Sebastian Hack committed
46

Sebastian Hack's avatar
Sebastian Hack committed
47
int arch_register_class_put(const arch_register_class_t *cls, bitset_t *bs)
48
{
Sebastian Hack's avatar
Sebastian Hack committed
49
50
51
52
53
  if(bs) {
    int i, n;
    for(i = 0, n = cls->n_regs; i < n; ++i)
      bitset_set(bs, i);
  }
54

Sebastian Hack's avatar
Sebastian Hack committed
55
  return cls->n_regs;
56
57
}

Sebastian Hack's avatar
Sebastian Hack committed
58
/**
Sebastian Hack's avatar
Sebastian Hack committed
59
60
61
62
 * 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
63
 */
Sebastian Hack's avatar
Sebastian Hack committed
64
65
static INLINE const arch_irn_ops_t *
get_irn_ops(const arch_env_t *env, const ir_node *irn)
66
{
Sebastian Hack's avatar
Sebastian Hack committed
67
  int i;
68

Sebastian Hack's avatar
Sebastian Hack committed
69
  for(i = env->handlers_tos - 1; i >= 0; --i) {
Sebastian Hack's avatar
Sebastian Hack committed
70
71
72
    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
73
74
75
    if(ops)
      return ops;
  }
Sebastian Hack's avatar
Sebastian Hack committed
76

Sebastian Hack's avatar
Sebastian Hack committed
77
  return fallback_irn_ops;
78
79
}

Sebastian Hack's avatar
Sebastian Hack committed
80
81
82
83
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
84
  req->type = arch_register_req_type_none;
85
  return ops->impl->get_irn_reg_req(ops, req, irn, pos);
Sebastian Hack's avatar
Sebastian Hack committed
86
87
}

Sebastian Hack's avatar
Sebastian Hack committed
88
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
89
{
Sebastian Hack's avatar
Sebastian Hack committed
90
  arch_register_req_t local_req;
Sebastian Hack's avatar
Sebastian Hack committed
91
  const arch_irn_ops_t *ops = get_irn_ops(env, irn);
92
  const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, &local_req, irn, pos);
Sebastian Hack's avatar
Sebastian Hack committed
93

Sebastian Hack's avatar
Sebastian Hack committed
94
  if(req->type == arch_register_req_type_none) {
Sebastian Hack's avatar
Sebastian Hack committed
95
96
	  bitset_clear_all(bs);
	  return 0;
Sebastian Hack's avatar
Sebastian Hack committed
97
  }
Sebastian Hack's avatar
Sebastian Hack committed
98

Sebastian Hack's avatar
Sebastian Hack committed
99
  if(arch_register_req_is(req, limited)) {
Sebastian Hack's avatar
Sebastian Hack committed
100
	  req->limited(req->limited_env, bs);
Sebastian Hack's avatar
Sebastian Hack committed
101
102
	  return bitset_popcnt(bs);
  }
Sebastian Hack's avatar
Sebastian Hack committed
103
104
105

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

Sebastian Hack's avatar
Sebastian Hack committed
108
109
110
111
112
113
114
115
116
117
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);
	}
}

118
119
120
int arch_is_register_operand(const arch_env_t *env,
    const ir_node *irn, int pos)
{
Sebastian Hack's avatar
Sebastian Hack committed
121
122
	arch_register_req_t local_req;
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
123
	const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, &local_req, irn, pos);
Sebastian Hack's avatar
Sebastian Hack committed
124
	return req != NULL;
125
126
}

Sebastian Hack's avatar
Sebastian Hack committed
127
128
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
129
{
130
131
132
	arch_register_req_t req;

	arch_get_register_req(env, &req, irn, pos);
Sebastian Hack's avatar
Sebastian Hack committed
133
134
135
136
137
138

	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
139
		req.limited(req.limited_env, bs);
Sebastian Hack's avatar
Sebastian Hack committed
140
		return bitset_is_set(bs, arch_register_get_index(reg));
141
142
	}

Sebastian Hack's avatar
Sebastian Hack committed
143
	return req.cls == reg->reg_class;
Sebastian Hack's avatar
Sebastian Hack committed
144
145
}

Sebastian Hack's avatar
Sebastian Hack committed
146
147
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
148
{
Sebastian Hack's avatar
Sebastian Hack committed
149
  arch_register_req_t local_req;
Sebastian Hack's avatar
Sebastian Hack committed
150
  const arch_irn_ops_t *ops = get_irn_ops(env, irn);
151
  const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, &local_req, irn, pos);
Sebastian Hack's avatar
Sebastian Hack committed
152
  return req ? req->cls : NULL;
Sebastian Hack's avatar
Sebastian Hack committed
153
154
}

Sebastian Hack's avatar
Sebastian Hack committed
155
extern const arch_register_t *
Sebastian Hack's avatar
Sebastian Hack committed
156
arch_get_irn_register(const arch_env_t *env, const ir_node *irn)
157
{
Sebastian Hack's avatar
Sebastian Hack committed
158
  const arch_irn_ops_t *ops = get_irn_ops(env, irn);
159
  return ops->impl->get_irn_reg(ops, irn);
160
}
161

Sebastian Hack's avatar
Sebastian Hack committed
162
extern void arch_set_irn_register(const arch_env_t *env,
Sebastian Hack's avatar
Sebastian Hack committed
163
    ir_node *irn, const arch_register_t *reg)
164
{
Sebastian Hack's avatar
Sebastian Hack committed
165
  const arch_irn_ops_t *ops = get_irn_ops(env, irn);
166
  ops->impl->set_irn_reg(ops, irn, reg);
167
}
168
169
170
171

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);
172
  return ops->impl->classify(ops, irn);
173
}
Sebastian Hack's avatar
Sebastian Hack committed
174
175
176
177

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);
178
  return ops->impl->get_flags(ops, irn);
Sebastian Hack's avatar
Sebastian Hack committed
179
}