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