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