Added new arch interface
[libfirm] / ir / be / bearch.c
1 /**
2  * Processor architecture specification.
3  * @author Sebastian Hack
4  * @date 11.2.2005
5  *
6  * $Id$
7  */
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11
12 #ifdef HAVE_ALLOCA_H
13 #include <alloca.h>
14 #endif
15 #ifdef HAVE_MALLOC_H
16 #include <malloc.h>
17 #endif
18
19 #include <string.h>
20
21 #include "bearch.h"
22
23 #include "pset.h"
24
25 #include "entity.h"
26 #include "ircons_t.h"
27
28 /* Needed for obstack copy */
29 #define bcopy(src,dst,n) memcpy(dst,src,n)
30
31 #define INIT_HEADER(tgt, kind_suffix, a_isa, str) \
32         do { \
33                 arch_header_t *h = (arch_header_t *) (tgt); \
34                 memset(tgt, 0, sizeof(*(tgt))); \
35                 h->kind = arch_kind_ ## kind_suffix; \
36                 h->name = new_id_from_str(str); \
37                 h->isa = a_isa; \
38         } while(0)
39
40 static INLINE int hash_header(const arch_header_t *header)
41 {
42         int res = HASH_PTR(header->isa);
43         res = 37 * res + HASH_STR(header->name, strlen(header->name));
44         res = 37 * res + header->kind;
45         return res;
46 }
47
48 static int cmp_header(const void *a, const void *b)
49 {
50         const arch_header_t *h1 = a;
51         const arch_header_t *h2 = b;
52
53         return !(h1->kind == h2->kind && h1->isa == h2->isa && strcmp(h1->name, h2->name) == 0);
54 }
55
56 /**
57  * The obstack and pset where the arch data is stored.
58  */
59 typedef struct _arch_data_t {
60         struct obstack obst;                    /**< Here is the data allocated. */
61         pset *header_set;                                       /**< Here reside copies of the headers. */
62 } arch_data_t;
63
64 /**
65  * Get the storage (obstack and pset) for the arch objects.
66  * @return A struct containing both, the obst and pset where
67  * the objects are allocated and their pointer are recorded.
68  */
69 static INLINE arch_data_t *get_arch_data(void)
70 {
71         static arch_data_t arch_data;
72         static int inited = 0;
73
74         if(!inited) {
75                 obstack_init(&arch_data.obst);
76                 arch_data.header_set = new_pset(cmp_header, 512);
77                 inited = 1;
78         }
79
80         return &arch_data;
81 }
82
83 /**
84  * Dump all arch objects in the arch_data collection.
85  */
86 static void dump_arch_data(void)
87 {
88         void *p;
89         arch_data_t *d = get_arch_data();
90         static const char *kind_names[] = {
91 #define ARCH_OBJ(name,in_list)  #name,
92 #include "bearch_obj.def"
93 #undef ARCH_OBJ
94                 ""
95         };
96
97         printf("arch set:\n");
98         for(p = pset_first(d->header_set); p; p = pset_next(d->header_set)) {
99                 arch_header_t *header = p;
100                 printf("%20s %10s %10s\n", kind_names[header->kind], header->name,
101                                 header->isa ? header->isa->header.name : "");
102         }
103 }
104
105 typedef struct _obj_info_t {
106         const char *name;
107         int listed_in_isa;
108         size_t size;
109 } obj_info_t;
110
111 static const obj_info_t obj_info[] = {
112 #define ARCH_OBJ(name,listed_in_isa)            { #name, listed_in_isa, sizeof(arch_ ## name ## _t) },
113 #include "bearch_obj.def"
114 #undef ARCH_OBJ
115         { 0 }
116 };
117
118 /**
119  * Insert an arch object to the global arch obj storage.
120  *
121  * If the object has already been created there, nothing is done and
122  * the old object is created.
123  *
124  * @param kind The kind of the arch object.
125  * @param isa The isa the object belongs to or NULL if it is the isa
126  * itself.
127  * @param name The name of the object.
128  * @return A pointer to the object.
129  */
130 static INLINE void *_arch_data_insert(arch_kind_t kind, arch_isa_t *isa,
131                 const char *name, size_t size)
132 {
133         arch_data_t *ad = get_arch_data();
134         const obj_info_t *info = &obj_info[kind];
135         arch_header_t *header = obstack_alloc(&ad->obst, size);
136         arch_header_t *res = NULL;
137
138         memset(header, 0, size);
139         header->name = name;
140         header->kind = kind;
141         header->isa = isa;
142         header->is_new = 1;
143
144         res = pset_insert(ad->header_set, header, hash_header(header));
145
146         /*
147          * If the object is newly created and thus not yet present
148          * in the set, add it to the isa
149          * The inserted object was no isa, list it in the isa if this is
150          * desired.
151          */
152         if(res->is_new && isa && info->listed_in_isa) {
153                 list_add(&res->list, &isa->heads[kind]);
154         }
155
156         /* if it was in the set, remove it from the obstack */
157         if(!res->is_new)
158                 obstack_free(&ad->obst, header);
159
160         /* Mark the object as NOT new. */
161         res->is_new = 0;
162
163         return res;
164 }
165
166 #define arch_data_insert(type_suffix, isa, name) \
167         _arch_data_insert(arch_kind_ ## type_suffix, isa, name, sizeof(arch_ ## type_suffix ## _t))
168
169 static INLINE void *_arch_data_find(arch_kind_t kind, const arch_isa_t *isa, const char *name)
170 {
171         arch_header_t header;
172
173         header.kind = kind;
174         header.isa = (arch_isa_t *) isa;
175         header.name = name;
176
177         return pset_find(get_arch_data()->header_set, &header, hash_header(&header));
178 }
179
180 #define arch_data_find(type_suffix, isa, name) \
181         _arch_data_find(arch_kind_ ## type_suffix, isa, name)
182
183 arch_isa_t *arch_add_isa(const char *name)
184 {
185         arch_isa_t *isa;
186         int i;
187
188         isa = arch_data_insert(isa, NULL, name);
189         for(i = 0; i < arch_kind_last; ++i)
190                 INIT_LIST_HEAD(&isa->heads[i]);
191
192         return isa;
193 }
194
195 arch_register_set_t *arch_add_register_set(arch_isa_t *isa,
196                 const arch_register_class_t *cls, const char *name)
197 {
198         arch_register_set_t *set =
199                 _arch_data_insert(arch_kind_register_set, isa, name,
200                                 sizeof(arch_register_set_t) + cls->n_regs * sizeof(set->regs[0]));
201
202         set->reg_class = cls;
203         memset(set->regs, 0, sizeof(set->regs[0]) * cls->n_regs);
204
205         return set;
206 }
207
208 arch_register_class_t *arch_add_register_class(arch_isa_t *isa, const char *name, int n_regs)
209 {
210         char buf[64];
211         char *set_name;
212         int i, n;
213
214         arch_register_class_t *cls =
215                 _arch_data_insert(arch_kind_register_class, isa, name,
216                                 sizeof(arch_register_class_t) + n_regs * sizeof(arch_register_t *));
217
218         /* Make a name for the set contianing all regs in this class. */
219         n = snprintf(buf, sizeof(buf), "%s$set", name);
220         set_name = obstack_copy(&get_arch_data()->obst, buf, n);
221
222         cls->n_regs = n_regs;
223
224         /* make the set of all registers in this class */
225         cls->set = arch_add_register_set(isa, cls, name);
226
227         /* Add each register in this class to the set */
228         for(i = 0; i < n_regs; ++i)
229                 cls->set->regs[i] = 1;
230
231         return cls;
232 }
233
234 void arch_register_set_add_register(arch_register_set_t *set, int index)
235 {
236         assert(index >= 0 && index < set->reg_class->n_regs);
237         set->regs[index] = 1;
238 }
239
240 arch_register_t *arch_add_register(arch_register_class_t *cls, int index, const char *name)
241 {
242         arch_register_t *reg = NULL;
243
244         assert(index >= 0 && index < cls->n_regs);
245         reg = _arch_data_insert(arch_kind_register, arch_obj_get_isa(cls), name,
246                         sizeof(arch_register_t));
247         cls->regs[index] = reg;
248
249         reg->index = index;
250         reg->reg_class = cls;
251         reg->flags = arch_register_flag_none;
252
253         return reg;
254 }
255
256 arch_immediate_t *arch_add_immediate(arch_isa_t *isa, const char *name, ir_mode *mode)
257 {
258         arch_immediate_t *imm = arch_data_insert(immediate, isa, name);
259         imm->mode = mode;
260         return imm;
261 }
262
263 /*
264  * Size of each operand type which should be allocated in an irn.
265  * Keep this list up to date with the arch_operand_type_t enum.
266  */
267 static const size_t operand_sizes[] = {
268 #define ARCH_OPERAND_TYPE(name,size_in_irn) size_in_irn,
269 #include "bearch_operand_types.def"
270 #undef ARCH_OPERAND_TYPE
271         0
272 };
273
274 /**
275  * Determine the amount of bytes which has to be extra allocated when a
276  * new ir node is made from a insn format.
277  * This size depends on the operands specified in the insn format.
278  * @param fmt The instruction format.
279  * @return The number of bytes which the operands of an instruction
280  * will need in an ir node.
281  */
282 static INLINE int arch_get_operands_size(const arch_insn_format_t *fmt)
283 {
284         int i, res = 0;
285
286         for(i = 0; i < fmt->n_in + fmt->n_out; ++i) {
287                 arch_operand_type_t type = fmt->operands[i].type;
288
289                 assert(type > arch_operand_type_invalid && type < arch_operand_type_last);
290                 res += operand_sizes[type];
291         }
292
293         return res;
294 }
295
296 arch_insn_format_t *arch_add_insn_format(arch_isa_t *isa, const char *name, int n_in, int n_out)
297 {
298         int i;
299
300         arch_insn_format_t *fmt =
301                 _arch_data_insert(arch_kind_insn_format, isa, name,
302                                 sizeof(arch_insn_format_t) + (n_in + n_out) * sizeof(arch_operand_t));
303
304         fmt->n_in = n_in;
305         fmt->n_out = n_out;
306
307         /* initialize each operand with invalid. */
308         for(i = 0; i < n_in + n_out; ++i)
309                 fmt->operands[i].type = arch_operand_type_invalid;
310
311         return fmt;
312 }
313
314 arch_insn_t *arch_add_insn(arch_insn_format_t *fmt, const char *name)
315 {
316         /* Get the size the operands will need in the irn. */
317         int operands_size = arch_get_operands_size(fmt);
318
319         /* Insert the insn into the isa. */
320         arch_insn_t *insn = arch_data_insert(insn, arch_obj_get_isa(fmt), name);
321
322         insn->format = fmt;
323         insn->op = new_ir_op(get_next_ir_opcode(), name, op_pin_state_pinned, 0,
324                         oparity_dynamic, 0, sizeof(arch_irn_data_t) + operands_size);
325
326         return insn;
327 }
328
329 arch_insn_format_t *arch_find_insn_format(const arch_isa_t *isa, const char *name)
330 {
331         return arch_data_find(insn_format, isa, name);
332 }
333
334 arch_isa_t *arch_find_isa(const char *name)
335 {
336         return arch_data_find(isa, NULL, name);
337 }
338
339 arch_insn_t *arch_find_insn(const arch_isa_t *isa, const char *name)
340 {
341         return arch_data_find(insn, isa, name);
342 }
343
344 arch_immediate_t *arch_find_immediate(const arch_isa_t *isa, const char *name)
345 {
346         return arch_data_find(immediate, isa, name);
347 }
348
349 arch_register_class_t *arch_find_register_class(const arch_isa_t *isa, const char *name)
350 {
351         return arch_data_find(register_class, isa, name);
352 }
353
354 arch_register_set_t *arch_find_register_set(const arch_isa_t *isa, const char *name)
355 {
356         return arch_data_find(register_set, isa, name);
357 }
358
359 arch_register_set_t *arch_get_register_set_for_class(arch_register_class_t *cls)
360 {
361         return _arch_get_register_set_for_class(cls);
362 }
363
364 static INLINE arch_operand_t *_arch_set_operand(arch_insn_format_t *fmt, int pos,
365                 arch_operand_type_t type)
366 {
367         arch_operand_t *operand;
368         int ofs = arch_inout_to_index(fmt, pos);
369
370         assert(ofs < fmt->n_in + fmt->n_out);
371
372         operand = &fmt->operands[ofs];
373         operand->type = type;
374         return operand;
375 }
376
377 arch_operand_t *arch_set_operand_register_set(arch_insn_format_t *fmt,
378                 int pos, const arch_register_set_t *set)
379 {
380         arch_operand_t *op = _arch_set_operand(fmt, pos, arch_operand_type_register_set);
381         op->data.set = set;
382         return op;
383 }
384
385 arch_operand_t *arch_set_operand_callback(arch_insn_format_t *fmt,
386                 int pos, arch_register_callback_t *cb)
387 {
388         arch_operand_t *op = _arch_set_operand(fmt, pos, arch_operand_type_callback);
389         op->data.callback = cb;
390         return op;
391 }
392
393 arch_operand_t *arch_set_operand_immediate(arch_insn_format_t *fmt,
394                 int pos, const arch_immediate_t *imm)
395 {
396         arch_operand_t *op = _arch_set_operand(fmt, pos, arch_operand_type_immediate);
397         op->data.imm = imm;
398         return op;
399 }
400
401 arch_operand_t *arch_set_operand_memory(arch_insn_format_t *fmt, int pos)
402 {
403         arch_operand_t *op = _arch_set_operand(fmt, pos, arch_operand_type_memory);
404         return op;
405 }
406
407 arch_operand_t *arch_set_operand_equals(arch_insn_format_t *fmt, int pos, int same_as_pos)
408 {
409         arch_operand_t *op = _arch_set_operand(fmt, pos, arch_operand_type_equals);
410         op->data.same_as_pos = same_as_pos;
411         return op;
412 }
413
414 ir_node *arch_new_node(const arch_insn_t *insn, ir_graph *irg, ir_node *block,
415                 ir_mode *mode, int arity, ir_node **in)
416 {
417         ir_node *irn = new_ir_node(NULL, irg, block, insn->op, mode, arity, in);
418         arch_irn_data_t *data = (void *) &irn->attr;
419
420         data->magic = ARCH_IRN_FOURCC;
421         data->insn = insn;
422
423         return irn;
424 }
425
426 ir_node *arch_new_node_bare(const arch_insn_t *insn, ir_graph *irg, int arity)
427 {
428         int i;
429         ir_node **in = alloca(sizeof(in[0]) * arity);
430
431         for(i = 0; i < arity; ++i)
432                 in[i] = new_Unknown(mode_Is);
433
434         return arch_new_node(insn, irg, new_Unknown(mode_BB), mode_Is, arity, in);
435 }
436
437 ir_mode *arch_get_unknown_mode(void)
438 {
439         return mode_Is;
440 }