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