2 * Processor architecture specification.
3 * @author Sebastian Hack
28 /* Needed for obstack copy */
29 #define bcopy(src,dst,n) memcpy(dst,src,n)
31 #define INIT_HEADER(tgt, kind_suffix, a_isa, str) \
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); \
40 static INLINE int hash_header(const arch_header_t *header)
42 int res = HASH_PTR(header->isa);
43 res = 37 * res + HASH_STR(header->name, strlen(header->name));
44 res = 37 * res + header->kind;
48 static int cmp_header(const void *a, const void *b)
50 const arch_header_t *h1 = a;
51 const arch_header_t *h2 = b;
53 return !(h1->kind == h2->kind && h1->isa == h2->isa && strcmp(h1->name, h2->name) == 0);
57 * The obstack and pset where the arch data is stored.
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. */
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.
69 static INLINE arch_data_t *get_arch_data(void)
71 static arch_data_t arch_data;
72 static int inited = 0;
75 obstack_init(&arch_data.obst);
76 arch_data.header_set = new_pset(cmp_header, 512);
84 * Dump all arch objects in the arch_data collection.
86 static void dump_arch_data(void)
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"
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 : "");
105 typedef struct _obj_info_t {
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"
119 * Insert an arch object to the global arch obj storage.
121 * If the object has already been created there, nothing is done and
122 * the old object is created.
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
127 * @param name The name of the object.
128 * @return A pointer to the object.
130 static INLINE void *_arch_data_insert(arch_kind_t kind, arch_isa_t *isa,
131 const char *name, size_t size)
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;
138 memset(header, 0, size);
144 res = pset_insert(ad->header_set, header, hash_header(header));
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
152 if(res->is_new && isa && info->listed_in_isa) {
153 list_add(&res->list, &isa->heads[kind]);
156 /* if it was in the set, remove it from the obstack */
158 obstack_free(&ad->obst, header);
160 /* Mark the object as NOT new. */
166 #define arch_data_insert(type_suffix, isa, name) \
167 _arch_data_insert(arch_kind_ ## type_suffix, isa, name, sizeof(arch_ ## type_suffix ## _t))
169 static INLINE void *_arch_data_find(arch_kind_t kind, const arch_isa_t *isa, const char *name)
171 arch_header_t header;
174 header.isa = (arch_isa_t *) isa;
177 return pset_find(get_arch_data()->header_set, &header, hash_header(&header));
180 #define arch_data_find(type_suffix, isa, name) \
181 _arch_data_find(arch_kind_ ## type_suffix, isa, name)
183 arch_isa_t *arch_add_isa(const char *name)
188 isa = arch_data_insert(isa, NULL, name);
189 for(i = 0; i < arch_kind_last; ++i)
190 INIT_LIST_HEAD(&isa->heads[i]);
195 arch_register_set_t *arch_add_register_set(arch_isa_t *isa,
196 const arch_register_class_t *cls, const char *name)
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]));
202 set->reg_class = cls;
203 memset(set->regs, 0, sizeof(set->regs[0]) * cls->n_regs);
208 arch_register_class_t *arch_add_register_class(arch_isa_t *isa, const char *name, int n_regs)
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 *));
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);
222 cls->n_regs = n_regs;
224 /* make the set of all registers in this class */
225 cls->set = arch_add_register_set(isa, cls, name);
227 /* Add each register in this class to the set */
228 for(i = 0; i < n_regs; ++i)
229 cls->set->regs[i] = 1;
234 void arch_register_set_add_register(arch_register_set_t *set, int index)
236 assert(index >= 0 && index < set->reg_class->n_regs);
237 set->regs[index] = 1;
240 arch_register_t *arch_add_register(arch_register_class_t *cls, int index, const char *name)
242 arch_register_t *reg = NULL;
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;
250 reg->reg_class = cls;
251 reg->flags = arch_register_flag_none;
256 arch_immediate_t *arch_add_immediate(arch_isa_t *isa, const char *name, ir_mode *mode)
258 arch_immediate_t *imm = arch_data_insert(immediate, isa, name);
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.
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
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.
282 static INLINE int arch_get_operands_size(const arch_insn_format_t *fmt)
286 for(i = 0; i < fmt->n_in + fmt->n_out; ++i) {
287 arch_operand_type_t type = fmt->operands[i].type;
289 assert(type > arch_operand_type_invalid && type < arch_operand_type_last);
290 res += operand_sizes[type];
296 arch_insn_format_t *arch_add_insn_format(arch_isa_t *isa, const char *name, int n_in, int n_out)
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));
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;
314 arch_insn_t *arch_add_insn(arch_insn_format_t *fmt, const char *name)
316 /* Get the size the operands will need in the irn. */
317 int operands_size = arch_get_operands_size(fmt);
319 /* Insert the insn into the isa. */
320 arch_insn_t *insn = arch_data_insert(insn, arch_obj_get_isa(fmt), name);
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);
329 arch_insn_format_t *arch_find_insn_format(const arch_isa_t *isa, const char *name)
331 return arch_data_find(insn_format, isa, name);
334 arch_isa_t *arch_find_isa(const char *name)
336 return arch_data_find(isa, NULL, name);
339 arch_insn_t *arch_find_insn(const arch_isa_t *isa, const char *name)
341 return arch_data_find(insn, isa, name);
344 arch_immediate_t *arch_find_immediate(const arch_isa_t *isa, const char *name)
346 return arch_data_find(immediate, isa, name);
349 arch_register_class_t *arch_find_register_class(const arch_isa_t *isa, const char *name)
351 return arch_data_find(register_class, isa, name);
354 arch_register_set_t *arch_find_register_set(const arch_isa_t *isa, const char *name)
356 return arch_data_find(register_set, isa, name);
359 arch_register_set_t *arch_get_register_set_for_class(arch_register_class_t *cls)
361 return _arch_get_register_set_for_class(cls);
364 static INLINE arch_operand_t *_arch_set_operand(arch_insn_format_t *fmt, int pos,
365 arch_operand_type_t type)
367 arch_operand_t *operand;
368 int ofs = arch_inout_to_index(fmt, pos);
370 assert(ofs < fmt->n_in + fmt->n_out);
372 operand = &fmt->operands[ofs];
373 operand->type = type;
377 arch_operand_t *arch_set_operand_register_set(arch_insn_format_t *fmt,
378 int pos, const arch_register_set_t *set)
380 arch_operand_t *op = _arch_set_operand(fmt, pos, arch_operand_type_register_set);
385 arch_operand_t *arch_set_operand_callback(arch_insn_format_t *fmt,
386 int pos, arch_register_callback_t *cb)
388 arch_operand_t *op = _arch_set_operand(fmt, pos, arch_operand_type_callback);
389 op->data.callback = cb;
393 arch_operand_t *arch_set_operand_immediate(arch_insn_format_t *fmt,
394 int pos, const arch_immediate_t *imm)
396 arch_operand_t *op = _arch_set_operand(fmt, pos, arch_operand_type_immediate);
401 arch_operand_t *arch_set_operand_memory(arch_insn_format_t *fmt, int pos)
403 arch_operand_t *op = _arch_set_operand(fmt, pos, arch_operand_type_memory);
407 arch_operand_t *arch_set_operand_equals(arch_insn_format_t *fmt, int pos, int same_as_pos)
409 arch_operand_t *op = _arch_set_operand(fmt, pos, arch_operand_type_equals);
410 op->data.same_as_pos = same_as_pos;
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)
417 ir_node *irn = new_ir_node(NULL, irg, block, insn->op, mode, arity, in);
418 arch_irn_data_t *data = (void *) &irn->attr;
420 data->magic = ARCH_IRN_FOURCC;
426 ir_node *arch_new_node_bare(const arch_insn_t *insn, ir_graph *irg, int arity)
429 ir_node **in = alloca(sizeof(in[0]) * arity);
431 for(i = 0; i < arity; ++i)
432 in[i] = new_Unknown(mode_Is);
434 return arch_new_node(insn, irg, new_Unknown(mode_BB), mode_Is, arity, in);
437 ir_mode *arch_get_unknown_mode(void)