Added be arch reflection/backend support infrastructure.
[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 #include "bearch_t.h"
10
11 #include "firm_config.h"
12 #include "set.h"
13
14 #include "entity.h"
15 #include "ircons_t.h"
16
17 #if 1 /* HAVE_ALLOCA_H */
18 #include <alloca.h>
19 #endif /* HAVE_ALLOCA_H */
20
21 #define INIT_HEADER(tgt, kind_suffix, a_isa, str) \
22         do { \
23                 arch_header_t *h = (arch_header_t *) (tgt); \
24                 memset(tgt, 0, sizeof(*(tgt))); \
25                 h->kind = arch_kind_ ## kind_suffix; \
26                 h->name = new_id_from_str(str); \
27                 h->isa = a_isa; \
28         } while(0)
29
30 static INLINE int hash_header(const arch_header_t *header)
31 {
32         int res = HASH_PTR(header->isa);
33         res = 37 * res + HASH_STR(header->name, strlen(header->name));
34         res = 37 * res + header->kind;
35         return res;
36 }
37
38 static int cmp_header(const void *a, const void *b, size_t size)
39 {
40         const arch_header_t *h1 = a;
41         const arch_header_t *h2 = b;
42
43         return !(h1->kind == h2->kind && strcmp(h1->name, h2->name) == 0);
44 }
45
46 static set *arch_data = NULL;
47
48 static set *get_arch_data(void)
49 {
50         if(!arch_data)
51                 arch_data = new_set(cmp_header, 256);
52
53         return arch_data;
54 }
55
56 typedef struct _obj_info_t {
57         const char *name;
58         int listed_in_isa;
59         size_t size;
60 } obj_info_t;
61
62 static const obj_info_t obj_info[] = {
63 #define ARCH_OBJ(name,listed_in_isa)            { #name, listed_in_isa, sizeof(arch_ ## name ## _t) },
64 #include "bearch_obj.def"
65 #undef ARCH_OBJ
66         { 0 }
67 };
68
69 /**
70  * Insert an arch object to the global arch obj storage.
71  *
72  * If the object has already been created there, nothing is done and
73  * the old object is created.
74  *
75  * @param kind The kind of the arch object.
76  * @param isa The isa the object belongs to or NULL if it is the isa
77  * itself.
78  * @param name The name of the object.
79  * @param was_new A pointer to an int where 1/0 is stored if the
80  * object was created or already present. If NULL, it is simply ignored.
81  * @return A pointer to the object.
82  */
83 static INLINE void *_arch_data_insert(arch_kind_t kind, arch_isa_t *isa,
84                 const char *name, size_t size, int *was_new)
85 {
86         const obj_info_t *info = &obj_info[kind];
87         arch_header_t *data = alloca(size);
88         arch_header_t *res = NULL;
89
90         memset(data, 0, size);
91         data->kind = kind;
92         data->isa = isa;
93         data->name = get_id_str(new_id_from_str(name));
94         data->is_new = 1;
95
96         res = set_insert(get_arch_data(), data, size, hash_header(data));
97
98         /* If the object is newly created and thus not yet present
99          * in the set, add it to the isa */
100         if(res->is_new) {
101
102                 /*
103                  * The inserted object was no isa, list it in the isa if this is
104                  * desired.
105                  */
106                 if(isa && info->listed_in_isa)
107                         list_add(&res->list, &isa->heads[kind]);
108
109                 /* The inserted object is an isa, so initialize all its list heads. */
110                 else {
111                         int i;
112                         arch_isa_t *isa = (arch_isa_t *) res;
113
114                         for(i = 0; i < arch_kind_last; ++i)
115                                 INIT_LIST_HEAD(&isa->heads[i]);
116                 }
117         }
118
119         /*
120          * If the caller wants to know, of the object was newly created,
121          * give it to him.
122          */
123         if(was_new)
124                 *was_new = res->is_new;
125
126         /* Mark the object as NOT new. */
127         res->is_new = 0;
128
129         return res;
130 }
131
132 #define arch_data_insert(type_suffix, isa, name, was_new) \
133         _arch_data_insert(arch_kind_ ## type_suffix, isa, name, sizeof(arch_ ## type_suffix ## _t), was_new)
134
135 static INLINE void *_arch_data_find(arch_kind_t kind, const arch_isa_t *isa, const char *name)
136 {
137         arch_header_t header;
138
139         memset(&header, 0, sizeof(header));
140         header.kind = kind;
141         header.isa = (arch_isa_t *) isa;
142         header.name = name;
143
144         return set_find(get_arch_data(), &header, sizeof(header), hash_header(&header));
145 }
146
147 #define arch_data_find(type_suffix, isa, name) \
148         _arch_data_find(arch_kind_ ## type_suffix, isa, name)
149
150 arch_isa_t *arch_add_isa(const char *name)
151 {
152         return arch_data_insert(isa, NULL, name, NULL);
153 }
154
155 arch_register_class_t *arch_add_register_class(arch_isa_t *isa, const char *name, int n_regs)
156 {
157         arch_register_class_t *cls =
158                 _arch_data_insert(arch_kind_register_class, isa, name,
159                                 sizeof(arch_register_class_t) + n_regs * sizeof(arch_register_t *), NULL);
160
161         cls->n_regs = n_regs;
162
163         return cls;
164 }
165
166 arch_register_t *arch_add_register(arch_register_class_t *cls, int index, const char *name)
167 {
168         arch_register_t *reg = NULL;
169
170         assert(index >= 0 && index < cls->n_regs);
171         reg = _arch_data_insert(arch_kind_register, arch_obj_get_isa(cls), name,
172                         sizeof(arch_register_t), NULL);
173         cls->regs[index] = reg;
174
175         reg->index = index;
176         reg->reg_class = cls;
177         reg->flags = arch_register_flag_none;
178
179         return reg;
180 }
181
182 arch_immediate_t *arch_add_immediate(arch_isa_t *isa, const char *name, ir_mode *mode)
183 {
184         arch_immediate_t *imm = arch_data_insert(immediate, isa, name, NULL);
185         imm->mode = mode;
186         return imm;
187 }
188
189 static const size_t operand_sizes[] = {
190         0,
191         0,
192         sizeof(entity *),
193         sizeof(arch_register_t *),
194         sizeof(tarval *)
195 };
196
197 arch_insn_format_t *arch_add_insn_format(arch_isa_t *isa, const char *name, int n_in, int n_out)
198 {
199         int i;
200
201         arch_insn_format_t *fmt =
202                 _arch_data_insert(arch_kind_insn_format, isa, name,
203                                 sizeof(arch_insn_format_t) + (n_in + n_out) * sizeof(arch_operand_type_t), NULL);
204
205         fmt->n_in = n_in;
206         fmt->n_out = n_out;
207         fmt->irn_data_size = 0;
208
209         /*
210          * Compute the number of bytes which must be extra allocated if this
211          * opcode is instantiated.
212          */
213         for(i = 0; i < fmt->n_in; ++i) {
214                 arch_operand_t *op = arch_get_in_operand(fmt, i);
215                 op->offset_in_irn_data = fmt->irn_data_size;
216                 fmt->irn_data_size += operand_sizes[op->type];
217         }
218
219         if(fmt->n_out == 1) {
220                 arch_operand_t *op = arch_get_in_operand(fmt, i);
221                 op->offset_in_irn_data = fmt->irn_data_size;
222                 fmt->irn_data_size += operand_sizes[op->type];
223         }
224
225         return fmt;
226 }
227
228 arch_insn_t *arch_add_insn(arch_insn_format_t *fmt, const char *name)
229 {
230         /* Insert the insn into the isa. */
231         arch_insn_t *insn = arch_data_insert(insn, arch_obj_get_isa(fmt), name, NULL);
232
233         insn->format = fmt;
234         insn->op = new_ir_op(get_next_ir_opcode(), name, op_pin_state_pinned, 0,
235                         oparity_dynamic, 0, sizeof(arch_irn_data_t) + fmt->irn_data_size);
236
237         return insn;
238 }
239
240 arch_insn_format_t *arch_find_insn_format(arch_isa_t *isa, const char *name)
241 {
242         return arch_data_find(insn_format, isa, name);
243 }
244
245 arch_isa_t *arch_find_isa(const char *name)
246 {
247         return arch_data_find(isa, NULL, name);
248 }
249
250 arch_register_class_t *arch_find_register_class_t(arch_isa_t *isa, const char *name)
251 {
252         return arch_data_find(register_class, isa, name);
253 }
254
255 arch_register_set_t *arch_get_register_set_for_class(arch_register_class_t *cls)
256 {
257         return _arch_get_register_set_for_class(cls);
258 }
259
260 ir_node *arch_new_node(const arch_insn_t *insn, ir_graph *irg, ir_node *block,
261                 ir_mode *mode, int arity, ir_node **in)
262 {
263         ir_node *irn = new_ir_node(NULL, irg, block, insn->op, mode, arity, in);
264         arch_irn_data_t *data = (void *) &irn->attr;
265
266         data->magic = ARCH_IRN_FOURCC;
267         data->insn = insn;
268
269         return irn;
270 }
271
272 ir_node *arch_new_node_bare(const arch_insn_t *insn, ir_graph *irg, int arity)
273 {
274         int i;
275         ir_node **in = alloca(sizeof(in[0]) * arity);
276
277         for(i = 0; i < arity; ++i)
278                 in[i] = new_Unknown(mode_Is);
279
280         return arch_new_node(insn, irg, new_Unknown(mode_BB), mode_Is, arity, in);
281 }