fixed handling of other_same/other_different constraint handling
[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 "entity.h"
40 #include "raw_bitset.h"
41
42 #include "irprintf.h"
43
44 /* Initialize the architecture environment struct. */
45 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)
46 {
47         memset(env, 0, sizeof(*env));
48         env->isa                  = isa_if->init(file_handle);
49         env->isa->main_env        = main_env;
50         return env;
51 }
52
53 arch_env_t *arch_env_push_irn_handler(arch_env_t *env,
54                                       const arch_irn_handler_t *handler)
55 {
56         assert(env->handlers_tos < ARCH_MAX_HANDLERS);
57         env->handlers[env->handlers_tos++] = handler;
58         return env;
59 }
60
61 const arch_irn_handler_t *arch_env_pop_irn_handler(arch_env_t *env)
62 {
63         assert(env->handlers_tos > 0 && env->handlers_tos <= ARCH_MAX_HANDLERS);
64         return env->handlers[--env->handlers_tos];
65 }
66
67 static const arch_irn_ops_t *fallback_irn_ops = NULL;
68
69 int arch_register_class_put(const arch_register_class_t *cls, bitset_t *bs)
70 {
71         if(bs) {
72                 int i, n;
73                 for(i = 0, n = cls->n_regs; i < n; ++i)
74                         bitset_set(bs, i);
75         }
76
77         return cls->n_regs;
78 }
79
80 /**
81  * Get the isa responsible for a node.
82  * @param env The arch environment with the isa stack.
83  * @param irn The node to get the responsible isa for.
84  * @return The irn operations given by the responsible isa.
85  */
86 static INLINE const arch_irn_ops_t *
87 get_irn_ops(const arch_env_t *env, const ir_node *irn)
88 {
89         int i;
90
91         for(i = env->handlers_tos - 1; i >= 0; --i) {
92                 const arch_irn_handler_t *handler = env->handlers[i];
93                 const arch_irn_ops_t *ops = handler->get_irn_ops(handler, irn);
94
95                 if(ops)
96                         return ops;
97         }
98
99         return fallback_irn_ops;
100 }
101
102 const arch_irn_ops_t *arch_get_irn_ops(const arch_env_t *env, const ir_node *irn) {
103         return get_irn_ops(env, irn);
104 }
105
106 const arch_register_req_t *arch_get_register_req(const arch_env_t *env,
107                                                  const ir_node *irn, int pos)
108 {
109         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
110         return ops->impl->get_irn_reg_req(ops, irn, pos);
111 }
112
113 void arch_set_frame_offset(const arch_env_t *env, ir_node *irn, int offset)
114 {
115         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
116         ops->impl->set_frame_offset(ops, irn, offset);
117 }
118
119 ir_entity *arch_get_frame_entity(const arch_env_t *env, const ir_node *irn)
120 {
121         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
122         return ops->impl->get_frame_entity(ops, irn);
123 }
124
125 void arch_set_frame_entity(const arch_env_t *env, ir_node *irn, ir_entity *ent)
126 {
127         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
128         ops->impl->set_frame_entity(ops, irn, ent);
129 }
130
131 int arch_get_sp_bias(const arch_env_t *env, ir_node *irn)
132 {
133         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
134         return ops->impl->get_sp_bias(ops, irn);
135 }
136
137 arch_inverse_t *arch_get_inverse(const arch_env_t *env, const ir_node *irn, int i, arch_inverse_t *inverse, struct obstack *obstack)
138 {
139         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
140
141         if(ops->impl->get_inverse) {
142                 return ops->impl->get_inverse(ops, irn, i, inverse, obstack);
143         } else {
144                 return NULL;
145         }
146 }
147
148 int arch_possible_memory_operand(const arch_env_t *env, const ir_node *irn, unsigned int i) {
149         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
150
151         if(ops->impl->possible_memory_operand) {
152                 return ops->impl->possible_memory_operand(ops, irn, i);
153         } else {
154                 return 0;
155         }
156 }
157
158 void arch_perform_memory_operand(const arch_env_t *env, ir_node *irn, ir_node *spill, unsigned int i) {
159         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
160
161         if(ops->impl->perform_memory_operand) {
162                 ops->impl->perform_memory_operand(ops, irn, spill, i);
163         } else {
164                 return;
165         }
166 }
167
168 int arch_get_op_estimated_cost(const arch_env_t *env, const ir_node *irn)
169 {
170         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
171
172         if(ops->impl->get_op_estimated_cost) {
173                 return ops->impl->get_op_estimated_cost(ops, irn);
174         } else {
175                 return 1;
176         }
177 }
178
179 int arch_is_possible_memory_operand(const arch_env_t *env, const ir_node *irn, int i)
180 {
181         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
182
183         if(ops->impl->possible_memory_operand) {
184                 return ops->impl->possible_memory_operand(ops, irn, i);
185         } else {
186                 return 0;
187         }
188 }
189
190 int arch_get_allocatable_regs(const arch_env_t *env, const ir_node *irn, int pos, bitset_t *bs)
191 {
192         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
193         const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, irn, pos);
194
195         if(req->type == arch_register_req_type_none) {
196                 bitset_clear_all(bs);
197                 return 0;
198         }
199
200         if(arch_register_req_is(req, limited)) {
201                 rbitset_copy_to_bitset(req->limited, bs);
202                 return bitset_popcnt(bs);
203         }
204
205         arch_register_class_put(req->cls, bs);
206         return req->cls->n_regs;
207 }
208
209 void arch_put_non_ignore_regs(const arch_env_t *env, const arch_register_class_t *cls, bitset_t *bs)
210 {
211         int i;
212
213         for(i = 0; i < cls->n_regs; ++i) {
214                 if(!arch_register_type_is(&cls->regs[i], ignore))
215                         bitset_set(bs, i);
216         }
217 }
218
219 int arch_count_non_ignore_regs(const arch_env_t *env, const arch_register_class_t *cls)
220 {
221         int i;
222         int result = 0;
223
224         for(i = 0; i < cls->n_regs; ++i) {
225                 if(!arch_register_type_is(&cls->regs[i], ignore))
226                         result++;
227         }
228
229         return result;
230 }
231
232 int arch_is_register_operand(const arch_env_t *env,
233     const ir_node *irn, int pos)
234 {
235         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
236         const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, irn, pos);
237
238         return req != NULL;
239 }
240
241 int arch_reg_is_allocatable(const arch_env_t *env, const ir_node *irn,
242     int pos, const arch_register_t *reg)
243 {
244         const arch_register_req_t *req;
245
246         req = arch_get_register_req(env, irn, pos);
247
248         if(req->type == arch_register_req_type_none)
249                 return 0;
250
251         if(arch_register_req_is(req, limited)) {
252                 assert(arch_register_get_class(reg) == req->cls);
253                 return rbitset_is_set(req->limited, arch_register_get_index(reg));
254         }
255
256         return req->cls == reg->reg_class;
257 }
258
259 const arch_register_class_t *
260 arch_get_irn_reg_class(const arch_env_t *env, const ir_node *irn, int pos)
261 {
262         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
263         const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, irn, pos);
264
265         assert(req->type != arch_register_req_type_none || req->cls == NULL);
266
267         return req->cls;
268 }
269
270 extern const arch_register_t *
271 arch_get_irn_register(const arch_env_t *env, const ir_node *irn)
272 {
273         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
274         return ops->impl->get_irn_reg(ops, irn);
275 }
276
277 extern void arch_set_irn_register(const arch_env_t *env,
278     ir_node *irn, const arch_register_t *reg)
279 {
280         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
281         ops->impl->set_irn_reg(ops, irn, reg);
282 }
283
284 extern arch_irn_class_t arch_irn_classify(const arch_env_t *env, const ir_node *irn)
285 {
286         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
287         return ops->impl->classify(ops, irn);
288 }
289
290 extern arch_irn_flags_t arch_irn_get_flags(const arch_env_t *env, const ir_node *irn)
291 {
292         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
293         return ops->impl->get_flags(ops, irn);
294 }
295
296 extern const char *arch_irn_flag_str(arch_irn_flags_t fl)
297 {
298         switch(fl) {
299 #define XXX(x) case arch_irn_flags_ ## x: return #x;
300                 XXX(dont_spill);
301                 XXX(ignore);
302                 XXX(rematerializable);
303                 XXX(modify_sp);
304                 XXX(none);
305 #undef XXX
306         }
307         return "n/a";
308 }
309
310 extern char *arch_register_req_format(char *buf, size_t len,
311                                       const arch_register_req_t *req,
312                                       const ir_node *node)
313 {
314         char tmp[128];
315         snprintf(buf, len, "class: %s", req->cls->name);
316
317         if(arch_register_req_is(req, limited)) {
318                 unsigned n_regs = req->cls->n_regs;
319                 unsigned i;
320
321                 strncat(buf, " limited:", len);
322                 for(i = 0; i < n_regs; ++i) {
323                         if(rbitset_is_set(req->limited, i)) {
324                                 const arch_register_t *reg = &req->cls->regs[i];
325                                 strncat(buf, " ", len);
326                                 strncat(buf, reg->name, len);
327                         }
328                 }
329         }
330
331         if(arch_register_req_is(req, should_be_same)) {
332                 const ir_node *same = get_irn_n(skip_Proj_const(node), req->other_same);
333                 ir_snprintf(tmp, sizeof(tmp), " same to: %+F", same);
334                 strncat(buf, tmp, len);
335         }
336
337         if(arch_register_req_is(req, should_be_different)) {
338                 const ir_node *different = get_irn_n(skip_Proj_const(node), req->other_different);
339                 ir_snprintf(tmp, sizeof(tmp), " different to: %+F", different);
340                 strncat(buf, tmp, len);
341         }
342
343         return buf;
344 }
345
346 static const arch_register_req_t no_requirement = {
347         arch_register_req_type_none,
348         NULL,
349         NULL,
350         -1,
351         -1
352 };
353 const arch_register_req_t *arch_no_register_req = &no_requirement;