2 * Processor architecture specification.
3 * @author Sebastian Hack
11 #include "firm_config.h"
17 #if 1 /* HAVE_ALLOCA_H */
19 #endif /* HAVE_ALLOCA_H */
21 #define INIT_HEADER(tgt, kind_suffix, a_isa, str) \
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); \
30 static INLINE int hash_header(const arch_header_t *header)
32 int res = HASH_PTR(header->isa);
33 res = 37 * res + HASH_STR(header->name, strlen(header->name));
34 res = 37 * res + header->kind;
38 static int cmp_header(const void *a, const void *b, size_t size)
40 const arch_header_t *h1 = a;
41 const arch_header_t *h2 = b;
43 return !(h1->kind == h2->kind && strcmp(h1->name, h2->name) == 0);
46 static set *arch_data = NULL;
48 static set *get_arch_data(void)
51 arch_data = new_set(cmp_header, 256);
56 typedef struct _obj_info_t {
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"
70 * Insert an arch object to the global arch obj storage.
72 * If the object has already been created there, nothing is done and
73 * the old object is created.
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
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.
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)
86 const obj_info_t *info = &obj_info[kind];
87 arch_header_t *data = alloca(size);
88 arch_header_t *res = NULL;
90 memset(data, 0, size);
93 data->name = get_id_str(new_id_from_str(name));
96 res = set_insert(get_arch_data(), data, size, hash_header(data));
98 /* If the object is newly created and thus not yet present
99 * in the set, add it to the isa */
103 * The inserted object was no isa, list it in the isa if this is
106 if(isa && info->listed_in_isa)
107 list_add(&res->list, &isa->heads[kind]);
109 /* The inserted object is an isa, so initialize all its list heads. */
112 arch_isa_t *isa = (arch_isa_t *) res;
114 for(i = 0; i < arch_kind_last; ++i)
115 INIT_LIST_HEAD(&isa->heads[i]);
120 * If the caller wants to know, of the object was newly created,
124 *was_new = res->is_new;
126 /* Mark the object as NOT new. */
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)
135 static INLINE void *_arch_data_find(arch_kind_t kind, const arch_isa_t *isa, const char *name)
137 arch_header_t header;
139 memset(&header, 0, sizeof(header));
141 header.isa = (arch_isa_t *) isa;
144 return set_find(get_arch_data(), &header, sizeof(header), hash_header(&header));
147 #define arch_data_find(type_suffix, isa, name) \
148 _arch_data_find(arch_kind_ ## type_suffix, isa, name)
150 arch_isa_t *arch_add_isa(const char *name)
152 return arch_data_insert(isa, NULL, name, NULL);
155 arch_register_class_t *arch_add_register_class(arch_isa_t *isa, const char *name, int n_regs)
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);
161 cls->n_regs = n_regs;
166 arch_register_t *arch_add_register(arch_register_class_t *cls, int index, const char *name)
168 arch_register_t *reg = NULL;
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;
176 reg->reg_class = cls;
177 reg->flags = arch_register_flag_none;
182 arch_immediate_t *arch_add_immediate(arch_isa_t *isa, const char *name, ir_mode *mode)
184 arch_immediate_t *imm = arch_data_insert(immediate, isa, name, NULL);
189 static const size_t operand_sizes[] = {
193 sizeof(arch_register_t *),
197 arch_insn_format_t *arch_add_insn_format(arch_isa_t *isa, const char *name, int n_in, int n_out)
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);
207 fmt->irn_data_size = 0;
210 * Compute the number of bytes which must be extra allocated if this
211 * opcode is instantiated.
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];
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];
228 arch_insn_t *arch_add_insn(arch_insn_format_t *fmt, const char *name)
230 /* Insert the insn into the isa. */
231 arch_insn_t *insn = arch_data_insert(insn, arch_obj_get_isa(fmt), name, NULL);
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);
240 arch_insn_format_t *arch_find_insn_format(arch_isa_t *isa, const char *name)
242 return arch_data_find(insn_format, isa, name);
245 arch_isa_t *arch_find_isa(const char *name)
247 return arch_data_find(isa, NULL, name);
250 arch_register_class_t *arch_find_register_class_t(arch_isa_t *isa, const char *name)
252 return arch_data_find(register_class, isa, name);
255 arch_register_set_t *arch_get_register_set_for_class(arch_register_class_t *cls)
257 return _arch_get_register_set_for_class(cls);
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)
263 ir_node *irn = new_ir_node(NULL, irg, block, insn->op, mode, arity, in);
264 arch_irn_data_t *data = (void *) &irn->attr;
266 data->magic = ARCH_IRN_FOURCC;
272 ir_node *arch_new_node_bare(const arch_insn_t *insn, ir_graph *irg, int arity)
275 ir_node **in = alloca(sizeof(in[0]) * arity);
277 for(i = 0; i < arity; ++i)
278 in[i] = new_Unknown(mode_Is);
280 return arch_new_node(insn, irg, new_Unknown(mode_BB), mode_Is, arity, in);