removed unnecessary function
[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);
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 const arch_irn_ops_if_t firm_irn_ops_if = {
332         firm_get_irn_reg_req,
333         firm_set_irn_reg,
334         firm_get_irn_reg,
335         firm_classify,
336         firm_get_flags
337 };
338
339 static const arch_irn_ops_t firm_irn_ops = {
340         &firm_irn_ops_if
341 };
342
343 static const void *firm_get_irn_ops(const arch_irn_handler_t *self,
344         const ir_node *irn)
345 {
346         return &firm_irn_ops;
347 }
348
349 const arch_irn_handler_t firm_irn_handler = {
350         firm_get_irn_ops,
351 };
352
353 static ir_node *new_Push(ir_graph *irg, ir_node *bl, ir_node *push, ir_node *arg)
354 {
355         ir_node *ins[2];
356         ins[0] = push;
357         ins[1] = arg;
358         return new_ir_node(NULL, irg, bl, op_push, mode_M, 2, ins);
359 }
360
361 /**
362  * Creates an op_Imm node from an op_Const.
363  */
364 static ir_node *new_Imm(ir_graph *irg, ir_node *bl, ir_node *cnst) {
365   ir_node    *ins[1];
366   ir_node    *res;
367   imm_attr_t *attr;
368
369   res = new_ir_node(NULL, irg, bl, op_imm, get_irn_mode(cnst), 0, ins);
370   attr = (imm_attr_t *) &res->attr;
371
372   switch (get_irn_opcode(cnst)) {
373     case iro_Const:
374       attr->tp      = imm_Const;
375       attr->data.cnst_attr = get_irn_const_attr(cnst);
376       break;
377     case iro_SymConst:
378       attr->tp             = imm_SymConst;
379       attr->data.symc_attr = get_irn_symconst_attr(cnst);
380       break;
381     case iro_Unknown:
382       break;
383     default:
384       assert(0 && "Cannot create Imm for this opcode");
385   }
386
387   return res;
388 }
389
390 static void prepare_walker(ir_node *irn, void *data)
391 {
392         opcode opc = get_irn_opcode(irn);
393
394         /* A replacement for this node has already been computed. */
395         if(get_irn_link(irn))
396                 return;
397
398         if(opc == iro_Call) {
399                 ir_node *bl   = get_nodes_block(irn);
400                 ir_graph *irg = get_irn_irg(bl);
401
402                 ir_node *store   = get_Call_mem(irn);
403                 ir_node *ptr     = get_Call_ptr(irn);
404                 ir_type *ct      = get_Call_type(irn);
405                 int np           = get_Call_n_params(irn) > 0 ? 1 : 0;
406
407                 if(np > 0) {
408                         ir_node *ins[1];
409                         char buf[128];
410                         ir_node *nc;
411                         int i, n = get_Call_n_params(irn);
412                         ir_type *nt;
413       unsigned cc = get_method_calling_convention(get_Call_type(irn));
414
415       if (cc & cc_last_on_top) {
416                           store = new_Push(irg, bl, store, get_Call_param(irn, 0));
417
418                           for (i = 1; i < n; ++i)
419                                   store = new_Push(irg, bl, store, get_Call_param(irn, i));
420       }
421       else {
422         store = new_Push(irg, bl, store, get_Call_param(irn, n - 1));
423
424         for (i = n - 2; i >= 0; --i)
425           store = new_Push(irg, bl, store, get_Call_param(irn, i));
426       }
427
428                         snprintf(buf, sizeof(buf), "push_%s", get_type_name(ct));
429
430                         n = get_method_n_ress(ct);
431                         nt = new_type_method(new_id_from_str(buf), 0, n);
432                         for(i = 0; i < n; ++i)
433                                 set_method_res_type(nt, i, get_method_res_type(ct, i));
434
435                         nc = new_r_Call(irg, bl, store, ptr, 0, ins, nt);
436                         exchange(irn, nc);
437                         set_irn_link(nc, nc);
438                 }
439         }
440 }
441
442 static void localize_const_walker(ir_node *irn, void *data)
443 {
444         if(!is_Block(irn)) {
445                 int i, n;
446                 ir_node *bl = get_nodes_block(irn);
447
448                 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
449                         ir_node *op    = get_irn_n(irn, i);
450                         opcode opc     = get_irn_opcode(op);
451
452                         if(opc == iro_Const
453                         || opc == iro_Unknown
454                         || (opc == iro_SymConst /*&& get_SymConst_kind(op) == symconst_addr_ent*/)) {
455                                 ir_graph *irg   = get_irn_irg(bl);
456                                 ir_node *imm_bl = is_Phi(irn) ? get_Block_cfgpred_block(bl, i) : bl;
457
458                                 ir_node *imm = new_Imm(irg, imm_bl, op);
459                                 set_irn_n(irn, i, imm);
460                         }
461                 }
462         }
463 }
464
465 static const arch_irn_handler_t *firm_get_irn_handler(const void *self)
466 {
467         return &firm_irn_handler;
468 }
469
470 typedef struct _firm_code_gen_t {
471         const arch_code_generator_if_t *impl;
472         ir_graph *irg;
473 } firm_code_gen_t;
474
475
476 static void clear_link(ir_node *irn, void *data)
477 {
478         set_irn_link(irn, NULL);
479 }
480
481 static void firm_prepare_graph(void *self)
482 {
483         firm_code_gen_t *cg = self;
484
485         irg_walk_graph(cg->irg, clear_link, localize_const_walker, NULL);
486         irg_walk_graph(cg->irg, NULL, prepare_walker, NULL);
487 }
488
489 static void firm_before_sched(void *self)
490 {
491 }
492
493 static void imm_scheduler(ir_node *irn, void *env) {
494         if(is_Imm(irn)) {
495                 const ir_edge_t *e;
496                 ir_node *user, *user_block, *before, *tgt_block;
497
498                 if (1 != get_irn_n_edges(irn)) {
499                         printf("Out edges: %d\n", get_irn_n_edges(irn));
500                         assert(1 == get_irn_n_edges(irn));
501                 }
502
503                 e = get_irn_out_edge_first(irn);
504                 user = e->src;
505                 user_block = get_nodes_block(user);
506                 if (is_Phi(user)) {
507                         before = get_Block_cfgpred_block(user_block, e->pos);
508                         tgt_block = before;
509                 } else {
510                         before = user;
511                         tgt_block = user_block;
512                 }
513
514                 sched_remove(irn);
515                 set_nodes_block(irn, tgt_block);
516                 sched_add_before(before, irn);
517         }
518 }
519
520 static void firm_before_ra(void *self)
521 {
522         firm_code_gen_t *cg = self;
523         irg_walk_graph(cg->irg, imm_scheduler, NULL, NULL);
524 }
525
526 static void firm_codegen_done(void *self)
527 {
528         free(self);
529 }
530
531 static void *firm_cg_init(FILE *file_handle, ir_graph *irg, const arch_env_t *env);
532
533 static const arch_code_generator_if_t firm_code_gen_if = {
534         firm_cg_init,
535         firm_prepare_graph,
536         firm_before_sched,
537         firm_before_ra,
538         NULL,  /* lower spill */
539         NULL,  /* lower reload */
540         firm_codegen_done
541 };
542
543 static void *firm_cg_init(FILE *file_handle, ir_graph *irg, const arch_env_t *env)
544 {
545         firm_code_gen_t *cg = xmalloc(sizeof(*cg));
546         cg->impl = &firm_code_gen_if;
547         cg->irg  = irg;
548         return cg;
549 }
550
551
552 static const arch_code_generator_if_t *firm_get_code_generator_if(void *self)
553 {
554         return &firm_code_gen_if;
555 }
556
557 static const list_sched_selector_t *firm_get_list_sched_selector(const void *self) {
558         return trivial_selector;
559 }
560
561 static long firm_handle_call_proj(const void *self, ir_node *proj, int is_keep) {
562         return get_Proj_proj(proj);
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         firm_handle_call_proj
585 };