- Allow an arbitrary (for arbitrary < 32) number of in_rBAR and !in_rBAR constraints...
[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                                 const unsigned other = reqs[i]->other_same;
95                                 int i;
96
97                                 ir_fprintf(F, " same as");
98                                 for (i = 0; 1U << i <= other; ++i) {
99                                         if (other & (1U << i)) {
100                                                 ir_fprintf(F, " %+F", get_irn_n(n, i));
101                                         }
102                                 }
103                         }
104
105                         if (reqs[i]->type & arch_register_req_type_should_be_different) {
106                                 const unsigned other = reqs[i]->other_different;
107                                 int i;
108
109                                 ir_fprintf(F, " different from");
110                                 for (i = 0; 1U << i <= other; ++i) {
111                                         if (other & (1U << i)) {
112                                                 ir_fprintf(F, " %+F", get_irn_n(n, i));
113                                         }
114                                 }
115                         }
116
117                         fprintf(F, "\n");
118                 }
119
120                 fprintf(F, "\n");
121         } else {
122                 fprintf(F, "%sreq = N/A\n", dir);
123         }
124 }
125
126
127 /**
128  * Dumper interface for dumping ppc32 nodes in vcg.
129  * @param n        the node to dump
130  * @param F        the output file
131  * @param reason   indicates which kind of information should be dumped
132  * @return 0 on success or != 0 on failure
133  */
134 static int ppc32_dump_node(ir_node *n, FILE *F, dump_reason_t reason) {
135         ir_mode     *mode = NULL;
136         int          bad  = 0;
137         int          i;
138         ppc32_attr_t *attr;
139         const arch_register_req_t **reqs;
140         const arch_register_t     **slots;
141
142         switch (reason) {
143                 case dump_node_opcode_txt:
144                         fprintf(F, "%s", get_irn_opname(n));
145                         break;
146
147                 case dump_node_mode_txt:
148                         mode = get_irn_mode(n);
149
150                         if (mode) {
151                                 fprintf(F, "[%s]", get_mode_name(mode));
152                         }
153                         else {
154                                 fprintf(F, "[?NOMODE?]");
155                         }
156                         break;
157
158                 case dump_node_nodeattr_txt:
159
160                         /* TODO: dump some attributes which should show up */
161                         /* in node name in dump (e.g. consts or the like)  */
162
163                         break;
164
165                 case dump_node_info_txt:
166                         attr = get_ppc32_attr(n);
167                         fprintf(F, "=== ppc attr begin ===\n");
168
169                         /* dump IN requirements */
170                         if (get_irn_arity(n) > 0) {
171                                 reqs = get_ppc32_in_req_all(n);
172                                 dump_reg_req(F, n, reqs, 0);
173                         }
174
175                         /* dump OUT requirements */
176                         if (ARR_LEN(attr->slots) > 0) {
177                                 reqs = get_ppc32_out_req_all(n);
178                                 dump_reg_req(F, n, reqs, 1);
179                         }
180
181                         /* dump assigned registers */
182                         slots = get_ppc32_slots(n);
183                         if (slots && ARR_LEN(attr->slots) > 0) {
184                                 for (i = 0; i < ARR_LEN(attr->slots); i++) {
185                                         if (slots[i]) {
186                                                 fprintf(F, "reg #%d = %s\n", i, slots[i]->name);
187                                         }
188                                         else {
189                                                 fprintf(F, "reg #%d = n/a\n", i);
190                                         }
191                                 }
192                         }
193                         fprintf(F, "\n");
194
195                         /* dump n_res */
196                         fprintf(F, "n_res = %d\n", get_ppc32_n_res(n));
197
198                         /* dump flags */
199                         fprintf(F, "flags =");
200                         if (attr->flags == arch_irn_flags_none) {
201                                 fprintf(F, " none");
202                         }
203                         else {
204                                 if (attr->flags & arch_irn_flags_dont_spill) {
205                                         fprintf(F, " unspillable");
206                                 }
207                                 if (attr->flags & arch_irn_flags_rematerializable) {
208                                         fprintf(F, " remat");
209                                 }
210                                 if (attr->flags & arch_irn_flags_ignore) {
211                                         fprintf(F, " ignore");
212                                 }
213                         }
214                         fprintf(F, " (%d)\n", attr->flags);
215
216                         /* TODO: dump all additional attributes */
217
218                         fprintf(F, "=== ppc attr end ===\n");
219                         /* end of: case dump_node_info_txt */
220                         break;
221         }
222
223
224         return bad;
225 }
226
227
228
229 /***************************************************************************************************
230  *        _   _                   _       __        _                    _   _               _
231  *       | | | |                 | |     / /       | |                  | | | |             | |
232  *   __ _| |_| |_ _ __   ___  ___| |_   / /_ _  ___| |_   _ __ ___   ___| |_| |__   ___   __| |___
233  *  / _` | __| __| '__| / __|/ _ \ __| / / _` |/ _ \ __| | '_ ` _ \ / _ \ __| '_ \ / _ \ / _` / __|
234  * | (_| | |_| |_| |    \__ \  __/ |_ / / (_| |  __/ |_  | | | | | |  __/ |_| | | | (_) | (_| \__ \
235  *  \__,_|\__|\__|_|    |___/\___|\__/_/ \__, |\___|\__| |_| |_| |_|\___|\__|_| |_|\___/ \__,_|___/
236  *                                        __/ |
237  *                                       |___/
238  ***************************************************************************************************/
239
240 /**
241  * Wraps get_irn_generic_attr() as it takes no const ir_node, so we need to do a cast.
242  * Firm was made by people hating const :-(
243  */
244 ppc32_attr_t *get_ppc32_attr(const ir_node *node) {
245         assert(is_ppc32_irn(node) && "need ppc node to get attributes");
246         return (ppc32_attr_t *)get_irn_generic_attr((ir_node *)node);
247 }
248
249 /**
250  * Returns the argument register requirements of a ppc node.
251  */
252 const arch_register_req_t **get_ppc32_in_req_all(const ir_node *node) {
253         ppc32_attr_t *attr = get_ppc32_attr(node);
254         return attr->in_req;
255 }
256
257 /**
258  * Returns the result register requirements of an ppc node.
259  */
260 const arch_register_req_t **get_ppc32_out_req_all(const ir_node *node) {
261         ppc32_attr_t *attr = get_ppc32_attr(node);
262         return attr->out_req;
263 }
264
265 /**
266  * Returns the argument register requirement at position pos of an ppc node.
267  */
268 const arch_register_req_t *get_ppc32_in_req(const ir_node *node, int pos) {
269         ppc32_attr_t *attr = get_ppc32_attr(node);
270         return attr->in_req[pos];
271 }
272
273 /**
274  * Returns the result register requirement at position pos of an ppc node.
275  */
276 const arch_register_req_t *get_ppc32_out_req(const ir_node *node, int pos) {
277         ppc32_attr_t *attr = get_ppc32_attr(node);
278         return attr->out_req[pos];
279 }
280
281 /**
282  * Sets the OUT register requirements at position pos.
283  */
284 void set_ppc32_req_out(ir_node *node, const arch_register_req_t *req, int pos) {
285         ppc32_attr_t *attr   = get_ppc32_attr(node);
286         attr->out_req[pos] = req;
287 }
288
289 /**
290  * Sets the IN register requirements at position pos.
291  */
292 void set_ppc32_req_in(ir_node *node, const arch_register_req_t *req, int pos) {
293         ppc32_attr_t *attr  = get_ppc32_attr(node);
294         attr->in_req[pos] = req;
295 }
296
297 /**
298  * Returns the register flag of an ppc node.
299  */
300 arch_irn_flags_t get_ppc32_flags(const ir_node *node) {
301         ppc32_attr_t *attr = get_ppc32_attr(node);
302         return attr->flags;
303 }
304
305 /**
306  * Sets the register flag of an ppc node.
307  */
308 void set_ppc32_flags(const ir_node *node, arch_irn_flags_t flags) {
309         ppc32_attr_t *attr = get_ppc32_attr(node);
310         attr->flags      = flags;
311 }
312
313 /**
314  * Returns the result register slots of an ppc node.
315  */
316 const arch_register_t **get_ppc32_slots(const ir_node *node) {
317         ppc32_attr_t *attr = get_ppc32_attr(node);
318         return attr->slots;
319 }
320
321 /**
322  * Returns the name of the OUT register at position pos.
323  */
324 const char *get_ppc32_out_reg_name(const ir_node *node, int pos) {
325         ppc32_attr_t *attr = get_ppc32_attr(node);
326
327         assert(is_ppc32_irn(node) && "Not an ppc node.");
328         assert(pos < ARR_LEN(attr->slots) && "Invalid OUT position.");
329         assert(attr->slots[pos]  && "No register assigned");
330
331         return arch_register_get_name(attr->slots[pos]);
332 }
333
334 /**
335  * Returns the index of the OUT register at position pos within its register class.
336  */
337 int get_ppc32_out_regnr(const ir_node *node, int pos) {
338         ppc32_attr_t *attr = get_ppc32_attr(node);
339
340         assert(is_ppc32_irn(node) && "Not an ppc node.");
341         assert(pos < ARR_LEN(attr->slots) && "Invalid OUT position.");
342         assert(attr->slots[pos]  && "No register assigned");
343
344         return arch_register_get_index(attr->slots[pos]);
345 }
346
347 /**
348  * Returns the OUT register at position pos.
349  */
350 const arch_register_t *get_ppc32_out_reg(const ir_node *node, int pos) {
351         ppc32_attr_t *attr = get_ppc32_attr(node);
352
353         assert(is_ppc32_irn(node) && "Not an ppc node.");
354         assert(pos < ARR_LEN(attr->slots) && "Invalid OUT position.");
355         assert(attr->slots[pos]  && "No register assigned");
356
357         return attr->slots[pos];
358 }
359
360 /**
361  * Returns the number of results.
362  */
363 int get_ppc32_n_res(const ir_node *node) {
364         ppc32_attr_t *attr = get_ppc32_attr(node);
365         return ARR_LEN(attr->slots);
366 }
367
368 /**
369  * Sets the type of the constant (if any)
370  * May be either iro_Const or iro_SymConst
371  */
372 /* void set_ppc32_type(const ir_node *node, opcode type) {
373         ppc32_attr_t *attr = get_ppc32_attr(node);
374         attr->type      = type;
375 }  */
376
377 /**
378  * Returns the type of the content (if any)
379  */
380 ppc32_attr_content_type get_ppc32_type(const ir_node *node) {
381         ppc32_attr_t *attr = get_ppc32_attr(node);
382         return attr->content_type;
383 }
384
385 /**
386  * Sets a tarval type content (also updating the content_type)
387  */
388 void set_ppc32_constant_tarval(const ir_node *node, tarval *const_tarval) {
389         ppc32_attr_t *attr = get_ppc32_attr(node);
390         attr->content_type = ppc32_ac_Const;
391         attr->data.constant_tarval = const_tarval;
392 }
393
394 /**
395  * Returns a tarval type constant
396  */
397 tarval *get_ppc32_constant_tarval(const ir_node *node) {
398         ppc32_attr_t *attr = get_ppc32_attr(node);
399         return attr->data.constant_tarval;
400 }
401
402 /**
403  * Sets an ident type constant (also updating the content_type)
404  */
405 void set_ppc32_symconst_ident(const ir_node *node, ident *symconst_ident) {
406         ppc32_attr_t *attr = get_ppc32_attr(node);
407         attr->content_type = ppc32_ac_SymConst;
408         attr->data.symconst_ident = symconst_ident;
409 }
410
411 /**
412  * Returns an ident type constant
413  */
414 ident *get_ppc32_symconst_ident(const ir_node *node) {
415         ppc32_attr_t *attr = get_ppc32_attr(node);
416         return attr->data.symconst_ident;
417 }
418
419
420 /**
421  * Sets an entity (also updating the content_type)
422  */
423 void set_ppc32_frame_entity(const ir_node *node, ir_entity *ent) {
424         ppc32_attr_t *attr = get_ppc32_attr(node);
425         attr->content_type = ppc32_ac_FrameEntity;
426         attr->data.frame_entity = ent;
427 }
428
429 /**
430  * Returns an entity
431  */
432 ir_entity *get_ppc32_frame_entity(const ir_node *node) {
433         ppc32_attr_t *attr = get_ppc32_attr(node);
434         return attr->data.frame_entity;
435 }
436
437 /**
438  * Sets a Rlwimi const (also updating the content_type)
439  */
440 void set_ppc32_rlwimi_const(const ir_node *node, unsigned shift, unsigned maskA, unsigned maskB) {
441         ppc32_attr_t *attr = get_ppc32_attr(node);
442         attr->content_type = ppc32_ac_RlwimiConst;
443         attr->data.rlwimi_const.shift = shift;
444         attr->data.rlwimi_const.maskA = maskA;
445         attr->data.rlwimi_const.maskB = maskB;
446 }
447
448 /**
449  * Returns the rlwimi const structure
450  */
451 rlwimi_const_t *get_ppc32_rlwimi_const(const ir_node *node) {
452         ppc32_attr_t *attr = get_ppc32_attr(node);
453         return &attr->data.rlwimi_const;
454 }
455
456 /**
457  * Sets a Proj number (also updating the content_type)
458  */
459 void set_ppc32_proj_nr(const ir_node *node, int proj_nr) {
460         ppc32_attr_t *attr = get_ppc32_attr(node);
461         attr->content_type = ppc32_ac_BranchProj;
462         attr->data.proj_nr = proj_nr;
463 }
464
465 /**
466  * Returns the proj number
467  */
468 int get_ppc32_proj_nr(const ir_node *node) {
469         ppc32_attr_t *attr = get_ppc32_attr(node);
470         return attr->data.proj_nr;
471 }
472
473 /**
474  * Sets an offset for a memory access (also updating the content_type)
475  */
476 void set_ppc32_offset(const ir_node *node, int offset) {
477         ppc32_attr_t *attr = get_ppc32_attr(node);
478         attr->content_type = ppc32_ac_Offset;
479         attr->data.offset  = offset;
480 }
481
482 /**
483  * Returns the offset
484  */
485 int get_ppc32_offset(const ir_node *node) {
486         ppc32_attr_t *attr = get_ppc32_attr(node);
487         return attr->data.offset;
488 }
489
490 /**
491  * Sets the offset mode (ppc32_ao_None, ppc32_ao_Lo16, ppc32_ao_Hi16 or ppc32_ao_Ha16)
492  */
493 void set_ppc32_offset_mode(const ir_node *node, ppc32_attr_offset_mode mode) {
494         ppc32_attr_t *attr = get_ppc32_attr(node);
495         attr->offset_mode = mode;
496 }
497
498 /**
499  * Returns the offset mode
500  */
501 ppc32_attr_offset_mode get_ppc32_offset_mode(const ir_node *node) {
502         ppc32_attr_t *attr = get_ppc32_attr(node);
503         return attr->offset_mode;
504 }
505
506
507 /**
508  * Initializes ppc specific node attributes
509  */
510 void init_ppc32_attributes(ir_node *node, int flags,
511                                                  const arch_register_req_t **in_reqs, const arch_register_req_t **out_reqs,
512                                                  const be_execution_unit_t ***execution_units,
513                                                  int n_res) {
514         ir_graph       *irg  = get_irn_irg(node);
515         struct obstack *obst = get_irg_obstack(irg);
516         ppc32_attr_t   *attr = get_ppc32_attr(node);
517         (void) execution_units;
518
519         attr->flags   = flags;
520         attr->in_req  = in_reqs;
521         attr->out_req = out_reqs;
522
523         attr->content_type = ppc32_ac_None;
524         attr->offset_mode  = ppc32_ao_Illegal;
525         attr->data.empty   = NULL;
526
527         attr->slots = NEW_ARR_D(const arch_register_t*, obst, n_res);
528         memset(attr->slots, 0, n_res * sizeof(attr->slots[0]));
529 }
530
531
532 /***************************************************************************************
533  *                  _                            _                   _
534  *                 | |                          | |                 | |
535  *  _ __   ___   __| | ___    ___ ___  _ __  ___| |_ _ __ _   _  ___| |_ ___  _ __ ___
536  * | '_ \ / _ \ / _` |/ _ \  / __/ _ \| '_ \/ __| __| '__| | | |/ __| __/ _ \| '__/ __|
537  * | | | | (_) | (_| |  __/ | (_| (_) | | | \__ \ |_| |  | |_| | (__| || (_) | |  \__ \
538  * |_| |_|\___/ \__,_|\___|  \___\___/|_| |_|___/\__|_|   \__,_|\___|\__\___/|_|  |___/
539  *
540  ***************************************************************************************/
541
542 /* Include the generated constructor functions */
543 #include "gen_ppc32_new_nodes.c.inl"