Removed the arch_irn_handler_t. This was just an additional redirection without
[libfirm] / ir / be / bearch.c
1 /*
2  * Copyright (C) 1995-2008 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                                       arch_get_irn_ops_t *handler)
54 {
55         assert(env->handlers_tos < ARCH_MAX_HANDLERS);
56         env->handlers[env->handlers_tos++] = handler;
57         return env;
58 }
59
60 arch_get_irn_ops_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 #if 1
89         int i;
90
91         for(i = env->handlers_tos - 1; i >= 0; --i) {
92                 arch_get_irn_ops_t *get_irn_ops = env->handlers[i];
93                 const arch_irn_ops_t *ops = get_irn_ops(irn);
94
95                 if(ops)
96                         return ops;
97         }
98 #else
99         if (is_Phi(irn) && !mode_is_datab(get_irn_mode(irn))) {
100                 const phi_handler_t *h;
101                 return &h->irn_ops;
102         }
103         if (is_Proj(irn)) {
104                 irn = get_Proj_pred(irn);
105                 if (is_Proj(irn)) {
106                         assert(get_irn_mode(irn) == mode_T);
107                         irn = get_Proj_pred(irn);
108                 }
109         }
110         if (is_be_node(irn))
111                 return &be_node_irn_ops;
112
113 #endif
114         return fallback_irn_ops;
115 }
116
117 const arch_irn_ops_t *arch_get_irn_ops(const arch_env_t *env, const ir_node *irn) {
118         return get_irn_ops(env, irn);
119 }
120
121 const arch_register_req_t *arch_get_register_req(const arch_env_t *env,
122                                                  const ir_node *irn, int pos)
123 {
124         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
125         return ops->impl->get_irn_reg_req(ops, irn, pos);
126 }
127
128 void arch_set_frame_offset(const arch_env_t *env, ir_node *irn, int offset)
129 {
130         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
131         ops->impl->set_frame_offset(ops, irn, offset);
132 }
133
134 ir_entity *arch_get_frame_entity(const arch_env_t *env, const ir_node *irn)
135 {
136         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
137         return ops->impl->get_frame_entity(ops, irn);
138 }
139
140 void arch_set_frame_entity(const arch_env_t *env, ir_node *irn, ir_entity *ent)
141 {
142         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
143         ops->impl->set_frame_entity(ops, irn, ent);
144 }
145
146 int arch_get_sp_bias(const arch_env_t *env, ir_node *irn)
147 {
148         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
149         return ops->impl->get_sp_bias(ops, irn);
150 }
151
152 arch_inverse_t *arch_get_inverse(const arch_env_t *env, const ir_node *irn, int i, arch_inverse_t *inverse, struct obstack *obstack)
153 {
154         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
155
156         if(ops->impl->get_inverse) {
157                 return ops->impl->get_inverse(ops, irn, i, inverse, obstack);
158         } else {
159                 return NULL;
160         }
161 }
162
163 int arch_possible_memory_operand(const arch_env_t *env, const ir_node *irn, unsigned int i) {
164         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
165
166         if(ops->impl->possible_memory_operand) {
167                 return ops->impl->possible_memory_operand(ops, irn, i);
168         } else {
169                 return 0;
170         }
171 }
172
173 void arch_perform_memory_operand(const arch_env_t *env, ir_node *irn, ir_node *spill, unsigned int i) {
174         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
175
176         if(ops->impl->perform_memory_operand) {
177                 ops->impl->perform_memory_operand(ops, irn, spill, i);
178         } else {
179                 return;
180         }
181 }
182
183 int arch_get_op_estimated_cost(const arch_env_t *env, const ir_node *irn)
184 {
185         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
186
187         if(ops->impl->get_op_estimated_cost) {
188                 return ops->impl->get_op_estimated_cost(ops, irn);
189         } else {
190                 return 1;
191         }
192 }
193
194 int arch_is_possible_memory_operand(const arch_env_t *env, const ir_node *irn, int i)
195 {
196         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
197
198         if(ops->impl->possible_memory_operand) {
199                 return ops->impl->possible_memory_operand(ops, irn, i);
200         } else {
201                 return 0;
202         }
203 }
204
205 int arch_get_allocatable_regs(const arch_env_t *env, const ir_node *irn, int pos, bitset_t *bs)
206 {
207         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
208         const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, irn, pos);
209
210         if(req->type == arch_register_req_type_none) {
211                 bitset_clear_all(bs);
212                 return 0;
213         }
214
215         if(arch_register_req_is(req, limited)) {
216                 rbitset_copy_to_bitset(req->limited, bs);
217                 return bitset_popcnt(bs);
218         }
219
220         arch_register_class_put(req->cls, bs);
221         return req->cls->n_regs;
222 }
223
224 void arch_put_non_ignore_regs(const arch_env_t *env,
225                               const arch_register_class_t *cls, bitset_t *bs)
226 {
227         unsigned i;
228         (void) env;
229
230         for(i = 0; i < cls->n_regs; ++i) {
231                 if(!arch_register_type_is(&cls->regs[i], ignore))
232                         bitset_set(bs, i);
233         }
234 }
235
236 int arch_count_non_ignore_regs(const arch_env_t *env,
237                                const arch_register_class_t *cls)
238 {
239         unsigned i;
240         int result = 0;
241         (void) env;
242
243         for(i = 0; i < cls->n_regs; ++i) {
244                 if(!arch_register_type_is(&cls->regs[i], ignore))
245                         result++;
246         }
247
248         return result;
249 }
250
251 int arch_is_register_operand(const arch_env_t *env,
252     const ir_node *irn, int pos)
253 {
254         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
255         const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, irn, pos);
256
257         return req != NULL;
258 }
259
260 int arch_reg_is_allocatable(const arch_env_t *env, const ir_node *irn,
261     int pos, const arch_register_t *reg)
262 {
263         const arch_register_req_t *req;
264
265         req = arch_get_register_req(env, irn, pos);
266
267         if(req->type == arch_register_req_type_none)
268                 return 0;
269
270         if(arch_register_req_is(req, limited)) {
271                 assert(arch_register_get_class(reg) == req->cls);
272                 return rbitset_is_set(req->limited, arch_register_get_index(reg));
273         }
274
275         return req->cls == reg->reg_class;
276 }
277
278 const arch_register_class_t *
279 arch_get_irn_reg_class(const arch_env_t *env, const ir_node *irn, int pos)
280 {
281         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
282         const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, irn, pos);
283
284         assert(req->type != arch_register_req_type_none || req->cls == NULL);
285
286         return req->cls;
287 }
288
289 extern const arch_register_t *
290 arch_get_irn_register(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_irn_reg(ops, irn);
294 }
295
296 extern void arch_set_irn_register(const arch_env_t *env,
297     ir_node *irn, const arch_register_t *reg)
298 {
299         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
300         ops->impl->set_irn_reg(ops, irn, reg);
301 }
302
303 extern arch_irn_class_t arch_irn_classify(const arch_env_t *env, const ir_node *irn)
304 {
305         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
306         return ops->impl->classify(ops, irn);
307 }
308
309 extern arch_irn_flags_t arch_irn_get_flags(const arch_env_t *env, const ir_node *irn)
310 {
311         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
312         return ops->impl->get_flags(ops, irn);
313 }
314
315 extern const char *arch_irn_flag_str(arch_irn_flags_t fl)
316 {
317         switch(fl) {
318 #define XXX(x) case arch_irn_flags_ ## x: return #x;
319                 XXX(dont_spill);
320                 XXX(ignore);
321                 XXX(rematerializable);
322                 XXX(modify_sp);
323                 XXX(modify_flags);
324                 XXX(none);
325 #undef XXX
326         }
327         return "n/a";
328 }
329
330 extern char *arch_register_req_format(char *buf, size_t len,
331                                       const arch_register_req_t *req,
332                                       const ir_node *node)
333 {
334         char tmp[128];
335         snprintf(buf, len, "class: %s", req->cls->name);
336
337         if(arch_register_req_is(req, limited)) {
338                 unsigned n_regs = req->cls->n_regs;
339                 unsigned i;
340
341                 strncat(buf, " limited:", len);
342                 for(i = 0; i < n_regs; ++i) {
343                         if(rbitset_is_set(req->limited, i)) {
344                                 const arch_register_t *reg = &req->cls->regs[i];
345                                 strncat(buf, " ", len);
346                                 strncat(buf, reg->name, len);
347                         }
348                 }
349         }
350
351         if(arch_register_req_is(req, should_be_same)) {
352                 const unsigned other = req->other_same;
353                 int i;
354
355                 ir_snprintf(tmp, sizeof(tmp), " same to:");
356                 for (i = 0; 1U << i <= other; ++i) {
357                         if (other & (1U << i)) {
358                                 ir_snprintf(tmp, sizeof(tmp), " %+F", get_irn_n(skip_Proj_const(node), i));
359                                 strncat(buf, tmp, len);
360                         }
361                 }
362         }
363
364         if(arch_register_req_is(req, should_be_different)) {
365                 const unsigned other = req->other_different;
366                 int i;
367
368                 ir_snprintf(tmp, sizeof(tmp), " different from:");
369                 for (i = 0; 1U << i <= other; ++i) {
370                         if (other & (1U << i)) {
371                                 ir_snprintf(tmp, sizeof(tmp), " %+F", get_irn_n(skip_Proj_const(node), i));
372                                 strncat(buf, tmp, len);
373                         }
374                 }
375         }
376
377         return buf;
378 }
379
380 static const arch_register_req_t no_requirement = {
381         arch_register_req_type_none,
382         NULL,
383         NULL,
384         0,
385         0
386 };
387 const arch_register_req_t *arch_no_register_req = &no_requirement;