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