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