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