added function to retireve ilp scheduler interface
[libfirm] / ir / be / firm / bearch_firm.c
1
2 /**
3  * ISA implementation for Firm IR nodes.
4  */
5 #ifdef HAVE_CONFIG_H
6 #include "config.h"
7 #endif
8
9 #ifdef WITH_LIBCORE
10 #include <libcore/lc_opts.h>
11 #endif
12
13 #include "bitset.h"
14 #include "obst.h"
15
16 #include "irmode_t.h"
17 #include "irnode_t.h"
18 #include "iredges_t.h"
19 #include "irgmod.h"
20 #include "ircons_t.h"
21 #include "irgwalk.h"
22 #include "type.h"
23 #include "irtools.h"
24
25 #include "../be_t.h"
26 #include "../bearch.h"
27 #include "../besched.h"
28 #include "../beutil.h"
29 #include "../beabi.h"
30
31 #define N_REGS 3
32
33 typedef struct {
34   enum  { imm_Const, imm_SymConst } tp;
35   union {
36     const_attr    cnst_attr;
37     symconst_attr symc_attr;
38   } data;
39 } imm_attr_t;
40
41 static arch_register_t datab_regs[N_REGS];
42
43 static arch_register_class_t reg_classes[] = {
44   { "datab", N_REGS, NULL, datab_regs },
45 };
46
47 static ir_op *op_push;
48 static ir_op *op_imm;
49
50 const arch_isa_if_t firm_isa;
51
52 #define N_CLASSES \
53   (sizeof(reg_classes) / sizeof(reg_classes[0]))
54
55 #define CLS_DATAB 0
56
57 tarval *get_Imm_tv(ir_node *n) {
58   imm_attr_t *attr = (imm_attr_t *)get_irn_generic_attr(n);
59   return attr->tp == imm_Const ? attr->data.cnst_attr.tv : NULL;
60 }
61
62 int is_Imm(const ir_node *irn) {
63   return get_irn_op(irn) == op_imm;
64 }
65
66 static int dump_node_Imm(ir_node *n, FILE *F, dump_reason_t reason) {
67   ir_mode    *mode;
68   int        bad = 0;
69   char       buf[1024];
70   tarval     *tv;
71   imm_attr_t *attr;
72
73   switch (reason) {
74     case dump_node_opcode_txt:
75       tv = get_Imm_tv(n);
76
77       if (tv) {
78         tarval_snprintf(buf, sizeof(buf), tv);
79         fprintf(F, "%s", buf);
80       }
81       else {
82         fprintf(F, "immSymC");
83       }
84       break;
85
86     case dump_node_mode_txt:
87       mode = get_irn_mode(n);
88
89       if (mode && mode != mode_BB && mode != mode_ANY && mode != mode_BAD && mode != mode_T) {
90         fprintf(F, "[%s]", get_mode_name(mode));
91       }
92       break;
93
94     case dump_node_nodeattr_txt:
95       attr = (imm_attr_t *)get_irn_generic_attr(n);
96
97       if (is_Imm(n) && attr->tp == imm_SymConst) {
98         const char *name    = NULL;
99         symconst_attr *sc_attr = &attr->data.symc_attr;
100
101         switch (sc_attr->num) {
102           case symconst_addr_name:
103             name = get_id_str(sc_attr->sym.ident_p);
104             break;
105
106           case symconst_addr_ent:
107             name = get_entity_ld_name(sc_attr->sym.entity_p);
108             break;
109
110           default:
111             assert(!"Unsupported SymConst");
112         }
113
114         fprintf(F, "&%s ", name);
115       }
116
117       break;
118
119     case dump_node_info_txt:
120       break;
121   }
122
123   return bad;
124 }
125
126 static void *firm_init(FILE *outfile)
127 {
128   static struct obstack obst;
129   static int inited = 0;
130   arch_isa_t *isa = xmalloc(sizeof(*isa));
131   int k;
132
133   isa->impl = &firm_isa;
134
135   if(inited)
136     return NULL;
137
138   inited = 1;
139   obstack_init(&obst);
140
141   for(k = 0; k < N_CLASSES; ++k) {
142     arch_register_class_t *cls = &reg_classes[k];
143     int i;
144
145     cls->mode = mode_Is;
146     for(i = 0; i < cls->n_regs; ++i) {
147       int n;
148       char buf[8];
149       char *name;
150       arch_register_t *reg = (arch_register_t *) &cls->regs[i];
151
152       n = snprintf(buf, sizeof(buf), "r%d", i);
153       name = obstack_copy0(&obst, buf, n);
154
155       reg->name = name;
156       reg->reg_class = cls;
157       reg->index = i;
158       reg->type = 0;
159     }
160   }
161
162         /*
163          * Create some opcodes and types to let firm look a little
164          * bit more like real machines.
165          */
166         if(!op_push) {
167                 int push_opc = get_next_ir_opcode();
168
169                 op_push = new_ir_op(push_opc, "Push",
170                                 op_pin_state_pinned, 0, oparity_binary, 0, 0, NULL);
171         }
172
173         if(!op_imm) {
174                 int imm_opc = get_next_ir_opcode();
175                 ir_op_ops ops;
176
177                 memset(&ops, 0, sizeof(ops));
178                 ops.dump_node = dump_node_Imm;
179
180                 op_imm = new_ir_op(imm_opc, "Imm",
181                                 op_pin_state_pinned, 0, oparity_zero, 0, sizeof(imm_attr_t), &ops);
182         }
183
184         return isa;
185 }
186
187 static void firm_done(void *self)
188 {
189         free(self);
190 }
191
192 static int firm_get_n_reg_class(const void *self)
193 {
194   return N_CLASSES;
195 }
196
197 static const arch_register_class_t *firm_get_reg_class(const void *self, int i)
198 {
199   assert(i >= 0 && i < N_CLASSES);
200   return &reg_classes[i];
201 }
202
203 static const arch_register_class_t *firm_get_reg_class_for_mode(const void *self, const ir_mode *irm)
204 {
205         return mode_is_datab(irm) ? &reg_classes[CLS_DATAB] : NULL;
206 }
207
208 static ir_type *firm_abi_get_between_type(void *self) {
209         static ir_type *between_type = NULL;
210
211         if(!between_type) {
212                 between_type = new_type_class(new_id_from_str("firm_be_between"));
213                 set_type_size_bytes(between_type, 0);
214         }
215
216         return between_type;
217 }
218
219 static const be_abi_callbacks_t firm_abi_callbacks = {
220         NULL,
221         NULL,
222         firm_abi_get_between_type,
223         NULL,
224         NULL,
225         NULL,
226 };
227
228 static void firm_get_call_abi(const void *self, ir_type *method_type, be_abi_call_t *abi)
229 {
230         const arch_register_class_t *cls = &reg_classes[CLS_DATAB];
231         int i, n;
232         be_abi_call_flags_t flags = { { 0, 0, 0, 0, 0 } };
233
234
235         for(i = 0, n = get_method_n_params(method_type); i < n; ++i) {
236                 ir_type *t = get_method_param_type(method_type, i);
237                 if(is_Primitive_type(t))
238                         be_abi_call_param_reg(abi, i, &cls->regs[i]);
239                 else
240                         be_abi_call_param_stack(abi, i, 1, 0, 0);
241         }
242
243         for(i = 0, n = get_method_n_ress(method_type); i < n; ++i) {
244                 ir_type *t = get_method_res_type(method_type, i);
245                 if(is_Primitive_type(t))
246                         be_abi_call_res_reg(abi, i, &cls->regs[i]);
247         }
248
249         flags.val = 0;
250         be_abi_call_set_flags(abi, flags, &firm_abi_callbacks);
251 }
252
253
254 static const arch_register_req_t firm_std_reg_req = {
255   arch_register_req_type_normal,
256   &reg_classes[CLS_DATAB],
257   NULL,
258   NULL
259 };
260
261 static const arch_register_req_t *
262 firm_get_irn_reg_req(const void *self,
263     arch_register_req_t *req, const ir_node *irn, int pos)
264 {
265   if(is_firm_be_mode(get_irn_mode(irn)))
266     memcpy(req, &firm_std_reg_req, sizeof(*req));
267   else
268     req = NULL;
269
270   return req;
271 }
272
273 struct irn_reg_assoc {
274   const ir_node *irn;
275   const arch_register_t *reg;
276 };
277
278 static int cmp_irn_reg_assoc(const void *a, const void *b, size_t len)
279 {
280   const struct irn_reg_assoc *x = a;
281   const struct irn_reg_assoc *y = b;
282
283   return x->irn != y->irn;
284 }
285
286 static struct irn_reg_assoc *get_irn_reg_assoc(const ir_node *irn)
287 {
288   static set *reg_set = NULL;
289   struct irn_reg_assoc templ;
290
291   if(!reg_set)
292     reg_set = new_set(cmp_irn_reg_assoc, 1024);
293
294   templ.irn = irn;
295   templ.reg = NULL;
296
297   return set_insert(reg_set, &templ, sizeof(templ), HASH_PTR(irn));
298 }
299
300 static void firm_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg)
301 {
302   struct irn_reg_assoc *assoc = get_irn_reg_assoc(irn);
303   assoc->reg = reg;
304 }
305
306 static const arch_register_t *firm_get_irn_reg(const void *self, const ir_node *irn)
307 {
308   struct irn_reg_assoc *assoc = get_irn_reg_assoc(irn);
309   return assoc->reg;
310 }
311
312 static arch_irn_class_t firm_classify(const void *self, const ir_node *irn)
313 {
314     arch_irn_class_t res;
315
316     switch(get_irn_opcode(irn)) {
317         case iro_Cond:
318         case iro_Jmp:
319             res = arch_irn_class_branch;
320             break;
321                 case iro_Call:
322                         res = arch_irn_class_call;
323                         break;
324         default:
325             res = arch_irn_class_normal;
326     }
327
328         return res;
329 }
330
331 static arch_irn_flags_t firm_get_flags(const void *self, const ir_node *irn)
332 {
333         arch_irn_flags_t res = 0;
334
335         if(get_irn_op(irn) == op_imm)
336                 res |= arch_irn_flags_rematerializable;
337
338         switch(get_irn_opcode(irn)) {
339                 case iro_Add:
340                 case iro_Sub:
341                 case iro_Shl:
342                 case iro_Shr:
343                 case iro_Shrs:
344                 case iro_And:
345                 case iro_Or:
346                 case iro_Eor:
347                 case iro_Not:
348                         res |= arch_irn_flags_rematerializable;
349                 default:
350                         res = res;
351         }
352
353         return res;
354 }
355
356 static void firm_set_stack_bias(const void *self, ir_node *irn, int bias)
357 {
358 }
359
360 static entity *firm_get_frame_entity(const void *self, const ir_node *irn)
361 {
362         return NULL;
363 }
364
365 static void firm_set_frame_entity(const void *self, const ir_node *irn, entity *ent)
366 {
367 }
368
369 static const arch_irn_ops_if_t firm_irn_ops_if = {
370         firm_get_irn_reg_req,
371         firm_set_irn_reg,
372         firm_get_irn_reg,
373         firm_classify,
374         firm_get_flags,
375         firm_get_frame_entity,
376         firm_set_frame_entity,
377         firm_set_stack_bias,
378         NULL,    /* get_inverse             */
379         NULL,    /* get_op_estimated_cost   */
380         NULL,    /* possible_memory_operand */
381         NULL,    /* perform_memory_operand  */
382 };
383
384 static const arch_irn_ops_t firm_irn_ops = {
385         &firm_irn_ops_if
386 };
387
388 static const void *firm_get_irn_ops(const arch_irn_handler_t *self,
389         const ir_node *irn)
390 {
391         return &firm_irn_ops;
392 }
393
394 const arch_irn_handler_t firm_irn_handler = {
395         firm_get_irn_ops,
396 };
397
398 static ir_node *new_Push(ir_graph *irg, ir_node *bl, ir_node *push, ir_node *arg)
399 {
400         ir_node *ins[2];
401         ins[0] = push;
402         ins[1] = arg;
403         return new_ir_node(NULL, irg, bl, op_push, mode_M, 2, ins);
404 }
405
406 /**
407  * Creates an op_Imm node from an op_Const.
408  */
409 static ir_node *new_Imm(ir_graph *irg, ir_node *bl, ir_node *cnst) {
410   ir_node    *ins[1];
411   ir_node    *res;
412   imm_attr_t *attr;
413
414   res = new_ir_node(NULL, irg, bl, op_imm, get_irn_mode(cnst), 0, ins);
415   attr = (imm_attr_t *) &res->attr;
416
417   switch (get_irn_opcode(cnst)) {
418     case iro_Const:
419       attr->tp      = imm_Const;
420       attr->data.cnst_attr = get_irn_const_attr(cnst);
421       break;
422     case iro_SymConst:
423       attr->tp             = imm_SymConst;
424       attr->data.symc_attr = get_irn_symconst_attr(cnst);
425       break;
426     case iro_Unknown:
427       break;
428     default:
429       assert(0 && "Cannot create Imm for this opcode");
430   }
431
432   return res;
433 }
434
435 static void prepare_walker(ir_node *irn, void *data)
436 {
437         opcode opc = get_irn_opcode(irn);
438
439         /* A replacement for this node has already been computed. */
440         if(get_irn_link(irn))
441                 return;
442
443         if(opc == iro_Call) {
444                 ir_node *bl   = get_nodes_block(irn);
445                 ir_graph *irg = get_irn_irg(bl);
446
447                 ir_node *store   = get_Call_mem(irn);
448                 ir_node *ptr     = get_Call_ptr(irn);
449                 ir_type *ct      = get_Call_type(irn);
450                 int np           = get_Call_n_params(irn) > 0 ? 1 : 0;
451
452                 if(np > 0) {
453                         ir_node *ins[1];
454                         char buf[128];
455                         ir_node *nc;
456                         int i, n = get_Call_n_params(irn);
457                         ir_type *nt;
458       unsigned cc = get_method_calling_convention(get_Call_type(irn));
459
460       if (cc & cc_last_on_top) {
461                           store = new_Push(irg, bl, store, get_Call_param(irn, 0));
462
463                           for (i = 1; i < n; ++i)
464                                   store = new_Push(irg, bl, store, get_Call_param(irn, i));
465       }
466       else {
467         store = new_Push(irg, bl, store, get_Call_param(irn, n - 1));
468
469         for (i = n - 2; i >= 0; --i)
470           store = new_Push(irg, bl, store, get_Call_param(irn, i));
471       }
472
473                         snprintf(buf, sizeof(buf), "push_%s", get_type_name(ct));
474
475                         n = get_method_n_ress(ct);
476                         nt = new_type_method(new_id_from_str(buf), 0, n);
477                         for(i = 0; i < n; ++i)
478                                 set_method_res_type(nt, i, get_method_res_type(ct, i));
479
480                         nc = new_r_Call(irg, bl, store, ptr, 0, ins, nt);
481                         exchange(irn, nc);
482                         set_irn_link(nc, nc);
483                 }
484         }
485 }
486
487 static void localize_const_walker(ir_node *irn, void *data)
488 {
489         if(!is_Block(irn)) {
490                 int i, n;
491                 ir_node *bl = get_nodes_block(irn);
492
493                 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
494                         ir_node *op    = get_irn_n(irn, i);
495                         opcode opc     = get_irn_opcode(op);
496
497                         if(opc == iro_Const
498                         || opc == iro_Unknown
499                         || (opc == iro_SymConst /*&& get_SymConst_kind(op) == symconst_addr_ent*/)) {
500                                 ir_graph *irg   = get_irn_irg(bl);
501                                 ir_node *imm_bl = is_Phi(irn) ? get_Block_cfgpred_block(bl, i) : bl;
502
503                                 ir_node *imm = new_Imm(irg, imm_bl, op);
504                                 set_irn_n(irn, i, imm);
505                         }
506                 }
507         }
508 }
509
510 static const arch_irn_handler_t *firm_get_irn_handler(const void *self)
511 {
512         return &firm_irn_handler;
513 }
514
515 typedef struct _firm_code_gen_t {
516         const arch_code_generator_if_t *impl;
517         ir_graph *irg;
518 } firm_code_gen_t;
519
520
521 static void firm_prepare_graph(void *self)
522 {
523         firm_code_gen_t *cg = self;
524
525         irg_walk_graph(cg->irg, firm_clear_link, localize_const_walker, NULL);
526         irg_walk_graph(cg->irg, NULL, prepare_walker, NULL);
527 }
528
529 static void firm_before_sched(void *self)
530 {
531 }
532
533 static void imm_scheduler(ir_node *irn, void *env) {
534         if(is_Imm(irn)) {
535                 const ir_edge_t *e;
536                 ir_node *user, *user_block, *before, *tgt_block;
537
538                 if (1 != get_irn_n_edges(irn)) {
539                         printf("Out edges: %d\n", get_irn_n_edges(irn));
540                         assert(1 == get_irn_n_edges(irn));
541                 }
542
543                 e = get_irn_out_edge_first(irn);
544                 user = e->src;
545                 user_block = get_nodes_block(user);
546                 if (is_Phi(user)) {
547                         before = get_Block_cfgpred_block(user_block, e->pos);
548                         tgt_block = before;
549                 } else {
550                         before = user;
551                         tgt_block = user_block;
552                 }
553
554                 sched_remove(irn);
555                 set_nodes_block(irn, tgt_block);
556                 sched_add_before(before, irn);
557         }
558 }
559
560 static void firm_before_ra(void *self)
561 {
562         firm_code_gen_t *cg = self;
563         irg_walk_graph(cg->irg, imm_scheduler, NULL, NULL);
564 }
565
566 static void firm_after_ra(void *self)
567 {
568 }
569
570 static void firm_codegen_done(void *self)
571 {
572         free(self);
573 }
574
575 static void *firm_cg_init(const be_irg_t *birg);
576
577 static const arch_code_generator_if_t firm_code_gen_if = {
578         firm_cg_init,
579         NULL,
580         firm_prepare_graph,
581         firm_before_sched,
582         firm_before_ra,
583         firm_after_ra,
584         firm_codegen_done
585 };
586
587 static void *firm_cg_init(const be_irg_t *birg)
588 {
589         firm_code_gen_t *cg = xmalloc(sizeof(*cg));
590         cg->impl = &firm_code_gen_if;
591         cg->irg  = birg->irg;
592         return cg;
593 }
594
595
596 static const arch_code_generator_if_t *firm_get_code_generator_if(void *self)
597 {
598         return &firm_code_gen_if;
599 }
600
601 static const list_sched_selector_t *firm_get_list_sched_selector(const void *self, list_sched_selector_t *selector) {
602         return trivial_selector;
603 }
604
605 static const ilp_sched_selector_t *firm_get_ilp_sched_selector(const void *self) {
606         return NULL;
607 }
608
609 /**
610  * Returns the necessary byte alignment for storing a register of given class.
611  */
612 static int firm_get_reg_class_alignment(const void *self, const arch_register_class_t *cls) {
613         ir_mode *mode = arch_register_class_mode(cls);
614         return get_mode_size_bytes(mode);
615 }
616
617 static const be_execution_unit_t ***firm_get_allowed_execution_units(const void *self, const ir_node *irn) {
618         /* TODO */
619         assert(0);
620         return NULL;
621 }
622
623 static const be_machine_t *firm_get_machine(const void *self) {
624         /* TODO */
625         assert(0);
626         return NULL;
627 }
628
629 /**
630  * Returns the libFirm configuration parameter for this backend.
631  */
632 static const backend_params *firm_get_libfirm_params(void) {
633         static arch_dep_params_t ad = {
634                 1,  /* allow subs */
635                 0,      /* Muls are fast enough on Firm */
636                 31, /* shift would be ok */
637                 0,  /* no Mulhs */
638                 0,  /* no Mulhu */
639                 0,  /* no Mulh */
640         };
641         static backend_params p = {
642                 NULL,  /* no additional opcodes */
643                 NULL,  /* will be set later */
644                 0,     /* no dword lowering */
645                 NULL,  /* no creator function */
646                 NULL,  /* context for create_intrinsic_fkt */
647         };
648
649         p.dep_param = &ad;
650         return &p;
651 }
652
653 #ifdef WITH_LIBCORE
654 static void firm_register_options(lc_opt_entry_t *ent)
655 {
656 }
657 #endif
658
659 const arch_isa_if_t firm_isa = {
660         firm_init,
661         firm_done,
662         firm_get_n_reg_class,
663         firm_get_reg_class,
664         firm_get_reg_class_for_mode,
665         firm_get_call_abi,
666         firm_get_irn_handler,
667         firm_get_code_generator_if,
668         firm_get_list_sched_selector,
669         firm_get_ilp_sched_selector,
670         firm_get_reg_class_alignment,
671         firm_get_libfirm_params,
672         firm_get_allowed_execution_units,
673         firm_get_machine,
674 #ifdef WITH_LIBCORE
675         firm_register_options,
676 #endif
677 };