3 * ISA implementation for Firm IR nodes.
10 #include <libcore/lc_opts.h>
18 #include "iredges_t.h"
24 #include "../bearch.h"
25 #include "../besched.h"
26 #include "../beutil.h"
31 enum { imm_Const, imm_SymConst } tp;
34 symconst_attr symc_attr;
38 static arch_register_t datab_regs[N_REGS];
40 static arch_register_class_t reg_classes[] = {
41 { "datab", N_REGS, datab_regs },
44 static ir_op *op_push;
47 const arch_isa_if_t firm_isa;
50 (sizeof(reg_classes) / sizeof(reg_classes[0]))
54 tarval *get_Imm_tv(ir_node *n) {
55 imm_attr_t *attr = (imm_attr_t *)get_irn_generic_attr(n);
56 return attr->tp == imm_Const ? attr->data.cnst_attr.tv : NULL;
59 int is_Imm(const ir_node *irn) {
60 return get_irn_op(irn) == op_imm;
63 static int dump_node_Imm(ir_node *n, FILE *F, dump_reason_t reason) {
71 case dump_node_opcode_txt:
75 tarval_snprintf(buf, sizeof(buf), tv);
76 fprintf(F, "%s", buf);
79 fprintf(F, "immSymC");
83 case dump_node_mode_txt:
84 mode = get_irn_mode(n);
86 if (mode && mode != mode_BB && mode != mode_ANY && mode != mode_BAD && mode != mode_T) {
87 fprintf(F, "[%s]", get_mode_name(mode));
91 case dump_node_nodeattr_txt:
92 attr = (imm_attr_t *)get_irn_generic_attr(n);
94 if (is_Imm(n) && attr->tp == imm_SymConst) {
95 const char *name = NULL;
96 symconst_attr *sc_attr = &attr->data.symc_attr;
98 switch (sc_attr->num) {
99 case symconst_addr_name:
100 name = get_id_str(sc_attr->sym.ident_p);
103 case symconst_addr_ent:
104 name = get_entity_ld_name(sc_attr->sym.entity_p);
108 assert(!"Unsupported SymConst");
111 fprintf(F, "&%s ", name);
116 case dump_node_info_txt:
123 static void *firm_init(void)
125 static struct obstack obst;
126 static int inited = 0;
127 arch_isa_t *isa = xmalloc(sizeof(*isa));
130 isa->impl = &firm_isa;
138 for(k = 0; k < N_CLASSES; ++k) {
139 const arch_register_class_t *cls = ®_classes[k];
142 for(i = 0; i < cls->n_regs; ++i) {
146 arch_register_t *reg = (arch_register_t *) &cls->regs[i];
148 n = snprintf(buf, sizeof(buf), "r%d", i);
149 name = obstack_copy0(&obst, buf, n);
152 reg->reg_class = cls;
159 * Create some opcodes and types to let firm look a little
160 * bit more like real machines.
163 int push_opc = get_next_ir_opcode();
165 op_push = new_ir_op(push_opc, "Push",
166 op_pin_state_pinned, 0, oparity_binary, 0, 0, NULL);
170 int imm_opc = get_next_ir_opcode();
173 memset(&ops, 0, sizeof(ops));
174 ops.dump_node = dump_node_Imm;
176 op_imm = new_ir_op(imm_opc, "Imm",
177 op_pin_state_pinned, 0, oparity_zero, 0, sizeof(imm_attr_t), &ops);
183 static void firm_done(void *self)
188 static int firm_get_n_reg_class(const void *self)
193 static const arch_register_class_t *firm_get_reg_class(const void *self, int i)
195 assert(i >= 0 && i < N_CLASSES);
196 return ®_classes[i];
199 static const arch_register_req_t firm_std_reg_req = {
200 arch_register_req_type_normal,
201 ®_classes[CLS_DATAB],
206 static const arch_register_req_t *
207 firm_get_irn_reg_req(const void *self,
208 arch_register_req_t *req, const ir_node *irn, int pos)
210 if(is_firm_be_mode(get_irn_mode(irn)))
211 memcpy(req, &firm_std_reg_req, sizeof(*req));
218 struct irn_reg_assoc {
220 const arch_register_t *reg;
223 static int cmp_irn_reg_assoc(const void *a, const void *b, size_t len)
225 const struct irn_reg_assoc *x = a;
226 const struct irn_reg_assoc *y = b;
228 return x->irn != y->irn;
231 static struct irn_reg_assoc *get_irn_reg_assoc(const ir_node *irn)
233 static set *reg_set = NULL;
234 struct irn_reg_assoc templ;
237 reg_set = new_set(cmp_irn_reg_assoc, 1024);
242 return set_insert(reg_set, &templ, sizeof(templ), HASH_PTR(irn));
245 static void firm_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg)
247 struct irn_reg_assoc *assoc = get_irn_reg_assoc(irn);
251 static const arch_register_t *firm_get_irn_reg(const void *self, const ir_node *irn)
253 struct irn_reg_assoc *assoc = get_irn_reg_assoc(irn);
257 static arch_irn_class_t firm_classify(const void *self, const ir_node *irn)
259 arch_irn_class_t res;
261 switch(get_irn_opcode(irn)) {
264 res = arch_irn_class_branch;
267 res = arch_irn_class_call;
270 res = arch_irn_class_normal;
276 static arch_irn_flags_t firm_get_flags(const void *self, const ir_node *irn)
278 arch_irn_flags_t res = 0;
280 if(get_irn_op(irn) == op_imm)
281 res |= arch_irn_flags_rematerializable;
283 switch(get_irn_opcode(irn)) {
293 res |= arch_irn_flags_rematerializable;
301 static const arch_irn_ops_if_t firm_irn_ops_if = {
302 firm_get_irn_reg_req,
309 static const arch_irn_ops_t firm_irn_ops = {
313 static const void *firm_get_irn_ops(const arch_irn_handler_t *self,
316 return &firm_irn_ops;
319 const arch_irn_handler_t firm_irn_handler = {
323 static ir_node *new_Push(ir_graph *irg, ir_node *bl, ir_node *push, ir_node *arg)
328 return new_ir_node(NULL, irg, bl, op_push, mode_M, 2, ins);
332 * Creates an op_Imm node from an op_Const.
334 static ir_node *new_Imm(ir_graph *irg, ir_node *bl, ir_node *cnst) {
339 res = new_ir_node(NULL, irg, bl, op_imm, get_irn_mode(cnst), 0, ins);
340 attr = (imm_attr_t *) &res->attr;
342 switch (get_irn_opcode(cnst)) {
344 attr->tp = imm_Const;
345 attr->data.cnst_attr = get_irn_const_attr(cnst);
348 attr->tp = imm_SymConst;
349 attr->data.symc_attr = get_irn_symconst_attr(cnst);
354 assert(0 && "Cannot create Imm for this opcode");
360 static void prepare_walker(ir_node *irn, void *data)
362 opcode opc = get_irn_opcode(irn);
364 /* A replacement for this node has already been computed. */
365 if(get_irn_link(irn))
368 if(opc == iro_Call) {
369 ir_node *bl = get_nodes_block(irn);
370 ir_graph *irg = get_irn_irg(bl);
372 ir_node *store = get_Call_mem(irn);
373 ir_node *ptr = get_Call_ptr(irn);
374 ir_type *ct = get_Call_type(irn);
375 int np = get_Call_n_params(irn) > 0 ? 1 : 0;
381 int i, n = get_Call_n_params(irn);
383 unsigned cc = get_method_calling_convention(get_Call_type(irn));
385 if (cc & cc_last_on_top) {
386 store = new_Push(irg, bl, store, get_Call_param(irn, 0));
388 for (i = 1; i < n; ++i)
389 store = new_Push(irg, bl, store, get_Call_param(irn, i));
392 store = new_Push(irg, bl, store, get_Call_param(irn, n - 1));
394 for (i = n - 2; i >= 0; --i)
395 store = new_Push(irg, bl, store, get_Call_param(irn, i));
398 snprintf(buf, sizeof(buf), "push_%s", get_type_name(ct));
400 n = get_method_n_ress(ct);
401 nt = new_type_method(new_id_from_str(buf), 0, n);
402 for(i = 0; i < n; ++i)
403 set_method_res_type(nt, i, get_method_res_type(ct, i));
405 nc = new_r_Call(irg, bl, store, ptr, 0, ins, nt);
407 set_irn_link(nc, nc);
412 static void localize_const_walker(ir_node *irn, void *data)
416 ir_node *bl = get_nodes_block(irn);
418 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
419 ir_node *op = get_irn_n(irn, i);
420 opcode opc = get_irn_opcode(op);
423 || opc == iro_Unknown
424 || (opc == iro_SymConst /*&& get_SymConst_kind(op) == symconst_addr_ent*/)) {
425 ir_graph *irg = get_irn_irg(bl);
426 ir_node *imm_bl = is_Phi(irn) ? get_Block_cfgpred_block(bl, i) : bl;
428 ir_node *imm = new_Imm(irg, imm_bl, op);
429 set_irn_n(irn, i, imm);
435 static const arch_irn_handler_t *firm_get_irn_handler(const void *self)
437 return &firm_irn_handler;
440 typedef struct _firm_code_gen_t {
441 const arch_code_generator_if_t *impl;
446 static void clear_link(ir_node *irn, void *data)
448 set_irn_link(irn, NULL);
451 static void firm_prepare_graph(void *self)
453 firm_code_gen_t *cg = self;
455 irg_walk_graph(cg->irg, clear_link, localize_const_walker, NULL);
456 irg_walk_graph(cg->irg, NULL, prepare_walker, NULL);
459 static void firm_before_sched(void *self)
463 static void imm_scheduler(ir_node *irn, void *env) {
466 ir_node *user, *user_block, *before, *tgt_block;
468 if (1 != get_irn_n_edges(irn)) {
469 printf("Out edges: %d\n", get_irn_n_edges(irn));
470 assert(1 == get_irn_n_edges(irn));
473 e = get_irn_out_edge_first(irn);
475 user_block = get_nodes_block(user);
477 before = get_Block_cfgpred_block(user_block, e->pos);
481 tgt_block = user_block;
485 set_nodes_block(irn, tgt_block);
486 sched_add_before(before, irn);
490 static void firm_before_ra(void *self)
492 firm_code_gen_t *cg = self;
493 irg_walk_graph(cg->irg, imm_scheduler, NULL, NULL);
496 static const arch_register_t *firm_get_stack_register(void *self) {
497 return &datab_regs[0];
500 static void firm_codegen_done(void *self)
505 static void *firm_cg_init(FILE *file_handle, ir_graph *irg, const arch_env_t *env);
507 static const arch_code_generator_if_t firm_code_gen_if = {
512 NULL, /* lower spill */
513 NULL, /* lower reload */
514 firm_get_stack_register,
518 static void *firm_cg_init(FILE *file_handle, ir_graph *irg, const arch_env_t *env)
520 firm_code_gen_t *cg = xmalloc(sizeof(*cg));
521 cg->impl = &firm_code_gen_if;
527 static const arch_code_generator_if_t *firm_get_code_generator_if(void *self)
529 return &firm_code_gen_if;
532 static const list_sched_selector_t *firm_get_list_sched_selector(const void *self) {
533 return trivial_selector;
536 static long firm_handle_call_proj(const void *self, ir_node *proj, int is_keep) {
537 return get_Proj_proj(proj);
541 static void firm_register_options(lc_opt_entry_t *ent)
546 const arch_isa_if_t firm_isa = {
548 firm_register_options,
552 firm_get_n_reg_class,
554 firm_get_irn_handler,
555 firm_get_code_generator_if,
556 firm_get_list_sched_selector,
557 firm_handle_call_proj