added callbacks to retrieve the cost of an operation and information about
[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 #include "irnode_t.h"
26
27 #include "bitset.h"
28 #include "pset.h"
29 #include "entity.h"
30
31 #include "irprintf.h"
32
33 /* Initialize the architecture environment struct. */
34 arch_env_t *arch_env_init(arch_env_t *env, const arch_isa_if_t *isa_if, FILE *file_handle)
35 {
36   memset(env, 0, sizeof(*env));
37   env->isa = isa_if->init(file_handle);
38   return env;
39 }
40
41 arch_env_t *arch_env_push_irn_handler(arch_env_t *env,
42     const arch_irn_handler_t *handler)
43 {
44   assert(env->handlers_tos <= ARCH_MAX_HANDLERS);
45   env->handlers[env->handlers_tos++] = handler;
46   return env;
47 }
48
49 const arch_irn_handler_t *arch_env_pop_irn_handler(arch_env_t *env)
50 {
51   assert(env->handlers_tos > 0 && env->handlers_tos <= ARCH_MAX_HANDLERS);
52   return env->handlers[--env->handlers_tos];
53 }
54
55 static const arch_irn_ops_t *fallback_irn_ops = NULL;
56
57 int arch_register_class_put(const arch_register_class_t *cls, bitset_t *bs)
58 {
59   if(bs) {
60     int i, n;
61     for(i = 0, n = cls->n_regs; i < n; ++i)
62       bitset_set(bs, i);
63   }
64
65   return cls->n_regs;
66 }
67
68 /**
69  * Get the isa responsible for a node.
70  * @param env The arch environment with the isa stack.
71  * @param irn The node to get the responsible isa for.
72  * @return The irn operations given by the responsible isa.
73  */
74 static INLINE const arch_irn_ops_t *
75 get_irn_ops(const arch_env_t *env, const ir_node *irn)
76 {
77   int i;
78
79   for(i = env->handlers_tos - 1; i >= 0; --i) {
80     const arch_irn_handler_t *handler = env->handlers[i];
81     const arch_irn_ops_t *ops = handler->get_irn_ops(handler, irn);
82
83     if(ops)
84       return ops;
85   }
86
87   return fallback_irn_ops;
88 }
89
90 const arch_register_req_t *arch_get_register_req(const arch_env_t *env,
91     arch_register_req_t *req, const ir_node *irn, int pos)
92 {
93   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
94   req->type = arch_register_req_type_none;
95   return ops->impl->get_irn_reg_req(ops, req, irn, pos);
96 }
97
98 void arch_set_frame_offset(const arch_env_t *env, ir_node *irn, int offset)
99 {
100   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
101   ops->impl->set_frame_offset(ops, irn, offset);
102 }
103
104 entity *arch_get_frame_entity(const arch_env_t *env, ir_node *irn)
105 {
106   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
107   return ops->impl->get_frame_entity(ops, irn);
108 }
109
110 arch_inverse_t *arch_get_inverse(const arch_env_t *env, const ir_node *irn, int i, arch_inverse_t *inverse, struct obstack *obstack)
111 {
112   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
113   if(ops->impl->get_inverse) {
114     return ops->impl->get_inverse(ops, irn, i, inverse, obstack);
115   } else {
116     return NULL;
117   }
118 }
119
120 int arch_get_op_estimated_cost(const arch_env_t *env, const ir_node *irn)
121 {
122   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
123   if(ops->impl->get_op_estimated_cost) {
124     return ops->impl->get_op_estimated_cost(ops, irn);
125   } else {
126     return 1;
127   }
128 }
129
130 int arch_is_possible_memory_operand(const arch_env_t *env, const ir_node *irn, int i)
131 {
132   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
133   if(ops->impl->possible_memory_operand) {
134     return ops->impl->possible_memory_operand(ops, irn, i);
135   } else {
136     return 0;
137   }
138 }
139
140 int arch_get_allocatable_regs(const arch_env_t *env, const ir_node *irn, int pos, bitset_t *bs)
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
146   if(req->type == arch_register_req_type_none) {
147           bitset_clear_all(bs);
148           return 0;
149   }
150
151   if(arch_register_req_is(req, limited)) {
152           req->limited(req->limited_env, bs);
153           return bitset_popcnt(bs);
154   }
155
156   arch_register_class_put(req->cls, bs);
157   return req->cls->n_regs;
158 }
159
160 void arch_put_non_ignore_regs(const arch_env_t *env, const arch_register_class_t *cls, bitset_t *bs)
161 {
162         int i;
163
164         for(i = 0; i < cls->n_regs; ++i) {
165                 if(!arch_register_type_is(&cls->regs[i], ignore))
166                         bitset_set(bs, i);
167         }
168 }
169
170 int arch_is_register_operand(const arch_env_t *env,
171     const ir_node *irn, int pos)
172 {
173         arch_register_req_t local_req;
174         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
175         const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, &local_req, irn, pos);
176         return req != NULL;
177 }
178
179 int arch_reg_is_allocatable(const arch_env_t *env, const ir_node *irn,
180     int pos, const arch_register_t *reg)
181 {
182         arch_register_req_t req;
183
184         arch_get_register_req(env, &req, irn, pos);
185
186         if(req.type == arch_register_req_type_none)
187                 return 0;
188
189         if(arch_register_req_is(&req, limited)) {
190                 bitset_t *bs = bitset_alloca(req.cls->n_regs);
191                 req.limited(req.limited_env, bs);
192                 return bitset_is_set(bs, arch_register_get_index(reg));
193         }
194
195         return req.cls == reg->reg_class;
196 }
197
198 const arch_register_class_t *
199 arch_get_irn_reg_class(const arch_env_t *env, const ir_node *irn, int pos)
200 {
201   arch_register_req_t local_req;
202   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
203   const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, &local_req, irn, pos);
204   return req ? req->cls : NULL;
205 }
206
207 extern const arch_register_t *
208 arch_get_irn_register(const arch_env_t *env, const ir_node *irn)
209 {
210   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
211   return ops->impl->get_irn_reg(ops, irn);
212 }
213
214 extern void arch_set_irn_register(const arch_env_t *env,
215     ir_node *irn, const arch_register_t *reg)
216 {
217   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
218   ops->impl->set_irn_reg(ops, irn, reg);
219 }
220
221 extern arch_irn_class_t arch_irn_classify(const arch_env_t *env, const ir_node *irn)
222 {
223   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
224   return ops->impl->classify(ops, irn);
225 }
226
227 extern arch_irn_flags_t arch_irn_get_flags(const arch_env_t *env, const ir_node *irn)
228 {
229   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
230   return ops->impl->get_flags(ops, irn);
231 }
232
233 extern const char *arch_irn_flag_str(arch_irn_flags_t fl)
234 {
235         switch(fl) {
236 #define XXX(x) case arch_irn_flags_ ## x: return #x;
237                 XXX(dont_spill);
238                 XXX(ignore);
239                 XXX(rematerializable);
240                 XXX(modify_sp);
241                 XXX(none);
242 #undef XXX
243         }
244         return "n/a";
245 }
246
247 extern char *arch_register_req_format(char *buf, size_t len, const arch_register_req_t *req)
248 {
249         char tmp[128];
250         snprintf(buf, len, "class: %s", req->cls->name);
251
252         if(arch_register_req_is(req, limited)) {
253                 bitset_pos_t elm;
254                 bitset_t *bs = bitset_alloca(req->cls->n_regs);
255                 req->limited(req->limited_env, bs);
256                 strncat(buf, " limited:", len);
257                 bitset_foreach(bs, elm) {
258                         strncat(buf, " ", len);
259                         strncat(buf, req->cls->regs[elm].name, len);
260                 }
261         }
262
263         if(arch_register_req_is(req, should_be_same)) {
264                 ir_snprintf(tmp, sizeof(tmp), " same to: %+F", req->other_different);
265                 strncat(buf, tmp, len);
266         }
267
268         if(arch_register_req_is(req, should_be_different)) {
269                 ir_snprintf(tmp, sizeof(tmp), " different to: %+F", req->other_different);
270                 strncat(buf, tmp, len);
271         }
272
273         return buf;
274 }