7ab8d670d76787199b11e5878faa6631ddde1931
[libfirm] / ir / be / arm / arm_new_nodes.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief  This file implements the creation of the architecture specific firm
23  *         opcodes and the corresponding node constructors for the arm
24  *         assembler irg.
25  * @author Oliver Richter, Tobias Gneist
26  * @version $Id$
27  */
28 #include "config.h"
29
30 #include <stdlib.h>
31
32 #include "irprog_t.h"
33 #include "irgraph_t.h"
34 #include "irnode_t.h"
35 #include "irmode_t.h"
36 #include "ircons_t.h"
37 #include "iropt_t.h"
38 #include "irop.h"
39 #include "irvrfy_t.h"
40 #include "irprintf.h"
41 #include "xmalloc.h"
42
43 #include "../bearch.h"
44
45 #include "arm_nodes_attr.h"
46 #include "arm_new_nodes.h"
47 #include "arm_optimize.h"
48
49 #include "../beabi.h"
50 #include "bearch_arm_t.h"
51
52 /**
53  * Returns the shift modifier string.
54  */
55 const char *arm_shf_mod_name(arm_shift_modifier mod) {
56         static const char *names[] = { NULL, NULL, "asr", "lsl", "lsr", "ror", "rrx" };
57         return names[mod];
58 }
59
60 /**
61  * Return the fpa immediate from the encoding.
62  */
63 const char *arm_get_fpa_imm_name(long imm_value) {
64         static const char *fpa_imm[] = {
65                 "0",
66                 "1",
67                 "2",
68                 "3",
69                 "4",
70                 "5",
71                 "10",
72                 "0.5"
73         };
74         return fpa_imm[imm_value];
75 }
76
77 /***********************************************************************************
78  *      _                                   _       _             __
79  *     | |                                 (_)     | |           / _|
80  *   __| |_   _ _ __ ___  _ __   ___ _ __   _ _ __ | |_ ___ _ __| |_ __ _  ___ ___
81  *  / _` | | | | '_ ` _ \| '_ \ / _ \ '__| | | '_ \| __/ _ \ '__|  _/ _` |/ __/ _ \
82  * | (_| | |_| | | | | | | |_) |  __/ |    | | | | | ||  __/ |  | || (_| | (_|  __/
83  *  \__,_|\__,_|_| |_| |_| .__/ \___|_|    |_|_| |_|\__\___|_|  |_| \__,_|\___\___|
84  *                       | |
85  *                       |_|
86  ***********************************************************************************/
87
88 /**
89  * Dumper interface for dumping arm nodes in vcg.
90  * @param n        the node to dump
91  * @param F        the output file
92  * @param reason   indicates which kind of information should be dumped
93  * @return 0 on success or != 0 on failure
94  */
95 static int arm_dump_node(ir_node *n, FILE *F, dump_reason_t reason)
96 {
97         ir_mode     *mode = NULL;
98         arm_attr_t  *attr = get_arm_attr(n);
99         arm_shift_modifier        mod;
100
101         switch (reason) {
102                 case dump_node_opcode_txt:
103                         fprintf(F, "%s", get_irn_opname(n));
104                         break;
105
106                 case dump_node_mode_txt:
107                         mode = get_irn_mode(n);
108
109                         if (mode) {
110                                 fprintf(F, "[%s]", get_mode_name(mode));
111                         }
112                         else {
113                                 fprintf(F, "[?NOMODE?]");
114                         }
115                         break;
116
117                 case dump_node_nodeattr_txt:
118                         mod = ARM_GET_SHF_MOD(attr);
119                         if (ARM_HAS_SHIFT(mod)) {
120                                 fprintf(F, "[%s #%ld]", arm_shf_mod_name(mod), attr->imm_value);
121                         }
122                         else if (mod == ARM_SHF_IMM) {
123                                 /* immediate */
124                                 fprintf(F, "[#0x%X]", arm_decode_imm_w_shift(attr->imm_value));
125                         }
126                         break;
127
128                 case dump_node_info_txt:
129                         arch_dump_reqs_and_registers(F, n);
130
131                         if (is_arm_CopyB(n)) {
132                                 fprintf(F, "size = %lu\n", get_arm_imm_value(n));
133                         } else {
134                                 long v =  get_arm_imm_value(n);
135                                 if (ARM_GET_FPA_IMM(attr)) {
136                                         fprintf(F, "immediate float value = %s\n", arm_get_fpa_imm_name(v));
137                                 } else {
138                                         fprintf(F, "immediate value = %ld (0x%08lx)\n", v, v);
139                                 }
140                         }
141
142                         if (is_arm_CmpBra(n) && get_arm_CondJmp_proj_num(n) >= 0) {
143                                 fprintf(F, "proj_num = (%d)\n", get_arm_CondJmp_proj_num(n));
144                         }
145                         break;
146         }
147
148         return 0;
149 }
150
151
152
153 /***************************************************************************************************
154  *        _   _                   _       __        _                    _   _               _
155  *       | | | |                 | |     / /       | |                  | | | |             | |
156  *   __ _| |_| |_ _ __   ___  ___| |_   / /_ _  ___| |_   _ __ ___   ___| |_| |__   ___   __| |___
157  *  / _` | __| __| '__| / __|/ _ \ __| / / _` |/ _ \ __| | '_ ` _ \ / _ \ __| '_ \ / _ \ / _` / __|
158  * | (_| | |_| |_| |    \__ \  __/ |_ / / (_| |  __/ |_  | | | | | |  __/ |_| | | | (_) | (_| \__ \
159  *  \__,_|\__|\__|_|    |___/\___|\__/_/ \__, |\___|\__| |_| |_| |_|\___|\__|_| |_|\___/ \__,_|___/
160  *                                        __/ |
161  *                                       |___/
162  ***************************************************************************************************/
163
164 /* Returns the attributes of a generic Arm node. */
165 arm_attr_t *get_arm_attr(ir_node *node) {
166         assert(is_arm_irn(node) && "need arm node to get attributes");
167         return get_irn_generic_attr(node);
168 }
169
170 const arm_attr_t *get_arm_attr_const(const ir_node *node) {
171         assert(is_arm_irn(node) && "need arm node to get attributes");
172         return get_irn_generic_attr_const(node);
173 }
174
175 /**
176  * Returns the attributes of an ARM SymConst node.
177  */
178 arm_SymConst_attr_t *get_arm_SymConst_attr(ir_node *node) {
179         assert(is_arm_SymConst(node));
180         return get_irn_generic_attr(node);
181 }
182
183 const arm_SymConst_attr_t *get_arm_SymConst_attr_const(const ir_node *node) {
184         assert(is_arm_SymConst(node));
185         return get_irn_generic_attr_const(node);
186 }
187
188 static const arm_fpaConst_attr_t *get_arm_fpaConst_attr_const(const ir_node *node) {
189         const arm_attr_t          *attr     = get_arm_attr_const(node);
190         const arm_fpaConst_attr_t *fpa_attr = CONST_CAST_ARM_ATTR(arm_fpaConst_attr_t, attr);
191
192         return fpa_attr;
193 }
194
195 static arm_fpaConst_attr_t *get_arm_fpaConst_attr(ir_node *node) {
196         arm_attr_t          *attr     = get_arm_attr(node);
197         arm_fpaConst_attr_t *fpa_attr = CAST_ARM_ATTR(arm_fpaConst_attr_t, attr);
198
199         return fpa_attr;
200 }
201
202 #ifndef NDEBUG
203 static int is_arm_CondJmp(const ir_node *node) {
204         int code = get_arm_irn_opcode(node);
205
206         return (code == iro_arm_CmpBra || code == iro_arm_fpaCmfBra ||
207                 code == iro_arm_fpaCnfBra || iro_arm_fpaCmfeBra ||
208                 code == iro_arm_fpaCnfeBra);
209 }
210 #endif
211
212 /* Returns the attributes of a CondJmp node. */
213 arm_CondJmp_attr_t *get_arm_CondJmp_attr(ir_node *node) {
214         assert(is_arm_CondJmp(node));
215         return get_irn_generic_attr(node);
216 }
217
218 const arm_CondJmp_attr_t *get_arm_CondJmp_attr_const(const ir_node *node) {
219         assert(is_arm_CondJmp(node));
220         return get_irn_generic_attr_const(node);
221 }
222
223 /* Returns the attributes of a SwitchJmp node. */
224 arm_SwitchJmp_attr_t *get_arm_SwitchJmp_attr(ir_node *node) {
225         assert(is_arm_SwitchJmp(node));
226         return get_irn_generic_attr(node);
227 }
228
229 const arm_SwitchJmp_attr_t *get_arm_SwitchJmp_attr_const(const ir_node *node) {
230         assert(is_arm_SwitchJmp(node));
231         return get_irn_generic_attr_const(node);
232 }
233
234 /**
235  * Returns the argument register requirements of a arm node.
236  */
237 const arch_register_req_t **get_arm_in_req_all(const ir_node *node) {
238         const arm_attr_t *attr = get_arm_attr_const(node);
239         return attr->in_req;
240 }
241
242 /**
243  * Returns the argument register requirement at position pos of an arm node.
244  */
245 const arch_register_req_t *get_arm_in_req(const ir_node *node, int pos) {
246         const arm_attr_t *attr = get_arm_attr_const(node);
247         return attr->in_req[pos];
248 }
249
250 /**
251  * Returns the result register requirement at position pos of an arm node.
252  */
253 const arch_register_req_t *get_arm_out_req(const ir_node *node, int pos)
254 {
255         const backend_info_t *info = be_get_info(node);
256         return info->out_infos[pos].req;
257 }
258
259 /**
260  * Sets the IN register requirements at position pos.
261  */
262 void set_arm_req_in(ir_node *node, const arch_register_req_t *req, int pos) {
263         arm_attr_t *attr  = get_arm_attr(node);
264         attr->in_req[pos] = req;
265 }
266
267 /**
268  * Returns the immediate value
269  */
270 long get_arm_imm_value(const ir_node *node) {
271         const arm_attr_t *attr = get_arm_attr_const(node);
272         return attr->imm_value;
273 }
274
275 /**
276  * Sets the tarval value
277  */
278 void set_arm_imm_value(ir_node *node, long imm_value) {
279         arm_attr_t *attr = get_arm_attr(node);
280         attr->imm_value = imm_value;
281 }
282
283 /**
284  * Returns the fpaConst value
285  */
286 tarval *get_fpaConst_value(const ir_node *node) {
287         const arm_fpaConst_attr_t *attr = get_arm_fpaConst_attr_const(node);
288         return attr->tv;
289 }
290
291 /**
292  * Sets the tarval value
293  */
294 void set_fpaConst_value(ir_node *node, tarval *tv) {
295         arm_fpaConst_attr_t *attr = get_arm_fpaConst_attr(node);
296         attr->tv = tv;
297 }
298
299 /**
300  * Returns the proj num
301  */
302 int get_arm_CondJmp_proj_num(const ir_node *node) {
303         const arm_CondJmp_attr_t *attr = get_arm_CondJmp_attr_const(node);
304         return attr->proj_num;
305 }
306
307 /**
308  * Sets the proj num
309  */
310 void set_arm_CondJmp_proj_num(ir_node *node, int proj_num) {
311         arm_CondJmp_attr_t *attr = get_arm_CondJmp_attr(node);
312         attr->proj_num   = proj_num;
313 }
314
315 /**
316  * Returns the SymConst label
317  */
318 ident *get_arm_symconst_id(const ir_node *node) {
319         const arm_SymConst_attr_t *attr = get_arm_SymConst_attr_const(node);
320         return attr->symconst_id;
321 }
322
323 /**
324  * Sets the SymConst label
325  */
326 void set_arm_symconst_id(ir_node *node, ident *symconst_id) {
327         arm_SymConst_attr_t *attr = get_arm_SymConst_attr(node);
328         attr->symconst_id = symconst_id;
329 }
330
331 /**
332  * Returns the number of projs of a SwitchJmp.
333  */
334 int get_arm_SwitchJmp_n_projs(const ir_node *node) {
335         const arm_SwitchJmp_attr_t *attr = get_arm_SwitchJmp_attr_const(node);
336         return attr->n_projs;
337 }
338
339 /**
340  * Sets the number of projs.
341  */
342 void set_arm_SwitchJmp_n_projs(ir_node *node, int n_projs) {
343         arm_SwitchJmp_attr_t *attr = get_arm_SwitchJmp_attr(node);
344         attr->n_projs = n_projs;
345 }
346
347 /**
348  * Returns the default_proj_num.
349  */
350 long get_arm_SwitchJmp_default_proj_num(const ir_node *node) {
351         const arm_SwitchJmp_attr_t *attr = get_arm_SwitchJmp_attr_const(node);
352         return attr->default_proj_num;
353 }
354
355 /**
356  * Sets the default_proj_num.
357  */
358 void set_arm_SwitchJmp_default_proj_num(ir_node *node, long default_proj_num) {
359         arm_SwitchJmp_attr_t *attr = get_arm_SwitchJmp_attr(node);
360         attr->default_proj_num = default_proj_num;
361 }
362
363 /**
364  * Gets the shift modifier attribute.
365  */
366 arm_shift_modifier get_arm_shift_modifier(const ir_node *node) {
367         const arm_attr_t *attr = get_arm_attr_const(node);
368         return ARM_GET_SHF_MOD(attr);
369 }
370
371 /* Set the ARM machine node attributes to default values. */
372 static void init_arm_attributes(ir_node *node, int flags,
373                          const arch_register_req_t ** in_reqs,
374                          const be_execution_unit_t ***execution_units,
375                                                  int n_res) {
376         ir_graph       *irg  = get_irn_irg(node);
377         struct obstack *obst = get_irg_obstack(irg);
378         arm_attr_t     *attr = get_arm_attr(node);
379         backend_info_t *info;
380         (void) execution_units;
381
382         arch_irn_set_flags(node, flags);
383         attr->in_req           = in_reqs;
384         attr->instr_fl         = (ARM_COND_AL << 3) | ARM_SHF_NONE;
385         attr->imm_value        = 0;
386
387         info            = be_get_info(node);
388         info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_res);
389         memset(info->out_infos, 0, n_res * sizeof(info->out_infos[0]));
390 }
391
392 /************************************************
393  *   ___        _   _           _               *
394  *  / _ \ _ __ | |_(_)_ __ ___ (_)_______ _ __  *
395  * | | | | '_ \| __| | '_ ` _ \| |_  / _ \ '__| *
396  * | |_| | |_) | |_| | | | | | | |/ /  __/ |    *
397  *  \___/| .__/ \__|_|_| |_| |_|_/___\___|_|    *
398  *       |_|                                    *
399  ************************************************/
400
401 typedef struct _opt_tuple {
402         ir_op *op_imm_left;             /**< immediate is left */
403         ir_op *op_imm_right;    /**< immediate is right */
404         ir_op *op_shf_left;             /**< shift operand on left */
405         ir_op *op_shf_right;    /**< shift operand on right */
406 } opt_tuple;
407
408 //static const opt_tuple *opt_ops[iro_arm_last];
409
410 void arm_set_optimizers(void) {
411         /*
412 #define STD(op)         p_##op = { op_arm_##op##_i, op_arm_##op##_i, op_arm_##op, op_arm_##op }
413 #define LEFT(op)        p_##op = { op_arm_##op##_i, NULL, op_arm_##op, NULL }
414 #define SET(op)   opt_ops[iro_arm_##op] = &p_##op;
415
416         static const opt_tuple
417                 STD(Add),
418                 STD(And),
419                 STD(Or),
420                 STD(Eor),
421                 LEFT(Bic),
422                 LEFT(Shl),
423                 LEFT(Shr),
424                 LEFT(Shrs),
425                 p_Sub = { op_arm_Sub_i, op_arm_Rsb_i, op_arm_Sub, op_arm_Rsb },
426
427         memset(opt_ops, 0, sizeof(opt_ops));
428         SET(Add);
429         SET(And);
430         SET(Or);
431         SET(Eor);
432         SET(Sub);
433         SET(Bic);
434         SET(Shl);
435         SET(Shr);
436         SET(Shrs);
437         */
438 }
439
440 static int cmp_attr_arm(ir_node *a, ir_node *b) {
441         arm_attr_t *attr_a = get_irn_generic_attr(a);
442         arm_attr_t *attr_b = get_irn_generic_attr(b);
443         return (attr_a->instr_fl != attr_b->instr_fl) || (attr_a->imm_value != attr_b->imm_value);
444 }
445
446 static int cmp_attr_arm_SymConst(ir_node *a, ir_node *b) {
447         const arm_SymConst_attr_t *attr_a;
448         const arm_SymConst_attr_t *attr_b;
449
450         if (cmp_attr_arm(a, b))
451                 return 1;
452
453         attr_a = get_irn_generic_attr_const(a);
454         attr_b = get_irn_generic_attr_const(b);
455         return attr_a->symconst_id != attr_b->symconst_id;
456 }
457
458 static int cmp_attr_arm_CondJmp(ir_node *a, ir_node *b) {
459         (void) a;
460         (void) b;
461         /* never identical */
462         return 1;
463 }
464
465 static int cmp_attr_arm_SwitchJmp(ir_node *a, ir_node *b) {
466         (void) a;
467         (void) b;
468         /* never identical */
469         return 1;
470 }
471
472 static int cmp_attr_arm_fpaConst(ir_node *a, ir_node *b) {
473         const arm_fpaConst_attr_t *attr_a;
474         const arm_fpaConst_attr_t *attr_b;
475
476         if (cmp_attr_arm(a, b))
477                 return 1;
478
479         attr_a = get_arm_fpaConst_attr_const(a);
480         attr_b = get_arm_fpaConst_attr_const(b);
481
482         return attr_a->tv != attr_b->tv;
483 }
484
485 /** copies the ARM attributes of a node. */
486 static void arm_copy_attr(const ir_node *old_node, ir_node *new_node) {
487         ir_graph          *irg     = get_irn_irg(new_node);
488         struct obstack    *obst    = get_irg_obstack(irg);
489         const arm_attr_t *attr_old = get_arm_attr_const(old_node);
490         arm_attr_t       *attr_new = get_arm_attr(new_node);
491         backend_info_t    *old_info = be_get_info(old_node);
492         backend_info_t    *new_info = be_get_info(new_node);
493
494         /* copy the attributes */
495         memcpy(attr_new, attr_old, get_op_attr_size(get_irn_op(old_node)));
496
497         /* copy out flags */
498         new_info->out_infos =
499                 DUP_ARR_D(reg_out_info_t, obst, old_info->out_infos);
500 }
501
502
503
504 /* Include the generated constructor functions */
505 #include "gen_arm_new_nodes.c.inl"