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