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