9e387ab16b3abf3733ade80c833b30ad750dbce0
[libfirm] / ir / be / sparc / sparc_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 achitecture specific firm
23  *          opcodes and the coresponding node constructors for the sparc
24  *          assembler irg.
25  * @version $Id$
26  */
27 #include "config.h"
28
29 #include <stdlib.h>
30
31 #include "irprog_t.h"
32 #include "irgraph_t.h"
33 #include "irnode_t.h"
34 #include "irmode_t.h"
35 #include "ircons_t.h"
36 #include "iropt_t.h"
37 #include "irop.h"
38 #include "irvrfy_t.h"
39 #include "irprintf.h"
40 #include "xmalloc.h"
41
42 #include "../bearch.h"
43
44 #include "sparc_nodes_attr.h"
45 #include "sparc_new_nodes.h"
46 #include "gen_sparc_regalloc_if.h"
47
48 static bool has_symconst_attr(const ir_node *node)
49 {
50         return is_sparc_SymConst(node) || is_sparc_FrameAddr(node);
51 }
52
53 static bool has_load_store_attr(const ir_node *node)
54 {
55         return is_sparc_Ld(node) || is_sparc_St(node);
56 }
57
58 static bool has_jmp_cond_attr(const ir_node *node)
59 {
60         return is_sparc_BXX(node);
61 }
62
63 static bool has_jmp_switch_attr(const ir_node *node)
64 {
65         return is_sparc_SwitchJmp(node);
66 }
67
68 static bool has_save_attr(const ir_node *node)
69 {
70         return is_sparc_Save(node);
71 }
72
73 /**
74  * Dumper interface for dumping sparc nodes in vcg.
75  * @param F        the output file
76  * @param n        the node to dump
77  * @param reason   indicates which kind of information should be dumped
78  */
79 static void sparc_dump_node(FILE *F, ir_node *n, dump_reason_t reason)
80 {
81         switch (reason) {
82         case dump_node_opcode_txt:
83                 fprintf(F, "%s", get_irn_opname(n));
84                 break;
85
86         case dump_node_mode_txt:
87                 break;
88
89         case dump_node_info_txt:
90                 arch_dump_reqs_and_registers(F, n);
91                 break;
92
93         case dump_node_nodeattr_txt:
94                 if (has_symconst_attr(n)) {
95                         const sparc_symconst_attr_t *attr = get_sparc_symconst_attr_const(n);
96                         fprintf(F, "fp_offset: 0x%X\n", attr->fp_offset);
97                 }
98                 if (has_load_store_attr(n)) {
99                         const sparc_load_store_attr_t *attr = get_sparc_load_store_attr_const(n);
100                         fprintf(F, "offset: 0x%lX\n", attr->offset);
101                         fprintf(F, "is_frame_entity: %s\n", attr->is_frame_entity == true ? "true" : "false");
102                 }
103                 break;
104         }
105 }
106
107 static void sparc_set_attr_imm(ir_node *res, int immediate_value)
108 {
109         sparc_attr_t *attr = get_irn_generic_attr(res);
110         attr->immediate_value = immediate_value;
111 }
112
113 static void init_sparc_jmp_cond_attr(ir_node *node, int proj_num,
114                                      bool is_unsigned)
115 {
116         sparc_jmp_cond_attr_t *attr = get_sparc_jmp_cond_attr(node);
117         attr->proj_num    = proj_num;
118         attr->is_unsigned = is_unsigned;
119 }
120
121 void set_sparc_jmp_switch_n_projs(ir_node *node, int n_projs)
122 {
123         sparc_jmp_switch_attr_t *attr = get_sparc_jmp_switch_attr(node);
124         attr->n_projs = n_projs;
125 }
126
127 void set_sparc_jmp_switch_default_proj_num(ir_node *node, long def_proj_num)
128 {
129         sparc_jmp_switch_attr_t *attr = get_sparc_jmp_switch_attr(node);
130         attr->default_proj_num = def_proj_num;
131 }
132
133
134
135 int get_sparc_jmp_switch_n_projs(const ir_node *node)
136 {
137         const sparc_jmp_switch_attr_t *attr = get_sparc_jmp_switch_attr_const(node);
138         return attr->n_projs;
139 }
140
141 long get_sparc_jmp_switch_default_proj_num(const ir_node *node)
142 {
143         const sparc_jmp_switch_attr_t *attr = get_sparc_jmp_switch_attr_const(node);
144         return attr->default_proj_num;
145 }
146
147 sparc_attr_t *get_sparc_attr(ir_node *node)
148 {
149         assert(is_sparc_irn(node));
150         return (sparc_attr_t*) get_irn_generic_attr(node);
151 }
152
153 const sparc_attr_t *get_sparc_attr_const(const ir_node *node)
154 {
155         assert(is_sparc_irn(node));
156         return (const sparc_attr_t*) get_irn_generic_attr_const(node);
157 }
158
159 sparc_load_store_attr_t *get_sparc_load_store_attr(ir_node *node)
160 {
161         assert(has_load_store_attr(node));
162         return (sparc_load_store_attr_t*) get_irn_generic_attr_const(node);
163 }
164
165 const sparc_load_store_attr_t *get_sparc_load_store_attr_const(const ir_node *node)
166 {
167         assert(has_load_store_attr(node));
168         return (const sparc_load_store_attr_t*) get_irn_generic_attr_const(node);
169 }
170
171 sparc_symconst_attr_t *get_sparc_symconst_attr(ir_node *node)
172 {
173         assert(has_symconst_attr(node));
174         return (sparc_symconst_attr_t*) get_irn_generic_attr_const(node);
175 }
176
177 const sparc_symconst_attr_t *get_sparc_symconst_attr_const(const ir_node *node)
178 {
179         assert(has_symconst_attr(node));
180         return (const sparc_symconst_attr_t*) get_irn_generic_attr_const(node);
181 }
182
183 sparc_jmp_cond_attr_t *get_sparc_jmp_cond_attr(ir_node *node)
184 {
185         assert(has_jmp_cond_attr(node));
186         return (sparc_jmp_cond_attr_t*) get_irn_generic_attr_const(node);
187 }
188
189 const sparc_jmp_cond_attr_t *get_sparc_jmp_cond_attr_const(const ir_node *node)
190 {
191         assert(has_jmp_cond_attr(node));
192         return (const sparc_jmp_cond_attr_t*) get_irn_generic_attr_const(node);
193 }
194
195 sparc_jmp_switch_attr_t *get_sparc_jmp_switch_attr(ir_node *node)
196 {
197         assert(has_jmp_switch_attr(node));
198         return (sparc_jmp_switch_attr_t*) get_irn_generic_attr_const(node);
199 }
200
201 const sparc_jmp_switch_attr_t *get_sparc_jmp_switch_attr_const(const ir_node *node)
202 {
203         assert(has_jmp_switch_attr(node));
204         return (const sparc_jmp_switch_attr_t*) get_irn_generic_attr_const(node);
205 }
206
207 sparc_save_attr_t *get_sparc_save_attr(ir_node *node)
208 {
209         assert(has_save_attr(node));
210         return (sparc_save_attr_t*) get_irn_generic_attr_const(node);
211 }
212
213 const sparc_save_attr_t *get_sparc_save_attr_const(const ir_node *node)
214 {
215         assert(has_save_attr(node));
216         return (const sparc_save_attr_t*) get_irn_generic_attr_const(node);
217 }
218
219 /**
220  * Returns the argument register requirements of a sparc node.
221  */
222 const arch_register_req_t **get_sparc_in_req_all(const ir_node *node)
223 {
224         const sparc_attr_t *attr = get_sparc_attr_const(node);
225         return attr->in_req;
226 }
227
228 void set_sparc_in_req_all(ir_node *node, const arch_register_req_t **reqs)
229 {
230         sparc_attr_t *attr = get_sparc_attr(node);
231         attr->in_req = reqs;
232 }
233
234 /**
235  * Returns the argument register requirement at position pos of an sparc node.
236  */
237 const arch_register_req_t *get_sparc_in_req(const ir_node *node, int pos)
238 {
239         const sparc_attr_t *attr = get_sparc_attr_const(node);
240         return attr->in_req[pos];
241 }
242
243 /**
244  * Sets the IN register requirements at position pos.
245  */
246 void set_sparc_req_in(ir_node *node, const arch_register_req_t *req, int pos)
247 {
248         sparc_attr_t *attr  = get_sparc_attr(node);
249         attr->in_req[pos] = req;
250 }
251
252 /**
253  * Initializes the nodes attributes.
254  */
255 static void init_sparc_attributes(ir_node *node, arch_irn_flags_t flags,
256                                   const arch_register_req_t **in_reqs,
257                                   const be_execution_unit_t ***execution_units,
258                                   int n_res)
259 {
260         ir_graph        *irg  = get_irn_irg(node);
261         struct obstack  *obst = get_irg_obstack(irg);
262         sparc_attr_t *attr = get_sparc_attr(node);
263         backend_info_t  *info;
264         (void) execution_units;
265
266         arch_irn_set_flags(node, flags);
267         attr->in_req  = in_reqs;
268         attr->is_load_store = false;
269
270         info            = be_get_info(node);
271         info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_res);
272         memset(info->out_infos, 0, n_res * sizeof(info->out_infos[0]));
273 }
274
275 /* CUSTOM ATTRIBUTE INIT FUNCTIONS */
276 static void init_sparc_load_store_attributes(ir_node *res, ir_mode *ls_mode,
277                                                                                         ir_entity *entity,
278                                                                                         int entity_sign, long offset,
279                                                                                         bool is_frame_entity)
280 {
281         sparc_load_store_attr_t *attr = get_sparc_load_store_attr(res);
282         attr->load_store_mode    = ls_mode;
283         attr->entity             = entity;
284         attr->entity_sign        = entity_sign;
285         attr->is_frame_entity    = is_frame_entity;
286         attr->offset             = offset;
287         attr->base.is_load_store = true;
288 }
289
290 static void init_sparc_symconst_attributes(ir_node *res, ir_entity *entity)
291 {
292         sparc_symconst_attr_t *attr = get_sparc_symconst_attr(res);
293         attr->entity    = entity;
294         attr->fp_offset = 0;
295 }
296
297 static void init_sparc_save_attr(ir_node *res, int initial_stacksize)
298 {
299         sparc_save_attr_t *attr = get_sparc_save_attr(res);
300         attr->initial_stacksize = initial_stacksize;
301 }
302
303 /**
304  * copies sparc attributes of  node
305  */
306 static void sparc_copy_attr(ir_graph *irg, const ir_node *old_node,
307                             ir_node *new_node)
308 {
309         struct obstack     *obst    = get_irg_obstack(irg);
310         const sparc_attr_t *attr_old = get_sparc_attr_const(old_node);
311         sparc_attr_t       *attr_new = get_sparc_attr(new_node);
312         backend_info_t     *old_info = be_get_info(old_node);
313         backend_info_t     *new_info = be_get_info(new_node);
314
315         /* copy the attributes */
316         memcpy(attr_new, attr_old, get_op_attr_size(get_irn_op(old_node)));
317         /* copy out flags */
318         new_info->out_infos =
319                 DUP_ARR_D(reg_out_info_t, obst, old_info->out_infos);
320 }
321
322 /**
323  * compare some node's attributes
324  */
325 static int cmp_attr_sparc(ir_node *a, ir_node *b)
326 {
327         const sparc_attr_t *attr_a = get_sparc_attr_const(a);
328         const sparc_attr_t *attr_b = get_sparc_attr_const(b);
329
330         return attr_a->immediate_value != attr_b->immediate_value
331                         || attr_a->is_load_store != attr_b->is_load_store;
332 }
333
334
335 /* CUSTOM ATTRIBUTE CMP FUNCTIONS */
336 static int cmp_attr_sparc_load_store(ir_node *a, ir_node *b)
337 {
338         const sparc_load_store_attr_t *attr_a = get_sparc_load_store_attr_const(a);
339         const sparc_load_store_attr_t *attr_b = get_sparc_load_store_attr_const(b);
340
341         if (cmp_attr_sparc(a, b))
342                         return 1;
343
344         return attr_a->entity != attr_b->entity
345                         || attr_a->entity_sign != attr_b->entity_sign
346                         || attr_a->is_frame_entity != attr_b->is_frame_entity
347                         || attr_a->load_store_mode != attr_b->load_store_mode
348                         || attr_a->offset != attr_b->offset;
349 }
350
351 static int cmp_attr_sparc_symconst(ir_node *a, ir_node *b)
352 {
353         const sparc_symconst_attr_t *attr_a = get_sparc_symconst_attr_const(a);
354         const sparc_symconst_attr_t *attr_b = get_sparc_symconst_attr_const(b);
355
356         if (cmp_attr_sparc(a, b))
357                         return 1;
358
359         return attr_a->entity != attr_b->entity
360                         || attr_a->fp_offset != attr_b->fp_offset;
361 }
362
363 static int cmp_attr_sparc_jmp_cond(ir_node *a, ir_node *b)
364 {
365         const sparc_jmp_cond_attr_t *attr_a = get_sparc_jmp_cond_attr_const(a);
366         const sparc_jmp_cond_attr_t *attr_b = get_sparc_jmp_cond_attr_const(b);
367
368         if (cmp_attr_sparc(a, b))
369                         return 1;
370
371         return attr_a->proj_num != attr_b->proj_num
372                 || attr_a->is_unsigned != attr_b->is_unsigned;
373 }
374
375 static int cmp_attr_sparc_jmp_switch(ir_node *a, ir_node *b)
376 {
377         const sparc_jmp_switch_attr_t *attr_a = get_sparc_jmp_switch_attr_const(a);
378         const sparc_jmp_switch_attr_t *attr_b = get_sparc_jmp_switch_attr_const(b);
379
380         if (cmp_attr_sparc(a, b))
381                         return 1;
382
383         return attr_a->default_proj_num != attr_b->default_proj_num
384                         || attr_a->n_projs != attr_b->n_projs;
385 }
386
387 static int cmp_attr_sparc_save(ir_node *a, ir_node *b)
388 {
389         const sparc_save_attr_t *attr_a = get_sparc_save_attr_const(a);
390         const sparc_save_attr_t *attr_b = get_sparc_save_attr_const(b);
391
392         if (cmp_attr_sparc(a, b))
393                         return 1;
394
395         return attr_a->initial_stacksize != attr_b->initial_stacksize;
396 }
397
398 /* Include the generated constructor functions */
399 #include "gen_sparc_new_nodes.c.inl"