removed unnecessary phases and functions which are now handled directly by the generi...
[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
24 #include "../bearch.h"
25 #include "../besched.h"
26 #include "../beutil.h"
27 #include "../beabi.h"
28
29 #define N_REGS 3
30
31 typedef struct {
32   enum  { imm_Const, imm_SymConst } tp;
33   union {
34     const_attr    cnst_attr;
35     symconst_attr symc_attr;
36   } data;
37 } imm_attr_t;
38
39 static arch_register_t datab_regs[N_REGS];
40
41 static arch_register_class_t reg_classes[] = {
42   { "datab", N_REGS, NULL, datab_regs },
43 };
44
45 static ir_op *op_push;
46 static ir_op *op_imm;
47
48 const arch_isa_if_t firm_isa;
49
50 #define N_CLASSES \
51   (sizeof(reg_classes) / sizeof(reg_classes[0]))
52
53 #define CLS_DATAB 0
54
55 tarval *get_Imm_tv(ir_node *n) {
56   imm_attr_t *attr = (imm_attr_t *)get_irn_generic_attr(n);
57   return attr->tp == imm_Const ? attr->data.cnst_attr.tv : NULL;
58 }
59
60 int is_Imm(const ir_node *irn) {
61   return get_irn_op(irn) == op_imm;
62 }
63
64 static int dump_node_Imm(ir_node *n, FILE *F, dump_reason_t reason) {
65   ir_mode    *mode;
66   int        bad = 0;
67   char       buf[1024];
68   tarval     *tv;
69   imm_attr_t *attr;
70
71   switch (reason) {
72     case dump_node_opcode_txt:
73       tv = get_Imm_tv(n);
74
75       if (tv) {
76         tarval_snprintf(buf, sizeof(buf), tv);
77         fprintf(F, "%s", buf);
78       }
79       else {
80         fprintf(F, "immSymC");
81       }
82       break;
83
84     case dump_node_mode_txt:
85       mode = get_irn_mode(n);
86
87       if (mode && mode != mode_BB && mode != mode_ANY && mode != mode_BAD && mode != mode_T) {
88         fprintf(F, "[%s]", get_mode_name(mode));
89       }
90       break;
91
92     case dump_node_nodeattr_txt:
93       attr = (imm_attr_t *)get_irn_generic_attr(n);
94
95       if (is_Imm(n) && attr->tp == imm_SymConst) {
96         const char *name    = NULL;
97         symconst_attr *sc_attr = &attr->data.symc_attr;
98
99         switch (sc_attr->num) {
100           case symconst_addr_name:
101             name = get_id_str(sc_attr->sym.ident_p);
102             break;
103
104           case symconst_addr_ent:
105             name = get_entity_ld_name(sc_attr->sym.entity_p);
106             break;
107
108           default:
109             assert(!"Unsupported SymConst");
110         }
111
112         fprintf(F, "&%s ", name);
113       }
114
115       break;
116
117     case dump_node_info_txt:
118       break;
119   }
120
121   return bad;
122 }
123
124 static void *firm_init(void)
125 {
126   static struct obstack obst;
127   static int inited = 0;
128         arch_isa_t *isa = xmalloc(sizeof(*isa));
129   int k;
130
131         isa->impl = &firm_isa;
132
133   if(inited)
134     return NULL;
135
136   inited = 1;
137   obstack_init(&obst);
138
139   for(k = 0; k < N_CLASSES; ++k) {
140     arch_register_class_t *cls = &reg_classes[k];
141     int i;
142
143         cls->mode = mode_Is;
144     for(i = 0; i < cls->n_regs; ++i) {
145       int n;
146       char buf[8];
147       char *name;
148       arch_register_t *reg = (arch_register_t *) &cls->regs[i];
149
150       n = snprintf(buf, sizeof(buf), "r%d", i);
151       name = obstack_copy0(&obst, buf, n);
152
153       reg->name = name;
154       reg->reg_class = cls;
155       reg->index = i;
156       reg->type = 0;
157     }
158   }
159
160         /*
161          * Create some opcodes and types to let firm look a little
162          * bit more like real machines.
163          */
164         if(!op_push) {
165                 int push_opc = get_next_ir_opcode();
166
167                 op_push = new_ir_op(push_opc, "Push",
168                                 op_pin_state_pinned, 0, oparity_binary, 0, 0, NULL);
169         }
170
171         if(!op_imm) {
172                 int imm_opc = get_next_ir_opcode();
173                 ir_op_ops ops;
174
175                 memset(&ops, 0, sizeof(ops));
176                 ops.dump_node = dump_node_Imm;
177
178                 op_imm = new_ir_op(imm_opc, "Imm",
179                                 op_pin_state_pinned, 0, oparity_zero, 0, sizeof(imm_attr_t), &ops);
180         }
181
182         return isa;
183 }
184
185 static void firm_done(void *self)
186 {
187         free(self);
188 }
189
190 static int firm_get_n_reg_class(const void *self)
191 {
192   return N_CLASSES;
193 }
194
195 static const arch_register_class_t *firm_get_reg_class(const void *self, int i)
196 {
197   assert(i >= 0 && i < N_CLASSES);
198   return &reg_classes[i];
199 }
200
201 static const arch_register_class_t *firm_get_reg_class_for_mode(const void *self, const ir_mode *irm)
202 {
203         return mode_is_datab(irm) ? &reg_classes[CLS_DATAB] : NULL;
204 }
205
206 static void firm_get_call_abi(const void *self, ir_type *method_type, be_abi_call_t *abi)
207 {
208         const arch_register_class_t *cls = &reg_classes[CLS_DATAB];
209         int i, n;
210
211         for(i = 0, n = get_method_n_params(method_type); i < n; ++i) {
212                 ir_type *t = get_method_param_type(method_type, i);
213                 if(is_Primitive_type(t))
214                         be_abi_call_param_reg(abi, i, &cls->regs[i]);
215                 else
216                         be_abi_call_param_stack(abi, i);
217         }
218
219         for(i = 0, n = get_method_n_ress(method_type); i < n; ++i) {
220                 ir_type *t = get_method_res_type(method_type, i);
221                 if(is_Primitive_type(t))
222                         be_abi_call_res_reg(abi, i, &cls->regs[i]);
223         }
224
225         be_abi_call_set_flags(abi, BE_ABI_NONE, 0);
226 }
227
228
229 static const arch_register_req_t firm_std_reg_req = {
230   arch_register_req_type_normal,
231   &reg_classes[CLS_DATAB],
232   NULL,
233   NULL
234 };
235
236 static const arch_register_req_t *
237 firm_get_irn_reg_req(const void *self,
238     arch_register_req_t *req, const ir_node *irn, int pos)
239 {
240   if(is_firm_be_mode(get_irn_mode(irn)))
241     memcpy(req, &firm_std_reg_req, sizeof(*req));
242   else
243     req = NULL;
244
245   return req;
246 }
247
248 struct irn_reg_assoc {
249   const ir_node *irn;
250   const arch_register_t *reg;
251 };
252
253 static int cmp_irn_reg_assoc(const void *a, const void *b, size_t len)
254 {
255   const struct irn_reg_assoc *x = a;
256   const struct irn_reg_assoc *y = b;
257
258   return x->irn != y->irn;
259 }
260
261 static struct irn_reg_assoc *get_irn_reg_assoc(const ir_node *irn)
262 {
263   static set *reg_set = NULL;
264   struct irn_reg_assoc templ;
265
266   if(!reg_set)
267     reg_set = new_set(cmp_irn_reg_assoc, 1024);
268
269   templ.irn = irn;
270   templ.reg = NULL;
271
272   return set_insert(reg_set, &templ, sizeof(templ), HASH_PTR(irn));
273 }
274
275 static void firm_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg)
276 {
277   struct irn_reg_assoc *assoc = get_irn_reg_assoc(irn);
278   assoc->reg = reg;
279 }
280
281 static const arch_register_t *firm_get_irn_reg(const void *self, const ir_node *irn)
282 {
283   struct irn_reg_assoc *assoc = get_irn_reg_assoc(irn);
284   return assoc->reg;
285 }
286
287 static arch_irn_class_t firm_classify(const void *self, const ir_node *irn)
288 {
289     arch_irn_class_t res;
290
291     switch(get_irn_opcode(irn)) {
292         case iro_Cond:
293         case iro_Jmp:
294             res = arch_irn_class_branch;
295             break;
296                 case iro_Call:
297                         res = arch_irn_class_call;
298                         break;
299         default:
300             res = arch_irn_class_normal;
301     }
302
303         return res;
304 }
305
306 static arch_irn_flags_t firm_get_flags(const void *self, const ir_node *irn)
307 {
308         arch_irn_flags_t res = 0;
309
310         if(get_irn_op(irn) == op_imm)
311                 res |= arch_irn_flags_rematerializable;
312
313         switch(get_irn_opcode(irn)) {
314                 case iro_Add:
315                 case iro_Sub:
316                 case iro_Shl:
317                 case iro_Shr:
318                 case iro_Shrs:
319                 case iro_And:
320                 case iro_Or:
321                 case iro_Eor:
322                 case iro_Not:
323                         res |= arch_irn_flags_rematerializable;
324                 default:
325                         res = res;
326         }
327
328         return res;
329 }
330
331 static void firm_set_stack_bias(const void *self, ir_node *irn, int bias) {
332 }
333
334 static const arch_irn_ops_if_t firm_irn_ops_if = {
335         firm_get_irn_reg_req,
336         firm_set_irn_reg,
337         firm_get_irn_reg,
338         firm_classify,
339         firm_get_flags,
340         firm_set_stack_bias
341 };
342
343 static const arch_irn_ops_t firm_irn_ops = {
344         &firm_irn_ops_if
345 };
346
347 static const void *firm_get_irn_ops(const arch_irn_handler_t *self,
348         const ir_node *irn)
349 {
350         return &firm_irn_ops;
351 }
352
353 const arch_irn_handler_t firm_irn_handler = {
354         firm_get_irn_ops,
355 };
356
357 static ir_node *new_Push(ir_graph *irg, ir_node *bl, ir_node *push, ir_node *arg)
358 {
359         ir_node *ins[2];
360         ins[0] = push;
361         ins[1] = arg;
362         return new_ir_node(NULL, irg, bl, op_push, mode_M, 2, ins);
363 }
364
365 /**
366  * Creates an op_Imm node from an op_Const.
367  */
368 static ir_node *new_Imm(ir_graph *irg, ir_node *bl, ir_node *cnst) {
369   ir_node    *ins[1];
370   ir_node    *res;
371   imm_attr_t *attr;
372
373   res = new_ir_node(NULL, irg, bl, op_imm, get_irn_mode(cnst), 0, ins);
374   attr = (imm_attr_t *) &res->attr;
375
376   switch (get_irn_opcode(cnst)) {
377     case iro_Const:
378       attr->tp      = imm_Const;
379       attr->data.cnst_attr = get_irn_const_attr(cnst);
380       break;
381     case iro_SymConst:
382       attr->tp             = imm_SymConst;
383       attr->data.symc_attr = get_irn_symconst_attr(cnst);
384       break;
385     case iro_Unknown:
386       break;
387     default:
388       assert(0 && "Cannot create Imm for this opcode");
389   }
390
391   return res;
392 }
393
394 static void prepare_walker(ir_node *irn, void *data)
395 {
396         opcode opc = get_irn_opcode(irn);
397
398         /* A replacement for this node has already been computed. */
399         if(get_irn_link(irn))
400                 return;
401
402         if(opc == iro_Call) {
403                 ir_node *bl   = get_nodes_block(irn);
404                 ir_graph *irg = get_irn_irg(bl);
405
406                 ir_node *store   = get_Call_mem(irn);
407                 ir_node *ptr     = get_Call_ptr(irn);
408                 ir_type *ct      = get_Call_type(irn);
409                 int np           = get_Call_n_params(irn) > 0 ? 1 : 0;
410
411                 if(np > 0) {
412                         ir_node *ins[1];
413                         char buf[128];
414                         ir_node *nc;
415                         int i, n = get_Call_n_params(irn);
416                         ir_type *nt;
417       unsigned cc = get_method_calling_convention(get_Call_type(irn));
418
419       if (cc & cc_last_on_top) {
420                           store = new_Push(irg, bl, store, get_Call_param(irn, 0));
421
422                           for (i = 1; i < n; ++i)
423                                   store = new_Push(irg, bl, store, get_Call_param(irn, i));
424       }
425       else {
426         store = new_Push(irg, bl, store, get_Call_param(irn, n - 1));
427
428         for (i = n - 2; i >= 0; --i)
429           store = new_Push(irg, bl, store, get_Call_param(irn, i));
430       }
431
432                         snprintf(buf, sizeof(buf), "push_%s", get_type_name(ct));
433
434                         n = get_method_n_ress(ct);
435                         nt = new_type_method(new_id_from_str(buf), 0, n);
436                         for(i = 0; i < n; ++i)
437                                 set_method_res_type(nt, i, get_method_res_type(ct, i));
438
439                         nc = new_r_Call(irg, bl, store, ptr, 0, ins, nt);
440                         exchange(irn, nc);
441                         set_irn_link(nc, nc);
442                 }
443         }
444 }
445
446 static void localize_const_walker(ir_node *irn, void *data)
447 {
448         if(!is_Block(irn)) {
449                 int i, n;
450                 ir_node *bl = get_nodes_block(irn);
451
452                 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
453                         ir_node *op    = get_irn_n(irn, i);
454                         opcode opc     = get_irn_opcode(op);
455
456                         if(opc == iro_Const
457                         || opc == iro_Unknown
458                         || (opc == iro_SymConst /*&& get_SymConst_kind(op) == symconst_addr_ent*/)) {
459                                 ir_graph *irg   = get_irn_irg(bl);
460                                 ir_node *imm_bl = is_Phi(irn) ? get_Block_cfgpred_block(bl, i) : bl;
461
462                                 ir_node *imm = new_Imm(irg, imm_bl, op);
463                                 set_irn_n(irn, i, imm);
464                         }
465                 }
466         }
467 }
468
469 static const arch_irn_handler_t *firm_get_irn_handler(const void *self)
470 {
471         return &firm_irn_handler;
472 }
473
474 typedef struct _firm_code_gen_t {
475         const arch_code_generator_if_t *impl;
476         ir_graph *irg;
477 } firm_code_gen_t;
478
479
480 static void clear_link(ir_node *irn, void *data)
481 {
482         set_irn_link(irn, NULL);
483 }
484
485 static void firm_prepare_graph(void *self)
486 {
487         firm_code_gen_t *cg = self;
488
489         irg_walk_graph(cg->irg, clear_link, localize_const_walker, NULL);
490         irg_walk_graph(cg->irg, NULL, prepare_walker, NULL);
491 }
492
493 static void firm_before_sched(void *self)
494 {
495 }
496
497 static void imm_scheduler(ir_node *irn, void *env) {
498         if(is_Imm(irn)) {
499                 const ir_edge_t *e;
500                 ir_node *user, *user_block, *before, *tgt_block;
501
502                 if (1 != get_irn_n_edges(irn)) {
503                         printf("Out edges: %d\n", get_irn_n_edges(irn));
504                         assert(1 == get_irn_n_edges(irn));
505                 }
506
507                 e = get_irn_out_edge_first(irn);
508                 user = e->src;
509                 user_block = get_nodes_block(user);
510                 if (is_Phi(user)) {
511                         before = get_Block_cfgpred_block(user_block, e->pos);
512                         tgt_block = before;
513                 } else {
514                         before = user;
515                         tgt_block = user_block;
516                 }
517
518                 sched_remove(irn);
519                 set_nodes_block(irn, tgt_block);
520                 sched_add_before(before, irn);
521         }
522 }
523
524 static void firm_before_ra(void *self)
525 {
526         firm_code_gen_t *cg = self;
527         irg_walk_graph(cg->irg, imm_scheduler, NULL, NULL);
528 }
529
530 static void firm_codegen_done(void *self)
531 {
532         free(self);
533 }
534
535 static void *firm_cg_init(FILE *file_handle, ir_graph *irg, const arch_env_t *env);
536
537 static const arch_code_generator_if_t firm_code_gen_if = {
538         firm_cg_init,
539         firm_prepare_graph,
540         firm_before_sched,
541         firm_before_ra,
542         NULL,  /* lower spill */
543         NULL,  /* lower reload */
544         firm_codegen_done
545 };
546
547 static void *firm_cg_init(FILE *file_handle, ir_graph *irg, const arch_env_t *env)
548 {
549         firm_code_gen_t *cg = xmalloc(sizeof(*cg));
550         cg->impl = &firm_code_gen_if;
551         cg->irg  = irg;
552         return cg;
553 }
554
555
556 static const arch_code_generator_if_t *firm_get_code_generator_if(void *self)
557 {
558         return &firm_code_gen_if;
559 }
560
561 static const list_sched_selector_t *firm_get_list_sched_selector(const void *self) {
562         return trivial_selector;
563 }
564
565 #ifdef WITH_LIBCORE
566 static void firm_register_options(lc_opt_entry_t *ent)
567 {
568 }
569 #endif
570
571 const arch_isa_if_t firm_isa = {
572 #ifdef WITH_LIBCORE
573         firm_register_options,
574 #endif
575         firm_init,
576         firm_done,
577         firm_get_n_reg_class,
578         firm_get_reg_class,
579         firm_get_reg_class_for_mode,
580         firm_get_call_abi,
581         firm_get_irn_handler,
582         firm_get_code_generator_if,
583         firm_get_list_sched_selector
584 };