Bugfixes
[libfirm] / ir / be / bearch.c
1 /**
2  * Processor architecture specification.
3  * @author Sebastian Hack
4  * @date 11.2.2005
5  *
6  * $Id$
7  */
8
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif
12
13 #ifdef HAVE_ALLOCA_H
14 #include <alloca.h>
15 #endif
16
17 #ifdef HAVE_MALLOC_H
18 #include <malloc.h>
19 #endif
20
21 #include <string.h>
22
23 #include "bearch.h"
24 #include "ircons_t.h"
25
26 #include "bitset.h"
27 #include "pset.h"
28 #include "entity.h"
29
30 arch_env_t *arch_env_init(arch_env_t *env, const arch_isa_if_t *isa_if)
31 {
32   memset(env, 0, sizeof(*env));
33   env->isa = isa_if->init();
34   return env;
35 }
36
37 arch_env_t *arch_env_add_irn_handler(arch_env_t *env,
38     const arch_irn_handler_t *handler)
39 {
40   assert(env->handlers_tos <= ARCH_MAX_HANDLERS);
41   env->handlers[env->handlers_tos++] = handler;
42   return env;
43 }
44
45 static const arch_irn_ops_t *fallback_irn_ops = NULL;
46
47 int arch_register_class_put(const arch_register_class_t *cls, bitset_t *bs)
48 {
49   if(bs) {
50     int i, n;
51     for(i = 0, n = cls->n_regs; i < n; ++i)
52       bitset_set(bs, i);
53   }
54
55   return cls->n_regs;
56 }
57
58 /**
59  * Get the isa responsible for a node.
60  * @param env The arch environment with the isa stack.
61  * @param irn The node to get the responsible isa for.
62  * @return The irn operations given by the responsible isa.
63  */
64 static INLINE const arch_irn_ops_t *
65 get_irn_ops(const arch_env_t *env, const ir_node *irn)
66 {
67   int i;
68
69   for(i = env->handlers_tos - 1; i >= 0; --i) {
70     const arch_irn_handler_t *handler = env->handlers[i];
71     const arch_irn_ops_t *ops = handler->get_irn_ops(handler, irn);
72
73     if(ops)
74       return ops;
75   }
76
77   return fallback_irn_ops;
78 }
79
80 const arch_register_req_t *arch_get_register_req(const arch_env_t *env,
81     arch_register_req_t *req, const ir_node *irn, int pos)
82 {
83   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
84   req->type = arch_register_req_type_none;
85   return ops->impl->get_irn_reg_req(ops, req, irn, pos);
86 }
87
88 void arch_set_stack_bias(const arch_env_t *env, ir_node *irn, int bias)
89 {
90   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
91   ops->impl->set_stack_bias(ops, irn, bias);
92 }
93
94
95 int arch_get_allocatable_regs(const arch_env_t *env, const ir_node *irn, int pos, bitset_t *bs)
96 {
97   arch_register_req_t local_req;
98   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
99   const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, &local_req, irn, pos);
100
101   if(req->type == arch_register_req_type_none) {
102           bitset_clear_all(bs);
103           return 0;
104   }
105
106   if(arch_register_req_is(req, limited)) {
107           req->limited(req->limited_env, bs);
108           return bitset_popcnt(bs);
109   }
110
111   arch_register_class_put(req->cls, bs);
112   return req->cls->n_regs;
113 }
114
115 void arch_put_non_ignore_regs(const arch_env_t *env, const arch_register_class_t *cls, bitset_t *bs)
116 {
117         int i;
118
119         for(i = 0; i < cls->n_regs; ++i) {
120                 if(!arch_register_type_is(&cls->regs[i], ignore))
121                         bitset_set(bs, i);
122         }
123 }
124
125 int arch_is_register_operand(const arch_env_t *env,
126     const ir_node *irn, int pos)
127 {
128         arch_register_req_t local_req;
129         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
130         const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, &local_req, irn, pos);
131         return req != NULL;
132 }
133
134 int arch_reg_is_allocatable(const arch_env_t *env, const ir_node *irn,
135     int pos, const arch_register_t *reg)
136 {
137         arch_register_req_t req;
138
139         arch_get_register_req(env, &req, irn, pos);
140
141         if(req.type == arch_register_req_type_none)
142                 return 0;
143
144         if(arch_register_req_is(&req, limited)) {
145                 bitset_t *bs = bitset_alloca(req.cls->n_regs);
146                 req.limited(req.limited_env, bs);
147                 return bitset_is_set(bs, arch_register_get_index(reg));
148         }
149
150         return req.cls == reg->reg_class;
151 }
152
153 const arch_register_class_t *
154 arch_get_irn_reg_class(const arch_env_t *env, const ir_node *irn, int pos)
155 {
156   arch_register_req_t local_req;
157   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
158   const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, &local_req, irn, pos);
159   return req ? req->cls : NULL;
160 }
161
162 extern const arch_register_t *
163 arch_get_irn_register(const arch_env_t *env, const ir_node *irn)
164 {
165   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
166   return ops->impl->get_irn_reg(ops, irn);
167 }
168
169 extern void arch_set_irn_register(const arch_env_t *env,
170     ir_node *irn, const arch_register_t *reg)
171 {
172   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
173   ops->impl->set_irn_reg(ops, irn, reg);
174 }
175
176 extern arch_irn_class_t arch_irn_classify(const arch_env_t *env, const ir_node *irn)
177 {
178   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
179   return ops->impl->classify(ops, irn);
180 }
181
182 extern arch_irn_flags_t arch_irn_get_flags(const arch_env_t *env, const ir_node *irn)
183 {
184   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
185   return ops->impl->get_flags(ops, irn);
186 }
187
188 extern const char *arch_irn_flag_str(arch_irn_flags_t fl)
189 {
190         switch(fl) {
191 #define XXX(x) case arch_irn_flags_ ## x: return #x;
192                 XXX(dont_spill);
193                 XXX(ignore);
194                 XXX(rematerializable);
195                 XXX(none);
196 #undef XXX
197         }
198         return "n/a";
199 }
200
201 extern char *arch_register_req_format(char *buf, size_t len, const arch_register_req_t *req)
202 {
203         char tmp[128];
204         snprintf(buf, len, "class: %s", req->cls->name);
205
206         if(arch_register_req_is(req, limited)) {
207                 bitset_pos_t elm;
208                 bitset_t *bs = bitset_alloca(req->cls->n_regs);
209                 req->limited(req->limited_env, bs);
210                 strncat(buf, " limited:", len);
211                 bitset_foreach(bs, elm) {
212                         strncat(buf, " ", len);
213                         strncat(buf, req->cls->regs[elm].name, len);
214                 }
215         }
216
217         if(arch_register_req_is(req, should_be_same)) {
218                 snprintf(tmp, sizeof(tmp), " same to: %+F", req->other_different);
219                 strncat(buf, tmp, len);
220         }
221
222         if(arch_register_req_is(req, should_be_different)) {
223                 snprintf(tmp, sizeof(tmp), " different to: %+F", req->other_different);
224                 strncat(buf, tmp, len);
225         }
226
227         return buf;
228 }