fixed some bugs
[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_push_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 const arch_irn_handler_t *arch_env_pop_irn_handler(arch_env_t *env)
46 {
47   assert(env->handlers_tos > 0 && env->handlers_tos <= ARCH_MAX_HANDLERS);
48   return env->handlers[--env->handlers_tos];
49 }
50
51 static const arch_irn_ops_t *fallback_irn_ops = NULL;
52
53 int arch_register_class_put(const arch_register_class_t *cls, bitset_t *bs)
54 {
55   if(bs) {
56     int i, n;
57     for(i = 0, n = cls->n_regs; i < n; ++i)
58       bitset_set(bs, i);
59   }
60
61   return cls->n_regs;
62 }
63
64 /**
65  * Get the isa responsible for a node.
66  * @param env The arch environment with the isa stack.
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 *
71 get_irn_ops(const arch_env_t *env, const ir_node *irn)
72 {
73   int i;
74
75   for(i = env->handlers_tos - 1; i >= 0; --i) {
76     const arch_irn_handler_t *handler = env->handlers[i];
77     const arch_irn_ops_t *ops = handler->get_irn_ops(handler, irn);
78
79     if(ops)
80       return ops;
81   }
82
83   return fallback_irn_ops;
84 }
85
86 const arch_register_req_t *arch_get_register_req(const arch_env_t *env,
87     arch_register_req_t *req, const ir_node *irn, int pos)
88 {
89   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
90   req->type = arch_register_req_type_none;
91   return ops->impl->get_irn_reg_req(ops, req, irn, pos);
92 }
93
94 void arch_set_frame_offset(const arch_env_t *env, ir_node *irn, int offset)
95 {
96   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
97   ops->impl->set_frame_offset(ops, irn, offset);
98 }
99
100 entity *arch_get_frame_entity(const arch_env_t *env, ir_node *irn)
101 {
102   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
103   return ops->impl->get_frame_entity(ops, irn);
104 }
105
106
107 int arch_get_allocatable_regs(const arch_env_t *env, const ir_node *irn, int pos, bitset_t *bs)
108 {
109   arch_register_req_t local_req;
110   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
111   const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, &local_req, irn, pos);
112
113   if(req->type == arch_register_req_type_none) {
114           bitset_clear_all(bs);
115           return 0;
116   }
117
118   if(arch_register_req_is(req, limited)) {
119           req->limited(req->limited_env, bs);
120           return bitset_popcnt(bs);
121   }
122
123   arch_register_class_put(req->cls, bs);
124   return req->cls->n_regs;
125 }
126
127 void arch_put_non_ignore_regs(const arch_env_t *env, const arch_register_class_t *cls, bitset_t *bs)
128 {
129         int i;
130
131         for(i = 0; i < cls->n_regs; ++i) {
132                 if(!arch_register_type_is(&cls->regs[i], ignore))
133                         bitset_set(bs, i);
134         }
135 }
136
137 int arch_is_register_operand(const arch_env_t *env,
138     const ir_node *irn, int pos)
139 {
140         arch_register_req_t local_req;
141         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
142         const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, &local_req, irn, pos);
143         return req != NULL;
144 }
145
146 int arch_reg_is_allocatable(const arch_env_t *env, const ir_node *irn,
147     int pos, const arch_register_t *reg)
148 {
149         arch_register_req_t req;
150
151         arch_get_register_req(env, &req, irn, pos);
152
153         if(req.type == arch_register_req_type_none)
154                 return 0;
155
156         if(arch_register_req_is(&req, limited)) {
157                 bitset_t *bs = bitset_alloca(req.cls->n_regs);
158                 req.limited(req.limited_env, bs);
159                 return bitset_is_set(bs, arch_register_get_index(reg));
160         }
161
162         return req.cls == reg->reg_class;
163 }
164
165 const arch_register_class_t *
166 arch_get_irn_reg_class(const arch_env_t *env, const ir_node *irn, int pos)
167 {
168   arch_register_req_t local_req;
169   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
170   const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, &local_req, irn, pos);
171   return req ? req->cls : NULL;
172 }
173
174 extern const arch_register_t *
175 arch_get_irn_register(const arch_env_t *env, const ir_node *irn)
176 {
177   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
178   return ops->impl->get_irn_reg(ops, irn);
179 }
180
181 extern void arch_set_irn_register(const arch_env_t *env,
182     ir_node *irn, const arch_register_t *reg)
183 {
184   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
185   ops->impl->set_irn_reg(ops, irn, reg);
186 }
187
188 extern arch_irn_class_t arch_irn_classify(const arch_env_t *env, const ir_node *irn)
189 {
190   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
191   return ops->impl->classify(ops, irn);
192 }
193
194 extern arch_irn_flags_t arch_irn_get_flags(const arch_env_t *env, const ir_node *irn)
195 {
196   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
197   return ops->impl->get_flags(ops, irn);
198 }
199
200 extern const char *arch_irn_flag_str(arch_irn_flags_t fl)
201 {
202         switch(fl) {
203 #define XXX(x) case arch_irn_flags_ ## x: return #x;
204                 XXX(dont_spill);
205                 XXX(ignore);
206                 XXX(rematerializable);
207                 XXX(none);
208 #undef XXX
209         }
210         return "n/a";
211 }
212
213 extern char *arch_register_req_format(char *buf, size_t len, const arch_register_req_t *req)
214 {
215         char tmp[128];
216         snprintf(buf, len, "class: %s", req->cls->name);
217
218         if(arch_register_req_is(req, limited)) {
219                 bitset_pos_t elm;
220                 bitset_t *bs = bitset_alloca(req->cls->n_regs);
221                 req->limited(req->limited_env, bs);
222                 strncat(buf, " limited:", len);
223                 bitset_foreach(bs, elm) {
224                         strncat(buf, " ", len);
225                         strncat(buf, req->cls->regs[elm].name, len);
226                 }
227         }
228
229         if(arch_register_req_is(req, should_be_same)) {
230                 snprintf(tmp, sizeof(tmp), " same to: %+F", req->other_different);
231                 strncat(buf, tmp, len);
232         }
233
234         if(arch_register_req_is(req, should_be_different)) {
235                 snprintf(tmp, sizeof(tmp), " different to: %+F", req->other_different);
236                 strncat(buf, tmp, len);
237         }
238
239         return buf;
240 }