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"
32 enum { imm_Const, imm_SymConst } tp;
35 symconst_attr symc_attr;
39 static arch_register_t datab_regs[N_REGS];
41 static arch_register_class_t reg_classes[] = {
42 { "datab", N_REGS, NULL, datab_regs },
45 static ir_op *op_push;
48 const arch_isa_if_t firm_isa;
51 (sizeof(reg_classes) / sizeof(reg_classes[0]))
55 tarval *get_Imm_tv(ir_node *n) {
56 imm_attr_t *attr = (imm_attr_t *)get_irn_generic_attr(n);
57 return attr->tp == imm_Const ? attr->data.cnst_attr.tv : NULL;
60 int is_Imm(const ir_node *irn) {
61 return get_irn_op(irn) == op_imm;
64 static int dump_node_Imm(ir_node *n, FILE *F, dump_reason_t reason) {
72 case dump_node_opcode_txt:
76 tarval_snprintf(buf, sizeof(buf), tv);
77 fprintf(F, "%s", buf);
80 fprintf(F, "immSymC");
84 case dump_node_mode_txt:
85 mode = get_irn_mode(n);
87 if (mode && mode != mode_BB && mode != mode_ANY && mode != mode_BAD && mode != mode_T) {
88 fprintf(F, "[%s]", get_mode_name(mode));
92 case dump_node_nodeattr_txt:
93 attr = (imm_attr_t *)get_irn_generic_attr(n);
95 if (is_Imm(n) && attr->tp == imm_SymConst) {
96 const char *name = NULL;
97 symconst_attr *sc_attr = &attr->data.symc_attr;
99 switch (sc_attr->num) {
100 case symconst_addr_name:
101 name = get_id_str(sc_attr->sym.ident_p);
104 case symconst_addr_ent:
105 name = get_entity_ld_name(sc_attr->sym.entity_p);
109 assert(!"Unsupported SymConst");
112 fprintf(F, "&%s ", name);
117 case dump_node_info_txt:
124 static void *firm_init(void)
126 static struct obstack obst;
127 static int inited = 0;
128 arch_isa_t *isa = xmalloc(sizeof(*isa));
131 isa->impl = &firm_isa;
139 for(k = 0; k < N_CLASSES; ++k) {
140 arch_register_class_t *cls = ®_classes[k];
144 for(i = 0; i < cls->n_regs; ++i) {
148 arch_register_t *reg = (arch_register_t *) &cls->regs[i];
150 n = snprintf(buf, sizeof(buf), "r%d", i);
151 name = obstack_copy0(&obst, buf, n);
154 reg->reg_class = cls;
161 * Create some opcodes and types to let firm look a little
162 * bit more like real machines.
165 int push_opc = get_next_ir_opcode();
167 op_push = new_ir_op(push_opc, "Push",
168 op_pin_state_pinned, 0, oparity_binary, 0, 0, NULL);
172 int imm_opc = get_next_ir_opcode();
175 memset(&ops, 0, sizeof(ops));
176 ops.dump_node = dump_node_Imm;
178 op_imm = new_ir_op(imm_opc, "Imm",
179 op_pin_state_pinned, 0, oparity_zero, 0, sizeof(imm_attr_t), &ops);
185 static void firm_done(void *self)
190 static int firm_get_n_reg_class(const void *self)
195 static const arch_register_class_t *firm_get_reg_class(const void *self, int i)
197 assert(i >= 0 && i < N_CLASSES);
198 return ®_classes[i];
201 static const arch_register_class_t *firm_get_reg_class_for_mode(const void *self, const ir_mode *irm)
203 return mode_is_datab(irm) ? ®_classes[CLS_DATAB] : NULL;
206 static void firm_get_call_abi(const void *self, ir_type *method_type, be_abi_call_t *abi)
208 const arch_register_class_t *cls = ®_classes[CLS_DATAB];
211 for(i = 0, n = get_method_n_params(method_type); i < n; ++i) {
212 ir_type *t = get_method_param_type(method_type, i);
213 if(is_Primitive_type(t))
214 be_abi_call_param_reg(abi, i, &cls->regs[i]);
216 be_abi_call_param_stack(abi, i);
219 for(i = 0, n = get_method_n_ress(method_type); i < n; ++i) {
220 ir_type *t = get_method_res_type(method_type, i);
221 if(is_Primitive_type(t))
222 be_abi_call_res_reg(abi, i, &cls->regs[i]);
225 be_abi_call_set_flags(abi, BE_ABI_NONE, 0);
229 static const arch_register_req_t firm_std_reg_req = {
230 arch_register_req_type_normal,
231 ®_classes[CLS_DATAB],
236 static const arch_register_req_t *
237 firm_get_irn_reg_req(const void *self,
238 arch_register_req_t *req, const ir_node *irn, int pos)
240 if(is_firm_be_mode(get_irn_mode(irn)))
241 memcpy(req, &firm_std_reg_req, sizeof(*req));
248 struct irn_reg_assoc {
250 const arch_register_t *reg;
253 static int cmp_irn_reg_assoc(const void *a, const void *b, size_t len)
255 const struct irn_reg_assoc *x = a;
256 const struct irn_reg_assoc *y = b;
258 return x->irn != y->irn;
261 static struct irn_reg_assoc *get_irn_reg_assoc(const ir_node *irn)
263 static set *reg_set = NULL;
264 struct irn_reg_assoc templ;
267 reg_set = new_set(cmp_irn_reg_assoc, 1024);
272 return set_insert(reg_set, &templ, sizeof(templ), HASH_PTR(irn));
275 static void firm_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg)
277 struct irn_reg_assoc *assoc = get_irn_reg_assoc(irn);
281 static const arch_register_t *firm_get_irn_reg(const void *self, const ir_node *irn)
283 struct irn_reg_assoc *assoc = get_irn_reg_assoc(irn);
287 static arch_irn_class_t firm_classify(const void *self, const ir_node *irn)
289 arch_irn_class_t res;
291 switch(get_irn_opcode(irn)) {
294 res = arch_irn_class_branch;
297 res = arch_irn_class_call;
300 res = arch_irn_class_normal;
306 static arch_irn_flags_t firm_get_flags(const void *self, const ir_node *irn)
308 arch_irn_flags_t res = 0;
310 if(get_irn_op(irn) == op_imm)
311 res |= arch_irn_flags_rematerializable;
313 switch(get_irn_opcode(irn)) {
323 res |= arch_irn_flags_rematerializable;
331 static void firm_set_stack_bias(const void *self, ir_node *irn, int bias) {
334 static const arch_irn_ops_if_t firm_irn_ops_if = {
335 firm_get_irn_reg_req,
343 static const arch_irn_ops_t firm_irn_ops = {
347 static const void *firm_get_irn_ops(const arch_irn_handler_t *self,
350 return &firm_irn_ops;
353 const arch_irn_handler_t firm_irn_handler = {
357 static ir_node *new_Push(ir_graph *irg, ir_node *bl, ir_node *push, ir_node *arg)
362 return new_ir_node(NULL, irg, bl, op_push, mode_M, 2, ins);
366 * Creates an op_Imm node from an op_Const.
368 static ir_node *new_Imm(ir_graph *irg, ir_node *bl, ir_node *cnst) {
373 res = new_ir_node(NULL, irg, bl, op_imm, get_irn_mode(cnst), 0, ins);
374 attr = (imm_attr_t *) &res->attr;
376 switch (get_irn_opcode(cnst)) {
378 attr->tp = imm_Const;
379 attr->data.cnst_attr = get_irn_const_attr(cnst);
382 attr->tp = imm_SymConst;
383 attr->data.symc_attr = get_irn_symconst_attr(cnst);
388 assert(0 && "Cannot create Imm for this opcode");
394 static void prepare_walker(ir_node *irn, void *data)
396 opcode opc = get_irn_opcode(irn);
398 /* A replacement for this node has already been computed. */
399 if(get_irn_link(irn))
402 if(opc == iro_Call) {
403 ir_node *bl = get_nodes_block(irn);
404 ir_graph *irg = get_irn_irg(bl);
406 ir_node *store = get_Call_mem(irn);
407 ir_node *ptr = get_Call_ptr(irn);
408 ir_type *ct = get_Call_type(irn);
409 int np = get_Call_n_params(irn) > 0 ? 1 : 0;
415 int i, n = get_Call_n_params(irn);
417 unsigned cc = get_method_calling_convention(get_Call_type(irn));
419 if (cc & cc_last_on_top) {
420 store = new_Push(irg, bl, store, get_Call_param(irn, 0));
422 for (i = 1; i < n; ++i)
423 store = new_Push(irg, bl, store, get_Call_param(irn, i));
426 store = new_Push(irg, bl, store, get_Call_param(irn, n - 1));
428 for (i = n - 2; i >= 0; --i)
429 store = new_Push(irg, bl, store, get_Call_param(irn, i));
432 snprintf(buf, sizeof(buf), "push_%s", get_type_name(ct));
434 n = get_method_n_ress(ct);
435 nt = new_type_method(new_id_from_str(buf), 0, n);
436 for(i = 0; i < n; ++i)
437 set_method_res_type(nt, i, get_method_res_type(ct, i));
439 nc = new_r_Call(irg, bl, store, ptr, 0, ins, nt);
441 set_irn_link(nc, nc);
446 static void localize_const_walker(ir_node *irn, void *data)
450 ir_node *bl = get_nodes_block(irn);
452 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
453 ir_node *op = get_irn_n(irn, i);
454 opcode opc = get_irn_opcode(op);
457 || opc == iro_Unknown
458 || (opc == iro_SymConst /*&& get_SymConst_kind(op) == symconst_addr_ent*/)) {
459 ir_graph *irg = get_irn_irg(bl);
460 ir_node *imm_bl = is_Phi(irn) ? get_Block_cfgpred_block(bl, i) : bl;
462 ir_node *imm = new_Imm(irg, imm_bl, op);
463 set_irn_n(irn, i, imm);
469 static const arch_irn_handler_t *firm_get_irn_handler(const void *self)
471 return &firm_irn_handler;
474 typedef struct _firm_code_gen_t {
475 const arch_code_generator_if_t *impl;
480 static void clear_link(ir_node *irn, void *data)
482 set_irn_link(irn, NULL);
485 static void firm_prepare_graph(void *self)
487 firm_code_gen_t *cg = self;
489 irg_walk_graph(cg->irg, clear_link, localize_const_walker, NULL);
490 irg_walk_graph(cg->irg, NULL, prepare_walker, NULL);
493 static void firm_before_sched(void *self)
497 static void imm_scheduler(ir_node *irn, void *env) {
500 ir_node *user, *user_block, *before, *tgt_block;
502 if (1 != get_irn_n_edges(irn)) {
503 printf("Out edges: %d\n", get_irn_n_edges(irn));
504 assert(1 == get_irn_n_edges(irn));
507 e = get_irn_out_edge_first(irn);
509 user_block = get_nodes_block(user);
511 before = get_Block_cfgpred_block(user_block, e->pos);
515 tgt_block = user_block;
519 set_nodes_block(irn, tgt_block);
520 sched_add_before(before, irn);
524 static void firm_before_ra(void *self)
526 firm_code_gen_t *cg = self;
527 irg_walk_graph(cg->irg, imm_scheduler, NULL, NULL);
530 static void firm_codegen_done(void *self)
535 static void *firm_cg_init(FILE *file_handle, ir_graph *irg, const arch_env_t *env);
537 static const arch_code_generator_if_t firm_code_gen_if = {
542 NULL, /* lower spill */
543 NULL, /* lower reload */
547 static void *firm_cg_init(FILE *file_handle, ir_graph *irg, const arch_env_t *env)
549 firm_code_gen_t *cg = xmalloc(sizeof(*cg));
550 cg->impl = &firm_code_gen_if;
556 static const arch_code_generator_if_t *firm_get_code_generator_if(void *self)
558 return &firm_code_gen_if;
561 static const list_sched_selector_t *firm_get_list_sched_selector(const void *self) {
562 return trivial_selector;
565 static long firm_handle_call_proj(const void *self, ir_node *proj, int is_keep) {
566 return get_Proj_proj(proj);
570 static void firm_register_options(lc_opt_entry_t *ent)
575 const arch_isa_if_t firm_isa = {
577 firm_register_options,
581 firm_get_n_reg_class,
583 firm_get_reg_class_for_mode,
585 firm_get_irn_handler,
586 firm_get_code_generator_if,
587 firm_get_list_sched_selector,
588 firm_handle_call_proj