cleanup and fix permutate_values (3-cycles and values being used 2 times with 1 being...
[libfirm] / ir / be / TEMPLATE / TEMPLATE_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 achitecture specific firm
23  *          opcodes and the coresponding node constructors for the TEMPLATE
24  *          assembler irg.
25  * @version $Id$
26  */
27 #include "config.h"
28
29 #include <stdlib.h>
30
31 #include "irprog_t.h"
32 #include "irgraph_t.h"
33 #include "irnode_t.h"
34 #include "irmode_t.h"
35 #include "ircons_t.h"
36 #include "iropt_t.h"
37 #include "irop.h"
38 #include "irvrfy_t.h"
39 #include "irprintf.h"
40 #include "xmalloc.h"
41
42 #include "../bearch.h"
43
44 #include "TEMPLATE_nodes_attr.h"
45 #include "TEMPLATE_new_nodes.h"
46 #include "gen_TEMPLATE_regalloc_if.h"
47
48
49
50 /***********************************************************************************
51  *      _                                   _       _             __
52  *     | |                                 (_)     | |           / _|
53  *   __| |_   _ _ __ ___  _ __   ___ _ __   _ _ __ | |_ ___ _ __| |_ __ _  ___ ___
54  *  / _` | | | | '_ ` _ \| '_ \ / _ \ '__| | | '_ \| __/ _ \ '__|  _/ _` |/ __/ _ \
55  * | (_| | |_| | | | | | | |_) |  __/ |    | | | | | ||  __/ |  | || (_| | (_|  __/
56  *  \__,_|\__,_|_| |_| |_| .__/ \___|_|    |_|_| |_|\__\___|_|  |_| \__,_|\___\___|
57  *                       | |
58  *                       |_|
59  ***********************************************************************************/
60
61 /**
62  * Dumps the register requirements for either in or out.
63  */
64 static void dump_reg_req(FILE *F, ir_node *n, const arch_register_req_t **reqs,
65                          int inout) {
66         char *dir = inout ? "out" : "in";
67         int   max = inout ? (int) arch_irn_get_n_outs(n) : get_irn_arity(n);
68         char  buf[1024];
69         int   i;
70
71         memset(buf, 0, sizeof(buf));
72
73         if (reqs) {
74                 for (i = 0; i < max; i++) {
75                         fprintf(F, "%sreq #%d =", dir, i);
76                         arch_dump_register_req(F, reqs[i], n);
77                         fprintf(F, "\n");
78                 }
79
80                 fprintf(F, "\n");
81         } else {
82                 fprintf(F, "%sreq = N/A\n", dir);
83         }
84 }
85
86
87 /**
88  * Dumper interface for dumping TEMPLATE nodes in vcg.
89  * @param n        the node to dump
90  * @param F        the output file
91  * @param reason   indicates which kind of information should be dumped
92  * @return 0 on success or != 0 on failure
93  */
94 static int TEMPLATE_dump_node(ir_node *n, FILE *F, dump_reason_t reason) {
95         ir_mode     *mode = NULL;
96         int         bad  = 0;
97         int         i, n_res, flags;
98         const arch_register_req_t **reqs;
99
100         switch (reason) {
101                 case dump_node_opcode_txt:
102                         fprintf(F, "%s", get_irn_opname(n));
103                         break;
104
105                 case dump_node_mode_txt:
106                         mode = get_irn_mode(n);
107
108                         if (mode) {
109                                 fprintf(F, "[%s]", get_mode_name(mode));
110                         }
111                         else {
112                                 fprintf(F, "[?NOMODE?]");
113                         }
114                         break;
115
116                 case dump_node_nodeattr_txt:
117
118                         /* TODO: dump some attributes which should show up */
119                         /* in node name in dump (e.g. consts or the like)  */
120
121                         break;
122
123                 case dump_node_info_txt:
124                         fprintf(F, "=== TEMPLATE attr begin ===\n");
125
126                         /* dump IN requirements */
127                         if (get_irn_arity(n) > 0) {
128                                 reqs = get_TEMPLATE_in_req_all(n);
129                                 dump_reg_req(F, n, reqs, 0);
130                         }
131
132                         n_res = arch_irn_get_n_outs(n);
133                         if (n_res > 0) {
134                                 /* dump OUT requirements */
135                                 reqs = get_TEMPLATE_out_req_all(n);
136                                 dump_reg_req(F, n, reqs, 1);
137
138                                 /* dump assigned registers */
139                                 for (i = 0; i < n_res; i++) {
140                                         const arch_register_t *reg = arch_irn_get_register(n, i);
141
142                                         fprintf(F, "reg #%d = %s\n", i, reg ? arch_register_get_name(reg) : "n/a");
143                                 }
144                                 fprintf(F, "\n");
145                         }
146
147                         /* dump n_res */
148                         fprintf(F, "n_res = %d\n", n_res);
149
150                         /* dump flags */
151                         fprintf(F, "flags =");
152                         flags = arch_irn_get_flags(n);
153                         if (flags == arch_irn_flags_none) {
154                                 fprintf(F, " none");
155                         }
156                         else {
157                                 if (flags & arch_irn_flags_dont_spill) {
158                                         fprintf(F, " unspillable");
159                                 }
160                                 if (flags & arch_irn_flags_rematerializable) {
161                                         fprintf(F, " remat");
162                                 }
163                                 if (flags & arch_irn_flags_modify_flags) {
164                                         fprintf(F, " modify_flags");
165                                 }
166                         }
167                         fprintf(F, " (%d)\n", flags);
168
169                         /* TODO: dump all additional attributes */
170
171                         fprintf(F, "=== TEMPLATE attr end ===\n");
172                         /* end of: case dump_node_info_txt */
173                         break;
174         }
175
176
177         return bad;
178 }
179
180
181
182 /***************************************************************************************************
183  *        _   _                   _       __        _                    _   _               _
184  *       | | | |                 | |     / /       | |                  | | | |             | |
185  *   __ _| |_| |_ _ __   ___  ___| |_   / /_ _  ___| |_   _ __ ___   ___| |_| |__   ___   __| |___
186  *  / _` | __| __| '__| / __|/ _ \ __| / / _` |/ _ \ __| | '_ ` _ \ / _ \ __| '_ \ / _ \ / _` / __|
187  * | (_| | |_| |_| |    \__ \  __/ |_ / / (_| |  __/ |_  | | | | | |  __/ |_| | | | (_) | (_| \__ \
188  *  \__,_|\__|\__|_|    |___/\___|\__/_/ \__, |\___|\__| |_| |_| |_|\___|\__|_| |_|\___/ \__,_|___/
189  *                                        __/ |
190  *                                       |___/
191  ***************************************************************************************************/
192
193 const TEMPLATE_attr_t *get_TEMPLATE_attr_const(const ir_node *node) {
194         assert(is_TEMPLATE_irn(node) && "need TEMPLATE node to get attributes");
195         return (const TEMPLATE_attr_t *)get_irn_generic_attr_const(node);
196 }
197
198 TEMPLATE_attr_t *get_TEMPLATE_attr(ir_node *node) {
199         assert(is_TEMPLATE_irn(node) && "need TEMPLATE node to get attributes");
200         return (TEMPLATE_attr_t *)get_irn_generic_attr(node);
201 }
202
203 /**
204  * Returns the argument register requirements of a TEMPLATE node.
205  */
206 const arch_register_req_t **get_TEMPLATE_in_req_all(const ir_node *node) {
207         const TEMPLATE_attr_t *attr = get_TEMPLATE_attr_const(node);
208         return attr->in_req;
209 }
210
211 /**
212  * Returns the result register requirements of an TEMPLATE node.
213  */
214 const arch_register_req_t **get_TEMPLATE_out_req_all(const ir_node *node) {
215         const TEMPLATE_attr_t *attr = get_TEMPLATE_attr_const(node);
216         return attr->out_req;
217 }
218
219 /**
220  * Returns the argument register requirement at position pos of an TEMPLATE node.
221  */
222 const arch_register_req_t *get_TEMPLATE_in_req(const ir_node *node, int pos) {
223         const TEMPLATE_attr_t *attr = get_TEMPLATE_attr_const(node);
224         return attr->in_req[pos];
225 }
226
227 /**
228  * Returns the result register requirement at position pos of an TEMPLATE node.
229  */
230 const arch_register_req_t *get_TEMPLATE_out_req(const ir_node *node, int pos) {
231         const TEMPLATE_attr_t *attr = get_TEMPLATE_attr_const(node);
232         return attr->out_req[pos];
233 }
234
235 /**
236  * Sets the OUT register requirements at position pos.
237  */
238 void set_TEMPLATE_req_out(ir_node *node, const arch_register_req_t *req, int pos) {
239         TEMPLATE_attr_t *attr   = get_TEMPLATE_attr(node);
240         attr->out_req[pos] = req;
241 }
242
243 /**
244  * Sets the IN register requirements at position pos.
245  */
246 void set_TEMPLATE_req_in(ir_node *node, const arch_register_req_t *req, int pos) {
247         TEMPLATE_attr_t *attr  = get_TEMPLATE_attr(node);
248         attr->in_req[pos] = req;
249 }
250
251 /**
252  * Initializes the nodes attributes.
253  */
254 void init_TEMPLATE_attributes(ir_node *node, arch_irn_flags_t flags,
255                               const arch_register_req_t **in_reqs,
256                               const arch_register_req_t **out_reqs,
257                               const be_execution_unit_t ***execution_units,
258                               int n_res)
259 {
260         ir_graph        *irg  = get_irn_irg(node);
261         struct obstack  *obst = get_irg_obstack(irg);
262         TEMPLATE_attr_t *attr = get_TEMPLATE_attr(node);
263         backend_info_t  *info;
264         (void) execution_units;
265
266         arch_irn_set_flags(node, flags);
267         attr->out_req = out_reqs;
268         attr->in_req  = in_reqs;
269
270         info            = be_get_info(node);
271         info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_res);
272         memset(info->out_infos, 0, n_res * sizeof(info->out_infos[0]));
273 }
274
275 /***************************************************************************************
276  *                  _                            _                   _
277  *                 | |                          | |                 | |
278  *  _ __   ___   __| | ___    ___ ___  _ __  ___| |_ _ __ _   _  ___| |_ ___  _ __ ___
279  * | '_ \ / _ \ / _` |/ _ \  / __/ _ \| '_ \/ __| __| '__| | | |/ __| __/ _ \| '__/ __|
280  * | | | | (_) | (_| |  __/ | (_| (_) | | | \__ \ |_| |  | |_| | (__| || (_) | |  \__ \
281  * |_| |_|\___/ \__,_|\___|  \___\___/|_| |_|___/\__|_|   \__,_|\___|\__\___/|_|  |___/
282  *
283  ***************************************************************************************/
284
285 static
286 int TEMPLATE_compare_attr(ir_node *a, ir_node *b)
287 {
288         const TEMPLATE_attr_t *attr_a = get_TEMPLATE_attr_const(a);
289         const TEMPLATE_attr_t *attr_b = get_TEMPLATE_attr_const(b);
290         (void) attr_a;
291         (void) attr_b;
292
293         return 0;
294 }
295
296
297 /* Include the generated constructor functions */
298 #include "gen_TEMPLATE_new_nodes.c.inl"