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