- BugFix: ensure that Convs are created in the right block
[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 "firm_common_t.h"
39 #include "irvrfy_t.h"
40 #include "irprintf.h"
41 #include "xmalloc.h"
42
43 #include "../bearch_t.h"
44
45 #include "TEMPLATE_nodes_attr.h"
46 #include "TEMPLATE_new_nodes.h"
47 #include "gen_TEMPLATE_regalloc_if.h"
48
49
50
51 /***********************************************************************************
52  *      _                                   _       _             __
53  *     | |                                 (_)     | |           / _|
54  *   __| |_   _ _ __ ___  _ __   ___ _ __   _ _ __ | |_ ___ _ __| |_ __ _  ___ ___
55  *  / _` | | | | '_ ` _ \| '_ \ / _ \ '__| | | '_ \| __/ _ \ '__|  _/ _` |/ __/ _ \
56  * | (_| | |_| | | | | | | |_) |  __/ |    | | | | | ||  __/ |  | || (_| | (_|  __/
57  *  \__,_|\__,_|_| |_| |_| .__/ \___|_|    |_|_| |_|\__\___|_|  |_| \__,_|\___\___|
58  *                       | |
59  *                       |_|
60  ***********************************************************************************/
61
62 /**
63  * Dumps the register requirements for either in or out.
64  */
65 static void dump_reg_req(FILE *F, ir_node *n, const arch_register_req_t **reqs,
66                          int inout) {
67         char *dir = inout ? "out" : "in";
68         int   max = inout ? get_TEMPLATE_n_res(n) : get_irn_arity(n);
69         char  buf[1024];
70         int   i;
71
72         memset(buf, 0, sizeof(buf));
73
74         if (reqs) {
75                 for (i = 0; i < max; i++) {
76                         fprintf(F, "%sreq #%d =", dir, i);
77
78                         if (reqs[i]->type == arch_register_req_type_none) {
79                                 fprintf(F, " n/a");
80                         }
81
82                         if (reqs[i]->type & arch_register_req_type_normal) {
83                                 fprintf(F, " %s", reqs[i]->cls->name);
84                         }
85
86                         if (reqs[i]->type & arch_register_req_type_limited) {
87                                 fprintf(F, " %s",
88                                                 arch_register_req_format(buf, sizeof(buf), reqs[i], n));
89                         }
90
91                         if (reqs[i]->type & arch_register_req_type_should_be_same) {
92                                 const unsigned other = reqs[i]->other_same;
93                                 int i;
94
95                                 ir_fprintf(F, " same as");
96                                 for (i = 0; 1U << i <= other; ++i) {
97                                         if (other & (1U << i)) {
98                                                 ir_fprintf(F, " %+F", get_irn_n(n, i));
99                                         }
100                                 }
101                         }
102
103                         if (reqs[i]->type & arch_register_req_type_must_be_different) {
104                                 const unsigned other = reqs[i]->other_different;
105                                 int i;
106
107                                 ir_fprintf(F, " different from");
108                                 for (i = 0; 1U << i <= other; ++i) {
109                                         if (other & (1U << i)) {
110                                                 ir_fprintf(F, " %+F", get_irn_n(n, i));
111                                         }
112                                 }
113                         }
114
115                         fprintf(F, "\n");
116                 }
117
118                 fprintf(F, "\n");
119         } else {
120                 fprintf(F, "%sreq = N/A\n", dir);
121         }
122 }
123
124
125 /**
126  * Dumper interface for dumping TEMPLATE nodes in vcg.
127  * @param n        the node to dump
128  * @param F        the output file
129  * @param reason   indicates which kind of information should be dumped
130  * @return 0 on success or != 0 on failure
131  */
132 static int TEMPLATE_dump_node(ir_node *n, FILE *F, dump_reason_t reason) {
133         ir_mode     *mode = NULL;
134         int          bad  = 0;
135         int          i;
136         const TEMPLATE_attr_t *attr;
137         const arch_register_req_t **reqs;
138         const arch_register_t     **slots;
139
140         switch (reason) {
141                 case dump_node_opcode_txt:
142                         fprintf(F, "%s", get_irn_opname(n));
143                         break;
144
145                 case dump_node_mode_txt:
146                         mode = get_irn_mode(n);
147
148                         if (mode) {
149                                 fprintf(F, "[%s]", get_mode_name(mode));
150                         }
151                         else {
152                                 fprintf(F, "[?NOMODE?]");
153                         }
154                         break;
155
156                 case dump_node_nodeattr_txt:
157
158                         /* TODO: dump some attributes which should show up */
159                         /* in node name in dump (e.g. consts or the like)  */
160
161                         break;
162
163                 case dump_node_info_txt:
164                         attr = get_TEMPLATE_attr_const(n);
165                         fprintf(F, "=== TEMPLATE attr begin ===\n");
166
167                         /* dump IN requirements */
168                         if (get_irn_arity(n) > 0) {
169                                 reqs = get_TEMPLATE_in_req_all(n);
170                                 dump_reg_req(F, n, reqs, 0);
171                         }
172
173                         /* dump OUT requirements */
174                         if (ARR_LEN(attr->slots) > 0) {
175                                 reqs = get_TEMPLATE_out_req_all(n);
176                                 dump_reg_req(F, n, reqs, 1);
177                         }
178
179                         /* dump assigned registers */
180                         slots = get_TEMPLATE_slots(n);
181                         if (slots && ARR_LEN(attr->slots) > 0) {
182                                 for (i = 0; i < ARR_LEN(attr->slots); i++) {
183                                         if (slots[i]) {
184                                                 fprintf(F, "reg #%d = %s\n", i, slots[i]->name);
185                                         }
186                                         else {
187                                                 fprintf(F, "reg #%d = n/a\n", i);
188                                         }
189                                 }
190                         }
191                         fprintf(F, "\n");
192
193                         /* dump n_res */
194                         fprintf(F, "n_res = %d\n", get_TEMPLATE_n_res(n));
195
196                         /* dump flags */
197                         fprintf(F, "flags =");
198                         if (attr->flags == arch_irn_flags_none) {
199                                 fprintf(F, " none");
200                         }
201                         else {
202                                 if (attr->flags & arch_irn_flags_dont_spill) {
203                                         fprintf(F, " unspillable");
204                                 }
205                                 if (attr->flags & arch_irn_flags_rematerializable) {
206                                         fprintf(F, " remat");
207                                 }
208                         }
209                         fprintf(F, " (%d)\n", attr->flags);
210
211                         /* TODO: dump all additional attributes */
212
213                         fprintf(F, "=== TEMPLATE attr end ===\n");
214                         /* end of: case dump_node_info_txt */
215                         break;
216         }
217
218
219         return bad;
220 }
221
222
223
224 /***************************************************************************************************
225  *        _   _                   _       __        _                    _   _               _
226  *       | | | |                 | |     / /       | |                  | | | |             | |
227  *   __ _| |_| |_ _ __   ___  ___| |_   / /_ _  ___| |_   _ __ ___   ___| |_| |__   ___   __| |___
228  *  / _` | __| __| '__| / __|/ _ \ __| / / _` |/ _ \ __| | '_ ` _ \ / _ \ __| '_ \ / _ \ / _` / __|
229  * | (_| | |_| |_| |    \__ \  __/ |_ / / (_| |  __/ |_  | | | | | |  __/ |_| | | | (_) | (_| \__ \
230  *  \__,_|\__|\__|_|    |___/\___|\__/_/ \__, |\___|\__| |_| |_| |_|\___|\__|_| |_|\___/ \__,_|___/
231  *                                        __/ |
232  *                                       |___/
233  ***************************************************************************************************/
234
235 const TEMPLATE_attr_t *get_TEMPLATE_attr_const(const ir_node *node) {
236         assert(is_TEMPLATE_irn(node) && "need TEMPLATE node to get attributes");
237         return (const TEMPLATE_attr_t *)get_irn_generic_attr_const(node);
238 }
239
240 TEMPLATE_attr_t *get_TEMPLATE_attr(ir_node *node) {
241         assert(is_TEMPLATE_irn(node) && "need TEMPLATE node to get attributes");
242         return (TEMPLATE_attr_t *)get_irn_generic_attr(node);
243 }
244
245 /**
246  * Returns the argument register requirements of a TEMPLATE node.
247  */
248 const arch_register_req_t **get_TEMPLATE_in_req_all(const ir_node *node) {
249         const TEMPLATE_attr_t *attr = get_TEMPLATE_attr_const(node);
250         return attr->in_req;
251 }
252
253 /**
254  * Returns the result register requirements of an TEMPLATE node.
255  */
256 const arch_register_req_t **get_TEMPLATE_out_req_all(const ir_node *node) {
257         const TEMPLATE_attr_t *attr = get_TEMPLATE_attr_const(node);
258         return attr->out_req;
259 }
260
261 /**
262  * Returns the argument register requirement at position pos of an TEMPLATE node.
263  */
264 const arch_register_req_t *get_TEMPLATE_in_req(const ir_node *node, int pos) {
265         const TEMPLATE_attr_t *attr = get_TEMPLATE_attr_const(node);
266         return attr->in_req[pos];
267 }
268
269 /**
270  * Returns the result register requirement at position pos of an TEMPLATE node.
271  */
272 const arch_register_req_t *get_TEMPLATE_out_req(const ir_node *node, int pos) {
273         const TEMPLATE_attr_t *attr = get_TEMPLATE_attr_const(node);
274         return attr->out_req[pos];
275 }
276
277 /**
278  * Sets the OUT register requirements at position pos.
279  */
280 void set_TEMPLATE_req_out(ir_node *node, const arch_register_req_t *req, int pos) {
281         TEMPLATE_attr_t *attr   = get_TEMPLATE_attr(node);
282         attr->out_req[pos] = req;
283 }
284
285 /**
286  * Sets the IN register requirements at position pos.
287  */
288 void set_TEMPLATE_req_in(ir_node *node, const arch_register_req_t *req, int pos) {
289         TEMPLATE_attr_t *attr  = get_TEMPLATE_attr(node);
290         attr->in_req[pos] = req;
291 }
292
293 /**
294  * Returns the register flag of an TEMPLATE node.
295  */
296 arch_irn_flags_t get_TEMPLATE_flags(const ir_node *node) {
297         const TEMPLATE_attr_t *attr = get_TEMPLATE_attr_const(node);
298         return attr->flags;
299 }
300
301 /**
302  * Sets the register flag of an TEMPLATE node.
303  */
304 void set_TEMPLATE_flags(ir_node *node, arch_irn_flags_t flags) {
305         TEMPLATE_attr_t *attr = get_TEMPLATE_attr(node);
306         attr->flags      = flags;
307 }
308
309 /**
310  * Returns the result register slots of an TEMPLATE node.
311  */
312 const arch_register_t **get_TEMPLATE_slots(ir_node *node) {
313         TEMPLATE_attr_t *attr = get_TEMPLATE_attr(node);
314         return attr->slots;
315 }
316
317 /**
318  * Returns the result register slots of an TEMPLATE node.
319  */
320 const arch_register_t * const *get_TEMPLATE_slots_const(const ir_node *node) {
321         const TEMPLATE_attr_t *attr = get_TEMPLATE_attr_const(node);
322         return attr->slots;
323 }
324
325 /**
326  * Returns the name of the OUT register at position pos.
327  */
328 const char *get_TEMPLATE_out_reg_name(const ir_node *node, int pos) {
329         const TEMPLATE_attr_t *attr = get_TEMPLATE_attr_const(node);
330
331         assert(is_TEMPLATE_irn(node) && "Not an TEMPLATE node.");
332         assert(pos < ARR_LEN(attr->slots) && "Invalid OUT position.");
333         assert(attr->slots[pos]  && "No register assigned");
334
335         return arch_register_get_name(attr->slots[pos]);
336 }
337
338 /**
339  * Returns the index of the OUT register at position pos within its register class.
340  */
341 int get_TEMPLATE_out_regnr(const ir_node *node, int pos) {
342         const TEMPLATE_attr_t *attr = get_TEMPLATE_attr_const(node);
343
344         assert(is_TEMPLATE_irn(node) && "Not an TEMPLATE node.");
345         assert(pos < ARR_LEN(attr->slots) && "Invalid OUT position.");
346         assert(attr->slots[pos]  && "No register assigned");
347
348         return arch_register_get_index(attr->slots[pos]);
349 }
350
351 /**
352  * Returns the OUT register at position pos.
353  */
354 const arch_register_t *get_TEMPLATE_out_reg(const ir_node *node, int pos) {
355         const TEMPLATE_attr_t *attr = get_TEMPLATE_attr_const(node);
356
357         assert(is_TEMPLATE_irn(node) && "Not an TEMPLATE node.");
358         assert(pos < ARR_LEN(attr->slots) && "Invalid OUT position.");
359         assert(attr->slots[pos]  && "No register assigned");
360
361         return attr->slots[pos];
362 }
363
364 /**
365  * Returns the number of results.
366  */
367 int get_TEMPLATE_n_res(const ir_node *node) {
368         const TEMPLATE_attr_t *attr = get_TEMPLATE_attr_const(node);
369         return ARR_LEN(attr->slots);
370 }
371
372 /**
373  * Initializes the nodes attributes.
374  */
375 void init_TEMPLATE_attributes(ir_node *node, arch_irn_flags_t flags,
376                               const arch_register_req_t **in_reqs,
377                               const arch_register_req_t **out_reqs,
378                               const be_execution_unit_t ***execution_units,
379                               int n_res)
380 {
381         ir_graph        *irg  = get_irn_irg(node);
382         struct obstack  *obst = get_irg_obstack(irg);
383         TEMPLATE_attr_t *attr = get_TEMPLATE_attr(node);
384         (void) execution_units;
385
386         attr->flags   = flags;
387         attr->out_req = out_reqs;
388         attr->in_req  = in_reqs;
389
390         attr->slots = NEW_ARR_D(const arch_register_t*, obst, n_res);
391         memset(attr->slots, 0, n_res * sizeof(attr->slots[0]));
392 }
393
394 /***************************************************************************************
395  *                  _                            _                   _
396  *                 | |                          | |                 | |
397  *  _ __   ___   __| | ___    ___ ___  _ __  ___| |_ _ __ _   _  ___| |_ ___  _ __ ___
398  * | '_ \ / _ \ / _` |/ _ \  / __/ _ \| '_ \/ __| __| '__| | | |/ __| __/ _ \| '__/ __|
399  * | | | | (_) | (_| |  __/ | (_| (_) | | | \__ \ |_| |  | |_| | (__| || (_) | |  \__ \
400  * |_| |_|\___/ \__,_|\___|  \___\___/|_| |_|___/\__|_|   \__,_|\___|\__\___/|_|  |___/
401  *
402  ***************************************************************************************/
403
404 static
405 int TEMPLATE_compare_attr(ir_node *a, ir_node *b)
406 {
407         const TEMPLATE_attr_t *attr_a = get_TEMPLATE_attr_const(a);
408         const TEMPLATE_attr_t *attr_b = get_TEMPLATE_attr_const(b);
409
410         if(attr_a->flags != attr_b->flags)
411                 return 1;
412
413         return 0;
414 }
415
416
417 /* Include the generated constructor functions */
418 #include "gen_TEMPLATE_new_nodes.c.inl"