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