- propagate r23142 changes to all backends
[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 ? (int) arch_irn_get_n_outs(n) : 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, n_res, flags;
138         const arch_register_req_t **reqs;
139
140         switch (reason) {
141                 case dump_node_opcode_txt:
142                         fprintf(F, "%s", get_irn_opname(n));
143                         break;
144
145                 case dump_node_mode_txt:
146                         break;
147
148                 case dump_node_nodeattr_txt:
149
150                         if(is_mips_Immediate(n)) {
151                                 const mips_immediate_attr_t *attr
152                                         = get_mips_immediate_attr_const(n);
153                                 switch(attr->imm_type) {
154                                 case MIPS_IMM_CONST:
155                                         fprintf(F, " %ld ", attr->val);
156                                         break;
157                                 case MIPS_IMM_SYMCONST_LO:
158                                         fprintf(F, " lo(%s", get_entity_ld_name(attr->entity));
159                                         if(attr->val != 0) {
160                                                 fprintf(F, "%+ld", attr->val);
161                                         }
162                                         fprintf(F, ") ");
163                                         break;
164                                 case MIPS_IMM_SYMCONST_HI:
165                                         fprintf(F, " hi(%s", get_entity_ld_name(attr->entity));
166                                         if(attr->val != 0) {
167                                                 fprintf(F, "%+ld", attr->val);
168                                         }
169                                         fprintf(F, ") ");
170                                         break;
171                                 default:
172                                         fprintf(F, " INVALID ");
173                                         break;
174                                 }
175                         }
176                         break;
177
178                 case dump_node_info_txt:
179                         fprintf(F, "=== mips attr begin ===\n");
180
181                         /* dump IN requirements */
182                         if (get_irn_arity(n) > 0) {
183                                 reqs = get_mips_in_req_all(n);
184                                 dump_reg_req(F, n, reqs, 0);
185                         }
186
187                         n_res = arch_irn_get_n_outs(n);
188                         if (n_res > 0) {
189                                 /* dump OUT requirements */
190                                 reqs = get_mips_out_req_all(n);
191                                 dump_reg_req(F, n, reqs, 1);
192
193                                 /* dump assigned registers */
194                                 for (i = 0; i < n_res; i++) {
195                                         const arch_register_t *reg = arch_irn_get_register(n, i);
196
197                                         fprintf(F, "reg #%d = %s\n", i, reg ? arch_register_get_name(reg) : "n/a");
198                                 }
199                                 fprintf(F, "\n");
200                         }
201
202                         /* dump n_res */
203                         fprintf(F, "n_res = %d\n", n_res);
204
205                         /* dump flags */
206                         fprintf(F, "flags =");
207                         flags = arch_irn_get_flags(n);
208                         if (flags == arch_irn_flags_none) {
209                                 fprintf(F, " none");
210                         }
211                         else {
212                                 if (flags & arch_irn_flags_dont_spill) {
213                                         fprintf(F, " unspillable");
214                                 }
215                                 if (flags & arch_irn_flags_rematerializable) {
216                                         fprintf(F, " remat");
217                                 }
218                                 if (flags & arch_irn_flags_modify_flags) {
219                                         fprintf(F, " modify_flags");
220                                 }
221                         }
222                         fprintf(F, " (%d)\n", flags);
223
224                         fprintf(F, "=== mips attr end ===\n");
225                         /* end of: case dump_node_info_txt */
226                         break;
227         }
228
229
230         return bad;
231 }
232
233
234
235 /***************************************************************************************************
236  *        _   _                   _       __        _                    _   _               _
237  *       | | | |                 | |     / /       | |                  | | | |             | |
238  *   __ _| |_| |_ _ __   ___  ___| |_   / /_ _  ___| |_   _ __ ___   ___| |_| |__   ___   __| |___
239  *  / _` | __| __| '__| / __|/ _ \ __| / / _` |/ _ \ __| | '_ ` _ \ / _ \ __| '_ \ / _ \ / _` / __|
240  * | (_| | |_| |_| |    \__ \  __/ |_ / / (_| |  __/ |_  | | | | | |  __/ |_| | | | (_) | (_| \__ \
241  *  \__,_|\__|\__|_|    |___/\___|\__/_/ \__, |\___|\__| |_| |_| |_|\___|\__|_| |_|\___/ \__,_|___/
242  *                                        __/ |
243  *                                       |___/
244  ***************************************************************************************************/
245
246 mips_attr_t *get_mips_attr(ir_node *node)
247 {
248         assert(is_mips_irn(node) && "need mips node to get attributes");
249         return (mips_attr_t *) get_irn_generic_attr(node);
250 }
251
252 const mips_attr_t *get_mips_attr_const(const ir_node *node)
253 {
254         assert(is_mips_irn(node) && "need mips node to get attributes");
255         return get_irn_generic_attr_const(node);
256 }
257
258 const mips_immediate_attr_t *get_mips_immediate_attr_const(const ir_node *node)
259 {
260         assert(is_mips_irn(node) && "need mips node to get attributes");
261         return get_irn_generic_attr_const(node);
262 }
263
264 const mips_load_store_attr_t *get_mips_load_store_attr_const(
265                 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 /**
272  * Returns the argument register requirements of a mips node.
273  */
274 const arch_register_req_t **get_mips_in_req_all(const ir_node *node)
275 {
276         const mips_attr_t *attr = get_mips_attr_const(node);
277         return attr->in_req;
278 }
279
280 /**
281  * Returns the result register requirements of an mips node.
282  */
283 const arch_register_req_t **get_mips_out_req_all(const ir_node *node)
284 {
285         const mips_attr_t *attr = get_mips_attr_const(node);
286         return attr->out_req;
287 }
288
289 /**
290  * Returns the argument register requirement at position pos of an mips node.
291  */
292 const arch_register_req_t *get_mips_in_req(const ir_node *node, int pos)
293 {
294         const mips_attr_t *attr = get_mips_attr_const(node);
295         return attr->in_req[pos];
296 }
297
298 /**
299  * Returns the result register requirement at position pos of an mips node.
300  */
301 const arch_register_req_t *get_mips_out_req(const ir_node *node, int pos)
302 {
303         const mips_attr_t *attr = get_mips_attr_const(node);
304         return attr->out_req[pos];
305 }
306
307 /**
308  * Sets the OUT register requirements at position pos.
309  */
310 void set_mips_req_out(ir_node *node, const arch_register_req_t *req, int pos)
311 {
312         mips_attr_t *attr   = get_mips_attr(node);
313         attr->out_req[pos] = req;
314 }
315
316 /**
317  * Sets the IN register requirements at position pos.
318  */
319 void set_mips_req_in(ir_node *node, const arch_register_req_t *req, int pos)
320 {
321         mips_attr_t *attr  = get_mips_attr(node);
322         attr->in_req[pos] = req;
323 }
324
325 /**
326  * Initializes the nodes attributes.
327  */
328 static void init_mips_attributes(ir_node *node, arch_irn_flags_t flags,
329                                  const arch_register_req_t **in_reqs,
330                                  const arch_register_req_t **out_reqs,
331                                  const be_execution_unit_t ***execution_units,
332                                  int n_res)
333 {
334         ir_graph       *irg  = get_irn_irg(node);
335         struct obstack *obst = get_irg_obstack(irg);
336         mips_attr_t    *attr = get_mips_attr(node);
337         backend_info_t *info;
338         (void) execution_units;
339
340         arch_irn_set_flags(node, flags);
341         attr->out_req = out_reqs;
342         attr->in_req  = in_reqs;
343
344         info            = be_get_info(node);
345         info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_res);
346         memset(info->out_infos, 0, n_res * sizeof(info->out_infos[0]));
347 }
348
349 static void init_mips_immediate_attributes(ir_node *node,
350                                            mips_immediate_type_t type,
351                                            ir_entity *entity, long val)
352 {
353         mips_immediate_attr_t *attr = get_irn_generic_attr(node);
354
355         attr->imm_type = type;
356         attr->entity   = entity;
357         attr->val      = val;
358 }
359
360 static void init_mips_load_store_attributes(ir_node *node, ir_entity *entity,
361                                             long offset)
362 {
363         mips_load_store_attr_t *attr = get_irn_generic_attr(node);
364         attr->stack_entity = entity;
365         attr->offset       = offset;
366 }
367
368 static int mips_compare_nodes_attr(ir_node *node_a, ir_node *node_b)
369 {
370         const mips_attr_t *a = get_mips_attr_const(node_a);
371         const mips_attr_t *b = get_mips_attr_const(node_b);
372
373         if(a->switch_default_pn != b->switch_default_pn)
374                 return 1;
375
376         return 0;
377 }
378
379 static int mips_compare_immediate_attr(ir_node *node_a, ir_node *node_b)
380 {
381         const mips_immediate_attr_t *a = get_mips_immediate_attr_const(node_a);
382         const mips_immediate_attr_t *b = get_mips_immediate_attr_const(node_b);
383
384         if(a->val != b->val)
385                 return 1;
386
387         return 0;
388 }
389
390 static int mips_compare_load_store_attr(ir_node *node_a, ir_node *node_b)
391 {
392         const mips_load_store_attr_t *a = get_mips_load_store_attr_const(node_a);
393         const mips_load_store_attr_t *b = get_mips_load_store_attr_const(node_b);
394
395         if(mips_compare_nodes_attr(node_a, node_b))
396                 return 1;
397         if(a->stack_entity != b->stack_entity)
398                 return 1;
399         if(a->offset != b->offset)
400                 return 1;
401
402         return 0;
403 }
404
405 static void mips_copy_attr(const ir_node *old_node , ir_node *new_node)
406 {
407         ir_graph          *irg      = get_irn_irg(new_node);
408         struct obstack    *obst     = get_irg_obstack(irg);
409         const mips_attr_t *attr_old = get_mips_attr_const(old_node);
410         mips_attr_t       *attr_new = get_mips_attr(new_node);
411         backend_info_t    *old_info = be_get_info(old_node);
412         backend_info_t    *new_info = be_get_info(new_node);
413
414         /* copy the attributes */
415         memcpy(attr_new, attr_old, get_op_attr_size(get_irn_op(old_node)));
416
417         /* copy out flags */
418         new_info->out_infos =
419                 DUP_ARR_D(reg_out_info_t, obst, old_info->out_infos);
420 }
421
422 /***************************************************************************************
423  *                  _                            _                   _
424  *                 | |                          | |                 | |
425  *  _ __   ___   __| | ___    ___ ___  _ __  ___| |_ _ __ _   _  ___| |_ ___  _ __ ___
426  * | '_ \ / _ \ / _` |/ _ \  / __/ _ \| '_ \/ __| __| '__| | | |/ __| __/ _ \| '__/ __|
427  * | | | | (_) | (_| |  __/ | (_| (_) | | | \__ \ |_| |  | |_| | (__| || (_) | |  \__ \
428  * |_| |_|\___/ \__,_|\___|  \___\___/|_| |_|___/\__|_|   \__,_|\___|\__\___/|_|  |___/
429  *
430  ***************************************************************************************/
431
432 /* Include the generated constructor functions */
433 #include "gen_mips_new_nodes.c.inl"