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