6d8f38f379e95fdd9a964f11fdf24fadc1be36b1
[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 #ifdef HAVE_MALLOC_H
17 #include <malloc.h>
18 #endif
19
20 #include <string.h>
21
22 #include "bearch.h"
23 #include "ircons_t.h"
24
25 #include "bitset.h"
26 #include "pset.h"
27 #include "entity.h"
28
29 arch_env_t *arch_env_init(arch_env_t *env, const arch_isa_if_t *isa_if, FILE *file_handle)
30 {
31   memset(env, 0, sizeof(*env));
32   env->isa = isa_if->init(file_handle);
33   return env;
34 }
35
36 arch_env_t *arch_env_add_irn_handler(arch_env_t *env,
37     const arch_irn_handler_t *handler)
38 {
39   assert(env->handlers_tos <= ARCH_MAX_HANDLERS);
40   env->handlers[env->handlers_tos++] = handler;
41   return env;
42 }
43
44 static const arch_irn_ops_t *fallback_irn_ops = NULL;
45
46 int arch_register_class_put(const arch_register_class_t *cls, bitset_t *bs)
47 {
48   if(bs) {
49     int i, n;
50     for(i = 0, n = cls->n_regs; i < n; ++i)
51       bitset_set(bs, i);
52   }
53
54   return cls->n_regs;
55 }
56
57 /**
58  * Get the isa responsible for a node.
59  * @param env The arch environment with the isa stack.
60  * @param irn The node to get the responsible isa for.
61  * @return The irn operations given by the responsible isa.
62  */
63 static INLINE const arch_irn_ops_t *
64 get_irn_ops(const arch_env_t *env, const ir_node *irn)
65 {
66   int i;
67
68   for(i = env->handlers_tos - 1; i >= 0; --i) {
69     const arch_irn_handler_t *handler = env->handlers[i];
70     const arch_irn_ops_t *ops = handler->get_irn_ops(handler, irn);
71
72     if(ops)
73       return ops;
74   }
75
76   return fallback_irn_ops;
77 }
78
79 const arch_register_req_t *arch_get_register_req(const arch_env_t *env,
80     arch_register_req_t *req, const ir_node *irn, int pos)
81 {
82   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
83   return ops->get_irn_reg_req(ops, req, irn, pos);
84 }
85
86 int arch_get_allocatable_regs(const arch_env_t *env, const ir_node *irn,
87     int pos, const arch_register_class_t *cls, bitset_t *bs)
88 {
89   arch_register_req_t local_req;
90   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
91   const arch_register_req_t *req = ops->get_irn_reg_req(ops, &local_req, irn, pos);
92
93   switch(req->type) {
94         case arch_register_req_type_none:
95                 bitset_clear_all(bs);
96                 return 0;
97
98         case arch_register_req_type_should_be_different:
99         case arch_register_req_type_should_be_same:
100     case arch_register_req_type_normal:
101       arch_register_class_put(req->cls, bs);
102       return req->cls->n_regs;
103
104     case arch_register_req_type_limited:
105       return req->data.limited(irn, pos, bs);
106
107     default:
108       assert(0 && "This register requirement case is not covered");
109   }
110
111   return 0;
112 }
113
114 int arch_is_register_operand(const arch_env_t *env,
115     const ir_node *irn, int pos)
116 {
117         arch_register_req_t local_req;
118         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
119         const arch_register_req_t *req = ops->get_irn_reg_req(ops, &local_req, irn, pos);
120         return req != NULL;
121 }
122
123 int arch_reg_is_allocatable(const arch_env_t *env, const ir_node *irn,
124     int pos, const arch_register_t *reg)
125 {
126         int res = 0;
127         arch_register_req_t req;
128
129         arch_get_register_req(env, &req, irn, pos);
130         switch(req.type) {
131                 case arch_register_req_type_normal:
132                 case arch_register_req_type_should_be_different:
133                 case arch_register_req_type_should_be_same:
134                         res = req.cls == reg->reg_class;
135                         break;
136                 case arch_register_req_type_limited:
137                         {
138                                 bitset_t *bs = bitset_alloca(req.cls->n_regs);
139                                 req.data.limited(irn, pos, bs);
140                                 res = bitset_is_set(bs, arch_register_get_index(reg));
141                         }
142                         break;
143                 default:
144                         res = 0;
145         }
146
147         return res;
148 }
149
150 const arch_register_class_t *
151 arch_get_irn_reg_class(const arch_env_t *env, const ir_node *irn, int pos)
152 {
153   arch_register_req_t local_req;
154   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
155   const arch_register_req_t *req = ops->get_irn_reg_req(ops, &local_req, irn, pos);
156   return req ? req->cls : NULL;
157 }
158
159 extern const arch_register_t *
160 arch_get_irn_register(const arch_env_t *env, const ir_node *irn)
161 {
162   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
163   return ops->get_irn_reg(ops, irn);
164 }
165
166 extern void arch_set_irn_register(const arch_env_t *env,
167     ir_node *irn, const arch_register_t *reg)
168 {
169   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
170   ops->set_irn_reg(ops, irn, reg);
171 }
172
173 extern arch_irn_class_t arch_irn_classify(const arch_env_t *env, const ir_node *irn)
174 {
175   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
176   return ops->classify(ops, irn);
177 }
178
179 extern arch_irn_flags_t arch_irn_get_flags(const arch_env_t *env, const ir_node *irn)
180 {
181   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
182   return ops->get_flags(ops, irn);
183 }