bearch.c 4.5 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
16
17
18
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifdef HAVE_ALLOCA_H
#include <alloca.h>
#endif
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
19

20
21
#include <string.h>

Michael Beck's avatar
Michael Beck committed
22
#include "bearch.h"
Sebastian Hack's avatar
Sebastian Hack committed
23
#include "ircons_t.h"
24

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

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

Sebastian Hack's avatar
Sebastian Hack committed
36
37
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
38
{
Sebastian Hack's avatar
Sebastian Hack committed
39
40
41
  assert(env->handlers_tos <= ARCH_MAX_HANDLERS);
  env->handlers[env->handlers_tos++] = handler;
  return env;
42
43
}

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

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

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

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

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

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

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

Sebastian Hack's avatar
Sebastian Hack committed
86
87
int arch_get_allocatable_regs(const arch_env_t *env, const ir_node *irn,
    int pos, const arch_register_class_t *cls, bitset_t *bs)
Sebastian Hack's avatar
Sebastian Hack committed
88
{
Sebastian Hack's avatar
Sebastian Hack committed
89
  arch_register_req_t local_req;
Sebastian Hack's avatar
Sebastian Hack committed
90
  const arch_irn_ops_t *ops = get_irn_ops(env, irn);
Sebastian Hack's avatar
Sebastian Hack committed
91
  const arch_register_req_t *req = ops->get_irn_reg_req(ops, &local_req, irn, pos);
Sebastian Hack's avatar
Sebastian Hack committed
92

Sebastian Hack's avatar
Sebastian Hack committed
93
  switch(req->type) {
Daniel Grund's avatar
Daniel Grund committed
94
95
96
97
98
99
	case arch_register_req_type_none:
		bitset_clear_all(bs);
		return 0;

	case arch_register_req_type_should_be_different:
	case arch_register_req_type_should_be_same:
Sebastian Hack's avatar
Sebastian Hack committed
100
101
102
    case arch_register_req_type_normal:
      arch_register_class_put(req->cls, bs);
      return req->cls->n_regs;
Sebastian Hack's avatar
Sebastian Hack committed
103

Sebastian Hack's avatar
Sebastian Hack committed
104
105
    case arch_register_req_type_limited:
      return req->data.limited(irn, pos, bs);
Sebastian Hack's avatar
Sebastian Hack committed
106

Sebastian Hack's avatar
Sebastian Hack committed
107
108
109
    default:
      assert(0 && "This register requirement case is not covered");
  }
Sebastian Hack's avatar
Sebastian Hack committed
110

Sebastian Hack's avatar
Sebastian Hack committed
111
  return 0;
Sebastian Hack's avatar
Sebastian Hack committed
112
113
}

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

Sebastian Hack's avatar
Sebastian Hack committed
123
124
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
125
{
126
127
128
129
130
131
	int res = 0;
	arch_register_req_t req;

	arch_get_register_req(env, &req, irn, pos);
	switch(req.type) {
		case arch_register_req_type_normal:
Daniel Grund's avatar
Daniel Grund committed
132
133
		case arch_register_req_type_should_be_different:
		case arch_register_req_type_should_be_same:
134
135
136
137
138
139
140
141
			res = req.cls == reg->reg_class;
			break;
		case arch_register_req_type_limited:
			{
				bitset_t *bs = bitset_alloca(req.cls->n_regs);
				req.data.limited(irn, pos, bs);
				res = bitset_is_set(bs, arch_register_get_index(reg));
			}
Daniel Grund's avatar
Daniel Grund committed
142
			break;
143
144
145
146
147
		default:
			res = 0;
	}

	return res;
Sebastian Hack's avatar
Sebastian Hack committed
148
149
}

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

Sebastian Hack's avatar
Sebastian Hack committed
159
extern const arch_register_t *
Sebastian Hack's avatar
Sebastian Hack committed
160
arch_get_irn_register(const arch_env_t *env, const ir_node *irn)
161
{
Sebastian Hack's avatar
Sebastian Hack committed
162
  const arch_irn_ops_t *ops = get_irn_ops(env, irn);
Sebastian Hack's avatar
Sebastian Hack committed
163
  return ops->get_irn_reg(ops, irn);
164
}
165

Sebastian Hack's avatar
Sebastian Hack committed
166
extern void arch_set_irn_register(const arch_env_t *env,
Sebastian Hack's avatar
Sebastian Hack committed
167
    ir_node *irn, const arch_register_t *reg)
168
{
Sebastian Hack's avatar
Sebastian Hack committed
169
  const arch_irn_ops_t *ops = get_irn_ops(env, irn);
Sebastian Hack's avatar
Sebastian Hack committed
170
  ops->set_irn_reg(ops, irn, reg);
171
}
172
173
174
175
176
177

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);
  return ops->classify(ops, irn);
}
Sebastian Hack's avatar
Sebastian Hack committed
178
179
180
181
182
183

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);
  return ops->get_flags(ops, irn);
}