95a18fde5f7b041a21044e7a8d8cef54c201c063
[libfirm] / ir / be / bearch.c
1 /**
2  * Processor architecture specification.
3  * @author Sebastian Hack
4  * @date 11.2.2005
5  *
6  * $Id$
7  */
8
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif
12
13 #ifdef HAVE_ALLOCA_H
14 #include <alloca.h>
15 #endif
16
17 #ifdef HAVE_MALLOC_H
18 #include <malloc.h>
19 #endif
20
21 #include <string.h>
22
23 #include "bearch.h"
24 #include "ircons_t.h"
25
26 #include "bitset.h"
27 #include "pset.h"
28 #include "entity.h"
29
30 #include "irprintf.h"
31
32 arch_env_t *arch_env_init(arch_env_t *env, const arch_isa_if_t *isa_if)
33 {
34   memset(env, 0, sizeof(*env));
35   env->isa = isa_if->init();
36   return env;
37 }
38
39 arch_env_t *arch_env_push_irn_handler(arch_env_t *env,
40     const arch_irn_handler_t *handler)
41 {
42   assert(env->handlers_tos <= ARCH_MAX_HANDLERS);
43   env->handlers[env->handlers_tos++] = handler;
44   return env;
45 }
46
47 const arch_irn_handler_t *arch_env_pop_irn_handler(arch_env_t *env)
48 {
49   assert(env->handlers_tos > 0 && env->handlers_tos <= ARCH_MAX_HANDLERS);
50   return env->handlers[--env->handlers_tos];
51 }
52
53 static const arch_irn_ops_t *fallback_irn_ops = NULL;
54
55 int arch_register_class_put(const arch_register_class_t *cls, bitset_t *bs)
56 {
57   if(bs) {
58     int i, n;
59     for(i = 0, n = cls->n_regs; i < n; ++i)
60       bitset_set(bs, i);
61   }
62
63   return cls->n_regs;
64 }
65
66 /**
67  * Get the isa responsible for a node.
68  * @param env The arch environment with the isa stack.
69  * @param irn The node to get the responsible isa for.
70  * @return The irn operations given by the responsible isa.
71  */
72 static INLINE const arch_irn_ops_t *
73 get_irn_ops(const arch_env_t *env, const ir_node *irn)
74 {
75   int i;
76
77   for(i = env->handlers_tos - 1; i >= 0; --i) {
78     const arch_irn_handler_t *handler = env->handlers[i];
79     const arch_irn_ops_t *ops = handler->get_irn_ops(handler, irn);
80
81     if(ops)
82       return ops;
83   }
84
85   return fallback_irn_ops;
86 }
87
88 const arch_register_req_t *arch_get_register_req(const arch_env_t *env,
89     arch_register_req_t *req, const ir_node *irn, int pos)
90 {
91   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
92   req->type = arch_register_req_type_none;
93   return ops->impl->get_irn_reg_req(ops, req, irn, pos);
94 }
95
96 void arch_set_frame_offset(const arch_env_t *env, ir_node *irn, int offset)
97 {
98   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
99   ops->impl->set_frame_offset(ops, irn, offset);
100 }
101
102 entity *arch_get_frame_entity(const arch_env_t *env, ir_node *irn)
103 {
104   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
105   return ops->impl->get_frame_entity(ops, irn);
106 }
107
108
109 int arch_get_allocatable_regs(const arch_env_t *env, const ir_node *irn, int pos, bitset_t *bs)
110 {
111   arch_register_req_t local_req;
112   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
113   const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, &local_req, irn, pos);
114
115   if(req->type == arch_register_req_type_none) {
116           bitset_clear_all(bs);
117           return 0;
118   }
119
120   if(arch_register_req_is(req, limited)) {
121           req->limited(req->limited_env, bs);
122           return bitset_popcnt(bs);
123   }
124
125   arch_register_class_put(req->cls, bs);
126   return req->cls->n_regs;
127 }
128
129 void arch_put_non_ignore_regs(const arch_env_t *env, const arch_register_class_t *cls, bitset_t *bs)
130 {
131         int i;
132
133         for(i = 0; i < cls->n_regs; ++i) {
134                 if(!arch_register_type_is(&cls->regs[i], ignore))
135                         bitset_set(bs, i);
136         }
137 }
138
139 int arch_is_register_operand(const arch_env_t *env,
140     const ir_node *irn, int pos)
141 {
142         arch_register_req_t local_req;
143         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
144         const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, &local_req, irn, pos);
145         return req != NULL;
146 }
147
148 int arch_reg_is_allocatable(const arch_env_t *env, const ir_node *irn,
149     int pos, const arch_register_t *reg)
150 {
151         arch_register_req_t req;
152
153         arch_get_register_req(env, &req, irn, pos);
154
155         if(req.type == arch_register_req_type_none)
156                 return 0;
157
158         if(arch_register_req_is(&req, limited)) {
159                 bitset_t *bs = bitset_alloca(req.cls->n_regs);
160                 req.limited(req.limited_env, bs);
161                 return bitset_is_set(bs, arch_register_get_index(reg));
162         }
163
164         return req.cls == reg->reg_class;
165 }
166
167 const arch_register_class_t *
168 arch_get_irn_reg_class(const arch_env_t *env, const ir_node *irn, int pos)
169 {
170   arch_register_req_t local_req;
171   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
172   const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, &local_req, irn, pos);
173   return req ? req->cls : NULL;
174 }
175
176 extern const arch_register_t *
177 arch_get_irn_register(const arch_env_t *env, const ir_node *irn)
178 {
179   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
180   return ops->impl->get_irn_reg(ops, irn);
181 }
182
183 extern void arch_set_irn_register(const arch_env_t *env,
184     ir_node *irn, const arch_register_t *reg)
185 {
186   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
187   ops->impl->set_irn_reg(ops, irn, reg);
188 }
189
190 extern arch_irn_class_t arch_irn_classify(const arch_env_t *env, const ir_node *irn)
191 {
192   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
193   return ops->impl->classify(ops, irn);
194 }
195
196 extern arch_irn_flags_t arch_irn_get_flags(const arch_env_t *env, const ir_node *irn)
197 {
198   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
199   return ops->impl->get_flags(ops, irn);
200 }
201
202 extern const char *arch_irn_flag_str(arch_irn_flags_t fl)
203 {
204         switch(fl) {
205 #define XXX(x) case arch_irn_flags_ ## x: return #x;
206                 XXX(dont_spill);
207                 XXX(ignore);
208                 XXX(rematerializable);
209                 XXX(none);
210 #undef XXX
211         }
212         return "n/a";
213 }
214
215 extern char *arch_register_req_format(char *buf, size_t len, const arch_register_req_t *req)
216 {
217         char tmp[128];
218         snprintf(buf, len, "class: %s", req->cls->name);
219
220         if(arch_register_req_is(req, limited)) {
221                 bitset_pos_t elm;
222                 bitset_t *bs = bitset_alloca(req->cls->n_regs);
223                 req->limited(req->limited_env, bs);
224                 strncat(buf, " limited:", len);
225                 bitset_foreach(bs, elm) {
226                         strncat(buf, " ", len);
227                         strncat(buf, req->cls->regs[elm].name, len);
228                 }
229         }
230
231         if(arch_register_req_is(req, should_be_same)) {
232                 ir_snprintf(tmp, sizeof(tmp), " same to: %+F", req->other_different);
233                 strncat(buf, tmp, len);
234         }
235
236         if(arch_register_req_is(req, should_be_different)) {
237                 ir_snprintf(tmp, sizeof(tmp), " different to: %+F", req->other_different);
238                 strncat(buf, tmp, len);
239         }
240
241         return buf;
242 }