updated header
[libfirm] / ir / be / mips / mips_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  * This file implements the creation of the achitecture specific firm opcodes
22  * and the coresponding node constructors for the mips assembler irg.
23  * $Id$
24  */
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include <stdlib.h>
30
31 #include "irprog_t.h"
32 #include "irgraph_t.h"
33 #include "irnode_t.h"
34 #include "irmode_t.h"
35 #include "ircons_t.h"
36 #include "iropt_t.h"
37 #include "irop.h"
38 #include "firm_common_t.h"
39 #include "irvrfy_t.h"
40 #include "irprintf.h"
41 #include "xmalloc.h"
42
43 #include "../bearch_t.h"
44
45 #include "mips_nodes_attr.h"
46 #include "mips_new_nodes.h"
47 #include "gen_mips_regalloc_if.h"
48
49
50
51 /***********************************************************************************
52  *      _                                   _       _             __
53  *     | |                                 (_)     | |           / _|
54  *   __| |_   _ _ __ ___  _ __   ___ _ __   _ _ __ | |_ ___ _ __| |_ __ _  ___ ___
55  *  / _` | | | | '_ ` _ \| '_ \ / _ \ '__| | | '_ \| __/ _ \ '__|  _/ _` |/ __/ _ \
56  * | (_| | |_| | | | | | | |_) |  __/ |    | | | | | ||  __/ |  | || (_| | (_|  __/
57  *  \__,_|\__,_|_| |_| |_| .__/ \___|_|    |_|_| |_|\__\___|_|  |_| \__,_|\___\___|
58  *                       | |
59  *                       |_|
60  ***********************************************************************************/
61
62 /**
63  * Dumps the register requirements for either in or out.
64  */
65 static void dump_reg_req(FILE *F, ir_node *n, const arch_register_req_t **reqs, int inout) {
66         char *dir = inout ? "out" : "in";
67         int   max = inout ? get_mips_n_res(n) : get_irn_arity(n);
68         char  buf[1024];
69         int   i;
70
71         memset(buf, 0, sizeof(buf));
72
73         if (reqs) {
74                 for (i = 0; i < max; i++) {
75                         fprintf(F, "%sreq #%d =", dir, i);
76
77                         if (reqs[i]->type == arch_register_req_type_none) {
78                                 fprintf(F, " n/a");
79                         }
80
81                         if (reqs[i]->type & arch_register_req_type_normal) {
82                                 fprintf(F, " %s", reqs[i]->cls->name);
83                         }
84
85                         if (reqs[i]->type & arch_register_req_type_limited) {
86                                 fprintf(F, " %s",
87                                         arch_register_req_format(buf, sizeof(buf), reqs[i], n));
88                         }
89
90                         if (reqs[i]->type & arch_register_req_type_should_be_same) {
91                                 ir_fprintf(F, " same as %+F", get_irn_n(n, reqs[i]->other_same));
92                         }
93
94                         if (reqs[i]->type & arch_register_req_type_should_be_different) {
95                                 ir_fprintf(F, " different from %+F", get_irn_n(n, reqs[i]->other_different));
96                         }
97
98                         fprintf(F, "\n");
99                 }
100
101                 fprintf(F, "\n");
102         } else {
103                 fprintf(F, "%sreq = N/A\n", dir);
104         }
105 }
106
107
108 /**
109  * Dumper interface for dumping mips nodes in vcg.
110  * @param n        the node to dump
111  * @param F        the output file
112  * @param reason   indicates which kind of information should be dumped
113  * @return 0 on success or != 0 on failure
114  */
115 static int mips_dump_node(ir_node *n, FILE *F, dump_reason_t reason) {
116         ir_mode     *mode = NULL;
117         int          bad  = 0;
118         int          i;
119         mips_attr_t *attr;
120         char buf[64];
121         const arch_register_req_t **reqs;
122         const arch_register_t     **slots;
123
124         switch (reason) {
125                 case dump_node_opcode_txt:
126                         fprintf(F, "%s", get_irn_opname(n));
127                         break;
128
129                 case dump_node_mode_txt:
130                         mode = get_irn_mode(n);
131
132                         if (mode) {
133                                 fprintf(F, "[%s]", get_mode_name(mode));
134                         }
135                         else {
136                                 fprintf(F, "[?NOMODE?]");
137                         }
138                         break;
139
140                 case dump_node_nodeattr_txt:
141
142                         /* TODO: dump some attributes which should show up */
143                         /* in node name in dump (e.g. consts or the like)  */
144
145                         break;
146
147                 case dump_node_info_txt:
148                         attr = get_mips_attr(n);
149                         fprintf(F, "=== mips attr begin ===\n");
150
151                         /* dump IN requirements */
152                         if (get_irn_arity(n) > 0) {
153                                 reqs = get_mips_in_req_all(n);
154                                 dump_reg_req(F, n, reqs, 0);
155                         }
156
157                         /* dump OUT requirements */
158                         if (attr->n_res > 0) {
159                                 reqs = get_mips_out_req_all(n);
160                                 dump_reg_req(F, n, reqs, 1);
161                         }
162
163                         /* dump assigned registers */
164                         slots = get_mips_slots(n);
165                         if (slots && attr->n_res > 0) {
166                                 for (i = 0; i < attr->n_res; i++) {
167                                         if (slots[i]) {
168                                                 fprintf(F, "reg #%d = %s\n", i, slots[i]->name);
169                                         }
170                                         else {
171                                                 fprintf(F, "reg #%d = n/a\n", i);
172                                         }
173                                 }
174                         }
175                         fprintf(F, "\n");
176
177                         /* dump n_res */
178                         fprintf(F, "n_res = %d\n", get_mips_n_res(n));
179
180                         /* dump flags */
181                         fprintf(F, "flags =");
182                         if (attr->flags == arch_irn_flags_none) {
183                                 fprintf(F, " none");
184                         }
185                         else {
186                                 if (attr->flags & arch_irn_flags_dont_spill) {
187                                         fprintf(F, " unspillable");
188                                 }
189                                 if (attr->flags & arch_irn_flags_rematerializable) {
190                                         fprintf(F, " remat");
191                                 }
192                                 if (attr->flags & arch_irn_flags_ignore) {
193                                         fprintf(F, " ignore");
194                                 }
195                         }
196                         fprintf(F, " (%d)\n", attr->flags);
197
198                         if(attr->modes.load_store_mode != NULL) {
199                                 fprintf(F, " load_store_mode %s\n", get_mode_name(attr->modes.load_store_mode));
200                         }
201                         if(attr->stack_entity != NULL) {
202                                 fprintf(F, " stack entity %s\n", get_entity_name(attr->stack_entity));
203                         }
204                         if(attr->tv != NULL) {
205                                 tarval_snprintf(buf, sizeof(buf), attr->tv);
206                                 fprintf(F, " tarval %s\n", buf);
207                         }
208                         if(attr->symconst_id != NULL) {
209                                 fprintf(F, " symconst '%s'\n", get_id_str(attr->symconst_id));
210                         }
211
212                         fprintf(F, "=== mips attr end ===\n");
213                         /* end of: case dump_node_info_txt */
214                         break;
215         }
216
217
218         return bad;
219 }
220
221
222
223 /***************************************************************************************************
224  *        _   _                   _       __        _                    _   _               _
225  *       | | | |                 | |     / /       | |                  | | | |             | |
226  *   __ _| |_| |_ _ __   ___  ___| |_   / /_ _  ___| |_   _ __ ___   ___| |_| |__   ___   __| |___
227  *  / _` | __| __| '__| / __|/ _ \ __| / / _` |/ _ \ __| | '_ ` _ \ / _ \ __| '_ \ / _ \ / _` / __|
228  * | (_| | |_| |_| |    \__ \  __/ |_ / / (_| |  __/ |_  | | | | | |  __/ |_| | | | (_) | (_| \__ \
229  *  \__,_|\__|\__|_|    |___/\___|\__/_/ \__, |\___|\__| |_| |_| |_|\___|\__|_| |_|\___/ \__,_|___/
230  *                                        __/ |
231  *                                       |___/
232  ***************************************************************************************************/
233
234 /**
235  * Wraps get_irn_generic_attr() as it takes no const ir_node, so we need to do a cast.
236  * Firm was made by people hating const :-(
237  */
238 mips_attr_t *get_mips_attr(const ir_node *node) {
239         assert(is_mips_irn(node) && "need mips node to get attributes");
240         return (mips_attr_t *)get_irn_generic_attr((ir_node *)node);
241 }
242
243 /**
244  * Returns the argument register requirements of a mips node.
245  */
246 const arch_register_req_t **get_mips_in_req_all(const ir_node *node) {
247         mips_attr_t *attr = get_mips_attr(node);
248         return attr->in_req;
249 }
250
251 /**
252  * Returns the result register requirements of an mips node.
253  */
254 const arch_register_req_t **get_mips_out_req_all(const ir_node *node) {
255         mips_attr_t *attr = get_mips_attr(node);
256         return attr->out_req;
257 }
258
259 /**
260  * Returns the argument register requirement at position pos of an mips node.
261  */
262 const arch_register_req_t *get_mips_in_req(const ir_node *node, int pos) {
263         mips_attr_t *attr = get_mips_attr(node);
264         return attr->in_req[pos];
265 }
266
267 /**
268  * Returns the result register requirement at position pos of an mips node.
269  */
270 const arch_register_req_t *get_mips_out_req(const ir_node *node, int pos) {
271         mips_attr_t *attr = get_mips_attr(node);
272         return attr->out_req[pos];
273 }
274
275 /**
276  * Sets the OUT register requirements at position pos.
277  */
278 void set_mips_req_out(ir_node *node, const arch_register_req_t *req, int pos) {
279         mips_attr_t *attr   = get_mips_attr(node);
280         attr->out_req[pos] = req;
281 }
282
283 /**
284  * Sets the IN register requirements at position pos.
285  */
286 void set_mips_req_in(ir_node *node, const arch_register_req_t *req, int pos) {
287         mips_attr_t *attr  = get_mips_attr(node);
288         attr->in_req[pos] = req;
289 }
290
291 /**
292  * Returns the register flag of an mips node.
293  */
294 arch_irn_flags_t get_mips_flags(const ir_node *node) {
295         mips_attr_t *attr = get_mips_attr(node);
296         return attr->flags;
297 }
298
299 /**
300  * Sets the register flag of an mips node.
301  */
302 void set_mips_flags(const ir_node *node, arch_irn_flags_t flags) {
303         mips_attr_t *attr = get_mips_attr(node);
304         attr->flags      = flags;
305 }
306
307 /**
308  * Returns the result register slots of an mips node.
309  */
310 const arch_register_t **get_mips_slots(const ir_node *node) {
311         mips_attr_t *attr = get_mips_attr(node);
312         return attr->slots;
313 }
314
315 /**
316  * Returns the name of the OUT register at position pos.
317  */
318 const char *get_mips_out_reg_name(const ir_node *node, int pos) {
319         mips_attr_t *attr = get_mips_attr(node);
320
321         assert(is_mips_irn(node) && "Not an mips node.");
322         assert(pos < attr->n_res && "Invalid OUT position.");
323         assert(attr->slots[pos]  && "No register assigned");
324
325         return arch_register_get_name(attr->slots[pos]);
326 }
327
328 /**
329  * Returns the index of the OUT register at position pos within its register class.
330  */
331 int get_mips_out_regnr(const ir_node *node, int pos) {
332         mips_attr_t *attr = get_mips_attr(node);
333
334         assert(is_mips_irn(node) && "Not an mips node.");
335         assert(pos < attr->n_res && "Invalid OUT position.");
336         assert(attr->slots[pos]  && "No register assigned");
337
338         return arch_register_get_index(attr->slots[pos]);
339 }
340
341 /**
342  * Returns the OUT register at position pos.
343  */
344 const arch_register_t *get_mips_out_reg(const ir_node *node, int pos) {
345         mips_attr_t *attr = get_mips_attr(node);
346
347         assert(is_mips_irn(node) && "Not an mips node.");
348         assert(pos < attr->n_res && "Invalid OUT position.");
349         assert(attr->slots[pos]  && "No register assigned");
350
351         return attr->slots[pos];
352 }
353
354 /**
355  * Sets the number of results.
356  */
357 void set_mips_n_res(ir_node *node, int n_res) {
358         mips_attr_t *attr = get_mips_attr(node);
359         attr->n_res      = n_res;
360 }
361
362 /**
363  * Returns the number of results.
364  */
365 int get_mips_n_res(const ir_node *node) {
366         mips_attr_t *attr = get_mips_attr(node);
367         return attr->n_res;
368 }
369
370 /**
371  * Initializes the nodes attributes.
372  */
373 void init_mips_attributes(ir_node *node, arch_irn_flags_t flags, const arch_register_req_t **in_reqs,
374                                                   const arch_register_req_t **out_reqs,
375                                                                                                   const be_execution_unit_t ***execution_units,
376                                                                                                   int n_res, unsigned latency)
377 {
378         mips_attr_t *attr = get_mips_attr(node);
379
380         attr->flags = flags;
381         attr->out_req = out_reqs;
382
383         attr->n_res = n_res;
384         attr->in_req = in_reqs;
385
386         memset((void *)attr->slots, 0, n_res * sizeof(attr->slots[0]));
387 }
388
389 /************************************************************************
390  *  ___ _____     _     _ _
391  * |_ _|  ___|__ | | __| (_)_ __   __ _
392  *  | || |_ / _ \| |/ _` | | '_ \ / _` |
393  *  | ||  _| (_) | | (_| | | | | | (_| |
394  * |___|_|  \___/|_|\__,_|_|_| |_|\__, |
395  *                                |___/
396  ************************************************************************/
397
398 // test if a tarval can be expressed in a 16bit immediate value
399 static int is_tarval_16(ir_node* node)
400 {
401         mips_attr_t *attr = get_mips_attr(node);
402         tarval* tv = attr->tv;
403         long val = get_tarval_long(tv);
404         if(get_mode_sign(get_irn_mode(node))) {
405                 if(val < -32768 || val > 32767)
406                         return 0;
407         } else {
408                 unsigned long uval = (unsigned long) val;
409                 if(uval > 65536)
410                         return 0;
411         }
412
413         return 1;
414 }
415
416 #define MIPS_MAKE_IFOLDING_TRANSFORM(srcnode, inode, commutative)       \
417 ir_node *mips_transform_##srcnode(ir_node* node)                                \
418 {                                                                                                                               \
419         ir_node* op1 = get_irn_n(node, 0);                                                      \
420         ir_node* op2 = get_irn_n(node, 1);                                                      \
421         ir_node* result;                                                                                        \
422         if(op1 == NULL || op2 == NULL)                                                          \
423                 return node;                                                                                    \
424                                                                                                                                 \
425         if((is_mips_lli(op2) || is_mips_lui(op2)) && is_tarval_16(op2)) {       \
426                 mips_attr_t *attr, *op_attr = get_mips_attr(op2);               \
427                 long val = get_tarval_long(op_attr->tv);                                \
428                 result = new_rd_mips_##inode(get_irn_dbg_info(node), get_irn_irg(node), get_nodes_block(node),  \
429                         op1);                                                                       \
430                 attr = get_mips_attr(result);                                                   \
431                 attr->tv = new_tarval_from_long(val, get_mode_sign(get_irn_mode(node)) ? mode_Hs : mode_Hu);    \
432                 return result;                                                                                  \
433         }                                                                                                                       \
434                                                                                                                                 \
435         if(commutative && (is_mips_lli(op1) || is_mips_lui(op1)) && is_tarval_16(op1)) {        \
436                 mips_attr_t *attr, *op_attr = get_mips_attr(op1);       \
437                 long val = get_tarval_long(op_attr->tv);                                \
438                 result = new_rd_mips_##inode(get_irn_dbg_info(node), get_irn_irg(node), get_nodes_block(node),  \
439                         op2);                                                                       \
440                 attr = get_mips_attr(result);                                                   \
441                 attr->tv = new_tarval_from_long(val, get_mode_sign(get_irn_mode(node)) ? mode_Hs : mode_Hu);    \
442                 return result;                                                                                  \
443         }                                                                                                                       \
444                                                                                                                                 \
445         return node;                                                                                            \
446 }
447
448 MIPS_MAKE_IFOLDING_TRANSFORM(addu, addiu, 1)
449 MIPS_MAKE_IFOLDING_TRANSFORM(and, andi, 1)
450 MIPS_MAKE_IFOLDING_TRANSFORM(or, ori, 1)
451 MIPS_MAKE_IFOLDING_TRANSFORM(sra, srai, 0)
452 MIPS_MAKE_IFOLDING_TRANSFORM(xor, xori, 1)
453 MIPS_MAKE_IFOLDING_TRANSFORM(sl, sli, 0)
454 MIPS_MAKE_IFOLDING_TRANSFORM(sr, sri, 0)
455 MIPS_MAKE_IFOLDING_TRANSFORM(slt, slti, 0)
456
457 void mips_init_opcode_transforms(void) {
458         op_mips_addu->ops.transform_node = mips_transform_addu;
459         op_mips_and->ops.transform_node = mips_transform_and;
460         op_mips_or->ops.transform_node  = mips_transform_or;
461         op_mips_sra->ops.transform_node = mips_transform_sra;
462         op_mips_xor->ops.transform_node = mips_transform_xor;
463         op_mips_sl->ops.transform_node  = mips_transform_sl;
464         op_mips_sr->ops.transform_node  = mips_transform_sr;
465         op_mips_slt->ops.transform_node = mips_transform_slt;
466 }
467
468 /***************************************************************************************
469  *                  _                            _                   _
470  *                 | |                          | |                 | |
471  *  _ __   ___   __| | ___    ___ ___  _ __  ___| |_ _ __ _   _  ___| |_ ___  _ __ ___
472  * | '_ \ / _ \ / _` |/ _ \  / __/ _ \| '_ \/ __| __| '__| | | |/ __| __/ _ \| '__/ __|
473  * | | | | (_) | (_| |  __/ | (_| (_) | | | \__ \ |_| |  | |_| | (__| || (_) | |  \__ \
474  * |_| |_|\___/ \__,_|\___|  \___\___/|_| |_|___/\__|_|   \__,_|\___|\__\___/|_|  |___/
475  *
476  ***************************************************************************************/
477
478 /* Include the generated constructor functions */
479 #include "gen_mips_new_nodes.c.inl"