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