sparc: redo and improve sparc immediate handling (low-part after sethi is now omitted...
[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 "irprintf.h"
39 #include "xmalloc.h"
40
41 #include "../bearch.h"
42
43 #include "sparc_nodes_attr.h"
44 #include "sparc_new_nodes.h"
45 #include "gen_sparc_regalloc_if.h"
46
47 static bool has_symconst_attr(const ir_node *node)
48 {
49         return is_sparc_SymConst(node) || is_sparc_FrameAddr(node);
50 }
51
52 static bool has_load_store_attr(const ir_node *node)
53 {
54         return is_sparc_Ld(node) || is_sparc_St(node) || is_sparc_Ldf(node)
55             || is_sparc_Stf(node);
56 }
57
58 static bool has_jmp_cond_attr(const ir_node *node)
59 {
60         return is_sparc_Bicc(node) || is_sparc_fbfcc(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 static bool has_fp_attr(const ir_node *node)
74 {
75         return is_sparc_fadd(node) || is_sparc_fsub(node)
76             || is_sparc_fmul(node) || is_sparc_fdiv(node)
77             || is_sparc_fftoi(node) || is_sparc_fitof(node)
78             || is_sparc_fneg(node) || is_sparc_fcmp(node);
79 }
80
81 static bool has_fp_conv_attr(const ir_node *node)
82 {
83         return is_sparc_fftof(node);
84 }
85
86 /**
87  * Dumper interface for dumping sparc nodes in vcg.
88  * @param F        the output file
89  * @param n        the node to dump
90  * @param reason   indicates which kind of information should be dumped
91  */
92 static void sparc_dump_node(FILE *F, ir_node *n, dump_reason_t reason)
93 {
94         switch (reason) {
95         case dump_node_opcode_txt:
96                 fprintf(F, "%s", get_irn_opname(n));
97                 break;
98
99         case dump_node_mode_txt:
100                 break;
101
102         case dump_node_info_txt:
103                 arch_dump_reqs_and_registers(F, n);
104                 if (has_save_attr(n)) {
105                         const sparc_save_attr_t *attr = get_sparc_save_attr_const(n);
106                         fprintf(F, "initial stacksize: %d\n", attr->initial_stacksize);
107                 }
108                 if (has_symconst_attr(n)) {
109                         const sparc_symconst_attr_t *attr = get_sparc_symconst_attr_const(n);
110                         ir_fprintf(F, "entity: %+F\n", attr->entity);
111                         fprintf(F, "fp_offset: %d\n", attr->fp_offset);
112                 }
113                 if (has_load_store_attr(n)) {
114                         const sparc_load_store_attr_t *attr = get_sparc_load_store_attr_const(n);
115                         ir_fprintf(F, "load store mode: %+F\n", attr->load_store_mode);
116                         ir_fprintf(F, "entity: (sign %d) %+F\n", attr->entity_sign,
117                                    attr->entity);
118                         fprintf(F, "offset: %ld\n", attr->offset);
119                         fprintf(F, "is frame entity: %s\n",
120                                 attr->is_frame_entity ? "true" : "false");
121                 }
122                 if (has_jmp_cond_attr(n)) {
123                         const sparc_jmp_cond_attr_t *attr
124                                 = get_sparc_jmp_cond_attr_const(n);
125                         fprintf(F, "pnc: %d (%s)\n", attr->pnc, get_pnc_string(attr->pnc));
126                         fprintf(F, "unsigned: %s\n", attr->is_unsigned ? "true" : "false");
127                 }
128                 if (has_jmp_switch_attr(n)) {
129                         const sparc_jmp_switch_attr_t *attr
130                                 = get_sparc_jmp_switch_attr_const(n);
131                         fprintf(F, "n projs: %d\n", attr->n_projs);
132                         fprintf(F, "default proj: %ld\n", attr->default_proj_num);
133                 }
134                 if (has_fp_attr(n)) {
135                         const sparc_fp_attr_t *attr = get_sparc_fp_attr_const(n);
136                         ir_fprintf(F, "fp_mode: %+F\n", attr->fp_mode);
137                 }
138                 if (has_fp_conv_attr(n)) {
139                         const sparc_fp_conv_attr_t *attr = get_sparc_fp_conv_attr_const(n);
140                         ir_fprintf(F, "conv from: %+F\n", attr->src_mode);
141                         ir_fprintf(F, "conv to: %+F\n", attr->dest_mode);
142                 }
143                 break;
144
145         case dump_node_nodeattr_txt:
146                 break;
147         }
148 }
149
150 static void sparc_set_attr_imm(ir_node *res, int32_t immediate_value)
151 {
152         sparc_attr_t *attr = get_irn_generic_attr(res);
153         attr->immediate_value = immediate_value;
154 }
155
156 static void init_sparc_jmp_cond_attr(ir_node *node, int pnc, bool is_unsigned)
157 {
158         sparc_jmp_cond_attr_t *attr = get_sparc_jmp_cond_attr(node);
159         attr->pnc         = pnc;
160         attr->is_unsigned = is_unsigned;
161 }
162
163 void set_sparc_jmp_switch_n_projs(ir_node *node, int n_projs)
164 {
165         sparc_jmp_switch_attr_t *attr = get_sparc_jmp_switch_attr(node);
166         attr->n_projs = n_projs;
167 }
168
169 void set_sparc_jmp_switch_default_proj_num(ir_node *node, long def_proj_num)
170 {
171         sparc_jmp_switch_attr_t *attr = get_sparc_jmp_switch_attr(node);
172         attr->default_proj_num = def_proj_num;
173 }
174
175
176
177 int get_sparc_jmp_switch_n_projs(const ir_node *node)
178 {
179         const sparc_jmp_switch_attr_t *attr = get_sparc_jmp_switch_attr_const(node);
180         return attr->n_projs;
181 }
182
183 long get_sparc_jmp_switch_default_proj_num(const ir_node *node)
184 {
185         const sparc_jmp_switch_attr_t *attr = get_sparc_jmp_switch_attr_const(node);
186         return attr->default_proj_num;
187 }
188
189 sparc_attr_t *get_sparc_attr(ir_node *node)
190 {
191         assert(is_sparc_irn(node));
192         return (sparc_attr_t*) get_irn_generic_attr(node);
193 }
194
195 const sparc_attr_t *get_sparc_attr_const(const ir_node *node)
196 {
197         assert(is_sparc_irn(node));
198         return (const sparc_attr_t*) get_irn_generic_attr_const(node);
199 }
200
201 sparc_load_store_attr_t *get_sparc_load_store_attr(ir_node *node)
202 {
203         assert(has_load_store_attr(node));
204         return (sparc_load_store_attr_t*) get_irn_generic_attr_const(node);
205 }
206
207 const sparc_load_store_attr_t *get_sparc_load_store_attr_const(const ir_node *node)
208 {
209         assert(has_load_store_attr(node));
210         return (const sparc_load_store_attr_t*) get_irn_generic_attr_const(node);
211 }
212
213 sparc_symconst_attr_t *get_sparc_symconst_attr(ir_node *node)
214 {
215         assert(has_symconst_attr(node));
216         return (sparc_symconst_attr_t*) get_irn_generic_attr_const(node);
217 }
218
219 const sparc_symconst_attr_t *get_sparc_symconst_attr_const(const ir_node *node)
220 {
221         assert(has_symconst_attr(node));
222         return (const sparc_symconst_attr_t*) get_irn_generic_attr_const(node);
223 }
224
225 sparc_jmp_cond_attr_t *get_sparc_jmp_cond_attr(ir_node *node)
226 {
227         assert(has_jmp_cond_attr(node));
228         return (sparc_jmp_cond_attr_t*) get_irn_generic_attr_const(node);
229 }
230
231 const sparc_jmp_cond_attr_t *get_sparc_jmp_cond_attr_const(const ir_node *node)
232 {
233         assert(has_jmp_cond_attr(node));
234         return (const sparc_jmp_cond_attr_t*) get_irn_generic_attr_const(node);
235 }
236
237 sparc_jmp_switch_attr_t *get_sparc_jmp_switch_attr(ir_node *node)
238 {
239         assert(has_jmp_switch_attr(node));
240         return (sparc_jmp_switch_attr_t*) get_irn_generic_attr_const(node);
241 }
242
243 const sparc_jmp_switch_attr_t *get_sparc_jmp_switch_attr_const(const ir_node *node)
244 {
245         assert(has_jmp_switch_attr(node));
246         return (const sparc_jmp_switch_attr_t*) get_irn_generic_attr_const(node);
247 }
248
249 sparc_save_attr_t *get_sparc_save_attr(ir_node *node)
250 {
251         assert(has_save_attr(node));
252         return (sparc_save_attr_t*) get_irn_generic_attr_const(node);
253 }
254
255 const sparc_save_attr_t *get_sparc_save_attr_const(const ir_node *node)
256 {
257         assert(has_save_attr(node));
258         return (const sparc_save_attr_t*) get_irn_generic_attr_const(node);
259 }
260
261 sparc_fp_attr_t *get_sparc_fp_attr(ir_node *node)
262 {
263         assert(has_fp_attr(node));
264         return (sparc_fp_attr_t*) get_irn_generic_attr(node);
265 }
266
267 const sparc_fp_attr_t *get_sparc_fp_attr_const(const ir_node *node)
268 {
269         assert(has_fp_attr(node));
270         return (const sparc_fp_attr_t*) get_irn_generic_attr_const(node);
271 }
272
273 sparc_fp_conv_attr_t *get_sparc_fp_conv_attr(ir_node *node)
274 {
275         assert(has_fp_conv_attr(node));
276         return (sparc_fp_conv_attr_t*) get_irn_generic_attr(node);
277 }
278
279 const sparc_fp_conv_attr_t *get_sparc_fp_conv_attr_const(const ir_node *node)
280 {
281         assert(has_fp_conv_attr(node));
282         return (const sparc_fp_conv_attr_t*) get_irn_generic_attr_const(node);
283 }
284
285 /**
286  * Returns the argument register requirements of a sparc node.
287  */
288 const arch_register_req_t **get_sparc_in_req_all(const ir_node *node)
289 {
290         const sparc_attr_t *attr = get_sparc_attr_const(node);
291         return attr->in_req;
292 }
293
294 void set_sparc_in_req_all(ir_node *node, const arch_register_req_t **reqs)
295 {
296         sparc_attr_t *attr = get_sparc_attr(node);
297         attr->in_req = reqs;
298 }
299
300 /**
301  * Returns the argument register requirement at position pos of an sparc node.
302  */
303 const arch_register_req_t *get_sparc_in_req(const ir_node *node, int pos)
304 {
305         const sparc_attr_t *attr = get_sparc_attr_const(node);
306         return attr->in_req[pos];
307 }
308
309 /**
310  * Sets the IN register requirements at position pos.
311  */
312 void set_sparc_req_in(ir_node *node, const arch_register_req_t *req, int pos)
313 {
314         sparc_attr_t *attr  = get_sparc_attr(node);
315         attr->in_req[pos] = req;
316 }
317
318 /**
319  * Initializes the nodes attributes.
320  */
321 static void init_sparc_attributes(ir_node *node, arch_irn_flags_t flags,
322                                   const arch_register_req_t **in_reqs,
323                                   const be_execution_unit_t ***execution_units,
324                                   int n_res)
325 {
326         ir_graph        *irg  = get_irn_irg(node);
327         struct obstack  *obst = get_irg_obstack(irg);
328         sparc_attr_t *attr = get_sparc_attr(node);
329         backend_info_t  *info;
330         (void) execution_units;
331
332         arch_irn_set_flags(node, flags);
333         attr->in_req  = in_reqs;
334         attr->is_load_store = false;
335
336         info            = be_get_info(node);
337         info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_res);
338         memset(info->out_infos, 0, n_res * sizeof(info->out_infos[0]));
339 }
340
341 static void init_sparc_load_store_attributes(ir_node *res, ir_mode *ls_mode,
342                                                                                         ir_entity *entity,
343                                                                                         int entity_sign, long offset,
344                                                                                         bool is_frame_entity)
345 {
346         sparc_load_store_attr_t *attr = get_sparc_load_store_attr(res);
347         attr->load_store_mode    = ls_mode;
348         attr->entity             = entity;
349         attr->entity_sign        = entity_sign;
350         attr->is_frame_entity    = is_frame_entity;
351         attr->offset             = offset;
352         attr->base.is_load_store = true;
353 }
354
355 static void init_sparc_symconst_attributes(ir_node *res, ir_entity *entity)
356 {
357         sparc_symconst_attr_t *attr = get_sparc_symconst_attr(res);
358         attr->entity    = entity;
359         attr->fp_offset = 0;
360 }
361
362 static void init_sparc_save_attributes(ir_node *res, int initial_stacksize)
363 {
364         sparc_save_attr_t *attr = get_sparc_save_attr(res);
365         attr->initial_stacksize = initial_stacksize;
366 }
367
368 static void init_sparc_fp_attributes(ir_node *res, ir_mode *fp_mode)
369 {
370         sparc_fp_attr_t *attr = get_sparc_fp_attr(res);
371         attr->fp_mode = fp_mode;
372 }
373
374 static void init_sparc_fp_conv_attributes(ir_node *res, ir_mode *src_mode,
375                                           ir_mode *dest_mode)
376 {
377         sparc_fp_conv_attr_t *attr = get_sparc_fp_conv_attr(res);
378         attr->src_mode = src_mode;
379         attr->dest_mode = dest_mode;
380 }
381
382 /**
383  * copies sparc attributes of  node
384  */
385 static void sparc_copy_attr(ir_graph *irg, const ir_node *old_node,
386                             ir_node *new_node)
387 {
388         struct obstack     *obst    = get_irg_obstack(irg);
389         const sparc_attr_t *attr_old = get_sparc_attr_const(old_node);
390         sparc_attr_t       *attr_new = get_sparc_attr(new_node);
391         backend_info_t     *old_info = be_get_info(old_node);
392         backend_info_t     *new_info = be_get_info(new_node);
393
394         /* copy the attributes */
395         memcpy(attr_new, attr_old, get_op_attr_size(get_irn_op(old_node)));
396         /* copy out flags */
397         new_info->out_infos =
398                 DUP_ARR_D(reg_out_info_t, obst, old_info->out_infos);
399 }
400
401 /**
402  * compare some node's attributes
403  */
404 static int cmp_attr_sparc(ir_node *a, ir_node *b)
405 {
406         const sparc_attr_t *attr_a = get_sparc_attr_const(a);
407         const sparc_attr_t *attr_b = get_sparc_attr_const(b);
408
409         return attr_a->immediate_value != attr_b->immediate_value
410                         || attr_a->is_load_store != attr_b->is_load_store;
411 }
412
413 static int cmp_attr_sparc_load_store(ir_node *a, ir_node *b)
414 {
415         const sparc_load_store_attr_t *attr_a = get_sparc_load_store_attr_const(a);
416         const sparc_load_store_attr_t *attr_b = get_sparc_load_store_attr_const(b);
417
418         if (cmp_attr_sparc(a, b))
419                 return 1;
420
421         return attr_a->entity != attr_b->entity
422                         || attr_a->entity_sign != attr_b->entity_sign
423                         || attr_a->is_frame_entity != attr_b->is_frame_entity
424                         || attr_a->load_store_mode != attr_b->load_store_mode
425                         || attr_a->offset != attr_b->offset;
426 }
427
428 static int cmp_attr_sparc_symconst(ir_node *a, ir_node *b)
429 {
430         const sparc_symconst_attr_t *attr_a = get_sparc_symconst_attr_const(a);
431         const sparc_symconst_attr_t *attr_b = get_sparc_symconst_attr_const(b);
432
433         if (cmp_attr_sparc(a, b))
434                 return 1;
435
436         return attr_a->entity != attr_b->entity
437                         || attr_a->fp_offset != attr_b->fp_offset;
438 }
439
440 static int cmp_attr_sparc_jmp_cond(ir_node *a, ir_node *b)
441 {
442         const sparc_jmp_cond_attr_t *attr_a = get_sparc_jmp_cond_attr_const(a);
443         const sparc_jmp_cond_attr_t *attr_b = get_sparc_jmp_cond_attr_const(b);
444
445         if (cmp_attr_sparc(a, b))
446                 return 1;
447
448         return attr_a->pnc != attr_b->pnc
449             || attr_a->is_unsigned != attr_b->is_unsigned;
450 }
451
452 static int cmp_attr_sparc_jmp_switch(ir_node *a, ir_node *b)
453 {
454         const sparc_jmp_switch_attr_t *attr_a = get_sparc_jmp_switch_attr_const(a);
455         const sparc_jmp_switch_attr_t *attr_b = get_sparc_jmp_switch_attr_const(b);
456
457         if (cmp_attr_sparc(a, b))
458                 return 1;
459
460         return attr_a->default_proj_num != attr_b->default_proj_num
461                         || attr_a->n_projs != attr_b->n_projs;
462 }
463
464 static int cmp_attr_sparc_save(ir_node *a, ir_node *b)
465 {
466         const sparc_save_attr_t *attr_a = get_sparc_save_attr_const(a);
467         const sparc_save_attr_t *attr_b = get_sparc_save_attr_const(b);
468
469         if (cmp_attr_sparc(a, b))
470                 return 1;
471
472         return attr_a->initial_stacksize != attr_b->initial_stacksize;
473 }
474
475 static int cmp_attr_sparc_fp(ir_node *a, ir_node *b)
476 {
477         const sparc_fp_attr_t *attr_a = get_sparc_fp_attr_const(a);
478         const sparc_fp_attr_t *attr_b = get_sparc_fp_attr_const(b);
479
480         if (cmp_attr_sparc(a, b))
481                 return 1;
482
483         return attr_a->fp_mode != attr_b->fp_mode;
484 }
485
486 static int cmp_attr_sparc_fp_conv(ir_node *a, ir_node *b)
487 {
488         const sparc_fp_conv_attr_t *attr_a = get_sparc_fp_conv_attr_const(a);
489         const sparc_fp_conv_attr_t *attr_b = get_sparc_fp_conv_attr_const(b);
490
491         if (cmp_attr_sparc(a, b))
492                 return 1;
493
494         return attr_a->src_mode != attr_b->src_mode
495             || attr_a->dest_mode != attr_b->dest_mode;;
496 }
497
498 /* Include the generated constructor functions */
499 #include "gen_sparc_new_nodes.c.inl"