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