- mostly implemented access to outer frame variables, however offset, is wrong yet
[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         char *dir = inout ? "out" : "in";
70         int   max = inout ? (int) arch_irn_get_n_outs(n) : get_irn_arity(n);
71         char  buf[1024];
72         int   i;
73
74         memset(buf, 0, sizeof(buf));
75
76         if (reqs) {
77                 for (i = 0; i < max; i++) {
78                         fprintf(F, "%sreq #%d =", dir, i);
79
80                         if (reqs[i]->type == arch_register_req_type_none) {
81                                 fprintf(F, " n/a");
82                         }
83
84                         if (reqs[i]->type & arch_register_req_type_normal) {
85                                 fprintf(F, " %s", reqs[i]->cls->name);
86                         }
87
88                         if (reqs[i]->type & arch_register_req_type_limited) {
89                                 fprintf(F, " %s",
90                                         arch_register_req_format(buf, sizeof(buf), reqs[i], n));
91                         }
92
93                         if (reqs[i]->type & arch_register_req_type_should_be_same) {
94                                 const unsigned other = reqs[i]->other_same;
95                                 int i;
96
97                                 ir_fprintf(F, " same as");
98                                 for (i = 0; 1U << i <= other; ++i) {
99                                         if (other & (1U << i)) {
100                                                 ir_fprintf(F, " %+F", i);
101                                         }
102                                 }
103                         }
104
105                         if (reqs[i]->type & arch_register_req_type_must_be_different) {
106                                 const unsigned other = reqs[i]->other_different;
107                                 int i;
108
109                                 ir_fprintf(F, " different from");
110                                 for (i = 0; 1U << i <= other; ++i) {
111                                         if (other & (1U << i)) {
112                                                 ir_fprintf(F, " %+F", i);
113                                         }
114                                 }
115                         }
116
117                         fprintf(F, "\n");
118                 }
119
120                 fprintf(F, "\n");
121         } else {
122                 fprintf(F, "%sreq = N/A\n", dir);
123         }
124 }
125
126
127 /**
128  * Dumper interface for dumping mips nodes in vcg.
129  * @param n        the node to dump
130  * @param F        the output file
131  * @param reason   indicates which kind of information should be dumped
132  * @return 0 on success or != 0 on failure
133  */
134 static int mips_dump_node(ir_node *n, FILE *F, dump_reason_t reason) {
135         int          bad  = 0;
136         int          i, n_res, flags;
137         const arch_register_req_t **reqs;
138
139         switch (reason) {
140                 case dump_node_opcode_txt:
141                         fprintf(F, "%s", get_irn_opname(n));
142                         break;
143
144                 case dump_node_mode_txt:
145                         break;
146
147                 case dump_node_nodeattr_txt:
148
149                         if(is_mips_Immediate(n)) {
150                                 const mips_immediate_attr_t *attr
151                                         = get_mips_immediate_attr_const(n);
152                                 switch(attr->imm_type) {
153                                 case MIPS_IMM_CONST:
154                                         fprintf(F, " %ld ", attr->val);
155                                         break;
156                                 case MIPS_IMM_SYMCONST_LO:
157                                         fprintf(F, " lo(%s", get_entity_ld_name(attr->entity));
158                                         if(attr->val != 0) {
159                                                 fprintf(F, "%+ld", attr->val);
160                                         }
161                                         fprintf(F, ") ");
162                                         break;
163                                 case MIPS_IMM_SYMCONST_HI:
164                                         fprintf(F, " hi(%s", get_entity_ld_name(attr->entity));
165                                         if(attr->val != 0) {
166                                                 fprintf(F, "%+ld", attr->val);
167                                         }
168                                         fprintf(F, ") ");
169                                         break;
170                                 default:
171                                         fprintf(F, " INVALID ");
172                                         break;
173                                 }
174                         }
175                         break;
176
177                 case dump_node_info_txt:
178                         fprintf(F, "=== mips attr begin ===\n");
179
180                         /* dump IN requirements */
181                         if (get_irn_arity(n) > 0) {
182                                 reqs = get_mips_in_req_all(n);
183                                 dump_reg_req(F, n, reqs, 0);
184                         }
185
186                         n_res = arch_irn_get_n_outs(n);
187                         if (n_res > 0) {
188                                 /* dump OUT requirements */
189                                 reqs = get_mips_out_req_all(n);
190                                 dump_reg_req(F, n, reqs, 1);
191
192                                 /* dump assigned registers */
193                                 for (i = 0; i < n_res; i++) {
194                                         const arch_register_t *reg = arch_irn_get_register(n, i);
195
196                                         fprintf(F, "reg #%d = %s\n", i, reg ? arch_register_get_name(reg) : "n/a");
197                                 }
198                                 fprintf(F, "\n");
199                         }
200
201                         /* dump n_res */
202                         fprintf(F, "n_res = %d\n", n_res);
203
204                         /* dump flags */
205                         fprintf(F, "flags =");
206                         flags = arch_irn_get_flags(n);
207                         if (flags == arch_irn_flags_none) {
208                                 fprintf(F, " none");
209                         }
210                         else {
211                                 if (flags & arch_irn_flags_dont_spill) {
212                                         fprintf(F, " unspillable");
213                                 }
214                                 if (flags & arch_irn_flags_rematerializable) {
215                                         fprintf(F, " remat");
216                                 }
217                                 if (flags & arch_irn_flags_modify_flags) {
218                                         fprintf(F, " modify_flags");
219                                 }
220                         }
221                         fprintf(F, " (%d)\n", flags);
222
223                         fprintf(F, "=== mips attr end ===\n");
224                         /* end of: case dump_node_info_txt */
225                         break;
226         }
227
228
229         return bad;
230 }
231
232
233
234 /***************************************************************************************************
235  *        _   _                   _       __        _                    _   _               _
236  *       | | | |                 | |     / /       | |                  | | | |             | |
237  *   __ _| |_| |_ _ __   ___  ___| |_   / /_ _  ___| |_   _ __ ___   ___| |_| |__   ___   __| |___
238  *  / _` | __| __| '__| / __|/ _ \ __| / / _` |/ _ \ __| | '_ ` _ \ / _ \ __| '_ \ / _ \ / _` / __|
239  * | (_| | |_| |_| |    \__ \  __/ |_ / / (_| |  __/ |_  | | | | | |  __/ |_| | | | (_) | (_| \__ \
240  *  \__,_|\__|\__|_|    |___/\___|\__/_/ \__, |\___|\__| |_| |_| |_|\___|\__|_| |_|\___/ \__,_|___/
241  *                                        __/ |
242  *                                       |___/
243  ***************************************************************************************************/
244
245 mips_attr_t *get_mips_attr(ir_node *node)
246 {
247         assert(is_mips_irn(node) && "need mips node to get attributes");
248         return (mips_attr_t *) get_irn_generic_attr(node);
249 }
250
251 const mips_attr_t *get_mips_attr_const(const ir_node *node)
252 {
253         assert(is_mips_irn(node) && "need mips node to get attributes");
254         return get_irn_generic_attr_const(node);
255 }
256
257 const mips_immediate_attr_t *get_mips_immediate_attr_const(const ir_node *node)
258 {
259         assert(is_mips_irn(node) && "need mips node to get attributes");
260         return get_irn_generic_attr_const(node);
261 }
262
263 const mips_load_store_attr_t *get_mips_load_store_attr_const(
264                 const ir_node *node)
265 {
266         assert(is_mips_irn(node) && "need mips node to get attributes");
267         return get_irn_generic_attr_const(node);
268 }
269
270 /**
271  * Returns the argument register requirements of a mips node.
272  */
273 const arch_register_req_t **get_mips_in_req_all(const ir_node *node)
274 {
275         const mips_attr_t *attr = get_mips_attr_const(node);
276         return attr->in_req;
277 }
278
279 /**
280  * Returns the result register requirements of an mips node.
281  */
282 const arch_register_req_t **get_mips_out_req_all(const ir_node *node)
283 {
284         const mips_attr_t *attr = get_mips_attr_const(node);
285         return attr->out_req;
286 }
287
288 /**
289  * Returns the argument register requirement at position pos of an mips node.
290  */
291 const arch_register_req_t *get_mips_in_req(const ir_node *node, int pos)
292 {
293         const mips_attr_t *attr = get_mips_attr_const(node);
294         return attr->in_req[pos];
295 }
296
297 /**
298  * Returns the result register requirement at position pos of an mips node.
299  */
300 const arch_register_req_t *get_mips_out_req(const ir_node *node, int pos)
301 {
302         const mips_attr_t *attr = get_mips_attr_const(node);
303         return attr->out_req[pos];
304 }
305
306 /**
307  * Sets the OUT register requirements at position pos.
308  */
309 void set_mips_req_out(ir_node *node, const arch_register_req_t *req, int pos)
310 {
311         mips_attr_t *attr   = get_mips_attr(node);
312         attr->out_req[pos] = req;
313 }
314
315 /**
316  * Sets the IN register requirements at position pos.
317  */
318 void set_mips_req_in(ir_node *node, const arch_register_req_t *req, int pos)
319 {
320         mips_attr_t *attr  = get_mips_attr(node);
321         attr->in_req[pos] = req;
322 }
323
324 /**
325  * Initializes the nodes attributes.
326  */
327 static void init_mips_attributes(ir_node *node, arch_irn_flags_t flags,
328                                  const arch_register_req_t **in_reqs,
329                                  const arch_register_req_t **out_reqs,
330                                  const be_execution_unit_t ***execution_units,
331                                  int n_res)
332 {
333         ir_graph       *irg  = get_irn_irg(node);
334         struct obstack *obst = get_irg_obstack(irg);
335         mips_attr_t    *attr = get_mips_attr(node);
336         backend_info_t *info;
337         (void) execution_units;
338
339         arch_irn_set_flags(node, flags);
340         attr->out_req = out_reqs;
341         attr->in_req  = in_reqs;
342
343         info            = be_get_info(node);
344         info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_res);
345         memset(info->out_infos, 0, n_res * sizeof(info->out_infos[0]));
346 }
347
348 static void init_mips_immediate_attributes(ir_node *node,
349                                            mips_immediate_type_t type,
350                                            ir_entity *entity, long val)
351 {
352         mips_immediate_attr_t *attr = get_irn_generic_attr(node);
353
354         attr->imm_type = type;
355         attr->entity   = entity;
356         attr->val      = val;
357 }
358
359 static void init_mips_load_store_attributes(ir_node *node, ir_entity *entity,
360                                             long offset)
361 {
362         mips_load_store_attr_t *attr = get_irn_generic_attr(node);
363         attr->stack_entity = entity;
364         attr->offset       = offset;
365 }
366
367 static int mips_compare_nodes_attr(ir_node *node_a, ir_node *node_b)
368 {
369         const mips_attr_t *a = get_mips_attr_const(node_a);
370         const mips_attr_t *b = get_mips_attr_const(node_b);
371
372         if(a->switch_default_pn != b->switch_default_pn)
373                 return 1;
374
375         return 0;
376 }
377
378 static int mips_compare_immediate_attr(ir_node *node_a, ir_node *node_b)
379 {
380         const mips_immediate_attr_t *a = get_mips_immediate_attr_const(node_a);
381         const mips_immediate_attr_t *b = get_mips_immediate_attr_const(node_b);
382
383         if(a->val != b->val)
384                 return 1;
385
386         return 0;
387 }
388
389 static int mips_compare_load_store_attr(ir_node *node_a, ir_node *node_b)
390 {
391         const mips_load_store_attr_t *a = get_mips_load_store_attr_const(node_a);
392         const mips_load_store_attr_t *b = get_mips_load_store_attr_const(node_b);
393
394         if(mips_compare_nodes_attr(node_a, node_b))
395                 return 1;
396         if(a->stack_entity != b->stack_entity)
397                 return 1;
398         if(a->offset != b->offset)
399                 return 1;
400
401         return 0;
402 }
403
404 static void mips_copy_attr(const ir_node *old_node , ir_node *new_node)
405 {
406         ir_graph          *irg      = get_irn_irg(new_node);
407         struct obstack    *obst     = get_irg_obstack(irg);
408         const mips_attr_t *attr_old = get_mips_attr_const(old_node);
409         mips_attr_t       *attr_new = get_mips_attr(new_node);
410         backend_info_t    *old_info = be_get_info(old_node);
411         backend_info_t    *new_info = be_get_info(new_node);
412
413         /* copy the attributes */
414         memcpy(attr_new, attr_old, get_op_attr_size(get_irn_op(old_node)));
415
416         /* copy out flags */
417         new_info->out_infos =
418                 DUP_ARR_D(reg_out_info_t, obst, old_info->out_infos);
419 }
420
421 /***************************************************************************************
422  *                  _                            _                   _
423  *                 | |                          | |                 | |
424  *  _ __   ___   __| | ___    ___ ___  _ __  ___| |_ _ __ _   _  ___| |_ ___  _ __ ___
425  * | '_ \ / _ \ / _` |/ _ \  / __/ _ \| '_ \/ __| __| '__| | | |/ __| __/ _ \| '__/ __|
426  * | | | | (_) | (_| |  __/ | (_| (_) | | | \__ \ |_| |  | |_| | (__| || (_) | |  \__ \
427  * |_| |_|\___/ \__,_|\___|  \___\___/|_| |_|___/\__|_|   \__,_|\___|\__\___/|_|  |___/
428  *
429  ***************************************************************************************/
430
431 /* Include the generated constructor functions */
432 #include "gen_mips_new_nodes.c.inl"