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