- Allow an arbitrary (for arbitrary < 32) number of in_rBAR and !in_rBAR constraints...
[libfirm] / ir / be / bearch.c
1 /*
2  * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       Processor architecture specification.
23  * @author      Sebastian Hack
24  * @version     $Id$
25  */
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <string.h>
31
32 #include "bearch_t.h"
33 #include "ircons_t.h"
34 #include "irnode_t.h"
35 #include "xmalloc.h"
36
37 #include "bitset.h"
38 #include "pset.h"
39 #include "raw_bitset.h"
40
41 #include "irprintf.h"
42
43 /* Initialize the architecture environment struct. */
44 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)
45 {
46         memset(env, 0, sizeof(*env));
47         env->isa                  = isa_if->init(file_handle);
48         env->isa->main_env        = main_env;
49         return env;
50 }
51
52 arch_env_t *arch_env_push_irn_handler(arch_env_t *env,
53                                       const arch_irn_handler_t *handler)
54 {
55         assert(env->handlers_tos < ARCH_MAX_HANDLERS);
56         env->handlers[env->handlers_tos++] = handler;
57         return env;
58 }
59
60 const arch_irn_handler_t *arch_env_pop_irn_handler(arch_env_t *env)
61 {
62         assert(env->handlers_tos > 0 && env->handlers_tos <= ARCH_MAX_HANDLERS);
63         return env->handlers[--env->handlers_tos];
64 }
65
66 static const arch_irn_ops_t *fallback_irn_ops = NULL;
67
68 int arch_register_class_put(const arch_register_class_t *cls, bitset_t *bs)
69 {
70         if(bs) {
71                 int i, n;
72                 for(i = 0, n = cls->n_regs; i < n; ++i)
73                         bitset_set(bs, i);
74         }
75
76         return cls->n_regs;
77 }
78
79 /**
80  * Get the isa responsible for a node.
81  * @param env The arch environment with the isa stack.
82  * @param irn The node to get the responsible isa for.
83  * @return The irn operations given by the responsible isa.
84  */
85 static INLINE const arch_irn_ops_t *
86 get_irn_ops(const arch_env_t *env, const ir_node *irn)
87 {
88         int i;
89
90         for(i = env->handlers_tos - 1; i >= 0; --i) {
91                 const arch_irn_handler_t *handler = env->handlers[i];
92                 const arch_irn_ops_t *ops = handler->get_irn_ops(handler, irn);
93
94                 if(ops)
95                         return ops;
96         }
97
98         return fallback_irn_ops;
99 }
100
101 const arch_irn_ops_t *arch_get_irn_ops(const arch_env_t *env, const ir_node *irn) {
102         return get_irn_ops(env, irn);
103 }
104
105 const arch_register_req_t *arch_get_register_req(const arch_env_t *env,
106                                                  const ir_node *irn, int pos)
107 {
108         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
109         return ops->impl->get_irn_reg_req(ops, irn, pos);
110 }
111
112 void arch_set_frame_offset(const arch_env_t *env, ir_node *irn, int offset)
113 {
114         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
115         ops->impl->set_frame_offset(ops, irn, offset);
116 }
117
118 ir_entity *arch_get_frame_entity(const arch_env_t *env, const ir_node *irn)
119 {
120         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
121         return ops->impl->get_frame_entity(ops, irn);
122 }
123
124 void arch_set_frame_entity(const arch_env_t *env, ir_node *irn, ir_entity *ent)
125 {
126         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
127         ops->impl->set_frame_entity(ops, irn, ent);
128 }
129
130 int arch_get_sp_bias(const arch_env_t *env, ir_node *irn)
131 {
132         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
133         return ops->impl->get_sp_bias(ops, irn);
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)
137 {
138         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
139
140         if(ops->impl->get_inverse) {
141                 return ops->impl->get_inverse(ops, irn, i, inverse, obstack);
142         } else {
143                 return NULL;
144         }
145 }
146
147 int arch_possible_memory_operand(const arch_env_t *env, const ir_node *irn, unsigned int i) {
148         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
149
150         if(ops->impl->possible_memory_operand) {
151                 return ops->impl->possible_memory_operand(ops, irn, i);
152         } else {
153                 return 0;
154         }
155 }
156
157 void arch_perform_memory_operand(const arch_env_t *env, ir_node *irn, ir_node *spill, unsigned int i) {
158         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
159
160         if(ops->impl->perform_memory_operand) {
161                 ops->impl->perform_memory_operand(ops, irn, spill, i);
162         } else {
163                 return;
164         }
165 }
166
167 int arch_get_op_estimated_cost(const arch_env_t *env, const ir_node *irn)
168 {
169         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
170
171         if(ops->impl->get_op_estimated_cost) {
172                 return ops->impl->get_op_estimated_cost(ops, irn);
173         } else {
174                 return 1;
175         }
176 }
177
178 int arch_is_possible_memory_operand(const arch_env_t *env, const ir_node *irn, int i)
179 {
180         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
181
182         if(ops->impl->possible_memory_operand) {
183                 return ops->impl->possible_memory_operand(ops, irn, i);
184         } else {
185                 return 0;
186         }
187 }
188
189 int arch_get_allocatable_regs(const arch_env_t *env, const ir_node *irn, int pos, bitset_t *bs)
190 {
191         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
192         const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, irn, pos);
193
194         if(req->type == arch_register_req_type_none) {
195                 bitset_clear_all(bs);
196                 return 0;
197         }
198
199         if(arch_register_req_is(req, limited)) {
200                 rbitset_copy_to_bitset(req->limited, bs);
201                 return bitset_popcnt(bs);
202         }
203
204         arch_register_class_put(req->cls, bs);
205         return req->cls->n_regs;
206 }
207
208 void arch_put_non_ignore_regs(const arch_env_t *env,
209                               const arch_register_class_t *cls, bitset_t *bs)
210 {
211         unsigned i;
212         (void) env;
213
214         for(i = 0; i < cls->n_regs; ++i) {
215                 if(!arch_register_type_is(&cls->regs[i], ignore))
216                         bitset_set(bs, i);
217         }
218 }
219
220 int arch_count_non_ignore_regs(const arch_env_t *env,
221                                const arch_register_class_t *cls)
222 {
223         unsigned i;
224         int result = 0;
225         (void) env;
226
227         for(i = 0; i < cls->n_regs; ++i) {
228                 if(!arch_register_type_is(&cls->regs[i], ignore))
229                         result++;
230         }
231
232         return result;
233 }
234
235 int arch_is_register_operand(const arch_env_t *env,
236     const ir_node *irn, int pos)
237 {
238         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
239         const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, irn, pos);
240
241         return req != NULL;
242 }
243
244 int arch_reg_is_allocatable(const arch_env_t *env, const ir_node *irn,
245     int pos, const arch_register_t *reg)
246 {
247         const arch_register_req_t *req;
248
249         req = arch_get_register_req(env, irn, pos);
250
251         if(req->type == arch_register_req_type_none)
252                 return 0;
253
254         if(arch_register_req_is(req, limited)) {
255                 assert(arch_register_get_class(reg) == req->cls);
256                 return rbitset_is_set(req->limited, arch_register_get_index(reg));
257         }
258
259         return req->cls == reg->reg_class;
260 }
261
262 const arch_register_class_t *
263 arch_get_irn_reg_class(const arch_env_t *env, const ir_node *irn, int pos)
264 {
265         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
266         const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, irn, pos);
267
268         assert(req->type != arch_register_req_type_none || req->cls == NULL);
269
270         return req->cls;
271 }
272
273 extern const arch_register_t *
274 arch_get_irn_register(const arch_env_t *env, const ir_node *irn)
275 {
276         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
277         return ops->impl->get_irn_reg(ops, irn);
278 }
279
280 extern void arch_set_irn_register(const arch_env_t *env,
281     ir_node *irn, const arch_register_t *reg)
282 {
283         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
284         ops->impl->set_irn_reg(ops, irn, reg);
285 }
286
287 extern arch_irn_class_t arch_irn_classify(const arch_env_t *env, const ir_node *irn)
288 {
289         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
290         return ops->impl->classify(ops, irn);
291 }
292
293 extern arch_irn_flags_t arch_irn_get_flags(const arch_env_t *env, const ir_node *irn)
294 {
295         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
296         return ops->impl->get_flags(ops, irn);
297 }
298
299 extern const char *arch_irn_flag_str(arch_irn_flags_t fl)
300 {
301         switch(fl) {
302 #define XXX(x) case arch_irn_flags_ ## x: return #x;
303                 XXX(dont_spill);
304                 XXX(ignore);
305                 XXX(rematerializable);
306                 XXX(modify_sp);
307                 XXX(modify_flags);
308                 XXX(none);
309 #undef XXX
310         }
311         return "n/a";
312 }
313
314 extern char *arch_register_req_format(char *buf, size_t len,
315                                       const arch_register_req_t *req,
316                                       const ir_node *node)
317 {
318         char tmp[128];
319         snprintf(buf, len, "class: %s", req->cls->name);
320
321         if(arch_register_req_is(req, limited)) {
322                 unsigned n_regs = req->cls->n_regs;
323                 unsigned i;
324
325                 strncat(buf, " limited:", len);
326                 for(i = 0; i < n_regs; ++i) {
327                         if(rbitset_is_set(req->limited, i)) {
328                                 const arch_register_t *reg = &req->cls->regs[i];
329                                 strncat(buf, " ", len);
330                                 strncat(buf, reg->name, len);
331                         }
332                 }
333         }
334
335         if(arch_register_req_is(req, should_be_same)) {
336                 const unsigned other = req->other_same;
337                 int i;
338
339                 ir_snprintf(tmp, sizeof(tmp), " same to:");
340                 for (i = 0; 1U << i <= other; ++i) {
341                         if (other & (1U << i)) {
342                                 ir_snprintf(tmp, sizeof(tmp), " %+F", get_irn_n(skip_Proj_const(node), i));
343                                 strncat(buf, tmp, len);
344                         }
345                 }
346         }
347
348         if(arch_register_req_is(req, should_be_different)) {
349                 const unsigned other = req->other_different;
350                 int i;
351
352                 ir_snprintf(tmp, sizeof(tmp), " different from:");
353                 for (i = 0; 1U << i <= other; ++i) {
354                         if (other & (1U << i)) {
355                                 ir_snprintf(tmp, sizeof(tmp), " %+F", get_irn_n(skip_Proj_const(node), i));
356                                 strncat(buf, tmp, len);
357                         }
358                 }
359         }
360
361         return buf;
362 }
363
364 static const arch_register_req_t no_requirement = {
365         arch_register_req_type_none,
366         NULL,
367         NULL,
368         0,
369         0
370 };
371 const arch_register_req_t *arch_no_register_req = &no_requirement;