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