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