2 * Processor architecture specification.
3 * @author Sebastian Hack
13 #include "firm_config.h"
19 #if 1 /* HAVE_ALLOCA_H */
21 #endif /* HAVE_ALLOCA_H */
23 #define INIT_HEADER(tgt, kind_suffix, a_isa, str) \
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); \
32 static INLINE int hash_header(const arch_header_t *header)
34 int res = HASH_PTR(header->isa);
35 res = 37 * res + HASH_STR(header->name, strlen(header->name));
36 res = 37 * res + header->kind;
40 static int cmp_header(const void *a, const void *b, size_t size)
42 const arch_header_t *h1 = a;
43 const arch_header_t *h2 = b;
45 return !(h1->kind == h2->kind && strcmp(h1->name, h2->name) == 0);
48 static set *arch_data = NULL;
50 static set *get_arch_data(void)
53 arch_data = new_set(cmp_header, 256);
58 typedef struct _obj_info_t {
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"
72 * Insert an arch object to the global arch obj storage.
74 * If the object has already been created there, nothing is done and
75 * the old object is created.
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
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.
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)
88 const obj_info_t *info = &obj_info[kind];
89 arch_header_t *data = alloca(size);
90 arch_header_t *res = NULL;
92 memset(data, 0, size);
95 data->name = get_id_str(new_id_from_str(name));
98 res = set_insert(get_arch_data(), data, size, hash_header(data));
100 /* If the object is newly created and thus not yet present
101 * in the set, add it to the isa */
105 * The inserted object was no isa, list it in the isa if this is
108 if(isa && info->listed_in_isa)
109 list_add(&res->list, &isa->heads[kind]);
111 /* The inserted object is an isa, so initialize all its list heads. */
114 arch_isa_t *isa = (arch_isa_t *) res;
116 for(i = 0; i < arch_kind_last; ++i)
117 INIT_LIST_HEAD(&isa->heads[i]);
122 * If the caller wants to know, of the object was newly created,
126 *was_new = res->is_new;
128 /* Mark the object as NOT new. */
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)
137 static INLINE void *_arch_data_find(arch_kind_t kind, const arch_isa_t *isa, const char *name)
139 arch_header_t header;
141 memset(&header, 0, sizeof(header));
143 header.isa = (arch_isa_t *) isa;
146 return set_find(get_arch_data(), &header, sizeof(header), hash_header(&header));
149 #define arch_data_find(type_suffix, isa, name) \
150 _arch_data_find(arch_kind_ ## type_suffix, isa, name)
152 arch_isa_t *arch_add_isa(const char *name)
154 return arch_data_insert(isa, NULL, name, NULL);
157 arch_register_class_t *arch_add_register_class(arch_isa_t *isa, const char *name, int n_regs)
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);
163 cls->n_regs = n_regs;
168 arch_register_t *arch_add_register(arch_register_class_t *cls, int index, const char *name)
170 arch_register_t *reg = NULL;
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;
178 reg->reg_class = cls;
179 reg->flags = arch_register_flag_none;
184 arch_immediate_t *arch_add_immediate(arch_isa_t *isa, const char *name, ir_mode *mode)
186 arch_immediate_t *imm = arch_data_insert(immediate, isa, name, NULL);
191 static const size_t operand_sizes[] = {
195 sizeof(arch_register_t *),
199 arch_insn_format_t *arch_add_insn_format(arch_isa_t *isa, const char *name, int n_in, int n_out)
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);
209 fmt->irn_data_size = 0;
212 * Compute the number of bytes which must be extra allocated if this
213 * opcode is instantiated.
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];
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];
230 arch_insn_t *arch_add_insn(arch_insn_format_t *fmt, const char *name)
232 /* Insert the insn into the isa. */
233 arch_insn_t *insn = arch_data_insert(insn, arch_obj_get_isa(fmt), name, NULL);
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);
242 arch_insn_format_t *arch_find_insn_format(const arch_isa_t *isa, const char *name)
244 return arch_data_find(insn_format, isa, name);
247 arch_isa_t *arch_find_isa(const char *name)
249 return arch_data_find(isa, NULL, name);
252 arch_insn_t *arch_find_insn(const arch_isa_t *isa, const char *name)
254 return arch_data_find(insn, isa, name);
257 arch_register_class_t *arch_find_register_class_t(arch_isa_t *isa, const char *name)
259 return arch_data_find(register_class, isa, name);
262 arch_register_set_t *arch_get_register_set_for_class(arch_register_class_t *cls)
264 return _arch_get_register_set_for_class(cls);
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)
270 ir_node *irn = new_ir_node(NULL, irg, block, insn->op, mode, arity, in);
271 arch_irn_data_t *data = (void *) &irn->attr;
273 data->magic = ARCH_IRN_FOURCC;
279 ir_node *arch_new_node_bare(const arch_insn_t *insn, ir_graph *irg, int arity)
282 ir_node **in = alloca(sizeof(in[0]) * arity);
284 for(i = 0; i < arity; ++i)
285 in[i] = new_Unknown(mode_Is);
287 return arch_new_node(insn, irg, new_Unknown(mode_BB), mode_Is, arity, in);
290 ir_mode *arch_get_unknown_mode(void)