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