2ce562febb4edff2d9b780ee1ef2ead1710b28f8
[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)
30 {
31   memset(env, 0, sizeof(*env));
32   env->isa = isa_if->init();
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   req->type = arch_register_req_type_none;
84   return ops->get_irn_reg_req(ops, req, irn, pos);
85 }
86
87 int arch_get_allocatable_regs(const arch_env_t *env, const ir_node *irn,
88     int pos, const arch_register_class_t *cls, bitset_t *bs)
89 {
90   arch_register_req_t local_req;
91   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
92   const arch_register_req_t *req = ops->get_irn_reg_req(ops, &local_req, irn, pos);
93
94   if(arch_register_req_is(req, none)) {
95           bitset_clear_all(bs);
96           return 0;
97   }
98
99   if(arch_register_req_is(req, limited))
100           return req->limited(irn, pos, bs);
101
102   arch_register_class_put(req->cls, bs);
103   return req->cls->n_regs;
104 }
105
106 int arch_is_register_operand(const arch_env_t *env,
107     const ir_node *irn, int pos)
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->get_irn_reg_req(ops, &local_req, irn, pos);
112         return req != NULL;
113 }
114
115 int arch_reg_is_allocatable(const arch_env_t *env, const ir_node *irn,
116     int pos, const arch_register_t *reg)
117 {
118         int res = 0;
119         arch_register_req_t req;
120
121         arch_get_register_req(env, &req, irn, pos);
122         switch(req.type) {
123                 case arch_register_req_type_normal:
124                 case arch_register_req_type_should_be_different:
125                 case arch_register_req_type_should_be_same:
126                         res = req.cls == reg->reg_class;
127                         break;
128                 case arch_register_req_type_limited:
129                         {
130                                 bitset_t *bs = bitset_alloca(req.cls->n_regs);
131                                 req.limited(irn, pos, bs);
132                                 res = bitset_is_set(bs, arch_register_get_index(reg));
133                         }
134                         break;
135                 default:
136                         res = 0;
137         }
138
139         return res;
140 }
141
142 const arch_register_class_t *
143 arch_get_irn_reg_class(const arch_env_t *env, const ir_node *irn, int pos)
144 {
145   arch_register_req_t local_req;
146   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
147   const arch_register_req_t *req = ops->get_irn_reg_req(ops, &local_req, irn, pos);
148   return req ? req->cls : NULL;
149 }
150
151 extern const arch_register_t *
152 arch_get_irn_register(const arch_env_t *env, const ir_node *irn)
153 {
154   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
155   return ops->get_irn_reg(ops, irn);
156 }
157
158 extern void arch_set_irn_register(const arch_env_t *env,
159     ir_node *irn, const arch_register_t *reg)
160 {
161   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
162   ops->set_irn_reg(ops, irn, reg);
163 }
164
165 extern arch_irn_class_t arch_irn_classify(const arch_env_t *env, const ir_node *irn)
166 {
167   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
168   return ops->classify(ops, irn);
169 }
170
171 extern arch_irn_flags_t arch_irn_get_flags(const arch_env_t *env, const ir_node *irn)
172 {
173   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
174   return ops->get_flags(ops, irn);
175 }