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);
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 const arch_irn_ops_if_t firm_irn_ops_if = {
332 firm_get_irn_reg_req,
339 static const arch_irn_ops_t firm_irn_ops = {
343 static const void *firm_get_irn_ops(const arch_irn_handler_t *self,
346 return &firm_irn_ops;
349 const arch_irn_handler_t firm_irn_handler = {
353 static ir_node *new_Push(ir_graph *irg, ir_node *bl, ir_node *push, ir_node *arg)
358 return new_ir_node(NULL, irg, bl, op_push, mode_M, 2, ins);
362 * Creates an op_Imm node from an op_Const.
364 static ir_node *new_Imm(ir_graph *irg, ir_node *bl, ir_node *cnst) {
369 res = new_ir_node(NULL, irg, bl, op_imm, get_irn_mode(cnst), 0, ins);
370 attr = (imm_attr_t *) &res->attr;
372 switch (get_irn_opcode(cnst)) {
374 attr->tp = imm_Const;
375 attr->data.cnst_attr = get_irn_const_attr(cnst);
378 attr->tp = imm_SymConst;
379 attr->data.symc_attr = get_irn_symconst_attr(cnst);
384 assert(0 && "Cannot create Imm for this opcode");
390 static void prepare_walker(ir_node *irn, void *data)
392 opcode opc = get_irn_opcode(irn);
394 /* A replacement for this node has already been computed. */
395 if(get_irn_link(irn))
398 if(opc == iro_Call) {
399 ir_node *bl = get_nodes_block(irn);
400 ir_graph *irg = get_irn_irg(bl);
402 ir_node *store = get_Call_mem(irn);
403 ir_node *ptr = get_Call_ptr(irn);
404 ir_type *ct = get_Call_type(irn);
405 int np = get_Call_n_params(irn) > 0 ? 1 : 0;
411 int i, n = get_Call_n_params(irn);
413 unsigned cc = get_method_calling_convention(get_Call_type(irn));
415 if (cc & cc_last_on_top) {
416 store = new_Push(irg, bl, store, get_Call_param(irn, 0));
418 for (i = 1; i < n; ++i)
419 store = new_Push(irg, bl, store, get_Call_param(irn, i));
422 store = new_Push(irg, bl, store, get_Call_param(irn, n - 1));
424 for (i = n - 2; i >= 0; --i)
425 store = new_Push(irg, bl, store, get_Call_param(irn, i));
428 snprintf(buf, sizeof(buf), "push_%s", get_type_name(ct));
430 n = get_method_n_ress(ct);
431 nt = new_type_method(new_id_from_str(buf), 0, n);
432 for(i = 0; i < n; ++i)
433 set_method_res_type(nt, i, get_method_res_type(ct, i));
435 nc = new_r_Call(irg, bl, store, ptr, 0, ins, nt);
437 set_irn_link(nc, nc);
442 static void localize_const_walker(ir_node *irn, void *data)
446 ir_node *bl = get_nodes_block(irn);
448 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
449 ir_node *op = get_irn_n(irn, i);
450 opcode opc = get_irn_opcode(op);
453 || opc == iro_Unknown
454 || (opc == iro_SymConst /*&& get_SymConst_kind(op) == symconst_addr_ent*/)) {
455 ir_graph *irg = get_irn_irg(bl);
456 ir_node *imm_bl = is_Phi(irn) ? get_Block_cfgpred_block(bl, i) : bl;
458 ir_node *imm = new_Imm(irg, imm_bl, op);
459 set_irn_n(irn, i, imm);
465 static const arch_irn_handler_t *firm_get_irn_handler(const void *self)
467 return &firm_irn_handler;
470 typedef struct _firm_code_gen_t {
471 const arch_code_generator_if_t *impl;
476 static void clear_link(ir_node *irn, void *data)
478 set_irn_link(irn, NULL);
481 static void firm_prepare_graph(void *self)
483 firm_code_gen_t *cg = self;
485 irg_walk_graph(cg->irg, clear_link, localize_const_walker, NULL);
486 irg_walk_graph(cg->irg, NULL, prepare_walker, NULL);
489 static void firm_before_sched(void *self)
493 static void imm_scheduler(ir_node *irn, void *env) {
496 ir_node *user, *user_block, *before, *tgt_block;
498 if (1 != get_irn_n_edges(irn)) {
499 printf("Out edges: %d\n", get_irn_n_edges(irn));
500 assert(1 == get_irn_n_edges(irn));
503 e = get_irn_out_edge_first(irn);
505 user_block = get_nodes_block(user);
507 before = get_Block_cfgpred_block(user_block, e->pos);
511 tgt_block = user_block;
515 set_nodes_block(irn, tgt_block);
516 sched_add_before(before, irn);
520 static void firm_before_ra(void *self)
522 firm_code_gen_t *cg = self;
523 irg_walk_graph(cg->irg, imm_scheduler, NULL, NULL);
526 static const arch_register_t *firm_get_stack_register(void *self) {
527 return &datab_regs[0];
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 */
544 firm_get_stack_register,
548 static void *firm_cg_init(FILE *file_handle, ir_graph *irg, const arch_env_t *env)
550 firm_code_gen_t *cg = xmalloc(sizeof(*cg));
551 cg->impl = &firm_code_gen_if;
557 static const arch_code_generator_if_t *firm_get_code_generator_if(void *self)
559 return &firm_code_gen_if;
562 static const list_sched_selector_t *firm_get_list_sched_selector(const void *self) {
563 return trivial_selector;
566 static long firm_handle_call_proj(const void *self, ir_node *proj, int is_keep) {
567 return get_Proj_proj(proj);
571 static void firm_register_options(lc_opt_entry_t *ent)
576 const arch_isa_if_t firm_isa = {
578 firm_register_options,
582 firm_get_n_reg_class,
584 firm_get_reg_class_for_mode,
586 firm_get_irn_handler,
587 firm_get_code_generator_if,
588 firm_get_list_sched_selector,
589 firm_handle_call_proj