3 * ISA implementation for Firm IR nodes.
10 #include <libcore/lc_opts.h>
18 #include "iredges_t.h"
25 #include "../bearch.h"
26 #include "../besched.h"
27 #include "../beutil.h"
33 enum { imm_Const, imm_SymConst } tp;
36 symconst_attr symc_attr;
40 static arch_register_t datab_regs[N_REGS];
42 static arch_register_class_t reg_classes[] = {
43 { "datab", N_REGS, NULL, datab_regs },
46 static ir_op *op_push;
49 const arch_isa_if_t firm_isa;
52 (sizeof(reg_classes) / sizeof(reg_classes[0]))
56 tarval *get_Imm_tv(ir_node *n) {
57 imm_attr_t *attr = (imm_attr_t *)get_irn_generic_attr(n);
58 return attr->tp == imm_Const ? attr->data.cnst_attr.tv : NULL;
61 int is_Imm(const ir_node *irn) {
62 return get_irn_op(irn) == op_imm;
65 static int dump_node_Imm(ir_node *n, FILE *F, dump_reason_t reason) {
73 case dump_node_opcode_txt:
77 tarval_snprintf(buf, sizeof(buf), tv);
78 fprintf(F, "%s", buf);
81 fprintf(F, "immSymC");
85 case dump_node_mode_txt:
86 mode = get_irn_mode(n);
88 if (mode && mode != mode_BB && mode != mode_ANY && mode != mode_BAD && mode != mode_T) {
89 fprintf(F, "[%s]", get_mode_name(mode));
93 case dump_node_nodeattr_txt:
94 attr = (imm_attr_t *)get_irn_generic_attr(n);
96 if (is_Imm(n) && attr->tp == imm_SymConst) {
97 const char *name = NULL;
98 symconst_attr *sc_attr = &attr->data.symc_attr;
100 switch (sc_attr->num) {
101 case symconst_addr_name:
102 name = get_id_str(sc_attr->sym.ident_p);
105 case symconst_addr_ent:
106 name = get_entity_ld_name(sc_attr->sym.entity_p);
110 assert(!"Unsupported SymConst");
113 fprintf(F, "&%s ", name);
118 case dump_node_info_txt:
125 static void *firm_init(FILE *outfile)
127 static struct obstack obst;
128 static int inited = 0;
129 arch_isa_t *isa = xmalloc(sizeof(*isa));
132 isa->impl = &firm_isa;
140 for(k = 0; k < N_CLASSES; ++k) {
141 arch_register_class_t *cls = ®_classes[k];
145 for(i = 0; i < cls->n_regs; ++i) {
149 arch_register_t *reg = (arch_register_t *) &cls->regs[i];
151 n = snprintf(buf, sizeof(buf), "r%d", i);
152 name = obstack_copy0(&obst, buf, n);
155 reg->reg_class = cls;
162 * Create some opcodes and types to let firm look a little
163 * bit more like real machines.
166 int push_opc = get_next_ir_opcode();
168 op_push = new_ir_op(push_opc, "Push",
169 op_pin_state_pinned, 0, oparity_binary, 0, 0, NULL);
173 int imm_opc = get_next_ir_opcode();
176 memset(&ops, 0, sizeof(ops));
177 ops.dump_node = dump_node_Imm;
179 op_imm = new_ir_op(imm_opc, "Imm",
180 op_pin_state_pinned, 0, oparity_zero, 0, sizeof(imm_attr_t), &ops);
186 static void firm_done(void *self)
191 static int firm_get_n_reg_class(const void *self)
196 static const arch_register_class_t *firm_get_reg_class(const void *self, int i)
198 assert(i >= 0 && i < N_CLASSES);
199 return ®_classes[i];
202 static const arch_register_class_t *firm_get_reg_class_for_mode(const void *self, const ir_mode *irm)
204 return mode_is_datab(irm) ? ®_classes[CLS_DATAB] : NULL;
207 static ir_type *firm_abi_get_between_type(void *self) {
208 static ir_type *between_type = NULL;
211 between_type = new_type_class(new_id_from_str("firm_be_between"));
212 set_type_size_bytes(between_type, 0);
218 static const be_abi_callbacks_t firm_abi_callbacks = {
221 firm_abi_get_between_type,
227 static void firm_get_call_abi(const void *self, ir_type *method_type, be_abi_call_t *abi)
229 const arch_register_class_t *cls = ®_classes[CLS_DATAB];
231 be_abi_call_flags_t flags = { { 0, 0, 0, 0, 0 } };
234 for(i = 0, n = get_method_n_params(method_type); i < n; ++i) {
235 ir_type *t = get_method_param_type(method_type, i);
236 if(is_Primitive_type(t))
237 be_abi_call_param_reg(abi, i, &cls->regs[i]);
239 be_abi_call_param_stack(abi, i, 1, 0, 0);
242 for(i = 0, n = get_method_n_ress(method_type); i < n; ++i) {
243 ir_type *t = get_method_res_type(method_type, i);
244 if(is_Primitive_type(t))
245 be_abi_call_res_reg(abi, i, &cls->regs[i]);
249 be_abi_call_set_flags(abi, flags, &firm_abi_callbacks);
253 static const arch_register_req_t firm_std_reg_req = {
254 arch_register_req_type_normal,
255 ®_classes[CLS_DATAB],
260 static const arch_register_req_t *
261 firm_get_irn_reg_req(const void *self,
262 arch_register_req_t *req, const ir_node *irn, int pos)
264 if(is_firm_be_mode(get_irn_mode(irn)))
265 memcpy(req, &firm_std_reg_req, sizeof(*req));
272 struct irn_reg_assoc {
274 const arch_register_t *reg;
277 static int cmp_irn_reg_assoc(const void *a, const void *b, size_t len)
279 const struct irn_reg_assoc *x = a;
280 const struct irn_reg_assoc *y = b;
282 return x->irn != y->irn;
285 static struct irn_reg_assoc *get_irn_reg_assoc(const ir_node *irn)
287 static set *reg_set = NULL;
288 struct irn_reg_assoc templ;
291 reg_set = new_set(cmp_irn_reg_assoc, 1024);
296 return set_insert(reg_set, &templ, sizeof(templ), HASH_PTR(irn));
299 static void firm_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg)
301 struct irn_reg_assoc *assoc = get_irn_reg_assoc(irn);
305 static const arch_register_t *firm_get_irn_reg(const void *self, const ir_node *irn)
307 struct irn_reg_assoc *assoc = get_irn_reg_assoc(irn);
311 static arch_irn_class_t firm_classify(const void *self, const ir_node *irn)
313 arch_irn_class_t res;
315 switch(get_irn_opcode(irn)) {
318 res = arch_irn_class_branch;
321 res = arch_irn_class_call;
324 res = arch_irn_class_normal;
330 static arch_irn_flags_t firm_get_flags(const void *self, const ir_node *irn)
332 arch_irn_flags_t res = 0;
334 if(get_irn_op(irn) == op_imm)
335 res |= arch_irn_flags_rematerializable;
337 switch(get_irn_opcode(irn)) {
347 res |= arch_irn_flags_rematerializable;
355 static void firm_set_stack_bias(const void *self, ir_node *irn, int bias)
359 static entity *firm_get_frame_entity(const void *self, const ir_node *irn)
364 static const arch_irn_ops_if_t firm_irn_ops_if = {
365 firm_get_irn_reg_req,
370 firm_get_frame_entity,
374 static const arch_irn_ops_t firm_irn_ops = {
378 static const void *firm_get_irn_ops(const arch_irn_handler_t *self,
381 return &firm_irn_ops;
384 const arch_irn_handler_t firm_irn_handler = {
388 static ir_node *new_Push(ir_graph *irg, ir_node *bl, ir_node *push, ir_node *arg)
393 return new_ir_node(NULL, irg, bl, op_push, mode_M, 2, ins);
397 * Creates an op_Imm node from an op_Const.
399 static ir_node *new_Imm(ir_graph *irg, ir_node *bl, ir_node *cnst) {
404 res = new_ir_node(NULL, irg, bl, op_imm, get_irn_mode(cnst), 0, ins);
405 attr = (imm_attr_t *) &res->attr;
407 switch (get_irn_opcode(cnst)) {
409 attr->tp = imm_Const;
410 attr->data.cnst_attr = get_irn_const_attr(cnst);
413 attr->tp = imm_SymConst;
414 attr->data.symc_attr = get_irn_symconst_attr(cnst);
419 assert(0 && "Cannot create Imm for this opcode");
425 static void prepare_walker(ir_node *irn, void *data)
427 opcode opc = get_irn_opcode(irn);
429 /* A replacement for this node has already been computed. */
430 if(get_irn_link(irn))
433 if(opc == iro_Call) {
434 ir_node *bl = get_nodes_block(irn);
435 ir_graph *irg = get_irn_irg(bl);
437 ir_node *store = get_Call_mem(irn);
438 ir_node *ptr = get_Call_ptr(irn);
439 ir_type *ct = get_Call_type(irn);
440 int np = get_Call_n_params(irn) > 0 ? 1 : 0;
446 int i, n = get_Call_n_params(irn);
448 unsigned cc = get_method_calling_convention(get_Call_type(irn));
450 if (cc & cc_last_on_top) {
451 store = new_Push(irg, bl, store, get_Call_param(irn, 0));
453 for (i = 1; i < n; ++i)
454 store = new_Push(irg, bl, store, get_Call_param(irn, i));
457 store = new_Push(irg, bl, store, get_Call_param(irn, n - 1));
459 for (i = n - 2; i >= 0; --i)
460 store = new_Push(irg, bl, store, get_Call_param(irn, i));
463 snprintf(buf, sizeof(buf), "push_%s", get_type_name(ct));
465 n = get_method_n_ress(ct);
466 nt = new_type_method(new_id_from_str(buf), 0, n);
467 for(i = 0; i < n; ++i)
468 set_method_res_type(nt, i, get_method_res_type(ct, i));
470 nc = new_r_Call(irg, bl, store, ptr, 0, ins, nt);
472 set_irn_link(nc, nc);
477 static void localize_const_walker(ir_node *irn, void *data)
481 ir_node *bl = get_nodes_block(irn);
483 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
484 ir_node *op = get_irn_n(irn, i);
485 opcode opc = get_irn_opcode(op);
488 || opc == iro_Unknown
489 || (opc == iro_SymConst /*&& get_SymConst_kind(op) == symconst_addr_ent*/)) {
490 ir_graph *irg = get_irn_irg(bl);
491 ir_node *imm_bl = is_Phi(irn) ? get_Block_cfgpred_block(bl, i) : bl;
493 ir_node *imm = new_Imm(irg, imm_bl, op);
494 set_irn_n(irn, i, imm);
500 static const arch_irn_handler_t *firm_get_irn_handler(const void *self)
502 return &firm_irn_handler;
505 typedef struct _firm_code_gen_t {
506 const arch_code_generator_if_t *impl;
511 static void clear_link(ir_node *irn, void *data)
513 set_irn_link(irn, NULL);
516 static void firm_prepare_graph(void *self)
518 firm_code_gen_t *cg = self;
520 irg_walk_graph(cg->irg, clear_link, localize_const_walker, NULL);
521 irg_walk_graph(cg->irg, NULL, prepare_walker, NULL);
524 static void firm_before_sched(void *self)
528 static void imm_scheduler(ir_node *irn, void *env) {
531 ir_node *user, *user_block, *before, *tgt_block;
533 if (1 != get_irn_n_edges(irn)) {
534 printf("Out edges: %d\n", get_irn_n_edges(irn));
535 assert(1 == get_irn_n_edges(irn));
538 e = get_irn_out_edge_first(irn);
540 user_block = get_nodes_block(user);
542 before = get_Block_cfgpred_block(user_block, e->pos);
546 tgt_block = user_block;
550 set_nodes_block(irn, tgt_block);
551 sched_add_before(before, irn);
555 static void firm_before_ra(void *self)
557 firm_code_gen_t *cg = self;
558 irg_walk_graph(cg->irg, imm_scheduler, NULL, NULL);
561 static void firm_after_ra(void *self)
565 static void firm_codegen_done(void *self)
570 static void *firm_cg_init(const be_irg_t *birg);
572 static const arch_code_generator_if_t firm_code_gen_if = {
582 static void *firm_cg_init(const be_irg_t *birg)
584 firm_code_gen_t *cg = xmalloc(sizeof(*cg));
585 cg->impl = &firm_code_gen_if;
591 static const arch_code_generator_if_t *firm_get_code_generator_if(void *self)
593 return &firm_code_gen_if;
596 static const list_sched_selector_t *firm_get_list_sched_selector(const void *self) {
597 return trivial_selector;
601 * Returns the necessary byte alignment for storing a register of given class.
603 static int firm_get_reg_class_alignment(const void *self, const arch_register_class_t *cls) {
604 ir_mode *mode = arch_register_class_mode(cls);
605 return get_mode_size_bytes(mode);
609 static void firm_register_options(lc_opt_entry_t *ent)
614 const arch_isa_if_t firm_isa = {
617 firm_get_n_reg_class,
619 firm_get_reg_class_for_mode,
621 firm_get_irn_handler,
622 firm_get_code_generator_if,
623 firm_get_list_sched_selector,
624 firm_get_reg_class_alignment,
626 firm_register_options,