ebc356ae60d04859e27f0fd26583fb47a9210721
[libfirm] / ir / be / TEMPLATE / TEMPLATE_new_nodes.c
1 /*
2  * Copyright (C) 1995-2007 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  * This file implements the creation of the achitecture specific firm opcodes
22  * and the coresponding node constructors for the TEMPLATE assembler irg.
23  * $Id$
24  */
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
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                                 ir_fprintf(F, " same as %+F", get_irn_n(n, reqs[i]->other_same));
93                         }
94
95                         if (reqs[i]->type & arch_register_req_type_should_be_different) {
96                                 ir_fprintf(F, " different from %+F", get_irn_n(n, reqs[i]->other_different));
97                         }
98
99                         fprintf(F, "\n");
100                 }
101
102                 fprintf(F, "\n");
103         } else {
104                 fprintf(F, "%sreq = N/A\n", dir);
105         }
106 }
107
108
109 /**
110  * Dumper interface for dumping TEMPLATE nodes in vcg.
111  * @param n        the node to dump
112  * @param F        the output file
113  * @param reason   indicates which kind of information should be dumped
114  * @return 0 on success or != 0 on failure
115  */
116 static int TEMPLATE_dump_node(ir_node *n, FILE *F, dump_reason_t reason) {
117         ir_mode     *mode = NULL;
118         int          bad  = 0;
119         int          i;
120         TEMPLATE_attr_t *attr;
121         const arch_register_req_t **reqs;
122         const arch_register_t     **slots;
123
124         switch (reason) {
125                 case dump_node_opcode_txt:
126                         fprintf(F, "%s", get_irn_opname(n));
127                         break;
128
129                 case dump_node_mode_txt:
130                         mode = get_irn_mode(n);
131
132                         if (mode) {
133                                 fprintf(F, "[%s]", get_mode_name(mode));
134                         }
135                         else {
136                                 fprintf(F, "[?NOMODE?]");
137                         }
138                         break;
139
140                 case dump_node_nodeattr_txt:
141
142                         /* TODO: dump some attributes which should show up */
143                         /* in node name in dump (e.g. consts or the like)  */
144
145                         break;
146
147                 case dump_node_info_txt:
148                         attr = get_TEMPLATE_attr(n);
149                         fprintf(F, "=== TEMPLATE attr begin ===\n");
150
151                         /* dump IN requirements */
152                         if (get_irn_arity(n) > 0) {
153                                 reqs = get_TEMPLATE_in_req_all(n);
154                                 dump_reg_req(F, n, reqs, 0);
155                         }
156
157                         /* dump OUT requirements */
158                         if (attr->n_res > 0) {
159                                 reqs = get_TEMPLATE_out_req_all(n);
160                                 dump_reg_req(F, n, reqs, 1);
161                         }
162
163                         /* dump assigned registers */
164                         slots = get_TEMPLATE_slots(n);
165                         if (slots && attr->n_res > 0) {
166                                 for (i = 0; i < attr->n_res; i++) {
167                                         if (slots[i]) {
168                                                 fprintf(F, "reg #%d = %s\n", i, slots[i]->name);
169                                         }
170                                         else {
171                                                 fprintf(F, "reg #%d = n/a\n", i);
172                                         }
173                                 }
174                         }
175                         fprintf(F, "\n");
176
177                         /* dump n_res */
178                         fprintf(F, "n_res = %d\n", get_TEMPLATE_n_res(n));
179
180                         /* dump flags */
181                         fprintf(F, "flags =");
182                         if (attr->flags == arch_irn_flags_none) {
183                                 fprintf(F, " none");
184                         }
185                         else {
186                                 if (attr->flags & arch_irn_flags_dont_spill) {
187                                         fprintf(F, " unspillable");
188                                 }
189                                 if (attr->flags & arch_irn_flags_rematerializable) {
190                                         fprintf(F, " remat");
191                                 }
192                                 if (attr->flags & arch_irn_flags_ignore) {
193                                         fprintf(F, " ignore");
194                                 }
195                         }
196                         fprintf(F, " (%d)\n", attr->flags);
197
198                         /* TODO: dump all additional attributes */
199
200                         fprintf(F, "=== TEMPLATE attr end ===\n");
201                         /* end of: case dump_node_info_txt */
202                         break;
203         }
204
205
206         return bad;
207 }
208
209
210
211 /***************************************************************************************************
212  *        _   _                   _       __        _                    _   _               _
213  *       | | | |                 | |     / /       | |                  | | | |             | |
214  *   __ _| |_| |_ _ __   ___  ___| |_   / /_ _  ___| |_   _ __ ___   ___| |_| |__   ___   __| |___
215  *  / _` | __| __| '__| / __|/ _ \ __| / / _` |/ _ \ __| | '_ ` _ \ / _ \ __| '_ \ / _ \ / _` / __|
216  * | (_| | |_| |_| |    \__ \  __/ |_ / / (_| |  __/ |_  | | | | | |  __/ |_| | | | (_) | (_| \__ \
217  *  \__,_|\__|\__|_|    |___/\___|\__/_/ \__, |\___|\__| |_| |_| |_|\___|\__|_| |_|\___/ \__,_|___/
218  *                                        __/ |
219  *                                       |___/
220  ***************************************************************************************************/
221
222 /**
223  * Wraps get_irn_generic_attr() as it takes no const ir_node, so we need to do a cast.
224  * Firm was made by people hating const :-(
225  */
226 TEMPLATE_attr_t *get_TEMPLATE_attr(const ir_node *node) {
227         assert(is_TEMPLATE_irn(node) && "need TEMPLATE node to get attributes");
228         return (TEMPLATE_attr_t *)get_irn_generic_attr((ir_node *)node);
229 }
230
231 /**
232  * Returns the argument register requirements of a TEMPLATE node.
233  */
234 const arch_register_req_t **get_TEMPLATE_in_req_all(const ir_node *node) {
235         TEMPLATE_attr_t *attr = get_TEMPLATE_attr(node);
236         return attr->in_req;
237 }
238
239 /**
240  * Returns the result register requirements of an TEMPLATE node.
241  */
242 const arch_register_req_t **get_TEMPLATE_out_req_all(const ir_node *node) {
243         TEMPLATE_attr_t *attr = get_TEMPLATE_attr(node);
244         return attr->out_req;
245 }
246
247 /**
248  * Returns the argument register requirement at position pos of an TEMPLATE node.
249  */
250 const arch_register_req_t *get_TEMPLATE_in_req(const ir_node *node, int pos) {
251         TEMPLATE_attr_t *attr = get_TEMPLATE_attr(node);
252         return attr->in_req[pos];
253 }
254
255 /**
256  * Returns the result register requirement at position pos of an TEMPLATE node.
257  */
258 const arch_register_req_t *get_TEMPLATE_out_req(const ir_node *node, int pos) {
259         TEMPLATE_attr_t *attr = get_TEMPLATE_attr(node);
260         return attr->out_req[pos];
261 }
262
263 /**
264  * Sets the OUT register requirements at position pos.
265  */
266 void set_TEMPLATE_req_out(ir_node *node, const arch_register_req_t *req, int pos) {
267         TEMPLATE_attr_t *attr   = get_TEMPLATE_attr(node);
268         attr->out_req[pos] = req;
269 }
270
271 /**
272  * Sets the IN register requirements at position pos.
273  */
274 void set_TEMPLATE_req_in(ir_node *node, const arch_register_req_t *req, int pos) {
275         TEMPLATE_attr_t *attr  = get_TEMPLATE_attr(node);
276         attr->in_req[pos] = req;
277 }
278
279 /**
280  * Returns the register flag of an TEMPLATE node.
281  */
282 arch_irn_flags_t get_TEMPLATE_flags(const ir_node *node) {
283         TEMPLATE_attr_t *attr = get_TEMPLATE_attr(node);
284         return attr->flags;
285 }
286
287 /**
288  * Sets the register flag of an TEMPLATE node.
289  */
290 void set_TEMPLATE_flags(const ir_node *node, arch_irn_flags_t flags) {
291         TEMPLATE_attr_t *attr = get_TEMPLATE_attr(node);
292         attr->flags      = flags;
293 }
294
295 /**
296  * Returns the result register slots of an TEMPLATE node.
297  */
298 const arch_register_t **get_TEMPLATE_slots(const ir_node *node) {
299         TEMPLATE_attr_t *attr = get_TEMPLATE_attr(node);
300         return attr->slots;
301 }
302
303 /**
304  * Returns the name of the OUT register at position pos.
305  */
306 const char *get_TEMPLATE_out_reg_name(const ir_node *node, int pos) {
307         TEMPLATE_attr_t *attr = get_TEMPLATE_attr(node);
308
309         assert(is_TEMPLATE_irn(node) && "Not an TEMPLATE node.");
310         assert(pos < attr->n_res && "Invalid OUT position.");
311         assert(attr->slots[pos]  && "No register assigned");
312
313         return arch_register_get_name(attr->slots[pos]);
314 }
315
316 /**
317  * Returns the index of the OUT register at position pos within its register class.
318  */
319 int get_TEMPLATE_out_regnr(const ir_node *node, int pos) {
320         TEMPLATE_attr_t *attr = get_TEMPLATE_attr(node);
321
322         assert(is_TEMPLATE_irn(node) && "Not an TEMPLATE node.");
323         assert(pos < attr->n_res && "Invalid OUT position.");
324         assert(attr->slots[pos]  && "No register assigned");
325
326         return arch_register_get_index(attr->slots[pos]);
327 }
328
329 /**
330  * Returns the OUT register at position pos.
331  */
332 const arch_register_t *get_TEMPLATE_out_reg(const ir_node *node, int pos) {
333         TEMPLATE_attr_t *attr = get_TEMPLATE_attr(node);
334
335         assert(is_TEMPLATE_irn(node) && "Not an TEMPLATE node.");
336         assert(pos < attr->n_res && "Invalid OUT position.");
337         assert(attr->slots[pos]  && "No register assigned");
338
339         return attr->slots[pos];
340 }
341
342 /**
343  * Sets the number of results.
344  */
345 void set_TEMPLATE_n_res(ir_node *node, int n_res) {
346         TEMPLATE_attr_t *attr = get_TEMPLATE_attr(node);
347         attr->n_res      = n_res;
348 }
349
350 /**
351  * Returns the number of results.
352  */
353 int get_TEMPLATE_n_res(const ir_node *node) {
354         TEMPLATE_attr_t *attr = get_TEMPLATE_attr(node);
355         return attr->n_res;
356 }
357
358 /**
359  * Initializes the nodes attributes.
360  */
361 void init_TEMPLATE_attributes(ir_node *node, arch_irn_flags_t flags,
362                               const arch_register_req_t **in_reqs,
363                               const arch_register_req_t **out_reqs,
364                               const be_execution_unit_t ***execution_units,
365                               int n_res, unsigned latency)
366 {
367         TEMPLATE_attr_t *attr = get_TEMPLATE_attr(node);
368
369         attr->flags = flags;
370         attr->out_req = out_reqs;
371         attr->in_req = in_reqs;
372         attr->n_res = n_res;
373
374         memset((void *)attr->slots, 0, n_res * sizeof(attr->slots[0]));
375 }
376
377 /***************************************************************************************
378  *                  _                            _                   _
379  *                 | |                          | |                 | |
380  *  _ __   ___   __| | ___    ___ ___  _ __  ___| |_ _ __ _   _  ___| |_ ___  _ __ ___
381  * | '_ \ / _ \ / _` |/ _ \  / __/ _ \| '_ \/ __| __| '__| | | |/ __| __/ _ \| '__/ __|
382  * | | | | (_) | (_| |  __/ | (_| (_) | | | \__ \ |_| |  | |_| | (__| || (_) | |  \__ \
383  * |_| |_|\___/ \__,_|\___|  \___\___/|_| |_|___/\__|_|   \__,_|\___|\__\___/|_|  |___/
384  *
385  ***************************************************************************************/
386
387 /* Include the generated constructor functions */
388 #include "gen_TEMPLATE_new_nodes.c.inl"