126aea2c7023d8b121d1aa39fbc4cbd71f61eed1
[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  * @file
22  * @brief    This file implements the creation of the architecture specific firm
23  *           opcodes and the corresponding node constructors for the MIPS
24  *           assembler irg.
25  * @author   Matthias Braun, Mehdi
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 "mips_nodes_attr.h"
49 #include "mips_new_nodes.h"
50 #include "gen_mips_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,
69                          int inout)
70 {
71         const mips_attr_t *attr = get_mips_attr_const(n);
72         char *dir = inout ? "out" : "in";
73         int   max = inout ? ARR_LEN(attr->slots) : get_irn_arity(n);
74         char  buf[1024];
75         int   i;
76
77         memset(buf, 0, sizeof(buf));
78
79         if (reqs) {
80                 for (i = 0; i < max; i++) {
81                         fprintf(F, "%sreq #%d =", dir, i);
82
83                         if (reqs[i]->type == arch_register_req_type_none) {
84                                 fprintf(F, " n/a");
85                         }
86
87                         if (reqs[i]->type & arch_register_req_type_normal) {
88                                 fprintf(F, " %s", reqs[i]->cls->name);
89                         }
90
91                         if (reqs[i]->type & arch_register_req_type_limited) {
92                                 fprintf(F, " %s",
93                                         arch_register_req_format(buf, sizeof(buf), reqs[i], n));
94                         }
95
96                         if (reqs[i]->type & arch_register_req_type_should_be_same) {
97                                 ir_fprintf(F, " same as %+F", get_irn_n(n, reqs[i]->other_same));
98                         }
99
100                         if (reqs[i]->type & arch_register_req_type_should_be_different) {
101                                 ir_fprintf(F, " different from %+F", get_irn_n(n, reqs[i]->other_different));
102                         }
103
104                         fprintf(F, "\n");
105                 }
106
107                 fprintf(F, "\n");
108         } else {
109                 fprintf(F, "%sreq = N/A\n", dir);
110         }
111 }
112
113
114 /**
115  * Dumper interface for dumping mips nodes in vcg.
116  * @param n        the node to dump
117  * @param F        the output file
118  * @param reason   indicates which kind of information should be dumped
119  * @return 0 on success or != 0 on failure
120  */
121 static int mips_dump_node(ir_node *n, FILE *F, dump_reason_t reason) {
122         int          bad  = 0;
123         int          i;
124         const mips_attr_t *attr = get_mips_attr_const(n);
125         const arch_register_req_t **reqs;
126         const arch_register_t     **slots;
127
128         switch (reason) {
129                 case dump_node_opcode_txt:
130                         fprintf(F, "%s", get_irn_opname(n));
131                         break;
132
133                 case dump_node_mode_txt:
134                         break;
135
136                 case dump_node_nodeattr_txt:
137
138                         if(is_mips_Immediate(n)) {
139                                 const mips_immediate_attr_t *attr
140                                         = get_mips_immediate_attr_const(n);
141                                 fprintf(F, " %ld", attr->val);
142                         }
143
144                         break;
145
146                 case dump_node_info_txt:
147                         attr = get_mips_attr(n);
148                         fprintf(F, "=== mips attr begin ===\n");
149
150                         /* dump IN requirements */
151                         if (get_irn_arity(n) > 0) {
152                                 reqs = get_mips_in_req_all(n);
153                                 dump_reg_req(F, n, reqs, 0);
154                         }
155
156                         /* dump OUT requirements */
157                         if (ARR_LEN(attr->slots) > 0) {
158                                 reqs = get_mips_out_req_all(n);
159                                 dump_reg_req(F, n, reqs, 1);
160                         }
161
162                         /* dump assigned registers */
163                         slots = get_mips_slots(n);
164                         if (slots && ARR_LEN(attr->slots) > 0) {
165                                 for (i = 0; i < ARR_LEN(attr->slots); i++) {
166                                         if (slots[i]) {
167                                                 fprintf(F, "reg #%d = %s\n", i, slots[i]->name);
168                                         }
169                                         else {
170                                                 fprintf(F, "reg #%d = n/a\n", i);
171                                         }
172                                 }
173                         }
174                         fprintf(F, "\n");
175
176                         /* dump n_res */
177                         fprintf(F, "n_res = %d\n", ARR_LEN(attr->slots));
178
179                         /* dump flags */
180                         fprintf(F, "flags =");
181                         if (attr->flags == arch_irn_flags_none) {
182                                 fprintf(F, " none");
183                         }
184                         else {
185                                 if (attr->flags & arch_irn_flags_dont_spill) {
186                                         fprintf(F, " unspillable");
187                                 }
188                                 if (attr->flags & arch_irn_flags_rematerializable) {
189                                         fprintf(F, " remat");
190                                 }
191                                 if (attr->flags & arch_irn_flags_ignore) {
192                                         fprintf(F, " ignore");
193                                 }
194                         }
195                         fprintf(F, " (%d)\n", attr->flags);
196
197                         fprintf(F, "=== mips attr end ===\n");
198                         /* end of: case dump_node_info_txt */
199                         break;
200         }
201
202
203         return bad;
204 }
205
206
207
208 /***************************************************************************************************
209  *        _   _                   _       __        _                    _   _               _
210  *       | | | |                 | |     / /       | |                  | | | |             | |
211  *   __ _| |_| |_ _ __   ___  ___| |_   / /_ _  ___| |_   _ __ ___   ___| |_| |__   ___   __| |___
212  *  / _` | __| __| '__| / __|/ _ \ __| / / _` |/ _ \ __| | '_ ` _ \ / _ \ __| '_ \ / _ \ / _` / __|
213  * | (_| | |_| |_| |    \__ \  __/ |_ / / (_| |  __/ |_  | | | | | |  __/ |_| | | | (_) | (_| \__ \
214  *  \__,_|\__|\__|_|    |___/\___|\__/_/ \__, |\___|\__| |_| |_| |_|\___|\__|_| |_|\___/ \__,_|___/
215  *                                        __/ |
216  *                                       |___/
217  ***************************************************************************************************/
218
219 mips_attr_t *get_mips_attr(ir_node *node)
220 {
221         assert(is_mips_irn(node) && "need mips node to get attributes");
222         return (mips_attr_t *) get_irn_generic_attr(node);
223 }
224
225 const mips_attr_t *get_mips_attr_const(const ir_node *node)
226 {
227         assert(is_mips_irn(node) && "need mips node to get attributes");
228         return get_irn_generic_attr_const(node);
229 }
230
231 const mips_immediate_attr_t *get_mips_immediate_attr_const(const ir_node *node)
232 {
233         assert(is_mips_irn(node) && "need mips node to get attributes");
234         return get_irn_generic_attr_const(node);
235 }
236
237 const mips_load_store_attr_t *get_mips_load_store_attr_const(
238                 const ir_node *node)
239 {
240         assert(is_mips_irn(node) && "need mips node to get attributes");
241         return get_irn_generic_attr_const(node);
242 }
243
244 /**
245  * Returns the argument register requirements of a mips node.
246  */
247 const arch_register_req_t **get_mips_in_req_all(const ir_node *node)
248 {
249         const mips_attr_t *attr = get_mips_attr_const(node);
250         return attr->in_req;
251 }
252
253 /**
254  * Returns the result register requirements of an mips node.
255  */
256 const arch_register_req_t **get_mips_out_req_all(const ir_node *node)
257 {
258         const mips_attr_t *attr = get_mips_attr_const(node);
259         return attr->out_req;
260 }
261
262 /**
263  * Returns the argument register requirement at position pos of an mips node.
264  */
265 const arch_register_req_t *get_mips_in_req(const ir_node *node, int pos)
266 {
267         const mips_attr_t *attr = get_mips_attr_const(node);
268         return attr->in_req[pos];
269 }
270
271 /**
272  * Returns the result register requirement at position pos of an mips node.
273  */
274 const arch_register_req_t *get_mips_out_req(const ir_node *node, int pos)
275 {
276         const mips_attr_t *attr = get_mips_attr_const(node);
277         return attr->out_req[pos];
278 }
279
280 /**
281  * Sets the OUT register requirements at position pos.
282  */
283 void set_mips_req_out(ir_node *node, const arch_register_req_t *req, int pos)
284 {
285         mips_attr_t *attr   = get_mips_attr(node);
286         attr->out_req[pos] = req;
287 }
288
289 /**
290  * Sets the IN register requirements at position pos.
291  */
292 void set_mips_req_in(ir_node *node, const arch_register_req_t *req, int pos)
293 {
294         mips_attr_t *attr  = get_mips_attr(node);
295         attr->in_req[pos] = req;
296 }
297
298 /**
299  * Returns the register flag of an mips node.
300  */
301 arch_irn_flags_t get_mips_flags(const ir_node *node)
302 {
303         const mips_attr_t *attr = get_mips_attr_const(node);
304         return attr->flags;
305 }
306
307 /**
308  * Sets the register flag of an mips node.
309  */
310 void set_mips_flags(ir_node *node, arch_irn_flags_t flags)
311 {
312         mips_attr_t *attr = get_mips_attr(node);
313         attr->flags      = flags;
314 }
315
316 /**
317  * Returns the result register slots of an mips node.
318  */
319 const arch_register_t **get_mips_slots(const ir_node *node)
320 {
321         const mips_attr_t *attr = get_mips_attr_const(node);
322         return attr->slots;
323 }
324
325 /**
326  * Returns the name of the OUT register at position pos.
327  */
328 const char *get_mips_out_reg_name(const ir_node *node, int pos)
329 {
330         const mips_attr_t *attr = get_mips_attr_const(node);
331
332         assert(is_mips_irn(node) && "Not an mips node.");
333         assert(pos < ARR_LEN(attr->slots) && "Invalid OUT position.");
334         assert(attr->slots[pos]  && "No register assigned");
335
336         return arch_register_get_name(attr->slots[pos]);
337 }
338
339 /**
340  * Returns the index of the OUT register at position pos within its register class.
341  */
342 int get_mips_out_regnr(const ir_node *node, int pos)
343 {
344         const mips_attr_t *attr = get_mips_attr_const(node);
345
346         assert(is_mips_irn(node) && "Not an mips node.");
347         assert(pos < ARR_LEN(attr->slots) && "Invalid OUT position.");
348         assert(attr->slots[pos]  && "No register assigned");
349
350         return arch_register_get_index(attr->slots[pos]);
351 }
352
353 /**
354  * Returns the OUT register at position pos.
355  */
356 const arch_register_t *get_mips_out_reg(const ir_node *node, int pos)
357 {
358         const mips_attr_t *attr = get_mips_attr_const(node);
359
360         assert(is_mips_irn(node) && "Not an mips node.");
361         assert(pos < ARR_LEN(attr->slots) && "Invalid OUT position.");
362         assert(attr->slots[pos]  && "No register assigned");
363
364         return attr->slots[pos];
365 }
366
367 /**
368  * Initializes the nodes attributes.
369  */
370 static void init_mips_attributes(ir_node *node, arch_irn_flags_t flags,
371                                  const arch_register_req_t **in_reqs,
372                                  const arch_register_req_t **out_reqs,
373                                  const be_execution_unit_t ***execution_units,
374                                  int n_res, unsigned latency)
375 {
376         ir_graph       *irg  = get_irn_irg(node);
377         struct obstack *obst = get_irg_obstack(irg);
378         mips_attr_t    *attr = get_mips_attr(node);
379         (void) execution_units;
380         (void) latency;
381
382         attr->flags   = flags;
383         attr->out_req = out_reqs;
384         attr->in_req  = in_reqs;
385
386         attr->slots = NEW_ARR_D(const arch_register_t*, obst, n_res);
387         memset(attr->slots, 0, n_res * sizeof(attr->slots[0]));
388 }
389
390 static void init_mips_immediate_attributes(ir_node *node,
391                                            mips_immediate_type_t type,
392                                            ir_entity *entity, long val)
393 {
394         mips_immediate_attr_t *attr = get_irn_generic_attr(node);
395
396         attr->imm_type = type;
397         attr->entity   = entity;
398         attr->val      = val;
399 }
400
401 static void init_mips_load_store_attributes(ir_node *node, ir_entity *entity,
402                                             long offset)
403 {
404         mips_load_store_attr_t *attr = get_irn_generic_attr(node);
405         attr->stack_entity = entity;
406         attr->offset       = offset;
407 }
408
409 static int mips_compare_nodes_attr(ir_node *node_a, ir_node *node_b)
410 {
411         const mips_attr_t *a = get_mips_attr_const(node_a);
412         const mips_attr_t *b = get_mips_attr_const(node_b);
413
414         if(a->flags != b->flags)
415                 return 1;
416         if(ARR_LEN(a->slots) != ARR_LEN(b->slots))
417                 return 1;
418         if(a->switch_default_pn != b->switch_default_pn)
419                 return 1;
420
421         return 0;
422 }
423
424 static int mips_compare_immediate_attr(ir_node *node_a, ir_node *node_b)
425 {
426         const mips_immediate_attr_t *a = get_mips_immediate_attr_const(node_a);
427         const mips_immediate_attr_t *b = get_mips_immediate_attr_const(node_b);
428
429         if(a->attr.flags != b->attr.flags)
430                 return 1;
431         if(a->val != b->val)
432                 return 1;
433
434         return 0;
435 }
436
437 static int mips_compare_load_store_attr(ir_node *node_a, ir_node *node_b)
438 {
439         const mips_load_store_attr_t *a = get_mips_load_store_attr_const(node_a);
440         const mips_load_store_attr_t *b = get_mips_load_store_attr_const(node_b);
441
442         if(mips_compare_nodes_attr(node_a, node_b))
443                 return 1;
444         if(a->stack_entity != b->stack_entity)
445                 return 1;
446         if(a->offset != b->offset)
447                 return 1;
448
449         return 0;
450 }
451
452 static void mips_copy_attr(const ir_node *old_node , ir_node *new_node)
453 {
454         ir_graph          *irg      = get_irn_irg(new_node);
455         struct obstack    *obst     = get_irg_obstack(irg);
456         const mips_attr_t *attr_old = get_mips_attr_const(old_node);
457         mips_attr_t       *attr_new = get_mips_attr(new_node);
458
459         /* copy the attributes */
460         memcpy(attr_new, attr_old, get_op_attr_size(get_irn_op(old_node)));
461
462         /* copy register assignments */
463         attr_new->slots = DUP_ARR_D(arch_register_t*, obst, attr_old->slots);
464 }
465
466 /***************************************************************************************
467  *                  _                            _                   _
468  *                 | |                          | |                 | |
469  *  _ __   ___   __| | ___    ___ ___  _ __  ___| |_ _ __ _   _  ___| |_ ___  _ __ ___
470  * | '_ \ / _ \ / _` |/ _ \  / __/ _ \| '_ \/ __| __| '__| | | |/ __| __/ _ \| '__/ __|
471  * | | | | (_) | (_| |  __/ | (_| (_) | | | \__ \ |_| |  | |_| | (__| || (_) | |  \__ \
472  * |_| |_|\___/ \__,_|\___|  \___\___/|_| |_|___/\__|_|   \__,_|\___|\__\___/|_|  |___/
473  *
474  ***************************************************************************************/
475
476 /* Include the generated constructor functions */
477 #include "gen_mips_new_nodes.c.inl"