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