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