sparc: remove entity_sign, gas/elf doesn't support it anyway
[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 bool sparc_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 (sparc_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: %+F\n", attr->entity);
117                         fprintf(F, "offset: %ld\n", attr->offset);
118                         fprintf(F, "is frame entity: %s\n",
119                                 attr->is_frame_entity ? "true" : "false");
120                 }
121                 if (has_jmp_cond_attr(n)) {
122                         const sparc_jmp_cond_attr_t *attr
123                                 = get_sparc_jmp_cond_attr_const(n);
124                         fprintf(F, "pnc: %d (%s)\n", attr->pnc, get_pnc_string(attr->pnc));
125                         fprintf(F, "unsigned: %s\n", attr->is_unsigned ? "true" : "false");
126                 }
127                 if (has_jmp_switch_attr(n)) {
128                         const sparc_jmp_switch_attr_t *attr
129                                 = get_sparc_jmp_switch_attr_const(n);
130                         fprintf(F, "n projs: %d\n", attr->n_projs);
131                         fprintf(F, "default proj: %ld\n", attr->default_proj_num);
132                 }
133                 if (has_fp_attr(n)) {
134                         const sparc_fp_attr_t *attr = get_sparc_fp_attr_const(n);
135                         ir_fprintf(F, "fp_mode: %+F\n", attr->fp_mode);
136                 }
137                 if (has_fp_conv_attr(n)) {
138                         const sparc_fp_conv_attr_t *attr = get_sparc_fp_conv_attr_const(n);
139                         ir_fprintf(F, "conv from: %+F\n", attr->src_mode);
140                         ir_fprintf(F, "conv to: %+F\n", attr->dest_mode);
141                 }
142                 break;
143
144         case dump_node_nodeattr_txt:
145                 break;
146         }
147 }
148
149 static void sparc_set_attr_imm(ir_node *res, int32_t immediate_value)
150 {
151         sparc_attr_t *attr = get_irn_generic_attr(res);
152         attr->immediate_value = immediate_value;
153 }
154
155 static void init_sparc_jmp_cond_attr(ir_node *node, int pnc, bool is_unsigned)
156 {
157         sparc_jmp_cond_attr_t *attr = get_sparc_jmp_cond_attr(node);
158         attr->pnc         = pnc;
159         attr->is_unsigned = is_unsigned;
160 }
161
162 void set_sparc_jmp_switch_n_projs(ir_node *node, int n_projs)
163 {
164         sparc_jmp_switch_attr_t *attr = get_sparc_jmp_switch_attr(node);
165         attr->n_projs = n_projs;
166 }
167
168 void set_sparc_jmp_switch_default_proj_num(ir_node *node, long def_proj_num)
169 {
170         sparc_jmp_switch_attr_t *attr = get_sparc_jmp_switch_attr(node);
171         attr->default_proj_num = def_proj_num;
172 }
173
174
175
176 int get_sparc_jmp_switch_n_projs(const ir_node *node)
177 {
178         const sparc_jmp_switch_attr_t *attr = get_sparc_jmp_switch_attr_const(node);
179         return attr->n_projs;
180 }
181
182 long get_sparc_jmp_switch_default_proj_num(const ir_node *node)
183 {
184         const sparc_jmp_switch_attr_t *attr = get_sparc_jmp_switch_attr_const(node);
185         return attr->default_proj_num;
186 }
187
188 sparc_attr_t *get_sparc_attr(ir_node *node)
189 {
190         assert(is_sparc_irn(node));
191         return (sparc_attr_t*) get_irn_generic_attr(node);
192 }
193
194 const sparc_attr_t *get_sparc_attr_const(const ir_node *node)
195 {
196         assert(is_sparc_irn(node));
197         return (const sparc_attr_t*) get_irn_generic_attr_const(node);
198 }
199
200 sparc_load_store_attr_t *get_sparc_load_store_attr(ir_node *node)
201 {
202         assert(sparc_has_load_store_attr(node));
203         return (sparc_load_store_attr_t*) get_irn_generic_attr_const(node);
204 }
205
206 const sparc_load_store_attr_t *get_sparc_load_store_attr_const(const ir_node *node)
207 {
208         assert(sparc_has_load_store_attr(node));
209         return (const sparc_load_store_attr_t*) get_irn_generic_attr_const(node);
210 }
211
212 sparc_symconst_attr_t *get_sparc_symconst_attr(ir_node *node)
213 {
214         assert(has_symconst_attr(node));
215         return (sparc_symconst_attr_t*) get_irn_generic_attr_const(node);
216 }
217
218 const sparc_symconst_attr_t *get_sparc_symconst_attr_const(const ir_node *node)
219 {
220         assert(has_symconst_attr(node));
221         return (const sparc_symconst_attr_t*) get_irn_generic_attr_const(node);
222 }
223
224 sparc_jmp_cond_attr_t *get_sparc_jmp_cond_attr(ir_node *node)
225 {
226         assert(has_jmp_cond_attr(node));
227         return (sparc_jmp_cond_attr_t*) get_irn_generic_attr_const(node);
228 }
229
230 const sparc_jmp_cond_attr_t *get_sparc_jmp_cond_attr_const(const ir_node *node)
231 {
232         assert(has_jmp_cond_attr(node));
233         return (const sparc_jmp_cond_attr_t*) get_irn_generic_attr_const(node);
234 }
235
236 sparc_jmp_switch_attr_t *get_sparc_jmp_switch_attr(ir_node *node)
237 {
238         assert(has_jmp_switch_attr(node));
239         return (sparc_jmp_switch_attr_t*) get_irn_generic_attr_const(node);
240 }
241
242 const sparc_jmp_switch_attr_t *get_sparc_jmp_switch_attr_const(const ir_node *node)
243 {
244         assert(has_jmp_switch_attr(node));
245         return (const sparc_jmp_switch_attr_t*) get_irn_generic_attr_const(node);
246 }
247
248 sparc_save_attr_t *get_sparc_save_attr(ir_node *node)
249 {
250         assert(has_save_attr(node));
251         return (sparc_save_attr_t*) get_irn_generic_attr_const(node);
252 }
253
254 const sparc_save_attr_t *get_sparc_save_attr_const(const ir_node *node)
255 {
256         assert(has_save_attr(node));
257         return (const sparc_save_attr_t*) get_irn_generic_attr_const(node);
258 }
259
260 sparc_fp_attr_t *get_sparc_fp_attr(ir_node *node)
261 {
262         assert(has_fp_attr(node));
263         return (sparc_fp_attr_t*) get_irn_generic_attr(node);
264 }
265
266 const sparc_fp_attr_t *get_sparc_fp_attr_const(const ir_node *node)
267 {
268         assert(has_fp_attr(node));
269         return (const sparc_fp_attr_t*) get_irn_generic_attr_const(node);
270 }
271
272 sparc_fp_conv_attr_t *get_sparc_fp_conv_attr(ir_node *node)
273 {
274         assert(has_fp_conv_attr(node));
275         return (sparc_fp_conv_attr_t*) get_irn_generic_attr(node);
276 }
277
278 const sparc_fp_conv_attr_t *get_sparc_fp_conv_attr_const(const ir_node *node)
279 {
280         assert(has_fp_conv_attr(node));
281         return (const sparc_fp_conv_attr_t*) get_irn_generic_attr_const(node);
282 }
283
284 /**
285  * Returns the argument register requirements of a sparc node.
286  */
287 const arch_register_req_t **get_sparc_in_req_all(const ir_node *node)
288 {
289         const sparc_attr_t *attr = get_sparc_attr_const(node);
290         return attr->in_req;
291 }
292
293 void set_sparc_in_req_all(ir_node *node, const arch_register_req_t **reqs)
294 {
295         sparc_attr_t *attr = get_sparc_attr(node);
296         attr->in_req = reqs;
297 }
298
299 /**
300  * Returns the argument register requirement at position pos of an sparc node.
301  */
302 const arch_register_req_t *get_sparc_in_req(const ir_node *node, int pos)
303 {
304         const sparc_attr_t *attr = get_sparc_attr_const(node);
305         return attr->in_req[pos];
306 }
307
308 /**
309  * Sets the IN register requirements at position pos.
310  */
311 void set_sparc_req_in(ir_node *node, const arch_register_req_t *req, int pos)
312 {
313         sparc_attr_t *attr  = get_sparc_attr(node);
314         attr->in_req[pos] = req;
315 }
316
317 /**
318  * Initializes the nodes attributes.
319  */
320 static void init_sparc_attributes(ir_node *node, arch_irn_flags_t flags,
321                                   const arch_register_req_t **in_reqs,
322                                   const be_execution_unit_t ***execution_units,
323                                   int n_res)
324 {
325         ir_graph        *irg  = get_irn_irg(node);
326         struct obstack  *obst = get_irg_obstack(irg);
327         sparc_attr_t *attr = get_sparc_attr(node);
328         backend_info_t  *info;
329         (void) execution_units;
330
331         arch_irn_set_flags(node, flags);
332         attr->in_req = in_reqs;
333
334         info            = be_get_info(node);
335         info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_res);
336         memset(info->out_infos, 0, n_res * sizeof(info->out_infos[0]));
337 }
338
339 static void init_sparc_load_store_attributes(ir_node *res, ir_mode *ls_mode,
340                                                                                         ir_entity *entity,
341                                                                                         long offset, bool is_frame_entity)
342 {
343         sparc_load_store_attr_t *attr = get_sparc_load_store_attr(res);
344         attr->load_store_mode    = ls_mode;
345         attr->entity             = entity;
346         attr->is_frame_entity    = is_frame_entity;
347         attr->offset             = offset;
348 }
349
350 static void init_sparc_symconst_attributes(ir_node *res, ir_entity *entity)
351 {
352         sparc_symconst_attr_t *attr = get_sparc_symconst_attr(res);
353         attr->entity    = entity;
354         attr->fp_offset = 0;
355 }
356
357 static void init_sparc_save_attributes(ir_node *res, int initial_stacksize)
358 {
359         sparc_save_attr_t *attr = get_sparc_save_attr(res);
360         attr->initial_stacksize = initial_stacksize;
361 }
362
363 static void init_sparc_fp_attributes(ir_node *res, ir_mode *fp_mode)
364 {
365         sparc_fp_attr_t *attr = get_sparc_fp_attr(res);
366         attr->fp_mode = fp_mode;
367 }
368
369 static void init_sparc_fp_conv_attributes(ir_node *res, ir_mode *src_mode,
370                                           ir_mode *dest_mode)
371 {
372         sparc_fp_conv_attr_t *attr = get_sparc_fp_conv_attr(res);
373         attr->src_mode = src_mode;
374         attr->dest_mode = dest_mode;
375 }
376
377 /**
378  * copies sparc attributes of  node
379  */
380 static void sparc_copy_attr(ir_graph *irg, const ir_node *old_node,
381                             ir_node *new_node)
382 {
383         struct obstack     *obst    = get_irg_obstack(irg);
384         const sparc_attr_t *attr_old = get_sparc_attr_const(old_node);
385         sparc_attr_t       *attr_new = get_sparc_attr(new_node);
386         backend_info_t     *old_info = be_get_info(old_node);
387         backend_info_t     *new_info = be_get_info(new_node);
388
389         /* copy the attributes */
390         memcpy(attr_new, attr_old, get_op_attr_size(get_irn_op(old_node)));
391         /* copy out flags */
392         new_info->out_infos =
393                 DUP_ARR_D(reg_out_info_t, obst, old_info->out_infos);
394 }
395
396 /**
397  * compare some node's attributes
398  */
399 static int cmp_attr_sparc(ir_node *a, ir_node *b)
400 {
401         const sparc_attr_t *attr_a = get_sparc_attr_const(a);
402         const sparc_attr_t *attr_b = get_sparc_attr_const(b);
403
404         return attr_a->immediate_value != attr_b->immediate_value
405                 || attr_a->immediate_value_entity != attr_b->immediate_value_entity;
406 }
407
408 static int cmp_attr_sparc_load_store(ir_node *a, ir_node *b)
409 {
410         const sparc_load_store_attr_t *attr_a = get_sparc_load_store_attr_const(a);
411         const sparc_load_store_attr_t *attr_b = get_sparc_load_store_attr_const(b);
412
413         if (cmp_attr_sparc(a, b))
414                 return 1;
415
416         return attr_a->entity != attr_b->entity
417                         || attr_a->is_frame_entity != attr_b->is_frame_entity
418                         || attr_a->load_store_mode != attr_b->load_store_mode
419                         || attr_a->offset != attr_b->offset;
420 }
421
422 static int cmp_attr_sparc_symconst(ir_node *a, ir_node *b)
423 {
424         const sparc_symconst_attr_t *attr_a = get_sparc_symconst_attr_const(a);
425         const sparc_symconst_attr_t *attr_b = get_sparc_symconst_attr_const(b);
426
427         if (cmp_attr_sparc(a, b))
428                 return 1;
429
430         return attr_a->entity != attr_b->entity
431                         || attr_a->fp_offset != attr_b->fp_offset;
432 }
433
434 static int cmp_attr_sparc_jmp_cond(ir_node *a, ir_node *b)
435 {
436         const sparc_jmp_cond_attr_t *attr_a = get_sparc_jmp_cond_attr_const(a);
437         const sparc_jmp_cond_attr_t *attr_b = get_sparc_jmp_cond_attr_const(b);
438
439         if (cmp_attr_sparc(a, b))
440                 return 1;
441
442         return attr_a->pnc != attr_b->pnc
443             || attr_a->is_unsigned != attr_b->is_unsigned;
444 }
445
446 static int cmp_attr_sparc_jmp_switch(ir_node *a, ir_node *b)
447 {
448         const sparc_jmp_switch_attr_t *attr_a = get_sparc_jmp_switch_attr_const(a);
449         const sparc_jmp_switch_attr_t *attr_b = get_sparc_jmp_switch_attr_const(b);
450
451         if (cmp_attr_sparc(a, b))
452                 return 1;
453
454         return attr_a->default_proj_num != attr_b->default_proj_num
455                         || attr_a->n_projs != attr_b->n_projs;
456 }
457
458 static int cmp_attr_sparc_save(ir_node *a, ir_node *b)
459 {
460         const sparc_save_attr_t *attr_a = get_sparc_save_attr_const(a);
461         const sparc_save_attr_t *attr_b = get_sparc_save_attr_const(b);
462
463         if (cmp_attr_sparc(a, b))
464                 return 1;
465
466         return attr_a->initial_stacksize != attr_b->initial_stacksize;
467 }
468
469 static int cmp_attr_sparc_fp(ir_node *a, ir_node *b)
470 {
471         const sparc_fp_attr_t *attr_a = get_sparc_fp_attr_const(a);
472         const sparc_fp_attr_t *attr_b = get_sparc_fp_attr_const(b);
473
474         if (cmp_attr_sparc(a, b))
475                 return 1;
476
477         return attr_a->fp_mode != attr_b->fp_mode;
478 }
479
480 static int cmp_attr_sparc_fp_conv(ir_node *a, ir_node *b)
481 {
482         const sparc_fp_conv_attr_t *attr_a = get_sparc_fp_conv_attr_const(a);
483         const sparc_fp_conv_attr_t *attr_b = get_sparc_fp_conv_attr_const(b);
484
485         if (cmp_attr_sparc(a, b))
486                 return 1;
487
488         return attr_a->src_mode != attr_b->src_mode
489             || attr_a->dest_mode != attr_b->dest_mode;;
490 }
491
492 /* Include the generated constructor functions */
493 #include "gen_sparc_new_nodes.c.inl"