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