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