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