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