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