Let dfs() discover only memory nodes
[libfirm] / ir / be / ppc32 / ppc32_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 ppc assembler
24  *         irg.
25  * @author  Moritz Kroll, Jens Mueller
26  * @version $Id$
27  */
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include <stdlib.h>
33
34 #include "irprog_t.h"
35 #include "irgraph_t.h"
36 #include "irnode_t.h"
37 #include "irmode_t.h"
38 #include "ircons_t.h"
39 #include "iropt_t.h"
40 #include "irop.h"
41 #include "firm_common_t.h"
42 #include "irvrfy_t.h"
43 #include "irprintf.h"
44 #include "xmalloc.h"
45
46 #include "../bearch_t.h"
47
48 #include "ppc32_nodes_attr.h"
49 #include "ppc32_new_nodes.h"
50 #include "gen_ppc32_regalloc_if.h"
51
52
53
54 /***********************************************************************************
55  *      _                                   _       _             __
56  *     | |                                 (_)     | |           / _|
57  *   __| |_   _ _ __ ___  _ __   ___ _ __   _ _ __ | |_ ___ _ __| |_ __ _  ___ ___
58  *  / _` | | | | '_ ` _ \| '_ \ / _ \ '__| | | '_ \| __/ _ \ '__|  _/ _` |/ __/ _ \
59  * | (_| | |_| | | | | | | |_) |  __/ |    | | | | | ||  __/ |  | || (_| | (_|  __/
60  *  \__,_|\__,_|_| |_| |_| .__/ \___|_|    |_|_| |_|\__\___|_|  |_| \__,_|\___\___|
61  *                       | |
62  *                       |_|
63  ***********************************************************************************/
64
65 /**
66  * Dumps the register requirements for either in or out.
67  */
68 static void dump_reg_req(FILE *F, ir_node *n, const arch_register_req_t **reqs, int inout) {
69         char *dir = inout ? "out" : "in";
70         int   max = inout ? get_ppc32_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 ppc32 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 ppc32_dump_node(ir_node *n, FILE *F, dump_reason_t reason) {
121         ir_mode     *mode = NULL;
122         int          bad  = 0;
123         int          i;
124         ppc32_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_ppc32_attr(n);
153                         fprintf(F, "=== ppc attr begin ===\n");
154
155                         /* dump IN requirements */
156                         if (get_irn_arity(n) > 0) {
157                                 reqs = get_ppc32_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_ppc32_out_req_all(n);
164                                 dump_reg_req(F, n, reqs, 1);
165                         }
166
167                         /* dump assigned registers */
168                         slots = get_ppc32_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_ppc32_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, "=== ppc 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 /**
227  * Wraps get_irn_generic_attr() as it takes no const ir_node, so we need to do a cast.
228  * Firm was made by people hating const :-(
229  */
230 ppc32_attr_t *get_ppc32_attr(const ir_node *node) {
231         assert(is_ppc32_irn(node) && "need ppc node to get attributes");
232         return (ppc32_attr_t *)get_irn_generic_attr((ir_node *)node);
233 }
234
235 /**
236  * Returns the argument register requirements of a ppc node.
237  */
238 const arch_register_req_t **get_ppc32_in_req_all(const ir_node *node) {
239         ppc32_attr_t *attr = get_ppc32_attr(node);
240         return attr->in_req;
241 }
242
243 /**
244  * Returns the result register requirements of an ppc node.
245  */
246 const arch_register_req_t **get_ppc32_out_req_all(const ir_node *node) {
247         ppc32_attr_t *attr = get_ppc32_attr(node);
248         return attr->out_req;
249 }
250
251 /**
252  * Returns the argument register requirement at position pos of an ppc node.
253  */
254 const arch_register_req_t *get_ppc32_in_req(const ir_node *node, int pos) {
255         ppc32_attr_t *attr = get_ppc32_attr(node);
256         return attr->in_req[pos];
257 }
258
259 /**
260  * Returns the result register requirement at position pos of an ppc node.
261  */
262 const arch_register_req_t *get_ppc32_out_req(const ir_node *node, int pos) {
263         ppc32_attr_t *attr = get_ppc32_attr(node);
264         return attr->out_req[pos];
265 }
266
267 /**
268  * Sets the OUT register requirements at position pos.
269  */
270 void set_ppc32_req_out(ir_node *node, const arch_register_req_t *req, int pos) {
271         ppc32_attr_t *attr   = get_ppc32_attr(node);
272         attr->out_req[pos] = req;
273 }
274
275 /**
276  * Sets the IN register requirements at position pos.
277  */
278 void set_ppc32_req_in(ir_node *node, const arch_register_req_t *req, int pos) {
279         ppc32_attr_t *attr  = get_ppc32_attr(node);
280         attr->in_req[pos] = req;
281 }
282
283 /**
284  * Returns the register flag of an ppc node.
285  */
286 arch_irn_flags_t get_ppc32_flags(const ir_node *node) {
287         ppc32_attr_t *attr = get_ppc32_attr(node);
288         return attr->flags;
289 }
290
291 /**
292  * Sets the register flag of an ppc node.
293  */
294 void set_ppc32_flags(const ir_node *node, arch_irn_flags_t flags) {
295         ppc32_attr_t *attr = get_ppc32_attr(node);
296         attr->flags      = flags;
297 }
298
299 /**
300  * Returns the result register slots of an ppc node.
301  */
302 const arch_register_t **get_ppc32_slots(const ir_node *node) {
303         ppc32_attr_t *attr = get_ppc32_attr(node);
304         return attr->slots;
305 }
306
307 /**
308  * Returns the name of the OUT register at position pos.
309  */
310 const char *get_ppc32_out_reg_name(const ir_node *node, int pos) {
311         ppc32_attr_t *attr = get_ppc32_attr(node);
312
313         assert(is_ppc32_irn(node) && "Not an ppc node.");
314         assert(pos < ARR_LEN(attr->slots) && "Invalid OUT position.");
315         assert(attr->slots[pos]  && "No register assigned");
316
317         return arch_register_get_name(attr->slots[pos]);
318 }
319
320 /**
321  * Returns the index of the OUT register at position pos within its register class.
322  */
323 int get_ppc32_out_regnr(const ir_node *node, int pos) {
324         ppc32_attr_t *attr = get_ppc32_attr(node);
325
326         assert(is_ppc32_irn(node) && "Not an ppc node.");
327         assert(pos < ARR_LEN(attr->slots) && "Invalid OUT position.");
328         assert(attr->slots[pos]  && "No register assigned");
329
330         return arch_register_get_index(attr->slots[pos]);
331 }
332
333 /**
334  * Returns the OUT register at position pos.
335  */
336 const arch_register_t *get_ppc32_out_reg(const ir_node *node, int pos) {
337         ppc32_attr_t *attr = get_ppc32_attr(node);
338
339         assert(is_ppc32_irn(node) && "Not an ppc node.");
340         assert(pos < ARR_LEN(attr->slots) && "Invalid OUT position.");
341         assert(attr->slots[pos]  && "No register assigned");
342
343         return attr->slots[pos];
344 }
345
346 /**
347  * Returns the number of results.
348  */
349 int get_ppc32_n_res(const ir_node *node) {
350         ppc32_attr_t *attr = get_ppc32_attr(node);
351         return ARR_LEN(attr->slots);
352 }
353
354 /**
355  * Sets the type of the constant (if any)
356  * May be either iro_Const or iro_SymConst
357  */
358 /* void set_ppc32_type(const ir_node *node, opcode type) {
359         ppc32_attr_t *attr = get_ppc32_attr(node);
360         attr->type      = type;
361 }  */
362
363 /**
364  * Returns the type of the content (if any)
365  */
366 ppc32_attr_content_type get_ppc32_type(const ir_node *node) {
367         ppc32_attr_t *attr = get_ppc32_attr(node);
368         return attr->content_type;
369 }
370
371 /**
372  * Sets a tarval type content (also updating the content_type)
373  */
374 void set_ppc32_constant_tarval(const ir_node *node, tarval *const_tarval) {
375         ppc32_attr_t *attr = get_ppc32_attr(node);
376         attr->content_type = ppc32_ac_Const;
377         attr->data.constant_tarval = const_tarval;
378 }
379
380 /**
381  * Returns a tarval type constant
382  */
383 tarval *get_ppc32_constant_tarval(const ir_node *node) {
384         ppc32_attr_t *attr = get_ppc32_attr(node);
385         return attr->data.constant_tarval;
386 }
387
388 /**
389  * Sets an ident type constant (also updating the content_type)
390  */
391 void set_ppc32_symconst_ident(const ir_node *node, ident *symconst_ident) {
392         ppc32_attr_t *attr = get_ppc32_attr(node);
393         attr->content_type = ppc32_ac_SymConst;
394         attr->data.symconst_ident = symconst_ident;
395 }
396
397 /**
398  * Returns an ident type constant
399  */
400 ident *get_ppc32_symconst_ident(const ir_node *node) {
401         ppc32_attr_t *attr = get_ppc32_attr(node);
402         return attr->data.symconst_ident;
403 }
404
405
406 /**
407  * Sets an entity (also updating the content_type)
408  */
409 void set_ppc32_frame_entity(const ir_node *node, ir_entity *ent) {
410         ppc32_attr_t *attr = get_ppc32_attr(node);
411         attr->content_type = ppc32_ac_FrameEntity;
412         attr->data.frame_entity = ent;
413 }
414
415 /**
416  * Returns an entity
417  */
418 ir_entity *get_ppc32_frame_entity(const ir_node *node) {
419         ppc32_attr_t *attr = get_ppc32_attr(node);
420         return attr->data.frame_entity;
421 }
422
423 /**
424  * Sets a Rlwimi const (also updating the content_type)
425  */
426 void set_ppc32_rlwimi_const(const ir_node *node, unsigned shift, unsigned maskA, unsigned maskB) {
427         ppc32_attr_t *attr = get_ppc32_attr(node);
428         attr->content_type = ppc32_ac_RlwimiConst;
429         attr->data.rlwimi_const.shift = shift;
430         attr->data.rlwimi_const.maskA = maskA;
431         attr->data.rlwimi_const.maskB = maskB;
432 }
433
434 /**
435  * Returns the rlwimi const structure
436  */
437 rlwimi_const_t *get_ppc32_rlwimi_const(const ir_node *node) {
438         ppc32_attr_t *attr = get_ppc32_attr(node);
439         return &attr->data.rlwimi_const;
440 }
441
442 /**
443  * Sets a Proj number (also updating the content_type)
444  */
445 void set_ppc32_proj_nr(const ir_node *node, int proj_nr) {
446         ppc32_attr_t *attr = get_ppc32_attr(node);
447         attr->content_type = ppc32_ac_BranchProj;
448         attr->data.proj_nr = proj_nr;
449 }
450
451 /**
452  * Returns the proj number
453  */
454 int get_ppc32_proj_nr(const ir_node *node) {
455         ppc32_attr_t *attr = get_ppc32_attr(node);
456         return attr->data.proj_nr;
457 }
458
459 /**
460  * Sets an offset for a memory access (also updating the content_type)
461  */
462 void set_ppc32_offset(const ir_node *node, int offset) {
463         ppc32_attr_t *attr = get_ppc32_attr(node);
464         attr->content_type = ppc32_ac_Offset;
465         attr->data.offset  = offset;
466 }
467
468 /**
469  * Returns the offset
470  */
471 int get_ppc32_offset(const ir_node *node) {
472         ppc32_attr_t *attr = get_ppc32_attr(node);
473         return attr->data.offset;
474 }
475
476 /**
477  * Sets the offset mode (ppc32_ao_None, ppc32_ao_Lo16, ppc32_ao_Hi16 or ppc32_ao_Ha16)
478  */
479 void set_ppc32_offset_mode(const ir_node *node, ppc32_attr_offset_mode mode) {
480         ppc32_attr_t *attr = get_ppc32_attr(node);
481         attr->offset_mode = mode;
482 }
483
484 /**
485  * Returns the offset mode
486  */
487 ppc32_attr_offset_mode get_ppc32_offset_mode(const ir_node *node) {
488         ppc32_attr_t *attr = get_ppc32_attr(node);
489         return attr->offset_mode;
490 }
491
492
493 /**
494  * Initializes ppc specific node attributes
495  */
496 void init_ppc32_attributes(ir_node *node, int flags,
497                                                  const arch_register_req_t **in_reqs, const arch_register_req_t **out_reqs,
498                                                  const be_execution_unit_t ***execution_units,
499                                                  int n_res) {
500         ir_graph       *irg  = get_irn_irg(node);
501         struct obstack *obst = get_irg_obstack(irg);
502         ppc32_attr_t   *attr = get_ppc32_attr(node);
503         (void) execution_units;
504
505         attr->flags   = flags;
506         attr->in_req  = in_reqs;
507         attr->out_req = out_reqs;
508
509         attr->content_type = ppc32_ac_None;
510         attr->offset_mode  = ppc32_ao_Illegal;
511         attr->data.empty   = NULL;
512
513         attr->slots = NEW_ARR_D(const arch_register_t*, obst, n_res);
514         memset(attr->slots, 0, n_res * sizeof(attr->slots[0]));
515 }
516
517
518 /***************************************************************************************
519  *                  _                            _                   _
520  *                 | |                          | |                 | |
521  *  _ __   ___   __| | ___    ___ ___  _ __  ___| |_ _ __ _   _  ___| |_ ___  _ __ ___
522  * | '_ \ / _ \ / _` |/ _ \  / __/ _ \| '_ \/ __| __| '__| | | |/ __| __/ _ \| '__/ __|
523  * | | | | (_) | (_| |  __/ | (_| (_) | | | \__ \ |_| |  | |_| | (__| || (_) | |  \__ \
524  * |_| |_|\___/ \__,_|\___|  \___\___/|_| |_|___/\__|_|   \__,_|\___|\__\___/|_|  |___/
525  *
526  ***************************************************************************************/
527
528 /* Include the generated constructor functions */
529 #include "gen_ppc32_new_nodes.c.inl"