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