Added new ifg interface
[libfirm] / ir / be / 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 #include "bitset.h"
10 #include "obst.h"
11
12 #include "irmode_t.h"
13 #include "irnode_t.h"
14 #include "irgmod.h"
15 #include "ircons_t.h"
16 #include "irgwalk.h"
17 #include "type.h"
18
19 #include "bearch.h"
20 #include "beutil.h"
21
22 #include "irreflect.h"
23
24 #include "bearch_firm.h"
25
26 #define N_REGS 3
27
28 static arch_register_t datab_regs[N_REGS];
29
30 static arch_register_class_t reg_classes[] = {
31   { "datab", N_REGS, datab_regs },
32 };
33
34 static ir_op *op_push;
35 static ir_op *op_imm;
36
37 #define N_CLASSES \
38   (sizeof(reg_classes) / sizeof(reg_classes[0]))
39
40 #define CLS_DATAB 0
41
42 static int dump_node_Imm(ir_node *n, FILE *F, dump_reason_t reason) {
43   ir_mode    *mode;
44   int        bad = 0;
45   char       buf[1024];
46   tarval     *tv;
47   imm_attr_t *attr;
48
49   switch (reason) {
50     case dump_node_opcode_txt:
51       tv = get_Imm_tv(n);
52
53       if (tv) {
54         tarval_snprintf(buf, sizeof(buf), tv);
55         fprintf(F, "%s", buf);
56       }
57       else {
58         fprintf(F, "immSymC");
59       }
60       break;
61
62     case dump_node_mode_txt:
63       mode = get_irn_mode(n);
64
65       if (mode && mode != mode_BB && mode != mode_ANY && mode != mode_BAD && mode != mode_T) {
66         fprintf(F, "[%s]", get_mode_name(mode));
67       }
68       break;
69
70     case dump_node_nodeattr_txt:
71       attr = (imm_attr_t *)get_irn_generic_attr(n);
72
73       if (is_Imm(n) && attr->tp == imm_SymConst) {
74         const char *name    = NULL;
75         ir_node    *old_sym = attr->data.symconst;
76
77         switch (get_SymConst_kind(old_sym)) {
78           case symconst_addr_name:
79             name = get_id_str(get_SymConst_name(old_sym));
80             break;
81
82           case symconst_addr_ent:
83             name = get_entity_ld_name(get_SymConst_entity(old_sym));
84             break;
85
86           default:
87             assert(!"Unsupported SymConst");
88         }
89
90         fprintf(F, "&%s ", name);
91       }
92
93       break;
94
95     case dump_node_info_txt:
96       break;
97   }
98
99   return bad;
100 }
101
102 static void firm_init(void)
103 {
104   static struct obstack obst;
105   static int inited = 0;
106   int k;
107
108   if(inited)
109     return;
110
111   inited = 1;
112   obstack_init(&obst);
113
114   for(k = 0; k < N_CLASSES; ++k) {
115     const arch_register_class_t *cls = &reg_classes[k];
116     int i;
117
118     for(i = 0; i < cls->n_regs; ++i) {
119       int n;
120       char buf[8];
121       char *name;
122       arch_register_t *reg = (arch_register_t *) &cls->regs[i];
123
124       n = snprintf(buf, sizeof(buf), "r%d", i);
125       name = obstack_copy0(&obst, buf, n);
126
127       reg->name = name;
128       reg->reg_class = cls;
129       reg->index = i;
130       reg->type = 0;
131     }
132   }
133
134         /*
135          * Create some opcodes and types to let firm look a little
136          * bit more like real machines.
137          */
138         if(!op_push) {
139                 rflct_sig_t *sig;
140                 int push_opc = get_next_ir_opcode();
141
142                 op_push = new_ir_op(push_opc, "Push",
143                                 op_pin_state_pinned, 0, oparity_binary, 0, 0, NULL);
144
145                 sig = rflct_signature_allocate(1, 3);
146                 rflct_signature_set_arg(sig, 0, 0, "Store", RFLCT_MC(Mem), 0, 0);
147                 rflct_signature_set_arg(sig, 1, 0, "Block", RFLCT_MC(BB), 0, 0);
148                 rflct_signature_set_arg(sig, 1, 1, "Store", RFLCT_MC(Mem), 0, 0);
149                 rflct_signature_set_arg(sig, 1, 2, "Arg", RFLCT_MC(Datab), 0, 0);
150
151                 rflct_new_opcode(push_opc, "Push", false);
152                 rflct_opcode_add_signature(push_opc, sig);
153         }
154
155         if(!op_imm) {
156                 rflct_sig_t *sig;
157                 int imm_opc = get_next_ir_opcode();
158                 ir_op_ops ops;
159
160                 memset(&ops, 0, sizeof(ops));
161                 ops.dump_node = dump_node_Imm;
162
163                 op_imm = new_ir_op(imm_opc, "Imm",
164                                 op_pin_state_pinned, 0, oparity_zero, 0, sizeof(imm_attr_t), &ops);
165
166                 sig = rflct_signature_allocate(1, 1);
167                 rflct_signature_set_arg(sig, 0, 0, "Imm", RFLCT_MC(Data), 0, 0);
168                 rflct_signature_set_arg(sig, 1, 0, "Block", RFLCT_MC(BB), 0, 0);
169                 rflct_new_opcode(imm_opc, "Imm", false);
170                 rflct_opcode_add_signature(imm_opc, sig);
171         }
172 }
173
174 static int firm_get_n_reg_class(void)
175 {
176   return N_CLASSES;
177 }
178
179 static const arch_register_class_t *firm_get_reg_class(int i)
180 {
181   assert(i >= 0 && i < N_CLASSES);
182   return &reg_classes[i];
183 }
184
185 static const arch_register_req_t firm_std_reg_req = {
186   arch_register_req_type_normal,
187   &reg_classes[CLS_DATAB],
188   { NULL }
189 };
190
191 static const rflct_arg_t *get_arg(const ir_node *irn, int pos)
192 {
193   int sig = rflct_get_signature(irn);
194   const rflct_arg_t *args =
195     rflct_get_args(get_irn_opcode(irn), sig, arch_pos_is_in(pos));
196   return &args[arch_pos_get_index(pos)];
197 }
198
199 static const arch_register_req_t *
200 firm_get_irn_reg_req(const arch_irn_ops_t *self,
201     arch_register_req_t *req, const ir_node *irn, int pos)
202 {
203   if(is_firm_be_mode(get_irn_mode(irn)))
204     memcpy(req, &firm_std_reg_req, sizeof(*req));
205   else
206     req = NULL;
207
208   return req;
209 }
210
211 static int firm_get_n_operands(const arch_irn_ops_t *self, const ir_node *irn, int in_out)
212 {
213   int sig;
214
215         while(is_Proj(irn))
216                 irn = get_Proj_pred(irn);
217
218         sig = rflct_get_signature(irn);
219   return rflct_get_args_count(get_irn_opcode(irn), sig, in_out >= 0);
220 }
221
222 struct irn_reg_assoc {
223   const ir_node *irn;
224   int pos;
225   const arch_register_t *reg;
226 };
227
228 static int cmp_irn_reg_assoc(const void *a, const void *b, size_t len)
229 {
230   const struct irn_reg_assoc *x = a;
231   const struct irn_reg_assoc *y = b;
232
233   return !(x->irn == y->irn && x->pos == y->pos);
234 }
235
236 static struct irn_reg_assoc *get_irn_reg_assoc(const ir_node *irn, int pos)
237 {
238   static set *reg_set = NULL;
239   struct irn_reg_assoc templ;
240   unsigned int hash;
241
242   if(!reg_set)
243     reg_set = new_set(cmp_irn_reg_assoc, 1024);
244
245   templ.irn = irn;
246   templ.pos = pos;
247   templ.reg = NULL;
248   hash = HASH_PTR(irn) + 7 * pos;
249
250   return set_insert(reg_set, &templ, sizeof(templ), hash);
251 }
252
253 static void firm_set_irn_reg(const arch_irn_ops_t *self, ir_node *irn,
254     int pos, const arch_register_t *reg)
255 {
256   struct irn_reg_assoc *assoc = get_irn_reg_assoc(irn, pos);
257   assoc->reg = reg;
258 }
259
260 static const arch_register_t *firm_get_irn_reg(const arch_irn_ops_t *self,
261     const ir_node *irn, int pos)
262 {
263   struct irn_reg_assoc *assoc = get_irn_reg_assoc(irn, pos);
264   return assoc->reg;
265 }
266
267 static arch_irn_class_t firm_classify(const arch_irn_ops_t *self, const ir_node *irn)
268 {
269     arch_irn_class_t res;
270
271     switch(get_irn_opcode(irn)) {
272         case iro_Cond:
273         case iro_Jmp:
274             res = arch_irn_class_branch;
275             break;
276         default:
277             res = arch_irn_class_normal;
278     }
279
280         return res;
281 }
282
283 static arch_irn_flags_t firm_get_flags(const arch_irn_ops_t *self, const ir_node *irn)
284 {
285         arch_irn_flags_t res = arch_irn_flags_spillable;
286
287         if(get_irn_op(irn) == op_imm)
288                 res |= arch_irn_flags_rematerializable;
289
290         switch(get_irn_opcode(irn)) {
291                 case iro_Add:
292                 case iro_Sub:
293                 case iro_Shl:
294                 case iro_Shr:
295                 case iro_Shrs:
296                 case iro_And:
297                 case iro_Or:
298                 case iro_Eor:
299                 case iro_Not:
300                         res |= arch_irn_flags_rematerializable;
301                 default:
302                         res = res;
303         }
304
305         return res;
306 }
307
308 static const arch_irn_ops_t irn_ops = {
309   firm_get_irn_reg_req,
310   firm_get_n_operands,
311   firm_set_irn_reg,
312   firm_get_irn_reg,
313   firm_classify,
314         firm_get_flags
315 };
316
317 static const arch_irn_ops_t *firm_get_irn_ops(const arch_irn_handler_t *self,
318     const ir_node *irn)
319 {
320   return &irn_ops;
321 }
322
323 const arch_irn_handler_t firm_irn_handler = {
324   firm_get_irn_ops,
325 };
326
327 static ir_node *new_Push(ir_graph *irg, ir_node *bl, ir_node *push, ir_node *arg)
328 {
329         ir_node *ins[2];
330         ins[0] = push;
331         ins[1] = arg;
332         return new_ir_node(NULL, irg, bl, op_push, mode_M, 2, ins);
333 }
334
335 /**
336  * Creates an op_Imm node from an op_Const.
337  */
338 static ir_node *new_Imm(ir_graph *irg, ir_node *bl, ir_node *cnst) {
339   ir_node    *ins[1];
340   ir_node    *res;
341   imm_attr_t *attr;
342
343   res = new_ir_node(NULL, irg, bl, op_imm, get_irn_mode(cnst), 0, ins);
344   attr = (imm_attr_t *) &res->attr;
345
346   switch (get_irn_opcode(cnst)) {
347     case iro_Const:
348       attr->tp      = imm_Const;
349       attr->data.tv = get_Const_tarval(cnst);
350       break;
351     case iro_SymConst:
352       attr->tp            = imm_SymConst;
353       attr->data.symconst = cnst;
354       break;
355     case iro_Unknown:
356       break;
357     default:
358       assert(0 && "Cannot create Imm for this opcode");
359   }
360
361   return res;
362 }
363
364 int is_Imm(const ir_node *irn) {
365   return get_irn_op(irn) == op_imm;
366 }
367
368 /**
369  * Returns the tarval from an Imm node or NULL in case of a SymConst
370  */
371 tarval *get_Imm_tv(ir_node *irn) {
372   imm_attr_t *attr;
373
374   assert(is_Imm(irn) && "Cannot get tv from non-Imm");
375   attr = (imm_attr_t *)get_irn_generic_attr(irn);
376   if (attr->tp == imm_Const) {
377     return attr->data.tv;
378   }
379   else
380     return NULL;
381 }
382
383 /**
384  * Returns the SymConst from an Imm node or NULL in case of a Const
385  */
386 ir_node *get_Imm_sc(ir_node *irn) {
387   imm_attr_t *attr;
388
389   assert(is_Imm(irn) && "Cannot get SymConst from non-Imm");
390   attr = (imm_attr_t *)get_irn_generic_attr(irn);
391   if (attr->tp == imm_SymConst) {
392     return attr->data.symconst;
393   }
394   else
395     return NULL;
396 }
397
398
399 static void prepare_walker(ir_node *irn, void *data)
400 {
401         opcode opc = get_irn_opcode(irn);
402
403         /* A replacement for this node has already been computed. */
404         if(get_irn_link(irn))
405                 return;
406
407         if(opc == iro_Call) {
408                 ir_node *bl   = get_nodes_block(irn);
409                 ir_graph *irg = get_irn_irg(bl);
410
411                 ir_node *store   = get_Call_mem(irn);
412                 ir_node *ptr     = get_Call_ptr(irn);
413                 type *ct         = get_Call_type(irn);
414                 int np           = get_Call_n_params(irn) > 0 ? 1 : 0;
415
416                 if(np > 0) {
417                         ir_node *ins[1];
418                         char buf[128];
419                         ir_node *nc;
420                         ir_node *push;
421                         int i, n = get_Call_n_params(irn);
422                         type *nt;
423       unsigned cc = get_method_calling_convention(get_Call_type(irn));
424
425       if (cc & cc_last_on_top) {
426                           store = new_Push(irg, bl, store, get_Call_param(irn, 0));
427
428                           for (i = 1; i < n; ++i)
429                                   store = new_Push(irg, bl, store, get_Call_param(irn, i));
430       }
431       else {
432         store = new_Push(irg, bl, store, get_Call_param(irn, n - 1));
433
434         for (i = n - 2; i >= 0; --i)
435           store = new_Push(irg, bl, store, get_Call_param(irn, i));
436       }
437
438                         snprintf(buf, sizeof(buf), "push_%s", get_type_name(ct));
439
440                         n = get_method_n_ress(ct);
441                         nt = new_type_method(new_id_from_str(buf), 0, n);
442                         for(i = 0; i < n; ++i)
443                                 set_method_res_type(nt, i, get_method_res_type(ct, i));
444
445                         nc = new_r_Call(irg, bl, store, ptr, 0, ins, nt);
446                         exchange(irn, nc);
447                         set_irn_link(nc, nc);
448                 }
449         }
450 }
451
452 static void localize_const_walker(ir_node *irn, void *data)
453 {
454         if(!is_Block(irn)) {
455                 int i, n;
456                 ir_node *bl = get_nodes_block(irn);
457
458                 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
459                         ir_node *op    = get_irn_n(irn, i);
460                         opcode opc     = get_irn_opcode(op);
461
462                         if(opc == iro_Const
463                         || opc == iro_Unknown
464                         || (opc == iro_SymConst /*&& get_SymConst_kind(op) == symconst_addr_ent*/)) {
465                                 ir_graph *irg   = get_irn_irg(bl);
466                                 ir_node *imm_bl = is_Phi(irn) ? get_Block_cfgpred_block(bl, i) : bl;
467
468                                 ir_node *imm = new_Imm(irg, imm_bl, op);
469                                 set_irn_n(irn, i, imm);
470                         }
471                 }
472         }
473 }
474
475 static void clear_link(ir_node *irn, void *data)
476 {
477         set_irn_link(irn, NULL);
478 }
479
480 static void firm_prepare_graph(ir_graph *irg)
481 {
482         irg_walk_graph(irg, clear_link, localize_const_walker, NULL);
483         irg_walk_graph(irg, NULL, prepare_walker, NULL);
484 }
485
486 const arch_isa_if_t firm_isa = {
487         firm_init,
488         firm_get_n_reg_class,
489         firm_get_reg_class,
490         firm_prepare_graph,
491         &firm_irn_handler
492 };