ed68e6a64c7cc41c14e55713c6970d93d38d1a72
[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         default:
267             res = arch_irn_class_normal;
268     }
269
270         return res;
271 }
272
273 static arch_irn_flags_t firm_get_flags(const arch_irn_ops_t *self, const ir_node *irn)
274 {
275         arch_irn_flags_t res = arch_irn_flags_spillable;
276
277         if(get_irn_op(irn) == op_imm)
278                 res |= arch_irn_flags_rematerializable;
279
280         switch(get_irn_opcode(irn)) {
281                 case iro_Add:
282                 case iro_Sub:
283                 case iro_Shl:
284                 case iro_Shr:
285                 case iro_Shrs:
286                 case iro_And:
287                 case iro_Or:
288                 case iro_Eor:
289                 case iro_Not:
290                         res |= arch_irn_flags_rematerializable;
291                 default:
292                         res = res;
293         }
294
295         return res;
296 }
297
298 static const arch_irn_ops_t irn_ops = {
299   firm_get_irn_reg_req,
300   firm_set_irn_reg,
301   firm_get_irn_reg,
302   firm_classify,
303         firm_get_flags
304 };
305
306 static const arch_irn_ops_t *firm_get_irn_ops(const arch_irn_handler_t *self,
307     const ir_node *irn)
308 {
309   return &irn_ops;
310 }
311
312 const arch_irn_handler_t firm_irn_handler = {
313   firm_get_irn_ops,
314 };
315
316 static ir_node *new_Push(ir_graph *irg, ir_node *bl, ir_node *push, ir_node *arg)
317 {
318         ir_node *ins[2];
319         ins[0] = push;
320         ins[1] = arg;
321         return new_ir_node(NULL, irg, bl, op_push, mode_M, 2, ins);
322 }
323
324 /**
325  * Creates an op_Imm node from an op_Const.
326  */
327 static ir_node *new_Imm(ir_graph *irg, ir_node *bl, ir_node *cnst) {
328   ir_node    *ins[1];
329   ir_node    *res;
330   imm_attr_t *attr;
331
332   res = new_ir_node(NULL, irg, bl, op_imm, get_irn_mode(cnst), 0, ins);
333   attr = (imm_attr_t *) &res->attr;
334
335   switch (get_irn_opcode(cnst)) {
336     case iro_Const:
337       attr->tp      = imm_Const;
338       attr->data.cnst_attr = get_irn_const_attr(cnst);
339       break;
340     case iro_SymConst:
341       attr->tp             = imm_SymConst;
342       attr->data.symc_attr = get_irn_symconst_attr(cnst);
343       break;
344     case iro_Unknown:
345       break;
346     default:
347       assert(0 && "Cannot create Imm for this opcode");
348   }
349
350   return res;
351 }
352
353 static void prepare_walker(ir_node *irn, void *data)
354 {
355         opcode opc = get_irn_opcode(irn);
356
357         /* A replacement for this node has already been computed. */
358         if(get_irn_link(irn))
359                 return;
360
361         if(opc == iro_Call) {
362                 ir_node *bl   = get_nodes_block(irn);
363                 ir_graph *irg = get_irn_irg(bl);
364
365                 ir_node *store   = get_Call_mem(irn);
366                 ir_node *ptr     = get_Call_ptr(irn);
367                 ir_type *ct      = get_Call_type(irn);
368                 int np           = get_Call_n_params(irn) > 0 ? 1 : 0;
369
370                 if(np > 0) {
371                         ir_node *ins[1];
372                         char buf[128];
373                         ir_node *nc;
374                         int i, n = get_Call_n_params(irn);
375                         ir_type *nt;
376       unsigned cc = get_method_calling_convention(get_Call_type(irn));
377
378       if (cc & cc_last_on_top) {
379                           store = new_Push(irg, bl, store, get_Call_param(irn, 0));
380
381                           for (i = 1; i < n; ++i)
382                                   store = new_Push(irg, bl, store, get_Call_param(irn, i));
383       }
384       else {
385         store = new_Push(irg, bl, store, get_Call_param(irn, n - 1));
386
387         for (i = n - 2; i >= 0; --i)
388           store = new_Push(irg, bl, store, get_Call_param(irn, i));
389       }
390
391                         snprintf(buf, sizeof(buf), "push_%s", get_type_name(ct));
392
393                         n = get_method_n_ress(ct);
394                         nt = new_type_method(new_id_from_str(buf), 0, n);
395                         for(i = 0; i < n; ++i)
396                                 set_method_res_type(nt, i, get_method_res_type(ct, i));
397
398                         nc = new_r_Call(irg, bl, store, ptr, 0, ins, nt);
399                         exchange(irn, nc);
400                         set_irn_link(nc, nc);
401                 }
402         }
403 }
404
405 static void localize_const_walker(ir_node *irn, void *data)
406 {
407         if(!is_Block(irn)) {
408                 int i, n;
409                 ir_node *bl = get_nodes_block(irn);
410
411                 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
412                         ir_node *op    = get_irn_n(irn, i);
413                         opcode opc     = get_irn_opcode(op);
414
415                         if(opc == iro_Const
416                         || opc == iro_Unknown
417                         || (opc == iro_SymConst /*&& get_SymConst_kind(op) == symconst_addr_ent*/)) {
418                                 ir_graph *irg   = get_irn_irg(bl);
419                                 ir_node *imm_bl = is_Phi(irn) ? get_Block_cfgpred_block(bl, i) : bl;
420
421                                 ir_node *imm = new_Imm(irg, imm_bl, op);
422                                 set_irn_n(irn, i, imm);
423                         }
424                 }
425         }
426 }
427
428 static const arch_irn_handler_t *firm_get_irn_handler(const void *self)
429 {
430         return &firm_irn_handler;
431 }
432
433 typedef struct _firm_code_gen_t {
434         const arch_code_generator_if_t *impl;
435         ir_graph *irg;
436 } firm_code_gen_t;
437
438
439 static void clear_link(ir_node *irn, void *data)
440 {
441         set_irn_link(irn, NULL);
442 }
443
444 static void firm_prepare_graph(void *self)
445 {
446         firm_code_gen_t *cg = self;
447
448         irg_walk_graph(cg->irg, clear_link, localize_const_walker, NULL);
449         irg_walk_graph(cg->irg, NULL, prepare_walker, NULL);
450 }
451
452 static void firm_before_sched(void *self)
453 {
454 }
455
456 static void imm_scheduler(ir_node *irn, void *env) {
457         if(is_Imm(irn)) {
458                 const ir_edge_t *e;
459                 ir_node *user, *user_block, *before, *tgt_block;
460
461                 if (1 != get_irn_n_edges(irn)) {
462                         printf("Out edges: %d\n", get_irn_n_edges(irn));
463                         assert(1 == get_irn_n_edges(irn));
464                 }
465
466                 e = get_irn_out_edge_first(irn);
467                 user = e->src;
468                 user_block = get_nodes_block(user);
469                 if (is_Phi(user)) {
470                         before = get_Block_cfgpred_block(user_block, e->pos);
471                         tgt_block = before;
472                 } else {
473                         before = user;
474                         tgt_block = user_block;
475                 }
476
477                 sched_remove(irn);
478                 set_nodes_block(irn, tgt_block);
479                 sched_add_before(before, irn);
480         }
481 }
482
483 static void firm_before_ra(void *self)
484 {
485         firm_code_gen_t *cg = self;
486         irg_walk_graph(cg->irg, imm_scheduler, NULL, NULL);
487 }
488
489 static void firm_codegen_done(void *self)
490 {
491         free(self);
492 }
493
494 static void *firm_cg_init(FILE *file_handle, ir_graph *irg, const arch_env_t *env);
495
496 static const arch_code_generator_if_t firm_code_gen = {
497         firm_cg_init,
498         firm_prepare_graph,
499         firm_before_sched,
500         firm_before_ra,
501         firm_codegen_done
502 };
503
504 static void *firm_cg_init(FILE *file_handle, ir_graph *irg, const arch_env_t *env)
505 {
506         firm_code_gen_t *cg = xmalloc(sizeof(*cg));
507         cg->impl = &firm_code_gen;
508         cg->irg  = irg;
509         return cg;
510 }
511
512
513 static const arch_code_generator_if_t *firm_get_code_generator(void *self)
514 {
515         return &firm_code_gen;
516 }
517
518 static const list_sched_selector_t *firm_get_list_sched_selector(const void *self) {
519         return trivial_selector;
520 }
521
522 #ifdef WITH_LIBCORE
523 static void firm_register_options(lc_opt_entry_t *ent)
524 {
525 }
526 #endif
527
528 const arch_isa_if_t firm_isa = {
529 #ifdef WITH_LIBCORE
530         firm_register_options,
531 #endif
532         firm_init,
533         firm_done,
534         firm_get_n_reg_class,
535         firm_get_reg_class,
536         firm_get_irn_handler,
537         firm_get_code_generator,
538         firm_get_list_sched_selector
539 };