made code C89 compliant
[libfirm] / ir / be / arm / arm_new_nodes.c
1 /**
2  * This file implements the creation of the achitecture specific firm opcodes
3  * and the coresponding node constructors for the arm assembler irg.
4  * $Id$
5  */
6 #ifdef HAVE_CONFIG_H
7 #include "config.h"
8 #endif
9
10 #ifdef _WIN32
11 #include <malloc.h>
12 #else
13 #include <alloca.h>
14 #endif
15
16 #include <stdlib.h>
17
18 #include "irprog_t.h"
19 #include "irgraph_t.h"
20 #include "irnode_t.h"
21 #include "irmode_t.h"
22 #include "ircons_t.h"
23 #include "iropt_t.h"
24 #include "irop.h"
25 #include "firm_common_t.h"
26 #include "irvrfy_t.h"
27 #include "irprintf.h"
28
29 #include "../bearch.h"
30
31 #include "arm_nodes_attr.h"
32 #include "arm_new_nodes.h"
33 #include "gen_arm_regalloc_if.h"
34
35 #include "../beabi.h"
36 #include "bearch_arm_t.h"
37
38
39 /***********************************************************************************
40  *      _                                   _       _             __
41  *     | |                                 (_)     | |           / _|
42  *   __| |_   _ _ __ ___  _ __   ___ _ __   _ _ __ | |_ ___ _ __| |_ __ _  ___ ___
43  *  / _` | | | | '_ ` _ \| '_ \ / _ \ '__| | | '_ \| __/ _ \ '__|  _/ _` |/ __/ _ \
44  * | (_| | |_| | | | | | | |_) |  __/ |    | | | | | ||  __/ |  | || (_| | (_|  __/
45  *  \__,_|\__,_|_| |_| |_| .__/ \___|_|    |_|_| |_|\__\___|_|  |_| \__,_|\___\___|
46  *                       | |
47  *                       |_|
48  ***********************************************************************************/
49
50 /**
51  * Returns a string containing the names of all registers within the limited bitset
52  */
53 static char *get_limited_regs(const arch_register_req_t *req, char *buf, int max) {
54         bitset_t *bs   = bitset_alloca(req->cls->n_regs);
55         char     *p    = buf;
56         int       size = 0;
57         int       i, cnt;
58
59         req->limited(NULL, bs);
60
61         for (i = 0; i < req->cls->n_regs; i++) {
62                 if (bitset_is_set(bs, i)) {
63                         cnt = snprintf(p, max - size, " %s", req->cls->regs[i].name);
64                         if (cnt < 0) {
65                                 fprintf(stderr, "dumper problem, exiting\n");
66                                 exit(1);
67                         }
68
69                         p    += cnt;
70                         size += cnt;
71
72                         if (size >= max)
73                                 break;
74                 }
75         }
76
77         return buf;
78 }
79
80 /**
81  * Dumps the register requirements for either in or out.
82  */
83 static void dump_reg_req(FILE *F, ir_node *n, const arm_register_req_t **reqs, int inout) {
84         char *dir = inout ? "out" : "in";
85         int   max = inout ? get_arm_n_res(n) : get_irn_arity(n);
86         char *buf = alloca(1024);
87         int   i;
88
89         memset(buf, 0, 1024);
90
91         if (reqs) {
92                 for (i = 0; i < max; i++) {
93                         fprintf(F, "%sreq #%d =", dir, i);
94
95                         if (reqs[i]->req.type == arch_register_req_type_none) {
96                                 fprintf(F, " n/a");
97                         }
98
99                         if (reqs[i]->req.type & arch_register_req_type_normal) {
100                                 fprintf(F, " %s", reqs[i]->req.cls->name);
101                         }
102
103                         if (reqs[i]->req.type & arch_register_req_type_limited) {
104                                 fprintf(F, " %s", get_limited_regs(&reqs[i]->req, buf, 1024));
105                         }
106
107                         if (reqs[i]->req.type & arch_register_req_type_should_be_same) {
108                                 ir_fprintf(F, " same as %+F", get_irn_n(n, reqs[i]->same_pos));
109                         }
110
111                         if (reqs[i]->req.type & arch_register_req_type_should_be_different) {
112                                 ir_fprintf(F, " different from %+F", get_irn_n(n, reqs[i]->different_pos));
113                         }
114
115                         fprintf(F, "\n");
116                 }
117
118                 fprintf(F, "\n");
119         }
120         else {
121                 fprintf(F, "%sreq = N/A\n", dir);
122         }
123 }
124
125
126 /**
127  * Dumper interface for dumping arm nodes in vcg.
128  * @param n        the node to dump
129  * @param F        the output file
130  * @param reason   indicates which kind of information should be dumped
131  * @return 0 on success or != 0 on failure
132  */
133 static int dump_node_arm(ir_node *n, FILE *F, dump_reason_t reason) {
134         ir_mode     *mode = NULL;
135         int          bad  = 0;
136         int          i;
137         arm_attr_t *attr;
138         const arm_register_req_t **reqs;
139         const arch_register_t     **slots;
140
141         switch (reason) {
142                 case dump_node_opcode_txt:
143                         fprintf(F, "%s", get_irn_opname(n));
144                         break;
145
146                 case dump_node_mode_txt:
147                         mode = get_irn_mode(n);
148
149                         if (mode) {
150                                 fprintf(F, "[%s]", get_mode_name(mode));
151                         }
152                         else {
153                                 fprintf(F, "[?NOMODE?]");
154                         }
155                         break;
156
157                 case dump_node_nodeattr_txt:
158
159                         /* TODO: dump some attributes which should show up */
160                         /* in node name in dump (e.g. consts or the like)  */
161
162                         break;
163
164                 case dump_node_info_txt:
165                         attr = get_arm_attr(n);
166                         fprintf(F, "=== arm attr begin ===\n");
167
168                         /* dump IN requirements */
169                         if (get_irn_arity(n) > 0) {
170                                 reqs = get_arm_in_req_all(n);
171                                 dump_reg_req(F, n, reqs, 0);
172                         }
173
174                         /* dump OUT requirements */
175                         if (attr->n_res > 0) {
176                                 reqs = get_arm_out_req_all(n);
177                                 dump_reg_req(F, n, reqs, 1);
178                         }
179
180                         /* dump assigned registers */
181                         slots = get_arm_slots(n);
182                         if (slots && attr->n_res > 0) {
183                                 for (i = 0; i < attr->n_res; i++) {
184                                         if (slots[i]) {
185                                                 fprintf(F, "reg #%d = %s\n", i, slots[i]->name);
186                                         }
187                                         else {
188                                                 fprintf(F, "reg #%d = n/a\n", i);
189                                         }
190                                 }
191                         }
192                         fprintf(F, "\n");
193
194                         /* dump n_res */
195                         fprintf(F, "n_res = %d\n", get_arm_n_res(n));
196
197                         /* dump flags */
198                         fprintf(F, "flags =");
199                         if (attr->flags == arch_irn_flags_none) {
200                                 fprintf(F, " none");
201                         }
202                         else {
203                                 if (attr->flags & arch_irn_flags_dont_spill) {
204                                         fprintf(F, " unspillable");
205                                 }
206                                 if (attr->flags & arch_irn_flags_rematerializable) {
207                                         fprintf(F, " remat");
208                                 }
209                                 if (attr->flags & arch_irn_flags_ignore) {
210                                         fprintf(F, " ignore");
211                                 }
212                         }
213                         fprintf(F, " (%d)\n", attr->flags);
214
215                         if (get_arm_value(n)) {
216                                 if (is_arm_CopyB(n)) {
217                                         fprintf(F, "size = %u\n", get_tarval_long(get_arm_value(n)));
218                                 } else {
219                                         if (mode_is_float(get_irn_mode(n))) {
220                                                 fprintf(F, "float value = (%lf)\n", get_tarval_double(get_arm_value(n)));
221                                         } else if (mode_is_int(get_irn_mode(n))) {
222                                                 long v =  get_tarval_long(get_arm_value(n));
223                                                 fprintf(F, "long value = %ld (0x%08lx)\n", v, v);
224                                         } else if (mode_is_reference(get_irn_mode(n))) {
225                                                 fprintf(F, "pointer\n");
226                                         } else {
227                                                 assert(0 && "unbehandelter Typ im const-Knoten");
228                                         }
229                                 }
230                         }
231                         if (get_arm_proj_num(n) >= 0) {
232                                 fprintf(F, "proj_num = (%d)\n", get_arm_proj_num(n));
233                         }
234                         /* TODO: dump all additional attributes */
235
236                         fprintf(F, "=== arm attr end ===\n");
237                         /* end of: case dump_node_info_txt */
238                         break;
239         }
240
241
242         return bad;
243 }
244
245
246
247 /***************************************************************************************************
248  *        _   _                   _       __        _                    _   _               _
249  *       | | | |                 | |     / /       | |                  | | | |             | |
250  *   __ _| |_| |_ _ __   ___  ___| |_   / /_ _  ___| |_   _ __ ___   ___| |_| |__   ___   __| |___
251  *  / _` | __| __| '__| / __|/ _ \ __| / / _` |/ _ \ __| | '_ ` _ \ / _ \ __| '_ \ / _ \ / _` / __|
252  * | (_| | |_| |_| |    \__ \  __/ |_ / / (_| |  __/ |_  | | | | | |  __/ |_| | | | (_) | (_| \__ \
253  *  \__,_|\__|\__|_|    |___/\___|\__/_/ \__, |\___|\__| |_| |_| |_|\___|\__|_| |_|\___/ \__,_|___/
254  *                                        __/ |
255  *                                       |___/
256  ***************************************************************************************************/
257
258 /**
259  * Wraps get_irn_generic_attr() as it takes no const ir_node, so we need to do a cast.
260  * Firm was made by people hating const :-(
261  */
262 arm_attr_t *get_arm_attr(const ir_node *node) {
263         assert(is_arm_irn(node) && "need arm node to get attributes");
264         return (arm_attr_t *)get_irn_generic_attr((ir_node *)node);
265 }
266
267 /**
268  * Returns the argument register requirements of a arm node.
269  */
270 const arm_register_req_t **get_arm_in_req_all(const ir_node *node) {
271         arm_attr_t *attr = get_arm_attr(node);
272         return attr->in_req;
273 }
274
275 /**
276  * Returns the result register requirements of an arm node.
277  */
278 const arm_register_req_t **get_arm_out_req_all(const ir_node *node) {
279         arm_attr_t *attr = get_arm_attr(node);
280         return attr->out_req;
281 }
282
283 /**
284  * Returns the argument register requirement at position pos of an arm node.
285  */
286 const arm_register_req_t *get_arm_in_req(const ir_node *node, int pos) {
287         arm_attr_t *attr = get_arm_attr(node);
288         return attr->in_req[pos];
289 }
290
291 /**
292  * Returns the result register requirement at position pos of an arm node.
293  */
294 const arm_register_req_t *get_arm_out_req(const ir_node *node, int pos) {
295         arm_attr_t *attr = get_arm_attr(node);
296         return attr->out_req[pos];
297 }
298
299 /**
300  * Sets the OUT register requirements at position pos.
301  */
302 void set_arm_req_out(ir_node *node, const arm_register_req_t *req, int pos) {
303         arm_attr_t *attr   = get_arm_attr(node);
304         attr->out_req[pos] = req;
305 }
306
307 /**
308  * Sets the complete OUT requirements of node.
309  */
310 void set_arm_req_out_all(ir_node *node, const arm_register_req_t **reqs) {
311         arm_attr_t *attr = get_arm_attr(node);
312         attr->out_req    = reqs;
313 }
314
315 /**
316  * Sets the IN register requirements at position pos.
317  */
318 void set_arm_req_in(ir_node *node, const arm_register_req_t *req, int pos) {
319         arm_attr_t *attr  = get_arm_attr(node);
320         attr->in_req[pos] = req;
321 }
322
323 /**
324  * Returns the register flag of an arm node.
325  */
326 arch_irn_flags_t get_arm_flags(const ir_node *node) {
327         arm_attr_t *attr = get_arm_attr(node);
328         return attr->flags;
329 }
330
331 /**
332  * Sets the register flag of an arm node.
333  */
334 void set_arm_flags(const ir_node *node, arch_irn_flags_t flags) {
335         arm_attr_t *attr = get_arm_attr(node);
336         attr->flags      = flags;
337 }
338
339 /**
340  * Returns the result register slots of an arm node.
341  */
342 const arch_register_t **get_arm_slots(const ir_node *node) {
343         arm_attr_t *attr = get_arm_attr(node);
344         return attr->slots;
345 }
346
347 /**
348  * Returns the name of the OUT register at position pos.
349  */
350 const char *get_arm_out_reg_name(const ir_node *node, int pos) {
351         arm_attr_t *attr = get_arm_attr(node);
352
353         assert(is_arm_irn(node) && "Not an arm node.");
354         assert(pos < attr->n_res && "Invalid OUT position.");
355         assert(attr->slots[pos]  && "No register assigned");
356
357         return arch_register_get_name(attr->slots[pos]);
358 }
359
360 /**
361  * Returns the index of the OUT register at position pos within its register class.
362  */
363 int get_arm_out_regnr(const ir_node *node, int pos) {
364         arm_attr_t *attr = get_arm_attr(node);
365
366         assert(is_arm_irn(node) && "Not an arm node.");
367         assert(pos < attr->n_res && "Invalid OUT position.");
368         assert(attr->slots[pos]  && "No register assigned");
369
370         return arch_register_get_index(attr->slots[pos]);
371 }
372
373 /**
374  * Returns the OUT register at position pos.
375  */
376 const arch_register_t *get_arm_out_reg(const ir_node *node, int pos) {
377         arm_attr_t *attr = get_arm_attr(node);
378
379         assert(is_arm_irn(node) && "Not an arm node.");
380         assert(pos < attr->n_res && "Invalid OUT position.");
381         assert(attr->slots[pos]  && "No register assigned");
382
383         return attr->slots[pos];
384 }
385
386 /**
387  * Sets the number of results.
388  */
389 void set_arm_n_res(ir_node *node, int n_res) {
390         arm_attr_t *attr = get_arm_attr(node);
391         attr->n_res      = n_res;
392 }
393
394 /**
395  * Returns the number of results.
396  */
397 int get_arm_n_res(const ir_node *node) {
398         arm_attr_t *attr = get_arm_attr(node);
399         return attr->n_res;
400 }
401 /**
402  * Returns the tarvalue
403  */
404 tarval *get_arm_value(const ir_node *node) {
405         arm_attr_t *attr = get_arm_attr(node);
406         return attr->value;
407 }
408
409 /**
410  * Sets the tarvalue
411  */
412 void set_arm_value(ir_node *node, tarval *tv) {
413         arm_attr_t *attr = get_arm_attr(node);
414         attr->value = tv;
415 }
416
417 /**
418  * Returns the proj num
419  */
420 int get_arm_proj_num(const ir_node *node) {
421         arm_attr_t *attr = get_arm_attr(node);
422         return attr->proj_num;
423 }
424
425 /**
426  * Sets the proj num
427  */
428 void set_arm_proj_num(ir_node *node, int proj_num) {
429         arm_attr_t *attr = get_arm_attr(node);
430         attr->proj_num      = proj_num;
431 }
432
433 /**
434  * Returns the SymConst label
435  */
436 const char *get_arm_symconst_label(ir_node *node) {
437         arm_attr_t *attr = get_arm_attr(node);
438         return attr->symconst_label;
439 }
440
441 /**
442  * Sets the SymConst label
443  */
444 void set_arm_symconst_label(ir_node *node, const char *symconst_label) {
445         arm_attr_t *attr = get_arm_attr(node);
446         attr->symconst_label = symconst_label;
447 }
448
449
450 /**
451  * Returns the number of projs.
452  */
453 int get_arm_n_projs(ir_node *node) {
454         arm_attr_t *attr = get_arm_attr(node);
455         return attr->n_projs;
456 }
457
458 /**
459  * Sets the number of projs.
460  */
461 void set_arm_n_projs(ir_node *node, int n_projs) {
462         arm_attr_t *attr = get_arm_attr(node);
463         attr->n_projs = n_projs;
464 }
465
466 /**
467  * Returns the default_proj_num.
468  */
469 long get_arm_default_proj_num(ir_node *node) {
470         arm_attr_t *attr = get_arm_attr(node);
471         return attr->default_proj_num;
472 }
473
474 /**
475  * Sets the default_proj_num.
476  */
477 void set_arm_default_proj_num(ir_node *node, long default_proj_num) {
478         arm_attr_t *attr = get_arm_attr(node);
479         attr->default_proj_num = default_proj_num;
480 }
481
482
483
484 void init_arm_attributes(ir_node *node, int flags, const arm_register_req_t ** in_reqs,
485                                                  const arm_register_req_t ** out_reqs, int n_res) {
486         arm_attr_t *attr = get_arm_attr(node);
487         attr->in_req = in_reqs;
488         attr->out_req = out_reqs;
489         attr->n_res = n_res;
490         attr->flags = flags;
491         attr->slots = xcalloc(n_res, sizeof(attr->slots[0]));
492         attr->value = NULL;
493         attr->proj_num = -42;
494         attr->symconst_label = NULL;
495         attr->n_projs = 0;
496         attr->default_proj_num = 0;
497 }
498
499 static int arm_comp_condJmp(arm_attr_t *attr_a, arm_attr_t *attr_b) {
500         return 1;
501 }
502
503 ir_node *arm_new_NoReg_gp(arm_code_gen_t *cg) {
504         return be_abi_get_callee_save_irn(cg->birg->abi, &arm_general_purpose_regs[REG_RXX]);
505 }
506
507 ir_node *arm_new_NoReg_fp(arm_code_gen_t *cg) {
508         return be_abi_get_callee_save_irn(cg->birg->abi, &arm_floating_point_regs[REG_FXX]);
509 }
510
511
512
513
514 /***************************************************************************************
515  *                  _                            _                   _
516  *                 | |                          | |                 | |
517  *  _ __   ___   __| | ___    ___ ___  _ __  ___| |_ _ __ _   _  ___| |_ ___  _ __ ___
518  * | '_ \ / _ \ / _` |/ _ \  / __/ _ \| '_ \/ __| __| '__| | | |/ __| __/ _ \| '__/ __|
519  * | | | | (_) | (_| |  __/ | (_| (_) | | | \__ \ |_| |  | |_| | (__| || (_) | |  \__ \
520  * |_| |_|\___/ \__,_|\___|  \___\___/|_| |_|___/\__|_|   \__,_|\___|\__\___/|_|  |___/
521  *
522  ***************************************************************************************/
523
524 /* limit the possible registers for sp in arm_StoreStackM4Inc */
525 static void limit_reg_arm_StoreStackM4Inc_sp(void *_unused, bitset_t *bs) {
526   bs = bitset_clear_all(bs);   /* disallow all register (positive constraints given) */
527   bitset_set(bs, 14);           /* allow r13 */
528   bitset_clear(bs, 13);         /* disallow ignore reg r12 */
529   bitset_clear(bs, 14);         /* disallow ignore reg r13 */
530   bitset_clear(bs, 15);         /* disallow ignore reg r15 */
531   bitset_clear(bs, 16);         /* disallow ignore reg rxx */
532   bitset_clear(bs, 17);         /* disallow ignore reg MURX */
533 }
534
535 static const arm_register_req_t _arm_req_sp = {
536   {
537     arch_register_req_type_limited,
538     &arm_reg_classes[CLASS_arm_general_purpose],
539     limit_reg_arm_StoreStackM4Inc_sp,
540     NULL,        /* limit environment */
541     NULL,        /* same node */
542     NULL         /* different node */
543   },
544   0,
545   0
546 };
547
548 /* construct Store: Store(ptr, val, mem) = ST ptr,val */
549 ir_node *new_r_arm_StoreStackMInc(ir_graph *irg, ir_node *block, ir_node *mem, ir_node *sp,
550                                                                   int n_regs, ir_node **regs, ir_mode *mode) {
551   ir_node *res;
552   ir_node *in[16];
553   int flags = 0;
554   static const arm_register_req_t *_in_req_arm_StoreStackM4Inc[] =
555   {
556         &arm_default_req_none,
557     &_arm_req_sp,
558     &arm_default_req_arm_general_purpose,
559     &arm_default_req_arm_general_purpose,
560     &arm_default_req_arm_general_purpose,
561     &arm_default_req_arm_general_purpose,
562     &arm_default_req_arm_general_purpose,
563     &arm_default_req_arm_general_purpose,
564     &arm_default_req_arm_general_purpose,
565     &arm_default_req_arm_general_purpose,
566     &arm_default_req_arm_general_purpose,
567     &arm_default_req_arm_general_purpose,
568     &arm_default_req_arm_general_purpose,
569     &arm_default_req_arm_general_purpose,
570     &arm_default_req_arm_general_purpose,
571     &arm_default_req_arm_general_purpose,
572   };
573
574   assert(n_regs <= 15);
575
576   in[0] = mem;
577   in[1] = sp;
578   memcpy(&in[2], regs, n_regs * sizeof(in[0]));
579   res = new_ir_node(NULL, irg, block, op_arm_StoreStackM4Inc, mode, 2 + n_regs, in);
580   flags |= arch_irn_flags_rematerializable;   /* op can be easily recalculated */
581
582   /* init node attributes */
583   init_arm_attributes(res, flags, _in_req_arm_StoreStackM4Inc, NULL, 0);
584
585   res = optimize_node(res);
586   irn_vrfy_irg(res, irg);
587
588   return res;
589 }
590
591 /**
592  * Register additional opcodes here.
593  */
594 static void arm_register_additional_opcodes(int cur_opcode) {
595 }
596
597
598 /* Include the generated constructor functions */
599 #include "gen_arm_new_nodes.c.inl"