- rework backend node dumping; add a dumper for Phi nodes
[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 "irvrfy_t.h"
40 #include "irprintf.h"
41 #include "xmalloc.h"
42
43 #include "../bearch.h"
44
45 #include "mips_nodes_attr.h"
46 #include "mips_new_nodes.h"
47 #include "gen_mips_regalloc_if.h"
48
49
50
51 /***********************************************************************************
52  *      _                                   _       _             __
53  *     | |                                 (_)     | |           / _|
54  *   __| |_   _ _ __ ___  _ __   ___ _ __   _ _ __ | |_ ___ _ __| |_ __ _  ___ ___
55  *  / _` | | | | '_ ` _ \| '_ \ / _ \ '__| | | '_ \| __/ _ \ '__|  _/ _` |/ __/ _ \
56  * | (_| | |_| | | | | | | |_) |  __/ |    | | | | | ||  __/ |  | || (_| | (_|  __/
57  *  \__,_|\__,_|_| |_| |_| .__/ \___|_|    |_|_| |_|\__\___|_|  |_| \__,_|\___\___|
58  *                       | |
59  *                       |_|
60  ***********************************************************************************/
61
62 /**
63  * Dumps the register requirements for either in or out.
64  */
65 static void dump_reg_req(FILE *F, ir_node *n, const arch_register_req_t **reqs,
66                          int inout)
67 {
68         char *dir = inout ? "out" : "in";
69         int   max = inout ? (int) arch_irn_get_n_outs(n) : get_irn_arity(n);
70         char  buf[1024];
71         int   i;
72
73         memset(buf, 0, sizeof(buf));
74
75         if (reqs) {
76                 for (i = 0; i < max; i++) {
77                         fprintf(F, "%sreq #%d =", dir, i);
78                         arch_dump_register_req(F, reqs[i], n);
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         int          bad  = 0;
98         int          i, n_res, flags;
99         const arch_register_req_t **reqs;
100
101         switch (reason) {
102                 case dump_node_opcode_txt:
103                         fprintf(F, "%s", get_irn_opname(n));
104                         break;
105
106                 case dump_node_mode_txt:
107                         break;
108
109                 case dump_node_nodeattr_txt:
110
111                         if(is_mips_Immediate(n)) {
112                                 const mips_immediate_attr_t *attr
113                                         = get_mips_immediate_attr_const(n);
114                                 switch(attr->imm_type) {
115                                 case MIPS_IMM_CONST:
116                                         fprintf(F, " %ld ", attr->val);
117                                         break;
118                                 case MIPS_IMM_SYMCONST_LO:
119                                         fprintf(F, " lo(%s", get_entity_ld_name(attr->entity));
120                                         if(attr->val != 0) {
121                                                 fprintf(F, "%+ld", attr->val);
122                                         }
123                                         fprintf(F, ") ");
124                                         break;
125                                 case MIPS_IMM_SYMCONST_HI:
126                                         fprintf(F, " hi(%s", get_entity_ld_name(attr->entity));
127                                         if(attr->val != 0) {
128                                                 fprintf(F, "%+ld", attr->val);
129                                         }
130                                         fprintf(F, ") ");
131                                         break;
132                                 default:
133                                         fprintf(F, " INVALID ");
134                                         break;
135                                 }
136                         }
137                         break;
138
139                 case dump_node_info_txt:
140                         fprintf(F, "=== mips attr begin ===\n");
141
142                         /* dump IN requirements */
143                         if (get_irn_arity(n) > 0) {
144                                 reqs = get_mips_in_req_all(n);
145                                 dump_reg_req(F, n, reqs, 0);
146                         }
147
148                         n_res = arch_irn_get_n_outs(n);
149                         if (n_res > 0) {
150                                 /* dump OUT requirements */
151                                 reqs = get_mips_out_req_all(n);
152                                 dump_reg_req(F, n, reqs, 1);
153
154                                 /* dump assigned registers */
155                                 for (i = 0; i < n_res; i++) {
156                                         const arch_register_t *reg = arch_irn_get_register(n, i);
157
158                                         fprintf(F, "reg #%d = %s\n", i, reg ? arch_register_get_name(reg) : "n/a");
159                                 }
160                                 fprintf(F, "\n");
161                         }
162
163                         /* dump n_res */
164                         fprintf(F, "n_res = %d\n", n_res);
165
166                         /* dump flags */
167                         fprintf(F, "flags =");
168                         flags = arch_irn_get_flags(n);
169                         if (flags == arch_irn_flags_none) {
170                                 fprintf(F, " none");
171                         }
172                         else {
173                                 if (flags & arch_irn_flags_dont_spill) {
174                                         fprintf(F, " unspillable");
175                                 }
176                                 if (flags & arch_irn_flags_rematerializable) {
177                                         fprintf(F, " remat");
178                                 }
179                                 if (flags & arch_irn_flags_modify_flags) {
180                                         fprintf(F, " modify_flags");
181                                 }
182                         }
183                         fprintf(F, " (%d)\n", flags);
184
185                         fprintf(F, "=== mips attr end ===\n");
186                         /* end of: case dump_node_info_txt */
187                         break;
188         }
189
190
191         return bad;
192 }
193
194
195
196 /***************************************************************************************************
197  *        _   _                   _       __        _                    _   _               _
198  *       | | | |                 | |     / /       | |                  | | | |             | |
199  *   __ _| |_| |_ _ __   ___  ___| |_   / /_ _  ___| |_   _ __ ___   ___| |_| |__   ___   __| |___
200  *  / _` | __| __| '__| / __|/ _ \ __| / / _` |/ _ \ __| | '_ ` _ \ / _ \ __| '_ \ / _ \ / _` / __|
201  * | (_| | |_| |_| |    \__ \  __/ |_ / / (_| |  __/ |_  | | | | | |  __/ |_| | | | (_) | (_| \__ \
202  *  \__,_|\__|\__|_|    |___/\___|\__/_/ \__, |\___|\__| |_| |_| |_|\___|\__|_| |_|\___/ \__,_|___/
203  *                                        __/ |
204  *                                       |___/
205  ***************************************************************************************************/
206
207 mips_attr_t *get_mips_attr(ir_node *node)
208 {
209         assert(is_mips_irn(node) && "need mips node to get attributes");
210         return (mips_attr_t *) get_irn_generic_attr(node);
211 }
212
213 const mips_attr_t *get_mips_attr_const(const ir_node *node)
214 {
215         assert(is_mips_irn(node) && "need mips node to get attributes");
216         return get_irn_generic_attr_const(node);
217 }
218
219 const mips_immediate_attr_t *get_mips_immediate_attr_const(const ir_node *node)
220 {
221         assert(is_mips_irn(node) && "need mips node to get attributes");
222         return get_irn_generic_attr_const(node);
223 }
224
225 const mips_load_store_attr_t *get_mips_load_store_attr_const(
226                 const ir_node *node)
227 {
228         assert(is_mips_irn(node) && "need mips node to get attributes");
229         return get_irn_generic_attr_const(node);
230 }
231
232 /**
233  * Returns the argument register requirements of a mips node.
234  */
235 const arch_register_req_t **get_mips_in_req_all(const ir_node *node)
236 {
237         const mips_attr_t *attr = get_mips_attr_const(node);
238         return attr->in_req;
239 }
240
241 /**
242  * Returns the result register requirements of an mips node.
243  */
244 const arch_register_req_t **get_mips_out_req_all(const ir_node *node)
245 {
246         const mips_attr_t *attr = get_mips_attr_const(node);
247         return attr->out_req;
248 }
249
250 /**
251  * Returns the argument register requirement at position pos of an mips node.
252  */
253 const arch_register_req_t *get_mips_in_req(const ir_node *node, int pos)
254 {
255         const mips_attr_t *attr = get_mips_attr_const(node);
256         return attr->in_req[pos];
257 }
258
259 /**
260  * Returns the result register requirement at position pos of an mips node.
261  */
262 const arch_register_req_t *get_mips_out_req(const ir_node *node, int pos)
263 {
264         const mips_attr_t *attr = get_mips_attr_const(node);
265         return attr->out_req[pos];
266 }
267
268 /**
269  * Sets the OUT register requirements at position pos.
270  */
271 void set_mips_req_out(ir_node *node, const arch_register_req_t *req, int pos)
272 {
273         mips_attr_t *attr   = get_mips_attr(node);
274         attr->out_req[pos] = req;
275 }
276
277 /**
278  * Sets the IN register requirements at position pos.
279  */
280 void set_mips_req_in(ir_node *node, const arch_register_req_t *req, int pos)
281 {
282         mips_attr_t *attr  = get_mips_attr(node);
283         attr->in_req[pos] = req;
284 }
285
286 /**
287  * Initializes the nodes attributes.
288  */
289 static void init_mips_attributes(ir_node *node, arch_irn_flags_t flags,
290                                  const arch_register_req_t **in_reqs,
291                                  const arch_register_req_t **out_reqs,
292                                  const be_execution_unit_t ***execution_units,
293                                  int n_res)
294 {
295         ir_graph       *irg  = get_irn_irg(node);
296         struct obstack *obst = get_irg_obstack(irg);
297         mips_attr_t    *attr = get_mips_attr(node);
298         backend_info_t *info;
299         (void) execution_units;
300
301         arch_irn_set_flags(node, flags);
302         attr->out_req = out_reqs;
303         attr->in_req  = in_reqs;
304
305         info            = be_get_info(node);
306         info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_res);
307         memset(info->out_infos, 0, n_res * sizeof(info->out_infos[0]));
308 }
309
310 static void init_mips_immediate_attributes(ir_node *node,
311                                            mips_immediate_type_t type,
312                                            ir_entity *entity, long val)
313 {
314         mips_immediate_attr_t *attr = get_irn_generic_attr(node);
315
316         attr->imm_type = type;
317         attr->entity   = entity;
318         attr->val      = val;
319 }
320
321 static void init_mips_load_store_attributes(ir_node *node, ir_entity *entity,
322                                             long offset)
323 {
324         mips_load_store_attr_t *attr = get_irn_generic_attr(node);
325         attr->stack_entity = entity;
326         attr->offset       = offset;
327 }
328
329 static int mips_compare_nodes_attr(ir_node *node_a, ir_node *node_b)
330 {
331         const mips_attr_t *a = get_mips_attr_const(node_a);
332         const mips_attr_t *b = get_mips_attr_const(node_b);
333
334         if(a->switch_default_pn != b->switch_default_pn)
335                 return 1;
336
337         return 0;
338 }
339
340 static int mips_compare_immediate_attr(ir_node *node_a, ir_node *node_b)
341 {
342         const mips_immediate_attr_t *a = get_mips_immediate_attr_const(node_a);
343         const mips_immediate_attr_t *b = get_mips_immediate_attr_const(node_b);
344
345         if(a->val != b->val)
346                 return 1;
347
348         return 0;
349 }
350
351 static int mips_compare_load_store_attr(ir_node *node_a, ir_node *node_b)
352 {
353         const mips_load_store_attr_t *a = get_mips_load_store_attr_const(node_a);
354         const mips_load_store_attr_t *b = get_mips_load_store_attr_const(node_b);
355
356         if(mips_compare_nodes_attr(node_a, node_b))
357                 return 1;
358         if(a->stack_entity != b->stack_entity)
359                 return 1;
360         if(a->offset != b->offset)
361                 return 1;
362
363         return 0;
364 }
365
366 static void mips_copy_attr(const ir_node *old_node , ir_node *new_node)
367 {
368         ir_graph          *irg      = get_irn_irg(new_node);
369         struct obstack    *obst     = get_irg_obstack(irg);
370         const mips_attr_t *attr_old = get_mips_attr_const(old_node);
371         mips_attr_t       *attr_new = get_mips_attr(new_node);
372         backend_info_t    *old_info = be_get_info(old_node);
373         backend_info_t    *new_info = be_get_info(new_node);
374
375         /* copy the attributes */
376         memcpy(attr_new, attr_old, get_op_attr_size(get_irn_op(old_node)));
377
378         /* copy out flags */
379         new_info->out_infos =
380                 DUP_ARR_D(reg_out_info_t, obst, old_info->out_infos);
381 }
382
383 /***************************************************************************************
384  *                  _                            _                   _
385  *                 | |                          | |                 | |
386  *  _ __   ___   __| | ___    ___ ___  _ __  ___| |_ _ __ _   _  ___| |_ ___  _ __ ___
387  * | '_ \ / _ \ / _` |/ _ \  / __/ _ \| '_ \/ __| __| '__| | | |/ __| __/ _ \| '__/ __|
388  * | | | | (_) | (_| |  __/ | (_| (_) | | | \__ \ |_| |  | |_| | (__| || (_) | |  \__ \
389  * |_| |_|\___/ \__,_|\___|  \___\___/|_| |_|___/\__|_|   \__,_|\___|\__\___/|_|  |___/
390  *
391  ***************************************************************************************/
392
393 /* Include the generated constructor functions */
394 #include "gen_mips_new_nodes.c.inl"