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