9f052ff5ee07742332efb471abef89eb828e433d
[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  * Sets the IN register requirements at position pos.
252  */
253 void set_arm_req_in(ir_node *node, const arch_register_req_t *req, int pos) {
254         arm_attr_t *attr  = get_arm_attr(node);
255         attr->in_req[pos] = req;
256 }
257
258 /**
259  * Returns the immediate value
260  */
261 long get_arm_imm_value(const ir_node *node) {
262         const arm_attr_t *attr = get_arm_attr_const(node);
263         return attr->imm_value;
264 }
265
266 /**
267  * Sets the tarval value
268  */
269 void set_arm_imm_value(ir_node *node, long imm_value) {
270         arm_attr_t *attr = get_arm_attr(node);
271         attr->imm_value = imm_value;
272 }
273
274 /**
275  * Returns the fpaConst value
276  */
277 tarval *get_fpaConst_value(const ir_node *node) {
278         const arm_fpaConst_attr_t *attr = get_arm_fpaConst_attr_const(node);
279         return attr->tv;
280 }
281
282 /**
283  * Sets the tarval value
284  */
285 void set_fpaConst_value(ir_node *node, tarval *tv) {
286         arm_fpaConst_attr_t *attr = get_arm_fpaConst_attr(node);
287         attr->tv = tv;
288 }
289
290 /**
291  * Returns the proj num
292  */
293 int get_arm_CondJmp_proj_num(const ir_node *node) {
294         const arm_CondJmp_attr_t *attr = get_arm_CondJmp_attr_const(node);
295         return attr->proj_num;
296 }
297
298 /**
299  * Sets the proj num
300  */
301 void set_arm_CondJmp_proj_num(ir_node *node, int proj_num) {
302         arm_CondJmp_attr_t *attr = get_arm_CondJmp_attr(node);
303         attr->proj_num   = proj_num;
304 }
305
306 /**
307  * Returns the SymConst label
308  */
309 ident *get_arm_symconst_id(const ir_node *node) {
310         const arm_SymConst_attr_t *attr = get_arm_SymConst_attr_const(node);
311         return attr->symconst_id;
312 }
313
314 /**
315  * Sets the SymConst label
316  */
317 void set_arm_symconst_id(ir_node *node, ident *symconst_id) {
318         arm_SymConst_attr_t *attr = get_arm_SymConst_attr(node);
319         attr->symconst_id = symconst_id;
320 }
321
322 /**
323  * Returns the number of projs of a SwitchJmp.
324  */
325 int get_arm_SwitchJmp_n_projs(const ir_node *node) {
326         const arm_SwitchJmp_attr_t *attr = get_arm_SwitchJmp_attr_const(node);
327         return attr->n_projs;
328 }
329
330 /**
331  * Sets the number of projs.
332  */
333 void set_arm_SwitchJmp_n_projs(ir_node *node, int n_projs) {
334         arm_SwitchJmp_attr_t *attr = get_arm_SwitchJmp_attr(node);
335         attr->n_projs = n_projs;
336 }
337
338 /**
339  * Returns the default_proj_num.
340  */
341 long get_arm_SwitchJmp_default_proj_num(const ir_node *node) {
342         const arm_SwitchJmp_attr_t *attr = get_arm_SwitchJmp_attr_const(node);
343         return attr->default_proj_num;
344 }
345
346 /**
347  * Sets the default_proj_num.
348  */
349 void set_arm_SwitchJmp_default_proj_num(ir_node *node, long default_proj_num) {
350         arm_SwitchJmp_attr_t *attr = get_arm_SwitchJmp_attr(node);
351         attr->default_proj_num = default_proj_num;
352 }
353
354 /**
355  * Gets the shift modifier attribute.
356  */
357 arm_shift_modifier get_arm_shift_modifier(const ir_node *node) {
358         const arm_attr_t *attr = get_arm_attr_const(node);
359         return ARM_GET_SHF_MOD(attr);
360 }
361
362 /* Set the ARM machine node attributes to default values. */
363 static void init_arm_attributes(ir_node *node, int flags,
364                          const arch_register_req_t ** in_reqs,
365                          const be_execution_unit_t ***execution_units,
366                                                  int n_res)
367 {
368         ir_graph       *irg  = get_irn_irg(node);
369         struct obstack *obst = get_irg_obstack(irg);
370         arm_attr_t     *attr = get_arm_attr(node);
371         backend_info_t *info;
372         (void) execution_units;
373
374         arch_irn_set_flags(node, flags);
375         attr->in_req           = in_reqs;
376         attr->instr_fl         = (ARM_COND_AL << 3) | ARM_SHF_NONE;
377         attr->imm_value        = 0;
378
379         info            = be_get_info(node);
380         info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_res);
381         memset(info->out_infos, 0, n_res * sizeof(info->out_infos[0]));
382 }
383
384 void init_arm_load_store_attributes(ir_node *res, ir_entity *entity,
385                                     int entity_sign, long offset)
386 {
387         arm_load_store_attr_t *attr = get_irn_generic_attr(res);
388         attr->entity      = entity;
389         attr->entity_sign = entity_sign;
390         attr->offset      = offset;
391 }
392
393 /************************************************
394  *   ___        _   _           _               *
395  *  / _ \ _ __ | |_(_)_ __ ___ (_)_______ _ __  *
396  * | | | | '_ \| __| | '_ ` _ \| |_  / _ \ '__| *
397  * | |_| | |_) | |_| | | | | | | |/ /  __/ |    *
398  *  \___/| .__/ \__|_|_| |_| |_|_/___\___|_|    *
399  *       |_|                                    *
400  ************************************************/
401
402 typedef struct _opt_tuple {
403         ir_op *op_imm_left;             /**< immediate is left */
404         ir_op *op_imm_right;    /**< immediate is right */
405         ir_op *op_shf_left;             /**< shift operand on left */
406         ir_op *op_shf_right;    /**< shift operand on right */
407 } opt_tuple;
408
409 //static const opt_tuple *opt_ops[iro_arm_last];
410
411 void arm_set_optimizers(void) {
412         /*
413 #define STD(op)         p_##op = { op_arm_##op##_i, op_arm_##op##_i, op_arm_##op, op_arm_##op }
414 #define LEFT(op)        p_##op = { op_arm_##op##_i, NULL, op_arm_##op, NULL }
415 #define SET(op)   opt_ops[iro_arm_##op] = &p_##op;
416
417         static const opt_tuple
418                 STD(Add),
419                 STD(And),
420                 STD(Or),
421                 STD(Eor),
422                 LEFT(Bic),
423                 LEFT(Shl),
424                 LEFT(Shr),
425                 LEFT(Shrs),
426                 p_Sub = { op_arm_Sub_i, op_arm_Rsb_i, op_arm_Sub, op_arm_Rsb },
427
428         memset(opt_ops, 0, sizeof(opt_ops));
429         SET(Add);
430         SET(And);
431         SET(Or);
432         SET(Eor);
433         SET(Sub);
434         SET(Bic);
435         SET(Shl);
436         SET(Shr);
437         SET(Shrs);
438         */
439 }
440
441 static int cmp_attr_arm(ir_node *a, ir_node *b) {
442         arm_attr_t *attr_a = get_irn_generic_attr(a);
443         arm_attr_t *attr_b = get_irn_generic_attr(b);
444         return (attr_a->instr_fl != attr_b->instr_fl) || (attr_a->imm_value != attr_b->imm_value);
445 }
446
447 static int cmp_attr_arm_SymConst(ir_node *a, ir_node *b) {
448         const arm_SymConst_attr_t *attr_a;
449         const arm_SymConst_attr_t *attr_b;
450
451         if (cmp_attr_arm(a, b))
452                 return 1;
453
454         attr_a = get_irn_generic_attr_const(a);
455         attr_b = get_irn_generic_attr_const(b);
456         return attr_a->symconst_id != attr_b->symconst_id;
457 }
458
459 static int cmp_attr_arm_CondJmp(ir_node *a, ir_node *b) {
460         (void) a;
461         (void) b;
462         /* never identical */
463         return 1;
464 }
465
466 static int cmp_attr_arm_SwitchJmp(ir_node *a, ir_node *b) {
467         (void) a;
468         (void) b;
469         /* never identical */
470         return 1;
471 }
472
473 static int cmp_attr_arm_fpaConst(ir_node *a, ir_node *b) {
474         const arm_fpaConst_attr_t *attr_a;
475         const arm_fpaConst_attr_t *attr_b;
476
477         if (cmp_attr_arm(a, b))
478                 return 1;
479
480         attr_a = get_arm_fpaConst_attr_const(a);
481         attr_b = get_arm_fpaConst_attr_const(b);
482
483         return attr_a->tv != attr_b->tv;
484 }
485
486 arm_load_store_attr_t *get_arm_load_store_attr(ir_node *node)
487 {
488         return (arm_load_store_attr_t*) get_irn_generic_attr(node);
489 }
490
491 static int cmp_attr_arm_load_store(ir_node *a, ir_node *b)
492 {
493         const arm_load_store_attr_t *attr_a;
494         const arm_load_store_attr_t *attr_b;
495
496         if (cmp_attr_arm(a, b))
497                 return 1;
498
499         attr_a = get_arm_load_store_attr(a);
500         attr_b = get_arm_load_store_attr(b);
501         if (attr_a->entity != attr_b->entity
502                         || attr_a->entity_sign != attr_b->entity_sign
503                         || attr_a->offset != attr_b->offset)
504                 return 1;
505
506         return 0;
507 }
508
509 /** copies the ARM attributes of a node. */
510 static void arm_copy_attr(const ir_node *old_node, ir_node *new_node) {
511         ir_graph          *irg     = get_irn_irg(new_node);
512         struct obstack    *obst    = get_irg_obstack(irg);
513         const arm_attr_t *attr_old = get_arm_attr_const(old_node);
514         arm_attr_t       *attr_new = get_arm_attr(new_node);
515         backend_info_t    *old_info = be_get_info(old_node);
516         backend_info_t    *new_info = be_get_info(new_node);
517
518         /* copy the attributes */
519         memcpy(attr_new, attr_old, get_op_attr_size(get_irn_op(old_node)));
520
521         /* copy out flags */
522         new_info->out_infos =
523                 DUP_ARR_D(reg_out_info_t, obst, old_info->out_infos);
524 }
525
526
527
528 /* Include the generated constructor functions */
529 #include "gen_arm_new_nodes.c.inl"