- Part1 of backend reorganisation:
[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                         }
226                         fprintf(F, " (%d)\n", attr->flags);
227
228                         fprintf(F, "=== mips attr end ===\n");
229                         /* end of: case dump_node_info_txt */
230                         break;
231         }
232
233
234         return bad;
235 }
236
237
238
239 /***************************************************************************************************
240  *        _   _                   _       __        _                    _   _               _
241  *       | | | |                 | |     / /       | |                  | | | |             | |
242  *   __ _| |_| |_ _ __   ___  ___| |_   / /_ _  ___| |_   _ __ ___   ___| |_| |__   ___   __| |___
243  *  / _` | __| __| '__| / __|/ _ \ __| / / _` |/ _ \ __| | '_ ` _ \ / _ \ __| '_ \ / _ \ / _` / __|
244  * | (_| | |_| |_| |    \__ \  __/ |_ / / (_| |  __/ |_  | | | | | |  __/ |_| | | | (_) | (_| \__ \
245  *  \__,_|\__|\__|_|    |___/\___|\__/_/ \__, |\___|\__| |_| |_| |_|\___|\__|_| |_|\___/ \__,_|___/
246  *                                        __/ |
247  *                                       |___/
248  ***************************************************************************************************/
249
250 mips_attr_t *get_mips_attr(ir_node *node)
251 {
252         assert(is_mips_irn(node) && "need mips node to get attributes");
253         return (mips_attr_t *) get_irn_generic_attr(node);
254 }
255
256 const mips_attr_t *get_mips_attr_const(const ir_node *node)
257 {
258         assert(is_mips_irn(node) && "need mips node to get attributes");
259         return get_irn_generic_attr_const(node);
260 }
261
262 const mips_immediate_attr_t *get_mips_immediate_attr_const(const ir_node *node)
263 {
264         assert(is_mips_irn(node) && "need mips node to get attributes");
265         return get_irn_generic_attr_const(node);
266 }
267
268 const mips_load_store_attr_t *get_mips_load_store_attr_const(
269                 const ir_node *node)
270 {
271         assert(is_mips_irn(node) && "need mips node to get attributes");
272         return get_irn_generic_attr_const(node);
273 }
274
275 /**
276  * Returns the argument register requirements of a mips node.
277  */
278 const arch_register_req_t **get_mips_in_req_all(const ir_node *node)
279 {
280         const mips_attr_t *attr = get_mips_attr_const(node);
281         return attr->in_req;
282 }
283
284 /**
285  * Returns the result register requirements of an mips node.
286  */
287 const arch_register_req_t **get_mips_out_req_all(const ir_node *node)
288 {
289         const mips_attr_t *attr = get_mips_attr_const(node);
290         return attr->out_req;
291 }
292
293 /**
294  * Returns the argument register requirement at position pos of an mips node.
295  */
296 const arch_register_req_t *get_mips_in_req(const ir_node *node, int pos)
297 {
298         const mips_attr_t *attr = get_mips_attr_const(node);
299         return attr->in_req[pos];
300 }
301
302 /**
303  * Returns the result register requirement at position pos of an mips node.
304  */
305 const arch_register_req_t *get_mips_out_req(const ir_node *node, int pos)
306 {
307         const mips_attr_t *attr = get_mips_attr_const(node);
308         return attr->out_req[pos];
309 }
310
311 /**
312  * Sets the OUT register requirements at position pos.
313  */
314 void set_mips_req_out(ir_node *node, const arch_register_req_t *req, int pos)
315 {
316         mips_attr_t *attr   = get_mips_attr(node);
317         attr->out_req[pos] = req;
318 }
319
320 /**
321  * Sets the IN register requirements at position pos.
322  */
323 void set_mips_req_in(ir_node *node, const arch_register_req_t *req, int pos)
324 {
325         mips_attr_t *attr  = get_mips_attr(node);
326         attr->in_req[pos] = req;
327 }
328
329 /**
330  * Returns the register flag of an mips node.
331  */
332 arch_irn_flags_t get_mips_flags(const ir_node *node)
333 {
334         const mips_attr_t *attr = get_mips_attr_const(node);
335         return attr->flags;
336 }
337
338 /**
339  * Sets the register flag of an mips node.
340  */
341 void set_mips_flags(ir_node *node, arch_irn_flags_t flags)
342 {
343         mips_attr_t *attr = get_mips_attr(node);
344         attr->flags      = flags;
345 }
346
347 /**
348  * Returns the result register slots of an mips node.
349  */
350 const arch_register_t **get_mips_slots(const ir_node *node)
351 {
352         const mips_attr_t *attr = get_mips_attr_const(node);
353         return attr->slots;
354 }
355
356 /**
357  * Returns the name of the OUT register at position pos.
358  */
359 const char *get_mips_out_reg_name(const ir_node *node, int pos)
360 {
361         const mips_attr_t *attr = get_mips_attr_const(node);
362
363         assert(is_mips_irn(node) && "Not an mips node.");
364         assert(pos < ARR_LEN(attr->slots) && "Invalid OUT position.");
365         assert(attr->slots[pos]  && "No register assigned");
366
367         return arch_register_get_name(attr->slots[pos]);
368 }
369
370 /**
371  * Returns the index of the OUT register at position pos within its register class.
372  */
373 int get_mips_out_regnr(const ir_node *node, int pos)
374 {
375         const mips_attr_t *attr = get_mips_attr_const(node);
376
377         assert(is_mips_irn(node) && "Not an mips node.");
378         assert(pos < ARR_LEN(attr->slots) && "Invalid OUT position.");
379         assert(attr->slots[pos]  && "No register assigned");
380
381         return arch_register_get_index(attr->slots[pos]);
382 }
383
384 /**
385  * Returns the OUT register at position pos.
386  */
387 const arch_register_t *get_mips_out_reg(const ir_node *node, int pos)
388 {
389         const mips_attr_t *attr = get_mips_attr_const(node);
390
391         assert(is_mips_irn(node) && "Not an mips node.");
392         assert(pos < ARR_LEN(attr->slots) && "Invalid OUT position.");
393         assert(attr->slots[pos]  && "No register assigned");
394
395         return attr->slots[pos];
396 }
397
398 /**
399  * Initializes the nodes attributes.
400  */
401 static void init_mips_attributes(ir_node *node, arch_irn_flags_t flags,
402                                  const arch_register_req_t **in_reqs,
403                                  const arch_register_req_t **out_reqs,
404                                  const be_execution_unit_t ***execution_units,
405                                  int n_res)
406 {
407         ir_graph       *irg  = get_irn_irg(node);
408         struct obstack *obst = get_irg_obstack(irg);
409         mips_attr_t    *attr = get_mips_attr(node);
410         (void) execution_units;
411
412         attr->flags   = flags;
413         attr->out_req = out_reqs;
414         attr->in_req  = in_reqs;
415
416         attr->slots = NEW_ARR_D(const arch_register_t*, obst, n_res);
417         memset(attr->slots, 0, n_res * sizeof(attr->slots[0]));
418 }
419
420 static void init_mips_immediate_attributes(ir_node *node,
421                                            mips_immediate_type_t type,
422                                            ir_entity *entity, long val)
423 {
424         mips_immediate_attr_t *attr = get_irn_generic_attr(node);
425
426         attr->imm_type = type;
427         attr->entity   = entity;
428         attr->val      = val;
429 }
430
431 static void init_mips_load_store_attributes(ir_node *node, ir_entity *entity,
432                                             long offset)
433 {
434         mips_load_store_attr_t *attr = get_irn_generic_attr(node);
435         attr->stack_entity = entity;
436         attr->offset       = offset;
437 }
438
439 static int mips_compare_nodes_attr(ir_node *node_a, ir_node *node_b)
440 {
441         const mips_attr_t *a = get_mips_attr_const(node_a);
442         const mips_attr_t *b = get_mips_attr_const(node_b);
443
444         if(a->flags != b->flags)
445                 return 1;
446         if(ARR_LEN(a->slots) != ARR_LEN(b->slots))
447                 return 1;
448         if(a->switch_default_pn != b->switch_default_pn)
449                 return 1;
450
451         return 0;
452 }
453
454 static int mips_compare_immediate_attr(ir_node *node_a, ir_node *node_b)
455 {
456         const mips_immediate_attr_t *a = get_mips_immediate_attr_const(node_a);
457         const mips_immediate_attr_t *b = get_mips_immediate_attr_const(node_b);
458
459         if(a->attr.flags != b->attr.flags)
460                 return 1;
461         if(a->val != b->val)
462                 return 1;
463
464         return 0;
465 }
466
467 static int mips_compare_load_store_attr(ir_node *node_a, ir_node *node_b)
468 {
469         const mips_load_store_attr_t *a = get_mips_load_store_attr_const(node_a);
470         const mips_load_store_attr_t *b = get_mips_load_store_attr_const(node_b);
471
472         if(mips_compare_nodes_attr(node_a, node_b))
473                 return 1;
474         if(a->stack_entity != b->stack_entity)
475                 return 1;
476         if(a->offset != b->offset)
477                 return 1;
478
479         return 0;
480 }
481
482 static void mips_copy_attr(const ir_node *old_node , ir_node *new_node)
483 {
484         ir_graph          *irg      = get_irn_irg(new_node);
485         struct obstack    *obst     = get_irg_obstack(irg);
486         const mips_attr_t *attr_old = get_mips_attr_const(old_node);
487         mips_attr_t       *attr_new = get_mips_attr(new_node);
488
489         /* copy the attributes */
490         memcpy(attr_new, attr_old, get_op_attr_size(get_irn_op(old_node)));
491
492         /* copy register assignments */
493         attr_new->slots = DUP_ARR_D(arch_register_t*, obst, attr_old->slots);
494 }
495
496 /***************************************************************************************
497  *                  _                            _                   _
498  *                 | |                          | |                 | |
499  *  _ __   ___   __| | ___    ___ ___  _ __  ___| |_ _ __ _   _  ___| |_ ___  _ __ ___
500  * | '_ \ / _ \ / _` |/ _ \  / __/ _ \| '_ \/ __| __| '__| | | |/ __| __/ _ \| '__/ __|
501  * | | | | (_) | (_| |  __/ | (_| (_) | | | \__ \ |_| |  | |_| | (__| || (_) | |  \__ \
502  * |_| |_|\___/ \__,_|\___|  \___\___/|_| |_|___/\__|_|   \__,_|\___|\__\___/|_|  |___/
503  *
504  ***************************************************************************************/
505
506 /* Include the generated constructor functions */
507 #include "gen_mips_new_nodes.c.inl"