sieve is not needed anymore
[libfirm] / ir / be / TEMPLATE / TEMPLATE_new_nodes.c
1 /**
2  * This file implements the creation of the achitecture specific firm opcodes
3  * and the coresponding node constructors for the TEMPLATE assembler irg.
4  * $Id$
5  */
6 #ifdef HAVE_CONFIG_H
7 #include <config.h>
8 #endif
9
10 #ifdef _WIN32
11 #include <malloc.h>
12 #else
13 #include <alloca.h>
14 #endif
15
16 #include <stdlib.h>
17
18 #include "irprog_t.h"
19 #include "irgraph_t.h"
20 #include "irnode_t.h"
21 #include "irmode_t.h"
22 #include "ircons_t.h"
23 #include "iropt_t.h"
24 #include "irop.h"
25 #include "firm_common_t.h"
26 #include "irvrfy_t.h"
27 #include "irprintf.h"
28
29 #include "../bearch.h"
30
31 #include "TEMPLATE_nodes_attr.h"
32 #include "TEMPLATE_new_nodes.h"
33 #include "gen_TEMPLATE_regalloc_if.h"
34
35
36
37 /***********************************************************************************
38  *      _                                   _       _             __
39  *     | |                                 (_)     | |           / _|
40  *   __| |_   _ _ __ ___  _ __   ___ _ __   _ _ __ | |_ ___ _ __| |_ __ _  ___ ___
41  *  / _` | | | | '_ ` _ \| '_ \ / _ \ '__| | | '_ \| __/ _ \ '__|  _/ _` |/ __/ _ \
42  * | (_| | |_| | | | | | | |_) |  __/ |    | | | | | ||  __/ |  | || (_| | (_|  __/
43  *  \__,_|\__,_|_| |_| |_| .__/ \___|_|    |_|_| |_|\__\___|_|  |_| \__,_|\___\___|
44  *                       | |
45  *                       |_|
46  ***********************************************************************************/
47
48 /**
49  * Dumps the register requirements for either in or out.
50  */
51 static void dump_reg_req(FILE *F, ir_node *n, const arch_register_req_t **reqs,
52                          int inout) {
53         char *dir = inout ? "out" : "in";
54         int   max = inout ? get_TEMPLATE_n_res(n) : get_irn_arity(n);
55         char  buf[1024];
56         int   i;
57
58         memset(buf, 0, sizeof(buf));
59
60         if (reqs) {
61                 for (i = 0; i < max; i++) {
62                         fprintf(F, "%sreq #%d =", dir, i);
63
64                         if (reqs[i]->type == arch_register_req_type_none) {
65                                 fprintf(F, " n/a");
66                         }
67
68                         if (reqs[i]->type & arch_register_req_type_normal) {
69                                 fprintf(F, " %s", reqs[i]->cls->name);
70                         }
71
72                         if (reqs[i]->type & arch_register_req_type_limited) {
73                                 fprintf(F, " %s",
74                                                 arch_register_req_format(buf, sizeof(buf), reqs[i], n));
75                         }
76
77                         if (reqs[i]->type & arch_register_req_type_should_be_same) {
78                                 ir_fprintf(F, " same as %+F", get_irn_n(n, reqs[i]->other_same));
79                         }
80
81                         if (reqs[i]->type & arch_register_req_type_should_be_different) {
82                                 ir_fprintf(F, " different from %+F", get_irn_n(n, reqs[i]->other_different));
83                         }
84
85                         fprintf(F, "\n");
86                 }
87
88                 fprintf(F, "\n");
89         } else {
90                 fprintf(F, "%sreq = N/A\n", dir);
91         }
92 }
93
94
95 /**
96  * Dumper interface for dumping TEMPLATE nodes in vcg.
97  * @param n        the node to dump
98  * @param F        the output file
99  * @param reason   indicates which kind of information should be dumped
100  * @return 0 on success or != 0 on failure
101  */
102 static int TEMPLATE_dump_node(ir_node *n, FILE *F, dump_reason_t reason) {
103         ir_mode     *mode = NULL;
104         int          bad  = 0;
105         int          i;
106         TEMPLATE_attr_t *attr;
107         const arch_register_req_t **reqs;
108         const arch_register_t     **slots;
109
110         switch (reason) {
111                 case dump_node_opcode_txt:
112                         fprintf(F, "%s", get_irn_opname(n));
113                         break;
114
115                 case dump_node_mode_txt:
116                         mode = get_irn_mode(n);
117
118                         if (mode) {
119                                 fprintf(F, "[%s]", get_mode_name(mode));
120                         }
121                         else {
122                                 fprintf(F, "[?NOMODE?]");
123                         }
124                         break;
125
126                 case dump_node_nodeattr_txt:
127
128                         /* TODO: dump some attributes which should show up */
129                         /* in node name in dump (e.g. consts or the like)  */
130
131                         break;
132
133                 case dump_node_info_txt:
134                         attr = get_TEMPLATE_attr(n);
135                         fprintf(F, "=== TEMPLATE attr begin ===\n");
136
137                         /* dump IN requirements */
138                         if (get_irn_arity(n) > 0) {
139                                 reqs = get_TEMPLATE_in_req_all(n);
140                                 dump_reg_req(F, n, reqs, 0);
141                         }
142
143                         /* dump OUT requirements */
144                         if (attr->n_res > 0) {
145                                 reqs = get_TEMPLATE_out_req_all(n);
146                                 dump_reg_req(F, n, reqs, 1);
147                         }
148
149                         /* dump assigned registers */
150                         slots = get_TEMPLATE_slots(n);
151                         if (slots && attr->n_res > 0) {
152                                 for (i = 0; i < attr->n_res; i++) {
153                                         if (slots[i]) {
154                                                 fprintf(F, "reg #%d = %s\n", i, slots[i]->name);
155                                         }
156                                         else {
157                                                 fprintf(F, "reg #%d = n/a\n", i);
158                                         }
159                                 }
160                         }
161                         fprintf(F, "\n");
162
163                         /* dump n_res */
164                         fprintf(F, "n_res = %d\n", get_TEMPLATE_n_res(n));
165
166                         /* dump flags */
167                         fprintf(F, "flags =");
168                         if (attr->flags == arch_irn_flags_none) {
169                                 fprintf(F, " none");
170                         }
171                         else {
172                                 if (attr->flags & arch_irn_flags_dont_spill) {
173                                         fprintf(F, " unspillable");
174                                 }
175                                 if (attr->flags & arch_irn_flags_rematerializable) {
176                                         fprintf(F, " remat");
177                                 }
178                                 if (attr->flags & arch_irn_flags_ignore) {
179                                         fprintf(F, " ignore");
180                                 }
181                         }
182                         fprintf(F, " (%d)\n", attr->flags);
183
184                         /* TODO: dump all additional attributes */
185
186                         fprintf(F, "=== TEMPLATE attr end ===\n");
187                         /* end of: case dump_node_info_txt */
188                         break;
189         }
190
191
192         return bad;
193 }
194
195
196
197 /***************************************************************************************************
198  *        _   _                   _       __        _                    _   _               _
199  *       | | | |                 | |     / /       | |                  | | | |             | |
200  *   __ _| |_| |_ _ __   ___  ___| |_   / /_ _  ___| |_   _ __ ___   ___| |_| |__   ___   __| |___
201  *  / _` | __| __| '__| / __|/ _ \ __| / / _` |/ _ \ __| | '_ ` _ \ / _ \ __| '_ \ / _ \ / _` / __|
202  * | (_| | |_| |_| |    \__ \  __/ |_ / / (_| |  __/ |_  | | | | | |  __/ |_| | | | (_) | (_| \__ \
203  *  \__,_|\__|\__|_|    |___/\___|\__/_/ \__, |\___|\__| |_| |_| |_|\___|\__|_| |_|\___/ \__,_|___/
204  *                                        __/ |
205  *                                       |___/
206  ***************************************************************************************************/
207
208 /**
209  * Wraps get_irn_generic_attr() as it takes no const ir_node, so we need to do a cast.
210  * Firm was made by people hating const :-(
211  */
212 TEMPLATE_attr_t *get_TEMPLATE_attr(const ir_node *node) {
213         assert(is_TEMPLATE_irn(node) && "need TEMPLATE node to get attributes");
214         return (TEMPLATE_attr_t *)get_irn_generic_attr((ir_node *)node);
215 }
216
217 /**
218  * Returns the argument register requirements of a TEMPLATE node.
219  */
220 const arch_register_req_t **get_TEMPLATE_in_req_all(const ir_node *node) {
221         TEMPLATE_attr_t *attr = get_TEMPLATE_attr(node);
222         return attr->in_req;
223 }
224
225 /**
226  * Returns the result register requirements of an TEMPLATE node.
227  */
228 const arch_register_req_t **get_TEMPLATE_out_req_all(const ir_node *node) {
229         TEMPLATE_attr_t *attr = get_TEMPLATE_attr(node);
230         return attr->out_req;
231 }
232
233 /**
234  * Returns the argument register requirement at position pos of an TEMPLATE node.
235  */
236 const arch_register_req_t *get_TEMPLATE_in_req(const ir_node *node, int pos) {
237         TEMPLATE_attr_t *attr = get_TEMPLATE_attr(node);
238         return attr->in_req[pos];
239 }
240
241 /**
242  * Returns the result register requirement at position pos of an TEMPLATE node.
243  */
244 const arch_register_req_t *get_TEMPLATE_out_req(const ir_node *node, int pos) {
245         TEMPLATE_attr_t *attr = get_TEMPLATE_attr(node);
246         return attr->out_req[pos];
247 }
248
249 /**
250  * Sets the OUT register requirements at position pos.
251  */
252 void set_TEMPLATE_req_out(ir_node *node, const arch_register_req_t *req, int pos) {
253         TEMPLATE_attr_t *attr   = get_TEMPLATE_attr(node);
254         attr->out_req[pos] = req;
255 }
256
257 /**
258  * Sets the IN register requirements at position pos.
259  */
260 void set_TEMPLATE_req_in(ir_node *node, const arch_register_req_t *req, int pos) {
261         TEMPLATE_attr_t *attr  = get_TEMPLATE_attr(node);
262         attr->in_req[pos] = req;
263 }
264
265 /**
266  * Returns the register flag of an TEMPLATE node.
267  */
268 arch_irn_flags_t get_TEMPLATE_flags(const ir_node *node) {
269         TEMPLATE_attr_t *attr = get_TEMPLATE_attr(node);
270         return attr->flags;
271 }
272
273 /**
274  * Sets the register flag of an TEMPLATE node.
275  */
276 void set_TEMPLATE_flags(const ir_node *node, arch_irn_flags_t flags) {
277         TEMPLATE_attr_t *attr = get_TEMPLATE_attr(node);
278         attr->flags      = flags;
279 }
280
281 /**
282  * Returns the result register slots of an TEMPLATE node.
283  */
284 const arch_register_t **get_TEMPLATE_slots(const ir_node *node) {
285         TEMPLATE_attr_t *attr = get_TEMPLATE_attr(node);
286         return attr->slots;
287 }
288
289 /**
290  * Returns the name of the OUT register at position pos.
291  */
292 const char *get_TEMPLATE_out_reg_name(const ir_node *node, int pos) {
293         TEMPLATE_attr_t *attr = get_TEMPLATE_attr(node);
294
295         assert(is_TEMPLATE_irn(node) && "Not an TEMPLATE node.");
296         assert(pos < attr->n_res && "Invalid OUT position.");
297         assert(attr->slots[pos]  && "No register assigned");
298
299         return arch_register_get_name(attr->slots[pos]);
300 }
301
302 /**
303  * Returns the index of the OUT register at position pos within its register class.
304  */
305 int get_TEMPLATE_out_regnr(const ir_node *node, int pos) {
306         TEMPLATE_attr_t *attr = get_TEMPLATE_attr(node);
307
308         assert(is_TEMPLATE_irn(node) && "Not an TEMPLATE node.");
309         assert(pos < attr->n_res && "Invalid OUT position.");
310         assert(attr->slots[pos]  && "No register assigned");
311
312         return arch_register_get_index(attr->slots[pos]);
313 }
314
315 /**
316  * Returns the OUT register at position pos.
317  */
318 const arch_register_t *get_TEMPLATE_out_reg(const ir_node *node, int pos) {
319         TEMPLATE_attr_t *attr = get_TEMPLATE_attr(node);
320
321         assert(is_TEMPLATE_irn(node) && "Not an TEMPLATE node.");
322         assert(pos < attr->n_res && "Invalid OUT position.");
323         assert(attr->slots[pos]  && "No register assigned");
324
325         return attr->slots[pos];
326 }
327
328 /**
329  * Sets the number of results.
330  */
331 void set_TEMPLATE_n_res(ir_node *node, int n_res) {
332         TEMPLATE_attr_t *attr = get_TEMPLATE_attr(node);
333         attr->n_res      = n_res;
334 }
335
336 /**
337  * Returns the number of results.
338  */
339 int get_TEMPLATE_n_res(const ir_node *node) {
340         TEMPLATE_attr_t *attr = get_TEMPLATE_attr(node);
341         return attr->n_res;
342 }
343
344 /**
345  * Initializes the nodes attributes.
346  */
347 void init_TEMPLATE_attributes(ir_node *node, arch_irn_flags_t flags,
348                               const arch_register_req_t **in_reqs,
349                               const arch_register_req_t **out_reqs,
350                               const be_execution_unit_t ***execution_units,
351                               int n_res, unsigned latency)
352 {
353         TEMPLATE_attr_t *attr = get_TEMPLATE_attr(node);
354
355         attr->flags = flags;
356         attr->out_req = out_reqs;
357         attr->in_req = in_reqs;
358         attr->n_res = n_res;
359
360         memset((void *)attr->slots, 0, n_res * sizeof(attr->slots[0]));
361 }
362
363 /***************************************************************************************
364  *                  _                            _                   _
365  *                 | |                          | |                 | |
366  *  _ __   ___   __| | ___    ___ ___  _ __  ___| |_ _ __ _   _  ___| |_ ___  _ __ ___
367  * | '_ \ / _ \ / _` |/ _ \  / __/ _ \| '_ \/ __| __| '__| | | |/ __| __/ _ \| '__/ __|
368  * | | | | (_) | (_| |  __/ | (_| (_) | | | \__ \ |_| |  | |_| | (__| || (_) | |  \__ \
369  * |_| |_|\___/ \__,_|\___|  \___\___/|_| |_|___/\__|_|   \__,_|\___|\__\___/|_|  |___/
370  *
371  ***************************************************************************************/
372
373 /* Include the generated constructor functions */
374 #include "gen_TEMPLATE_new_nodes.c.inl"