changed targets for new_nodes.[ch] creation
[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 void firm_init(void)
43 {
44   static struct obstack obst;
45   static int inited = 0;
46   int k;
47
48   if(inited)
49     return;
50
51   inited = 1;
52   obstack_init(&obst);
53
54   for(k = 0; k < N_CLASSES; ++k) {
55     const arch_register_class_t *cls = &reg_classes[k];
56     int i;
57
58     for(i = 0; i < cls->n_regs; ++i) {
59       int n;
60       char buf[8];
61       char *name;
62       arch_register_t *reg = (arch_register_t *) &cls->regs[i];
63
64       n = snprintf(buf, sizeof(buf), "r%d", i);
65       name = obstack_copy0(&obst, buf, n);
66
67       reg->name = name;
68       reg->reg_class = cls;
69       reg->index = i;
70       reg->type = 0;
71     }
72   }
73
74         /*
75          * Create some opcodes and types to let firm look a little
76          * bit more like real machines.
77          */
78         if(!op_push) {
79                 rflct_sig_t *sig;
80                 int push_opc = get_next_ir_opcode();
81
82                 op_push = new_ir_op(push_opc, "Push",
83                                 op_pin_state_pinned, 0, oparity_binary, 0, 0, NULL);
84
85                 sig = rflct_signature_allocate(1, 3);
86                 rflct_signature_set_arg(sig, 0, 0, "Store", RFLCT_MC(Mem), 0, 0);
87                 rflct_signature_set_arg(sig, 1, 0, "Block", RFLCT_MC(BB), 0, 0);
88                 rflct_signature_set_arg(sig, 1, 1, "Store", RFLCT_MC(Mem), 0, 0);
89                 rflct_signature_set_arg(sig, 1, 2, "Arg", RFLCT_MC(Datab), 0, 0);
90
91                 rflct_new_opcode(push_opc, "Push", false);
92                 rflct_opcode_add_signature(push_opc, sig);
93         }
94
95         if(!op_imm) {
96                 rflct_sig_t *sig;
97                 int imm_opc = get_next_ir_opcode();
98
99                 op_imm = new_ir_op(imm_opc, "Imm",
100                                 op_pin_state_pinned, 0, oparity_zero, 0, sizeof(imm_attr_t), NULL);
101
102                 sig = rflct_signature_allocate(1, 1);
103                 rflct_signature_set_arg(sig, 0, 0, "Imm", RFLCT_MC(Data), 0, 0);
104                 rflct_signature_set_arg(sig, 1, 0, "Block", RFLCT_MC(BB), 0, 0);
105                 rflct_new_opcode(imm_opc, "Imm", false);
106                 rflct_opcode_add_signature(imm_opc, sig);
107         }
108 }
109
110 static int firm_get_n_reg_class(void)
111 {
112   return N_CLASSES;
113 }
114
115 static const arch_register_class_t *firm_get_reg_class(int i)
116 {
117   assert(i >= 0 && i < N_CLASSES);
118   return &reg_classes[i];
119 }
120
121 static const arch_register_req_t firm_std_reg_req = {
122   arch_register_req_type_normal,
123   &reg_classes[CLS_DATAB],
124   { NULL }
125 };
126
127 static const rflct_arg_t *get_arg(const ir_node *irn, int pos)
128 {
129   int sig = rflct_get_signature(irn);
130   const rflct_arg_t *args =
131     rflct_get_args(get_irn_opcode(irn), sig, arch_pos_is_in(pos));
132   return &args[arch_pos_get_index(pos)];
133 }
134
135 static const arch_register_req_t *
136 firm_get_irn_reg_req(const arch_irn_ops_t *self,
137     arch_register_req_t *req, const ir_node *irn, int pos)
138 {
139   if(is_firm_be_mode(get_irn_mode(irn)))
140     memcpy(req, &firm_std_reg_req, sizeof(*req));
141   else
142     req = NULL;
143
144   return req;
145 }
146
147 static int firm_get_n_operands(const arch_irn_ops_t *self, const ir_node *irn, int in_out)
148 {
149   int sig;
150
151         while(is_Proj(irn))
152                 irn = get_Proj_pred(irn);
153
154         sig = rflct_get_signature(irn);
155   return rflct_get_args_count(get_irn_opcode(irn), sig, in_out >= 0);
156 }
157
158 struct irn_reg_assoc {
159   const ir_node *irn;
160   int pos;
161   const arch_register_t *reg;
162 };
163
164 static int cmp_irn_reg_assoc(const void *a, const void *b, size_t len)
165 {
166   const struct irn_reg_assoc *x = a;
167   const struct irn_reg_assoc *y = b;
168
169   return !(x->irn == y->irn && x->pos == y->pos);
170 }
171
172 static struct irn_reg_assoc *get_irn_reg_assoc(const ir_node *irn, int pos)
173 {
174   static set *reg_set = NULL;
175   struct irn_reg_assoc templ;
176   unsigned int hash;
177
178   if(!reg_set)
179     reg_set = new_set(cmp_irn_reg_assoc, 1024);
180
181   templ.irn = irn;
182   templ.pos = pos;
183   templ.reg = NULL;
184   hash = HASH_PTR(irn) + 7 * pos;
185
186   return set_insert(reg_set, &templ, sizeof(templ), hash);
187 }
188
189 static void firm_set_irn_reg(const arch_irn_ops_t *self, ir_node *irn,
190     int pos, const arch_register_t *reg)
191 {
192   struct irn_reg_assoc *assoc = get_irn_reg_assoc(irn, pos);
193   assoc->reg = reg;
194 }
195
196 static const arch_register_t *firm_get_irn_reg(const arch_irn_ops_t *self,
197     const ir_node *irn, int pos)
198 {
199   struct irn_reg_assoc *assoc = get_irn_reg_assoc(irn, pos);
200   return assoc->reg;
201 }
202
203 static arch_irn_class_t firm_classify(const arch_irn_ops_t *self, const ir_node *irn)
204 {
205     arch_irn_class_t res;
206
207     switch(get_irn_opcode(irn)) {
208         case iro_Cond:
209         case iro_Jmp:
210             res = arch_irn_class_branch;
211             break;
212         default:
213             res = arch_irn_class_normal;
214     }
215
216         return res;
217 }
218
219 static arch_irn_flags_t firm_get_flags(const arch_irn_ops_t *self, const ir_node *irn)
220 {
221         arch_irn_flags_t res = arch_irn_flags_spillable;
222
223         if(get_irn_op(irn) == op_imm)
224                 res |= arch_irn_flags_rematerializable;
225
226         switch(get_irn_opcode(irn)) {
227                 case iro_Add:
228                 case iro_Sub:
229                 case iro_Shl:
230                 case iro_Shr:
231                 case iro_Shrs:
232                 case iro_And:
233                 case iro_Or:
234                 case iro_Eor:
235                 case iro_Not:
236                         res |= arch_irn_flags_rematerializable;
237                 default:
238                         res = res;
239         }
240
241         return res;
242 }
243
244 static const arch_irn_ops_t irn_ops = {
245   firm_get_irn_reg_req,
246   firm_get_n_operands,
247   firm_set_irn_reg,
248   firm_get_irn_reg,
249   firm_classify,
250         firm_get_flags
251 };
252
253 static const arch_irn_ops_t *firm_get_irn_ops(const arch_irn_handler_t *self,
254     const ir_node *irn)
255 {
256   return &irn_ops;
257 }
258
259 const arch_irn_handler_t firm_irn_handler = {
260   firm_get_irn_ops,
261 };
262
263 static ir_node *new_Push(ir_graph *irg, ir_node *bl, ir_node *push, ir_node *arg)
264 {
265         ir_node *ins[2];
266         ins[0] = push;
267         ins[1] = arg;
268         return new_ir_node(NULL, irg, bl, op_push, mode_M, 2, ins);
269 }
270
271 static ir_node *new_Imm(ir_graph *irg, ir_node *bl, ir_node *cnst)
272 {
273         ir_node *ins[1];
274         ir_node *res;
275         imm_attr_t *attr;
276
277         res = new_ir_node(NULL, irg, bl, op_imm, get_irn_mode(cnst), 0, ins);
278         attr = (imm_attr_t *) &res->attr;
279
280         switch (get_irn_opcode(cnst)) {
281                 case iro_Const:
282                         attr->tp = imm_Const;
283                         attr->data.tv = get_Const_tarval(cnst);
284                         break;
285                 case iro_SymConst:
286                         attr->tp = imm_SymConst;
287                         //attr->data.ent = get_SymConst_entity(cnst);
288                         break;
289                 case iro_Unknown:
290                         break;
291                 default:                assert(0 && "Cannot create Imm for this opcode");
292         }
293
294         return res;
295 }
296
297 int is_Imm(const ir_node *irn) {
298         return get_irn_op(irn) == op_imm;
299 }
300
301 static void prepare_walker(ir_node *irn, void *data)
302 {
303         opcode opc = get_irn_opcode(irn);
304
305         /* A replacement for this node has already been computed. */
306         if(get_irn_link(irn))
307                 return;
308
309         if(opc == iro_Call) {
310                 ir_node *bl   = get_nodes_block(irn);
311                 ir_graph *irg = get_irn_irg(bl);
312
313                 ir_node *store   = get_Call_mem(irn);
314                 ir_node *ptr     = get_Call_ptr(irn);
315                 type *ct         = get_Call_type(irn);
316                 int np           = get_Call_n_params(irn) > 0 ? 1 : 0;
317
318                 if(np > 0) {
319                         ir_node *ins[1];
320                         char buf[128];
321                         ir_node *nc;
322                         ir_node *push;
323                         int i, n;
324                         type *nt;
325
326                         store = new_Push(irg, bl, store, get_Call_param(irn, 0));
327
328                         for(i = 1, n = get_Call_n_params(irn); i < n; ++i) {
329                                 store = new_Push(irg, bl, store, get_Call_param(irn, i));
330                         }
331
332                         snprintf(buf, sizeof(buf), "push_%s", get_type_name(ct));
333
334                         n = get_method_n_ress(ct);
335                         nt = new_type_method(new_id_from_str(buf), 0, n);
336                         for(i = 0; i < n; ++i)
337                                 set_method_res_type(nt, i, get_method_res_type(ct, i));
338
339                         nc = new_r_Call(irg, bl, store, ptr, 0, ins, nt);
340                         exchange(irn, nc);
341                         set_irn_link(nc, nc);
342                 }
343         }
344 }
345
346 static void localize_const_walker(ir_node *irn, void *data)
347 {
348         if(!is_Block(irn)) {
349                 int i, n;
350                 ir_node *bl = get_nodes_block(irn);
351
352                 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
353                         ir_node *op    = get_irn_n(irn, i);
354                         opcode opc     = get_irn_opcode(op);
355
356                         if(opc == iro_Const
357                         || opc == iro_Unknown
358                         || (opc == iro_SymConst /*&& get_SymConst_kind(op) == symconst_addr_ent*/)) {
359                                 ir_graph *irg   = get_irn_irg(bl);
360                                 ir_node *imm_bl = is_Phi(irn) ? get_Block_cfgpred_block(bl, i) : bl;
361
362                                 ir_node *imm = new_Imm(irg, imm_bl, op);
363                                 set_irn_n(irn, i, imm);
364                         }
365                 }
366         }
367 }
368
369 static void clear_link(ir_node *irn, void *data)
370 {
371         set_irn_link(irn, NULL);
372 }
373
374 static void firm_prepare_graph(ir_graph *irg)
375 {
376         irg_walk_graph(irg, clear_link, localize_const_walker, NULL);
377         irg_walk_graph(irg, NULL, prepare_walker, NULL);
378 }
379
380 const arch_isa_if_t firm_isa = {
381   firm_init,
382   firm_get_n_reg_class,
383   firm_get_reg_class,
384         firm_prepare_graph
385 };