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