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