- further refactoring and finally eliminated the callback for get_out_reg_reqs
[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  * Dumper interface for dumping mips nodes in vcg.
64  * @param n        the node to dump
65  * @param F        the output file
66  * @param reason   indicates which kind of information should be dumped
67  * @return 0 on success or != 0 on failure
68  */
69 static int mips_dump_node(ir_node *n, FILE *F, dump_reason_t reason)
70 {
71         switch (reason) {
72                 case dump_node_opcode_txt:
73                         fprintf(F, "%s", get_irn_opname(n));
74                         break;
75
76                 case dump_node_mode_txt:
77                         break;
78
79                 case dump_node_nodeattr_txt:
80
81                         if(is_mips_Immediate(n)) {
82                                 const mips_immediate_attr_t *attr
83                                         = get_mips_immediate_attr_const(n);
84                                 switch(attr->imm_type) {
85                                 case MIPS_IMM_CONST:
86                                         fprintf(F, " %ld ", attr->val);
87                                         break;
88                                 case MIPS_IMM_SYMCONST_LO:
89                                         fprintf(F, " lo(%s", get_entity_ld_name(attr->entity));
90                                         if(attr->val != 0) {
91                                                 fprintf(F, "%+ld", attr->val);
92                                         }
93                                         fprintf(F, ") ");
94                                         break;
95                                 case MIPS_IMM_SYMCONST_HI:
96                                         fprintf(F, " hi(%s", get_entity_ld_name(attr->entity));
97                                         if(attr->val != 0) {
98                                                 fprintf(F, "%+ld", attr->val);
99                                         }
100                                         fprintf(F, ") ");
101                                         break;
102                                 default:
103                                         fprintf(F, " INVALID ");
104                                         break;
105                                 }
106                         }
107                         break;
108
109                 case dump_node_info_txt:
110                         arch_dump_reqs_and_registers(F, n);
111                         break;
112         }
113
114         return 0;
115 }
116
117
118
119 /***************************************************************************************************
120  *        _   _                   _       __        _                    _   _               _
121  *       | | | |                 | |     / /       | |                  | | | |             | |
122  *   __ _| |_| |_ _ __   ___  ___| |_   / /_ _  ___| |_   _ __ ___   ___| |_| |__   ___   __| |___
123  *  / _` | __| __| '__| / __|/ _ \ __| / / _` |/ _ \ __| | '_ ` _ \ / _ \ __| '_ \ / _ \ / _` / __|
124  * | (_| | |_| |_| |    \__ \  __/ |_ / / (_| |  __/ |_  | | | | | |  __/ |_| | | | (_) | (_| \__ \
125  *  \__,_|\__|\__|_|    |___/\___|\__/_/ \__, |\___|\__| |_| |_| |_|\___|\__|_| |_|\___/ \__,_|___/
126  *                                        __/ |
127  *                                       |___/
128  ***************************************************************************************************/
129
130 mips_attr_t *get_mips_attr(ir_node *node)
131 {
132         assert(is_mips_irn(node) && "need mips node to get attributes");
133         return (mips_attr_t *) get_irn_generic_attr(node);
134 }
135
136 const mips_attr_t *get_mips_attr_const(const ir_node *node)
137 {
138         assert(is_mips_irn(node) && "need mips node to get attributes");
139         return get_irn_generic_attr_const(node);
140 }
141
142 const mips_immediate_attr_t *get_mips_immediate_attr_const(const ir_node *node)
143 {
144         assert(is_mips_irn(node) && "need mips node to get attributes");
145         return get_irn_generic_attr_const(node);
146 }
147
148 const mips_load_store_attr_t *get_mips_load_store_attr_const(
149                 const ir_node *node)
150 {
151         assert(is_mips_irn(node) && "need mips node to get attributes");
152         return get_irn_generic_attr_const(node);
153 }
154
155 /**
156  * Returns the argument register requirements of a mips node.
157  */
158 const arch_register_req_t **get_mips_in_req_all(const ir_node *node)
159 {
160         const mips_attr_t *attr = get_mips_attr_const(node);
161         return attr->in_req;
162 }
163
164 /**
165  * Returns the argument register requirement at position pos of an mips node.
166  */
167 const arch_register_req_t *get_mips_in_req(const ir_node *node, int pos)
168 {
169         const mips_attr_t *attr = get_mips_attr_const(node);
170         return attr->in_req[pos];
171 }
172
173 /**
174  * Sets the IN register requirements at position pos.
175  */
176 void set_mips_req_in(ir_node *node, const arch_register_req_t *req, int pos)
177 {
178         mips_attr_t *attr  = get_mips_attr(node);
179         attr->in_req[pos] = req;
180 }
181
182 /**
183  * Initializes the nodes attributes.
184  */
185 static void init_mips_attributes(ir_node *node, arch_irn_flags_t flags,
186                                  const arch_register_req_t **in_reqs,
187                                  const be_execution_unit_t ***execution_units,
188                                  int n_res)
189 {
190         ir_graph       *irg  = get_irn_irg(node);
191         struct obstack *obst = get_irg_obstack(irg);
192         mips_attr_t    *attr = get_mips_attr(node);
193         backend_info_t *info;
194         (void) execution_units;
195
196         arch_irn_set_flags(node, flags);
197         attr->in_req  = in_reqs;
198
199         info            = be_get_info(node);
200         info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_res);
201         memset(info->out_infos, 0, n_res * sizeof(info->out_infos[0]));
202 }
203
204 static void init_mips_immediate_attributes(ir_node *node,
205                                            mips_immediate_type_t type,
206                                            ir_entity *entity, long val)
207 {
208         mips_immediate_attr_t *attr = get_irn_generic_attr(node);
209
210         attr->imm_type = type;
211         attr->entity   = entity;
212         attr->val      = val;
213 }
214
215 static void init_mips_load_store_attributes(ir_node *node, ir_entity *entity,
216                                             long offset)
217 {
218         mips_load_store_attr_t *attr = get_irn_generic_attr(node);
219         attr->stack_entity = entity;
220         attr->offset       = offset;
221 }
222
223 static int mips_compare_nodes_attr(ir_node *node_a, ir_node *node_b)
224 {
225         const mips_attr_t *a = get_mips_attr_const(node_a);
226         const mips_attr_t *b = get_mips_attr_const(node_b);
227
228         if(a->switch_default_pn != b->switch_default_pn)
229                 return 1;
230
231         return 0;
232 }
233
234 static int mips_compare_immediate_attr(ir_node *node_a, ir_node *node_b)
235 {
236         const mips_immediate_attr_t *a = get_mips_immediate_attr_const(node_a);
237         const mips_immediate_attr_t *b = get_mips_immediate_attr_const(node_b);
238
239         if(a->val != b->val)
240                 return 1;
241
242         return 0;
243 }
244
245 static int mips_compare_load_store_attr(ir_node *node_a, ir_node *node_b)
246 {
247         const mips_load_store_attr_t *a = get_mips_load_store_attr_const(node_a);
248         const mips_load_store_attr_t *b = get_mips_load_store_attr_const(node_b);
249
250         if(mips_compare_nodes_attr(node_a, node_b))
251                 return 1;
252         if(a->stack_entity != b->stack_entity)
253                 return 1;
254         if(a->offset != b->offset)
255                 return 1;
256
257         return 0;
258 }
259
260 static void mips_copy_attr(const ir_node *old_node , ir_node *new_node)
261 {
262         ir_graph          *irg      = get_irn_irg(new_node);
263         struct obstack    *obst     = get_irg_obstack(irg);
264         const mips_attr_t *attr_old = get_mips_attr_const(old_node);
265         mips_attr_t       *attr_new = get_mips_attr(new_node);
266         backend_info_t    *old_info = be_get_info(old_node);
267         backend_info_t    *new_info = be_get_info(new_node);
268
269         /* copy the attributes */
270         memcpy(attr_new, attr_old, get_op_attr_size(get_irn_op(old_node)));
271
272         /* copy out flags */
273         new_info->out_infos =
274                 DUP_ARR_D(reg_out_info_t, obst, old_info->out_infos);
275 }
276
277 /***************************************************************************************
278  *                  _                            _                   _
279  *                 | |                          | |                 | |
280  *  _ __   ___   __| | ___    ___ ___  _ __  ___| |_ _ __ _   _  ___| |_ ___  _ __ ___
281  * | '_ \ / _ \ / _` |/ _ \  / __/ _ \| '_ \/ __| __| '__| | | |/ __| __/ _ \| '__/ __|
282  * | | | | (_) | (_| |  __/ | (_| (_) | | | \__ \ |_| |  | |_| | (__| || (_) | |  \__ \
283  * |_| |_|\___/ \__,_|\___|  \___\___/|_| |_|___/\__|_|   \__,_|\___|\__\___/|_|  |___/
284  *
285  ***************************************************************************************/
286
287 /* Include the generated constructor functions */
288 #include "gen_mips_new_nodes.c.inl"