fixed the fix of the fix (that was fix)
[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, const arch_register_class_t *cls, bitset_t *bs)
209 {
210         int i;
211
212         for(i = 0; i < cls->n_regs; ++i) {
213                 if(!arch_register_type_is(&cls->regs[i], ignore))
214                         bitset_set(bs, i);
215         }
216 }
217
218 int arch_count_non_ignore_regs(const arch_env_t *env, const arch_register_class_t *cls)
219 {
220         int i;
221         int result = 0;
222
223         for(i = 0; i < cls->n_regs; ++i) {
224                 if(!arch_register_type_is(&cls->regs[i], ignore))
225                         result++;
226         }
227
228         return result;
229 }
230
231 int arch_is_register_operand(const arch_env_t *env,
232     const ir_node *irn, int pos)
233 {
234         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
235         const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, irn, pos);
236
237         return req != NULL;
238 }
239
240 int arch_reg_is_allocatable(const arch_env_t *env, const ir_node *irn,
241     int pos, const arch_register_t *reg)
242 {
243         const arch_register_req_t *req;
244
245         req = arch_get_register_req(env, irn, pos);
246
247         if(req->type == arch_register_req_type_none)
248                 return 0;
249
250         if(arch_register_req_is(req, limited)) {
251                 assert(arch_register_get_class(reg) == req->cls);
252                 return rbitset_is_set(req->limited, arch_register_get_index(reg));
253         }
254
255         return req->cls == reg->reg_class;
256 }
257
258 const arch_register_class_t *
259 arch_get_irn_reg_class(const arch_env_t *env, const ir_node *irn, int pos)
260 {
261         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
262         const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, irn, pos);
263
264         assert(req->type != arch_register_req_type_none || req->cls == NULL);
265
266         return req->cls;
267 }
268
269 extern const arch_register_t *
270 arch_get_irn_register(const arch_env_t *env, const ir_node *irn)
271 {
272         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
273         return ops->impl->get_irn_reg(ops, irn);
274 }
275
276 extern void arch_set_irn_register(const arch_env_t *env,
277     ir_node *irn, const arch_register_t *reg)
278 {
279         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
280         ops->impl->set_irn_reg(ops, irn, reg);
281 }
282
283 extern arch_irn_class_t arch_irn_classify(const arch_env_t *env, const ir_node *irn)
284 {
285         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
286         return ops->impl->classify(ops, irn);
287 }
288
289 extern arch_irn_flags_t arch_irn_get_flags(const arch_env_t *env, const ir_node *irn)
290 {
291         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
292         return ops->impl->get_flags(ops, irn);
293 }
294
295 extern const char *arch_irn_flag_str(arch_irn_flags_t fl)
296 {
297         switch(fl) {
298 #define XXX(x) case arch_irn_flags_ ## x: return #x;
299                 XXX(dont_spill);
300                 XXX(ignore);
301                 XXX(rematerializable);
302                 XXX(modify_sp);
303                 XXX(none);
304 #undef XXX
305         }
306         return "n/a";
307 }
308
309 extern char *arch_register_req_format(char *buf, size_t len,
310                                       const arch_register_req_t *req,
311                                       const ir_node *node)
312 {
313         char tmp[128];
314         snprintf(buf, len, "class: %s", req->cls->name);
315
316         if(arch_register_req_is(req, limited)) {
317                 unsigned n_regs = req->cls->n_regs;
318                 unsigned i;
319
320                 strncat(buf, " limited:", len);
321                 for(i = 0; i < n_regs; ++i) {
322                         if(rbitset_is_set(req->limited, i)) {
323                                 const arch_register_t *reg = &req->cls->regs[i];
324                                 strncat(buf, " ", len);
325                                 strncat(buf, reg->name, len);
326                         }
327                 }
328         }
329
330         if(arch_register_req_is(req, should_be_same)) {
331                 const ir_node *same = get_irn_n(skip_Proj_const(node), req->other_same);
332                 ir_snprintf(tmp, sizeof(tmp), " same to: %+F", same);
333                 strncat(buf, tmp, len);
334         }
335
336         if(arch_register_req_is(req, should_be_different)) {
337                 const ir_node *different = get_irn_n(skip_Proj_const(node), req->other_different);
338                 ir_snprintf(tmp, sizeof(tmp), " different to: %+F", different);
339                 strncat(buf, tmp, len);
340         }
341
342         return buf;
343 }
344
345 static const arch_register_req_t no_requirement = {
346         arch_register_req_type_none,
347         NULL,
348         NULL,
349         -1,
350         -1
351 };
352 const arch_register_req_t *arch_no_register_req = &no_requirement;