Adapted to changes in ABI handling
[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 entity *arch_get_frame_entity(const arch_env_t *env, ir_node *irn)
95 {
96   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
97   return ops->impl->get_frame_entity(ops, irn);
98 }
99
100
101 int arch_get_allocatable_regs(const arch_env_t *env, const ir_node *irn, int pos, bitset_t *bs)
102 {
103   arch_register_req_t local_req;
104   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
105   const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, &local_req, irn, pos);
106
107   if(req->type == arch_register_req_type_none) {
108           bitset_clear_all(bs);
109           return 0;
110   }
111
112   if(arch_register_req_is(req, limited)) {
113           req->limited(req->limited_env, bs);
114           return bitset_popcnt(bs);
115   }
116
117   arch_register_class_put(req->cls, bs);
118   return req->cls->n_regs;
119 }
120
121 void arch_put_non_ignore_regs(const arch_env_t *env, const arch_register_class_t *cls, bitset_t *bs)
122 {
123         int i;
124
125         for(i = 0; i < cls->n_regs; ++i) {
126                 if(!arch_register_type_is(&cls->regs[i], ignore))
127                         bitset_set(bs, i);
128         }
129 }
130
131 int arch_is_register_operand(const arch_env_t *env,
132     const ir_node *irn, int pos)
133 {
134         arch_register_req_t local_req;
135         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
136         const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, &local_req, irn, pos);
137         return req != NULL;
138 }
139
140 int arch_reg_is_allocatable(const arch_env_t *env, const ir_node *irn,
141     int pos, const arch_register_t *reg)
142 {
143         arch_register_req_t req;
144
145         arch_get_register_req(env, &req, irn, pos);
146
147         if(req.type == arch_register_req_type_none)
148                 return 0;
149
150         if(arch_register_req_is(&req, limited)) {
151                 bitset_t *bs = bitset_alloca(req.cls->n_regs);
152                 req.limited(req.limited_env, bs);
153                 return bitset_is_set(bs, arch_register_get_index(reg));
154         }
155
156         return req.cls == reg->reg_class;
157 }
158
159 const arch_register_class_t *
160 arch_get_irn_reg_class(const arch_env_t *env, const ir_node *irn, int pos)
161 {
162   arch_register_req_t local_req;
163   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
164   const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, &local_req, irn, pos);
165   return req ? req->cls : NULL;
166 }
167
168 extern const arch_register_t *
169 arch_get_irn_register(const arch_env_t *env, const ir_node *irn)
170 {
171   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
172   return ops->impl->get_irn_reg(ops, irn);
173 }
174
175 extern void arch_set_irn_register(const arch_env_t *env,
176     ir_node *irn, const arch_register_t *reg)
177 {
178   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
179   ops->impl->set_irn_reg(ops, irn, reg);
180 }
181
182 extern arch_irn_class_t arch_irn_classify(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->classify(ops, irn);
186 }
187
188 extern arch_irn_flags_t arch_irn_get_flags(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->get_flags(ops, irn);
192 }
193
194 extern const char *arch_irn_flag_str(arch_irn_flags_t fl)
195 {
196         switch(fl) {
197 #define XXX(x) case arch_irn_flags_ ## x: return #x;
198                 XXX(dont_spill);
199                 XXX(ignore);
200                 XXX(rematerializable);
201                 XXX(none);
202 #undef XXX
203         }
204         return "n/a";
205 }
206
207 extern char *arch_register_req_format(char *buf, size_t len, const arch_register_req_t *req)
208 {
209         char tmp[128];
210         snprintf(buf, len, "class: %s", req->cls->name);
211
212         if(arch_register_req_is(req, limited)) {
213                 bitset_pos_t elm;
214                 bitset_t *bs = bitset_alloca(req->cls->n_regs);
215                 req->limited(req->limited_env, bs);
216                 strncat(buf, " limited:", len);
217                 bitset_foreach(bs, elm) {
218                         strncat(buf, " ", len);
219                         strncat(buf, req->cls->regs[elm].name, len);
220                 }
221         }
222
223         if(arch_register_req_is(req, should_be_same)) {
224                 snprintf(tmp, sizeof(tmp), " same to: %+F", req->other_different);
225                 strncat(buf, tmp, len);
226         }
227
228         if(arch_register_req_is(req, should_be_different)) {
229                 snprintf(tmp, sizeof(tmp), " different to: %+F", req->other_different);
230                 strncat(buf, tmp, len);
231         }
232
233         return buf;
234 }